Speedup code reviews using Danger-Swift on Jenkins CI

swift Jun 23, 2021

Halodoc started with a mission of simplifying healthcare across Indonesia. Starting with few people and an ambitious destination, executing quickly was the choice we took. The quick development process is imperative and for us, the code review process has paramount importance to meet code quality standards and team conventions.

To maintain code quality standards reviewers invests a lot of time for merge request to be merged, which slows down the development process. Let's learn about automating the coding conventions with the integration of Danger-swift on Jenkins which allows reviewers to focus purely on the core logic and thus speedup our code review process.

What is Danger Swift?

Danger Swift is an automation tool that runs during the CI process to automate common code review chores.

For example, you can write rules that enforce

  1. Changelogs entries
  2. Encourage smaller merge requests
  3. Merge requests have assignees
  4. Swiftlint rules

Danger-swift Installation

Danger-swift supports almost all CIs, widely used. Danger-swift integration might differ depending on the CI and source repository. We will discuss how to enable Danger-swift using GitLab and Jenkins.

How it works

  • Upon creating or updating a merge request in Gitlab, it will trigger build on Jenkins.
  • At the end of your build, Jenkins CI installs and runs Danger. which invokes Rest API to add comments on your merge request in Gitlab.

Setup Danger file

Prerequisite: Gem & bundler should be installed
Go to your repository and add Dangerfile and Gemfile with the following steps

1. Create Gemfile.

bundle init

2. Add the below code in your Gemfile

3.  Use Bundler to install the gem.

 bundle install

4. After the gem is installed, you can run Danger's interactive getting started guide:

bundle exec danger init

after completion of above steps, you will have Dangerfile in your folder

5. Add the below code to your Danger file

In the above lines of code we have set the following rules

  • git.lines_of_code is number of lines of code, and is restricted to 250.  
  • swiftlint.config_file is the path to .swiftlint.yml file, if value is not set it will run default swiftlint rules provided by Danger-swift.
  • swiftlint.max_num_violations  is the maximum number of violations which gets display in the merge request, if it finds more it will add a new comment i.e. SwiftLint also found X more violations with this MR.
  • swiftlint.lint_files command will execute swiftlint rules and post comments on merge request in the overview section.
  • Please refer here for more rules

6. Add your custom .swiftlint.yml file to the same path of Gemfile and Dangerfile

7. We periodically enhance the rules depending upon the situation. For example, there is a new swift version available or we found out new conventions which will help us improves readability.

Please refer here  for how we do Continuous code inspection with SwiftLint

Setup Jenkins

We have created one Jenkins job especially for Danger-swift, which supports all repositories of our iOS projects.
In the Configure section of Job, we did the following setup

1. Source Code Management

2. Build Triggers for Webhook

  • Select Build when a change is pushed to GitLab
  • enable Push events and Open Merge Request Events

3. Bindings

  • Setup Username and password
  • Enable Inject environment variables to the build process and add following env. variables in Properties Content
DANGER_GITLAB_HOST={host_url}
DANGER_GITLAB_API_BASE_URL={host_url}/api/v4
DANGER_GITLAB_API_TOKEN= {token}

4. Build Command

  • Add /usr/local/bin/bundle exec danger --verbose command to run danger on your checkout branch.    

Gitlab Webhook Setup

  1. Create a bot account with the ability to post a comment in your concerned project merge request for Danger to use.
  2. Configure Gitlab webhook to trigger build for merge request.

With the Danger-swift integration now reviewers can check the coding convention in each merge request as depicted below


Conclusion:

With the integration of Danger-swift in our CI i.e. Jenkins, we can maintain the coding conventions throughout the team. A reviewer can easily detect swiftlint rules violations, save time in the code review process and focus more on the core logic of merge requests. Here in this blog, we used Danger-swiftlint to show Swiftlint warning as a merge request comments. You can use additional plugins provided to enhance productivity.

We are always looking out to hire for all roles for our tech team. If challenging problems that drive big impact enthral you, do reach out to us at careers.india@halodoc.com


About Halodoc

Halodoc is the number 1 all around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek and many more. We recently closed our Series C round and In total have raised around USD$180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.