Automatically maintain your team’s code with Swiftformat and GitHub PR webhook and CI/CD
How does your team maintain code conventions?
Are you spending part of your development time maintaining code conventions or pointing out conventions during code review? This is a question I ask you, but it is also a question I have often asked myself before.
I am one of those developers who takes code convention very seriously. Maintaining code conventions ensures that whoever writes the code remains readable and understandable. It takes a lot of time and attention to maintain this state.
I was hoping to maintain code conventions in the team I work for. This may be my greed in some way, but I have no doubts that it is a direction that will help improve the productivity of the team.
After joining the team and working, we defined and shared conventions for the team, but in practice, keeping them was not an easy task. How can I finish and maintain code conventions with my colleagues?
I found out about SwiftFormat’s code auto-correction feature on a startup tech blog shared by a close colleague, and I thought this was a ray of light that could save me.
In the process of introducing SwiftFormat to the team
I did a lot of testing on when to run the code auto-correction command, but I couldn’t find the right timing.
Luckily, my genius colleague suggested a way to use GitHub webhooks and CI/CD tools to execute commands at the perfect time, and they quickly integrated it.
Consider a typical development collaboration cycle.
1. The developer creates a Git Branch for developing the function in charge.
2. Have fun writing code.
3. After completing development, send a Pull Request to ask colleagues to review the code.
Github provides a webhook function to catch events when PR occurs. Based on this, it was a simple and incredible idea to automate by executing the SwiftFormat command using a webhook when a PR event occurs.
It’s working very well, and it keeps our team from wasting any more time maintaining code conventions.
CI/CD settings
From now on, I will explain the process of integrating Github PR webhook and Teamcity step by step. Of course, it doesn’t matter what CI/CD tool you use. It can be integrated into most tools, and your team will also be able to easily integrate it.
Please understand that we cannot provide detailed information for security reasons, and please refer to the official Teamcity document that is trapped as a link.
New Configuration
- Create a new Configuration in your Teamcity project.
Attach VCS root
- Add your Git Repository to the Version Control Settings.
https://www.jetbrains.com/help/teamcity/configuring-vcs-roots.html
Add build feature
- Add Pull Requests to Build Features.
Build Steps
Three sequences of build steps are required and the execution type is Command Line.
- checkout
#!/bin/bash
git config user.name %devbot.git.user.name%
git config user.email %devbot.git.user.email%
git remote update
git remote prune origin
git checkout %teamcity.pullRequest.source.branch%
git pull
2. run swiftformat
If you use the Auto Correction tool for platforms other than iOS, it can be applied to all platforms.
eg.) ktlint — kotlin lint and auto formatting tool
#!/bin/sh
brew install swiftformat
/usr/local/bin/swiftformat .
3. commit and push
#!/bin/sh
git status
git add .
git commit -m "Update files by auto fomatting (CI)"
git push
All settings are now complete. From now on, when a PR request is made, automatic code correction by Teamcity will work.
As shown below, the bot executes SwiftFormat through the git PR webhook and then performs git commit & push.
Conclusion
The greatest value of code review is the collaboration between team members to create and maintain better quality code. It is not appropriate to waste code review time to maintain code conventions. We hope that you also adopt the method suggested in this article to perform more efficient code review while maintaining code conventions.
“Automation to improve productivity” This is what our team is aiming for. I think this challenge was a very good example in that I achieved the team’s goal while maintaining the code convention I wanted. In the future, we will think more about whether there is something that can be automated to improve productivity, try better challenges, and share good cases.