Exercise - Add a rule to require a review
In this unit, you'll 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'll 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'll then verify that the rule 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 left menu, select Branches.
- Make sure that main is selected as your default branch.
- Select Add branch protection rule.
- Under Branch name pattern, enter main.
- Select the Require a pull request before merging check box.
- Select the Require approvals check box.
- Keep the Required approving reviews value at 1.
- Select Create.
- Select Save changes.
Note
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. Remember that the word "official" is mistyped as "oficial".
In Visual Studio Code, go to the terminal.
To check out the
main
branch, rungit checkout
:git checkout main
To pull down the latest changes to the
main
branch from GitHub, rungit pull
: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'd 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
bugfix/home-page-typo
branch.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'd assign a team member to review your change. In this unit, you can merge your own pull request for learning purposes.
Select the Merge without waiting for requirements to be met (bypass branch protections) check box, and then select Merge pull request.
Select Confirm merge.
Your change is merged.
To delete the
bugfix/home-page-typo
branch, select Delete branch.