Skip to main content

A simple wrapper to convert Black outputs to codeclimate report format for GitLab-CI

Project description

Black GitLab Code Quality

This project aim to convert Black report to CodeClimate format that can be ingest by GitLab.

→ poetry run black-gl-cq src/black_gl_code_quality | jq
[
  {
    "type": "issue",
    "description": "Black would reformat",
    "location": {
      "lines": {
        "begin": 1,
        "end": 1
      },
      "path": "src/black_gl_code_quality/__main__.py"
    },
    "severity": "major"
  },
  {
    "type": "issue",
    "description": "Black would reformat",
    "location": {
      "lines": {
        "begin": 1,
        "end": 1
      },
      "path": "src/black_gl_code_quality/error.py"
    },
    "severity": "major"
  }
]

Motivation

For security concerns, Docker-in-Docker has been disabled in all GitLab runners that I have access/manage. Because the Code Quality template shipped with GitLab instances use CodeClimate CLI that requires a Docker environment, we can't use it and an alternative solution was required to obtain Black errors in our Code Quality reports.

How to install

Simply run the following command:

pip install black-gl-code-quality

If you use Poetry, you can add it to your dev-dependencies:

poetry add --group dev black-gl-code-quality

Usage

There is two ways to use this tool:

  • by piping Black
  • by calling black-gl-code-quality (or by it's alias black-gl-cq) directly

Piping with Black

Piping with Black requires to forward stderr to stdout. You can use the following command in the .gitlab-ci.yml:

black --check src/ 2>&1 | black-gl-cq > black-code-quality-report.json

Here's an example for a GitLab-CI job:

lint:black:
  stage: test
  script:
    - source .venv/bin/activate
    - black --check src/ 2>&1 | black-gl-cq > black-code-quality-report.json
  artifacts:
    when: always
    reports:
      codequality: black-code-quality-report.json

Calling black-gl-code-quality directly

Calling black-gl-code-quality (or it's alias black-gl-cq) execute Black with the --check argument. It forwards all arguments you pass if you need to configure Black via the CLI.

Specifying source folders is MANDATORY.

Here's an example for a GitLab-CI job:

lint:black:
  stage: test
  script:
    - source .venv/bin/activate
    - black-gl-cq src/ > black-code-quality-report.json
  artifacts:
    when: always
    reports:
      codequality: black-code-quality-report.json

Admit we want to skip string normalization:

lint:black:
  stage: test
  script:
    - source .venv/bin/activate
    - black-gl-cq -S src/ > black-code-quality-report.json
  artifacts:
    when: always
    reports:
      codequality: black-code-quality-report.json

Change severity

By default, all errors have the severity major. Depending how you consider Black issues important, you can change the severity for all errors returned by the report by using one of the following values with the BLACK_GL_SEVERITY environment variable: info, minor, major, critical, blocker.

Here's an example for a GitLab-CI job:

lint:black:
  stage: test
  variables:
    BLACK_GL_SEVERITY: minor
  script:
    - source .venv/bin/activate
    - black-gl-cq src/ > black-code-quality-report.json
  artifacts:
    when: always
    reports:
      codequality: black-code-quality-report.json

Debugging

It is possible to pass the -v argument to black-gl-cq to stream Black's output to stderr.

Here's an example:

$ poetry run black-gl-cq -v src > black-code-quality-report.json
Identified `/home/themimitoof/repos/black-gl-code-quality` as project root containing a .git directory.
Sources to be formatted: "src"
/home/themimitoof/repos/black-gl-code-quality/src/black_gl_code_quality/__init__.py wasn't modified on disk since last run.
/home/themimitoof/repos/black-gl-code-quality/src/black_gl_code_quality/parser.py wasn't modified on disk since last run.
/home/themimitoof/repos/black-gl-code-quality/src/black_gl_code_quality/error.py already well formatted, good job.
/home/themimitoof/repos/black-gl-code-quality/src/black_gl_code_quality/__main__.py already well formatted, good job.

All done! ✨ 🍰 ✨
4 files would be left unchanged.

Contributions

In case you have a suggestion or want a new feature, feel free to open a discussion.

If you found a bug, you can open an issue.

In order to maintain an overall good code quality, this project use the following tools:

Linting and formatting tools are configured to match the current default rules of Black.

Please ensure to run these tools before commiting and submiting a Pull request. In case one of these mentionned tools report an error, the CI will automatically fail.

In case you are able to fix by yourself a bug, enhance the code or implement a new feature, feel free to send a Pull request.

License

This project is released under the BSD-3 Clause. Feel free to use, contribute, fork and do what you want with it. Please keep all licenses, copyright notices and mentions in case you use, re-use, steal, fork code from this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

black_gl_code_quality-0.3.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

black_gl_code_quality-0.3.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file black_gl_code_quality-0.3.1.tar.gz.

File metadata

  • Download URL: black_gl_code_quality-0.3.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.6.48-1-lts

File hashes

Hashes for black_gl_code_quality-0.3.1.tar.gz
Algorithm Hash digest
SHA256 989c191e370bfeec5af7dd787b191598bd43edd8d5916f4d5af8af9e2a3d83f1
MD5 c17a0042a61b5c5621b31721406a9c57
BLAKE2b-256 412d81ba9d139b931436a31d8638bafea7993a2c192885d86efc2ff315a6f146

See more details on using hashes here.

File details

Details for the file black_gl_code_quality-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for black_gl_code_quality-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 74590b1c5bdf28a41c30bb4cc02ec36078a88c1b6535c58f0b9ea10f13dff9b8
MD5 3e2b9ba6cd7e3b42475abcd8ddb299cf
BLAKE2b-256 e8a144261d63c7bb7528760a20265963c3ab4e974b97057c420e80e52485758a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page