At Don’t Panic Labs, we love tools that help us ensure we are writing quality code and following our development patterns and practices. We are using Visual Studio Online for many of our current projects and we have found some very useful features for protecting the master branches.
Requiring pull requests for a branch
On the branches page in Visual Studio Online, click the ellipsis for the branch you want to protect and click Branch policies.
First we want to enable Protect this branch.
By simply setting this option, changes are no longer allowed to be pushed to the master branch. Only by submitting a pull request with a valid build can code merge into master.
Saving changes and attempting to push a change to master results in the following error on the command line.
Require a minimum number of reviewers
Set how many reviewers you would like to require to review a pull request and whether they can approve their own changes.
Check for linked work items
Being able to link the work items a developer is completing with a pull request is a great way for code reviewers and project managers to know what to expect to be completed in the pull request.
Check for comment resolution
We require all our pull requests to have reviewers (as we’ll see in the next section) so we also want all the comments to be addressed before a pull request can be completed.
If you like to require a specific type of merge setting, the Merge requirements policy will enforce that when completing a pull request.
Here we can specify a build to be run when a pull request is created. Pull requests would not be allowed to complete until the build is successful.
Automatically include code reviewers
This feature allows you to target certain areas of your code base for review by a specific developer. For example, this could allow a UI developer to be automatically added as a reviewer if changes are made in the UI layer.
The End Result: An Example
I made a change to the SpecialCode path from above and submitted a pull request to master.
I now have a list of required policies in the top left corner that must be met before I can complete the pull request. The build was automatically run and succeeded, and I was automatically assigned as a required reviewer because code was changed in the directory I specified.
I also put a comment in the pull request that is now active and will need to resolve it before completing.
Completing all the policies will give us a pull request that is ready to complete.
Completing the pull request will require us to perform a squash merge set by our policies.
If you’re a Visual Studio Online user, I highly recommend heading to the branch policy page and setting up all these policies to help improve the quality of your code and your development process.