Anyone who has maintained open-source software has probably felt the burnout or challenge of maintaining software around their day job. Wouldn’t it be great if you could use CI to get some merge/pull requests against your projects when the coding standards that you are following are updated? If the answer is yes then look no further.

Drupal Coding Standard Patch Generation

phpcs-drupal:
  image: willhallonline/drupal-phpcs:alpine
  allow_failure: yes
  script:
    - phpcs ./

phpcbf-drupal:
  image: willhallonline/drupal-phpcs:alpine
  allow_failure: yes
  script:
    - set +e
    - phpcbf ./
    - git status
    - git -c user.name='Will Hall' -c user.email='incoming+willhallonline/[email protected]' commit -am "Coding Standards Formatting"
    - git diff origin/$CI_COMMIT_REF_NAME > $CI_COMMIT_SHA.patch
  artifacts:
    paths:
    - $CI_COMMIT_SHA.patch
    expire_in: 1 week

The process of this is rather simple:

  1. Run phpcs (PHP_CodeSniffer) against your code with the code fixing item which will…
  2. … Fix the code
  3. Create a patch from what has been fixed
  4. Create that patch as an artifact that can be downloaded and added to the existing project.

Live Example in Use

As you may already know, I made a file_system_switch module for Drupal to help move files between public and private storage. If you don’t really know Drupal too well, that is not a problem. What you need to understand is that I use Drupal coding standards.

You should be able to see this GitLab CI job which gives an output of the coding standards issues on file_system_switch:

...
PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
...line/file_system_switch/file_system_switch.module  2      4
----------------------------------------------------------------------
A TOTAL OF 2 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------

Time: 244ms; Memory: 12MB

Now we already have an artifact that has these fixes (or some of them inside it). So I can apply that to the code and push again making the process more seamless. As you can see it is an intentional mistake and also we can’t fix everything with code fixing, however, it generally gives you the ability to easily fix your code and recieve updates if anything can easily be changed.

Applying the changes

This is a relatively simple process:

  1. Download the artifact
  2. Unzip it into a local version of your project
  3. Use git apply [name-of-patch] to apply it to the project.
  4. Commit the changes git commit -am "Coding standards".
  5. Push changes back to your git repo git push origin mybranch.

Because you are already using git, applying it is pretty seamless using git apply.

You will now be able to create a merge/pull request to your project for your new changes.

What next?

You may have seen the previous blog post about Automatically creating patches when linting using CI and you can use almost any kind of processing which has fixing to do a very similar job including most of my other docker containers:

Or Stylelint/ESLint:

You may have already guessed that we could push this further to leverage the GitLab API and actually create Merge Requests with the changes in which could be approved, but that is going to have to be a subsequent post.