Gitlab - format code automatically upon commit

Viewed 2423

I'm having some trouble finding info that describes how to update the code base automatically in Gitlab

Scenario

Let's imagine a developer working on a project commits some code, but forgets to format it before committing. The .gitlab-ci.yml can have a job that will check the formatting, and display an error/warning if found. Is it possible to update the git commit with the code formatted automatically?

So the flow would be:

  1. Developer commits unformatted code to gitlab
  2. Git checks the code for formatting
  3. If an issue is found, git will run tool X to automatically format the code, and commit it with a git message like "Automated Git commit -- formatting"
  4. .gitlab-ci.yml continues as normal

Is this possible in Gitlab?

1 Answers

Using GitLab 13.09, the answer appears to be "no".

I used this .gitlab-ci.yml:

image: python-latest

format-job:
    script:
        - pip install black
        - black src

As expected, black echos that my files were reformatted, yet when I check the repo they haven't been fixed.

My experience is consistent with this post.

A potential alternative is to use a pre-commit server hook.

Related