Only this! How to prevent forgetting code format in team development

image.png

Introduction

** "Code format is not applied." ** The person who pointed out this, the person who was. Isn't he there? This time, I will introduce a mechanism to execute the code format with ** CI tool ** and prevent the developer from executing the formatter.

Motivation to write this article

** A product with a code style specified by the customer ** faced the following problems, resulting in increased man-hours.

: star: ** Case 1. A case where "Code Formatter" is not applied locally and a Pull Request is issued ** image.png

: star: ** Case 2. After modifying based on the slip-through code and applying the code formatter, the differences other than the repaired part were extracted **

image.png

: star: ** Case 3. Increase man-hours by creating a system for "cross-checking" ** image.png

** [Why code formatter is not applied] **

--The code formatter is not described in the development environment construction procedure manual / development flow. (Ambiguous document)

image.png

--There is no transfer of flow between development members (development members are in flux)

image.png

Even if you prepare a configuration file for the code formatter, it is meaningless if it is not in operation **. Even if you write the documentation well, there is a limit to making sure that all project members have the same development preferences **. Therefore, I wanted to introduce how to apply the code formatter on the CI tool side for code owners who have similar problems.

Run code formatter with CI tools

** Solution idea **: Rather than relying on the development environment, the formatter should be applied automatically before the review is conducted.

image.png

Premise

The following requirements must be met in order for CI tools to work with code formatters.

  1. Mechanism for detecting Push and Pull Request (Merge Request) to version control tools
  2. There must be a code formatter that supports the following:

For the method described below, we have selected tools that meet the above requirements, but if they do not exist (especially [1.]), please consider making your own.

Code format automatic application example

This sample shall be created under the following specifications.

item Contents
Programming language Java
Code style Google Java Style GuideCompliant with
tool google-java-format

Run code formatter automatically with CI tools

Code for CI (GitHub Action)

: star: Point: Since the branch acquired by on pull_reqest is different from the branch that made the Pull Request, it is necessary to explicitly acquire the branch that made the Pull Request at checkout.

name: Reformat Java source code

on:
  pull_request:

jobs:
  format:
    runs-on: ubuntu-latest
    steps:
      #Check out the pull requested branch(★ ref is the point)
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
      #Java setup
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      #Git settings
      - name: Git settings
        run: |
          #Git repository settings
          git config --local user.name "User name"
          git config --local user.email "E-Mail address"
      #Executing code format
      - name: Code format
        run: |
          wget -q -O google-java-format.jar \
            https://github.com/google/google-java-format/releases/download/google-java-format-1.9/google-java-format-1.9-all-deps.jar
          java -jar google-java-format.jar -replace $(git ls-files src/**/*.java)
      #Change confirmation
      - name: Check for modified files
        id: git-check
        run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
      #If it has changed, do a commit push
      - name: Push
        if: steps.git-check.outputs.modified == 'true'
        run: |
          git commit -am "Automated :Reformat Java source code."
          git push

Execution result

** PullRequst implemented, new commit with code formatter applied at the time of change is added **

github_001.gif

** Change difference ** image.png

Sample repository

The above is realized in the following repositories. (Branch: feature/format-action) https://github.com/yukiusa/github-action-auto-java-format-sample/tree/feature/format-action

at the end

How was that? This time, I introduced "a mechanism to comply with the rules without depending on the development environment" using CI tools. By implementing this initiative

--Reduce the trouble of creating a development environment --Reduce check man-hours --Reduce the load of reviewers and reviewers

I think that you can expect such effects. We hope that you will find it useful as one of the future business improvement plans.

Thank you for reading to the end.

Recommended Posts

Only this! How to prevent forgetting code format in team development
How to implement UICollectionView in Swift with code only
How to color code console output in Eclipse
[IntelliJ IDEA] How to format only local changes when saving the source code
How to build Java development environment with VS Code
How to use GitHub for super beginners (team development)
How to specify character code and line feed code in JAXB
How to set character code and line feed code in Eclipse
How to select a specified date by code in FSCalendar
[Swift5] How to avoid applying dark mode (dark appearance) in code
How to implement one-line display of TextView in Android development
How to apply C code format from the command line
How to prevent past dates from being entered in Rails forms
Measure the bottleneck! How to trace only slow methods in AspectJ
[rails6.0.0] How to save images using Active Storage in wizard format
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
[Rails] How to prevent screen transition
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
How to use InjectorHolder in OpenAM
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to concatenate strings in java
How to install Swiper in Rails
How to execute with commands of normal development language in Docker development environment
[Android application development] How to display in full screen (notification bar hidden)
How to get the current date as a string in yyyyMMdd format
How to debug the processing in the Ruby on Rails model only on the console
Android development, how to check null in the value of JSON object
[Ruby] How to prevent errors when nil is included in the operation
How to bind request parameters in list format with bean property in Spring