Automating Code Reviews with Amazon CodeGuru

Updated: 2023-09-01
3 min read

Lab

Associating Amazon CodeGuru with a CodeCommit Repository

1. Navigate to the Amazon CodeCommit console.

2. Click java-web-app:

alt

3. Notice that at the moment, only a README file has been committed to the master branch. Next, you’ll associate CodeGuru with this repository, so that CodeGuru can begin to analyze the code therein.

4. Go to the CodeGuru dashboard.

5. Click Associate Repository and run analysis:

alt

6. Select AWS CodeCommit as the provider, choose java-web-app from the repository dropdown, enter _master_into Source branch and click Associate:

alt

In roughly one minute, you’ll see that CodeGuru has associated with your repository:

alt

Triggering an Amazon CodeGuru Review

1. Navigate to :8080 in your browser.

 Note: This is the IP of an EC2 instance that can be found in the EC2 console.

2. Click the file icon in the top left to open the file tree:

alt

Note: During the creation of this lab, two things were performed automatically. One is that the CodeCommit repository you visited earlier was cloned to the directory you’re looking at in the IDE now. Another is that the framework for a Java web app was added in addition to the single README you saw. This is so that you can see the benefits of CodeGuru without having to work heavily with code.

In this lab step, you’ll push all the new code to the nearly-empty Code Commit repository, to trigger a CodeGuru review.

3. Open the terminal in your IDE:

alt

4. In the terminal, add the new files to a Git branch, and commit and push the changes:

cd /cloudacademy/lab
git add .
git checkout -b trigger_branch
git commit -m "trigger a CodeGuru analysis by pushing Java code"
git push origin trigger_branch

This will create a Git commit that includes all the Java files in a branch called trigger_branch, so that you can make a pull request in CodeCommit. Since CodeGuru analyses are triggered by pull requests, this is what will trigger a CodeGuru analysis.

5. Back on the CodeCommit dashboard, click Create pull request:

alt

6. Set the Destination to master and the source to trigger_branch and click Compare:

alt

7. Type Trigger a CodeGuru Reviewer Analysis into the Title field and click Create pull request:

alt

This will create a pull request and trigger a CodeGuru review.

Viewing Amazon CodeGuru Comments

1. If you weren’t automatically brought to the pull request details page after creating your pull request, click Pull Requests beneath Repositories on the left side of the page:

alt

2. Click the only available pull request:

alt

3. Notice the section mentioning CodeGuru Reviewer:

alt

This section will display in each pull request made in any repository associated with CodeGuru. As of the time this lab was released, CodeGuru is still in preview. As the section on your pull request details tab mentions, because it’s in preview mode, CodeGuru can take a while to process a pull request. There isn’t a way to track its progress, and you currently won’t be alerted when that processing begins or finishes.

4. Select the Changes tab:

alt

5. In the Go to file filter, enter dockerservlet and click the result to navigate to the file:

alt

You may need to scroll down the page to find the DockerServlet.java file changes. This file is known to have CodeGuru Reviewer comments that usually appear a few minutes after creating the pull request.

6. Scroll down to line 60 to see an example of a comment from CodeGuru Reviewer (If you don’t see any comment you may try refreshing the page every minute until one appears):

alt

You can then make updates as you see fit, and submit more pull requests to see if you’ve addressed CodeGuru’s suggestions.