Exercise - Add a rule to require a review
In this unit, you set up a rule on GitHub that requires a reviewer to approve changes before they can be merged into the
main branch. As a bonus, you'll also fix the typing error on the Space Game website's home page.
Currently, the team allows anyone who makes a pull request to merge the code into the
main branch. Because no review is required, it's possible for incorrect or unstable code to creep in.
Andy decides that he wants to add a check to the pull request in the form of another pair of eyes. He wants to set up GitHub to require someone other than the pull request author to review the code before it's merged. Let's see how to do this.
Andy heads off to find Mara and spots her at her desk working away, her head bobbing to the music in her earbuds.
Andy: Mara, I've been meaning to talk to you about something.
Mara looks up.
Mara: What can I help you with?
Andy: Several small mistakes are making it through the build. Just today, a typing error showed up on the home page. Amita is spending too much time on these things. We need to stop them before they make it to the
main branch. We need another pair of eyes on the code before the pull request is approved.
Mara: I can set that up. In GitHub, there's a way to make sure that no pull request is merged before someone else reviews and approves it.
Set up approvals
In this section, you set up a rule on GitHub that requires at least one reviewer to approve a pull request before it can be merged into the
main branch. You set up the rule and then verify that it works by pushing up a fix to the typing error that Mara made earlier.
Add the rule
- In GitHub, go to your Space Game project repository.
- Select the Settings tab near the top of the page.
- On the menu, select Branches.
- Make sure that main is selected as your default branch.
- Select Add rule.
- Under Branch name pattern, enter main.
- Select the Require pull request reviews before merging check box.
- Keep the Required approving reviews value at 1.
- Select Create.
- Select Save changes.
At the bottom of the list of choices is an option named Include Administrators. This option requires repository administrators to follow the rule. You don't set that, because you're an administrator of your repository and there isn't another reviewer. In this unit, for learning purposes, you review and approve your own pull requests.
Submit the fix
In this section, you submit a fix to the typing error on the home page. Recall that the word "official" is mistyped as "oficial".
In Visual Studio Code, go to the terminal.
To check out the
git checkout main
To pull down the latest changes to the
mainbranch from GitHub, run
git pull origin main
You can see that two files are updated:
- README.md: Contains the Markdown code for displaying the build badge.
- Index.cshtml: Contains the updated home page text, which includes the typing error.
To fix the error, create and check out a branch:
git checkout -B bugfix/home-page-typo
In File Explorer, open Index.cshtml.
Locate the error.
<p>Welcome to the oficial Space Game site!</p>
Change the line to correct the error.
<p>Welcome to the official Space Game site!</p>
Save the file.
In the terminal, stage and commit the change.
git status git add Tailspin.SpaceGame.Web/Views/Home/Index.cshtml git commit -m "Fix typing error on the home page"
In practice, you would ordinarily build and run the site locally to verify the change. In this unit, for the sake of brevity, let's skip that step.
Push the branch to GitHub.
git push origin bugfix/home-page-typo
Test the rule
In GitHub, locate and select the
To start your pull request, select Contribute and then Open pull request.
Set your forked repository as the base repository.
Select Create pull request.
You can see that a human review is required before you can merge the change.
In practice, you would assign a team member to review your change. In this unit, for learning purposes, you merge your own pull request.
To merge the pull request, select Merge pull request.
Select the Use your Administrator privileges to merge this pull request check box, and then select Confirm merge.
Your change is merged.
To delete the
bugfix/home-page-typobranch, select Delete branch.