Level up your code review with continuous integration

At Atlassian we are big believers in reviewing code via pull requests. Every single line of code that is written follows a code review process before it is merged and deployed to production. Besides the benefit of diminishing the likelihood of bugs reaching production and the collaboration and learning that occurs when reviewing code with your team, a pull request is also a great opportunity to execute tests to make sure both new and existing functionality continues to work as expected.

Turning code review up a notch: running CI against pull requests

We recently announced a new feature in Bitbucket Pipelines that lets you configure pull request builds to automate checks when you create a pull request. From the moment a pull request is created Bitbucket Pipelines will perform a local merge from your destination branch every time you push changes to your branch. Because Bitbucket Pipelines first constructs the merge and then executes your tests on the result, you get a pretty good idea how your code changes will behave when they are actually merged.

Successful builds not only help you determine if your code is in good shape to be merged, they also help reviewers verify that code changes are good from a functional perspective. To that end we make the status of builds clearly visible in a pull request.

You can even enforce that pull requests can’t be merged until your builds are green, by configuring a merge check. All this adds up to help keep your master branch green, clean and, most importantly of all, deployable!

Configuring a pull request build is pretty straightforward. In the example below we have configured our workflow to execute a pull request build for every pull request that is created in the repository (through the use of the ** pattern).

pipelines:
  pull-requests:
    '**':
      - step:
          name: Build and test
          script:
            - #test script
  branches:
    'master':
      - step:
          name: Build and test
          script: 
            - #test script
      - step:
          name: Deploy to production
          deployment: production
          script:
            - #deploy script

Just in case there are changes to master between a pull request being created and its eventual merge, all the tests are executed again to ensure there are no breakages. Once these tests have passed the pipeline can continue its progress towards a production deployment.

Pull request pipelines vs branch pipelines

In the above example you can see that Bitbucket Pipelines allows you to set up branch specific pipelines. Branch pipelines are triggered every time new changes are pushed to a branch, and will then operate on a checkout of that specific branch.

Pull request pipelines, while also triggered when changes are pushed to the source branch, differ from branch pipelines as they only occur after a pull request has been created. When triggered a local merge of the source and destination branches is performed before the execution of your pipeline, letting you run your tests on a preview of your codebase as if the pull request were merged.

Pipelines use cases

So when would you use a branch pipeline or pull request pipeline? If you want to push changes on a regular basis to the destination branch and check whether tests pass then configuring a branch pipeline for your feature branches would be a sensible approach.

If, however, you only care about the test results once a pull request is created then a pull request pipeline would be more your cup of tea.

You can also go one step further and set up both a branch and pull request pipeline to, for example, split your tests. You would use the branch pipeline to execute all of your (fast) unit tests to get quick feedback every time you push changes to your branch, and once a pull request is created the pull request pipeline would trigger additional (slower) high level tests such as browser or integration tests.

Level up your code review today

Code review is a great way to improve overall code quality and empower collaboration amongst your team, and by utilising pull request pipelines you can incorporate continuous integration and really supercharge your code review process.

Try pull request pipelines today and let us know what you think!