Evaluate source code using machine learning

with Amazon CodeGuru Reviewer

In this tutorial, you learn how to use machine learning to evaluate source in the cloud with Amazon CodeGuru Reviewer.

Amazon CodeGuru Reviewer is a service that uses program analysis and machine learning to detect potential defects that are difficult for developers to find and offers suggestions for improving your Java and Python code. By proactively detecting code defects, CodeGuru Reviewer can provide guidelines for addressing them and implementing best practices to improve the overall quality and maintainability of your code base during the code review stage.

In this tutorial, you learn how to:

  1. Create a sample respository in AWS CodeCommit using AWS CloudShell
  2. Associate the sample repository with CodeGuru Reviewer
  3. Create a new code commit in AWS CodeCommit
  4. Create a pull request in AWS CodeCommit
  5. View recommendations from and provide feedback to CodeGuru Reviewer
  6. Clean up tutorial resources

This tutorial is AWS Free Tier eligible.

About this Tutorial
Time 10 minutes                                  
Cost AWS Free Tier eligible
Use Case Machine Learning
Products Amazon CodeGuru, AWS CodeCommit
Audience Developer
Level Beginner
Last Updated February 18, 2021

Before you begin

You must have an AWS account to complete this tutorial. If you do not already have an account, choose Sign up for AWS and create a new account.

Already have an account?
Log in to your account

Step 1. Create a sample respository

Complete the following steps to create a sample respository in AWS CodeCommit. For this step, you use AWS CloudShell, a browser-based shell, to run the commands that create and then clone the sample respository. In later steps, you associate this respository with CodeGuru Reviewer and generate recommendations for the source code.

Note: For more information, see Create a respository for your source code in the Amazon CodeGuru documentation.


a. Sign in to AWS CloudShell.

AWS CloudShell

b. Run the following command to create the new sample repository in AWS CodeCommit.

aws codecommit create-repository --repository-name CodeGuruReviewerRepo --repository-description "CodeGuru Reviewer demonstration repository"

c. Run the following command to clone the CodeGuru Reviewer sample repository.
git clone --mirror https://github.com/aws-samples/amazon-codeguru-reviewer-sample-app

d. Run the following command to change directories to the directory where you made the clone.
cd amazon-codeguru-reviewer-sample-app.git

e. Run the following command to push the clone to your AWS CodeCommit repository.

Note: This command is using git-remote-codecommit installed by default to AWS CloudShell. For details on this utility, see Setup steps for HTTPS connections to AWS CodeCommit with git-remote-codecommit.

git push codecommit://CodeGuruReviewerRepo --all

Step 2. Associate the sample repository with CodeGuru Reviewer

In this step, you create an association of CodeGuru Reviewer with the sample code repository created in the previous step. Since CodeGuru is now subscribed to the pull request notification from that repository, any new pull request created notifies CodeGuru and triggers a code review scan.

Complete the following steps to associate the sample repository.

Note: For more information, see Working with repository associations in Amazon CodeGuru Reviewer in the Amazon CodeGuru Reviewer documentation.


a. Open the Amazon CodeGuru console. In the Getting started box, choose CodeGuru Reviewer and then choose Get started.
tutorial-codeguru-reviewer-get-started

b. On the Associate repository page, for Select source provider, choose AWS CodeCommit. For Repository location, choose CodeGuruReviewerRepo. Then, choose Associate.
tutorial-codeguru-reviewer-associate-repository
CodeGuru Reviewer begins associating the repository and displays a Status of Associating.
tutorial-codeguru-reviewer-associating-repository
Wait for the Status to change from Associating to Associated before continuing to the next step.
tutorial-codeguru-reviewer-associated-repository-status

Step 3. Create a new code commit

In this step, you use the AWS CloudShell browser-based shell to create a new branch and code commit.

Complete the following steps to create the branch and code commit.

Note: For more information, see Tutorial: Using CodeCommit in AWS CloudShell in the AWS CloudShell documentation.


a. Open AWS CloudShell.

AWS CloudShell

b. Run the following command to change directories to the default directory.
cd ~

c. Run the following command to clone the sample repository and create a new branch.
git clone codecommit://CodeGuruReviewerRepo 

d. Run the following command to change directories.
cd CodeGuruReviewerRepo

e. Run the following command to check out the dev branch.
git checkout -b dev

f. Run the following command to copy the Java class at src/main/java/com/shipmentEvents/handlers/EventHandler.java into src/main/java/com/shipmentEvents/demo.
cp src/main/java/com/shipmentEvents/handlers/EventHandler.java src/main/java/com/shipmentEvents/demo/

g. Stage your files in the repository.
git add --all

h. Commit the staged version of the file to to the repository.
git commit -m "new demo file"

Note: If you are prompted for configuration infomation to complete the commit, use the following format.

git config --global user.name "Jane Doe"
git config --global user.email janedoe@example.com

i. Push your changes to the CodeCommit repository.
git push --set-upstream origin dev

Step 4. Create a pull request

In this step, you create a pull request in the AWS CodeCommit console. Once the pull request has been created, the source code changes are automatically evaluated by Amazon CodeGuru Reviewer. Pull request code reviews are created automatically when you create a pull request from your repository context on an associated repository. These code reviews scan the changed code in a pull request.

Complete the following steps to create a pull request and start the CodeGuru Reviewer job.

Note: For more information, see About repository analysis and pull request code reviews in the Amazon CodeGuru Reviewer documentation.


a. Open the AWS CodeCommit console and in the list of Repositories, choose CodeGuruReviewerRepo.
tutorial-codeguru-reviewer-choose-repo

b. On the Repository details page, choose Create pull request.
tutorial-codeguru-reviewer-create-pull-request

c. On the Create pull request page, for Destination, choose master. For Source, choose dev. Then, choose Compare.
tutorial-codeguru-reviewer-create-pull-request-compare

d. In the Details section, for Title, type CodeGuru Reviewer Demo. Then, choose Create pull request.
tutorial-codeguru-reviewer-create-pull-request-start

e. On the Pull request details page, choose the Activity tab to view the Amazon CodeGuru Reviewer job status. Once the job status changes from In progress to Completed, you can view your recommendations.
tutorial-codeguru-reviewer-job-status

Step 5. View recommendations and provide feedback

In this step, you view the recommendations generated by the Amazon CodeGuru Reviewer job. You can provide feedback on the recommendations, incorporate recommendations, and reply to recommendations. The recommendations are available through the Amazon CodeGuru Reviewer console and the AWS CodeCommit console. This step walks you through viewing and providing feedback to recommendations in the AWS CodeCommit console. 

Complete the following steps to view recommendations from and provide feedback on CodeGuru Reviewer job.

Note: For more information, see Review a pull request in the AWS CodeCommit documentation and see View recommendations and provide feedback in the Amazon CodeGuru Reviewer documentation.


a. On the Pull request details page, wait for the Amazon CodeGuru Reviewer job status to show Completed. Then, scroll down to Activity history to view recommendations.

Each recommendation highlights the line of code at risk along with an explanation of the problem detected, the proposed fix, and any additional examples to reference.

tutorial-codeguru-reviewer-recommendations-list

b. Optionally, choose the emoji reaction drop-down list to indicate whether the recommendation is helpful.

tutorial-codeguru-reviewer-emoji-feedback

c. Optionally, choose Reply and provide text feedback.

Note: Feedback and comments will be shared with Amazon CodeGuru Reviewer and might be used to improve the service.

tutorial-codeguru-reviewer-text-feedback

Step 6. Clean up

In this step, you terminate the resources you used in this lab.

Important: Terminating resources that are not actively being used reduces costs and is a best practice. Not terminating your resources will result in charges to your account.


Disassociate the sample respository from CodeGuru Reviewer:

  1. Open the Amazon CodeGuru console.
  2. In the left navigation pane, choose Repositories.
  3. Choose the CodeGuruReviewerRepo then choose Disassociate repository.

Delete the CodeCommit files and repository:

  1. Open AWS CloudShell.
  2. Run the following commands.
cd ~
rm -rf amazon-codeguru-reviewer-sample-app.git
rm -rf CodeGuruReviewerRepo
aws codecommit delete-repository --repository-name CodeGuruReviewerRepo

Congratulations

You evaluated source code changes using machine learning in the cloud with Amazon CodeGuru Reviewer! You completed this task by associating an AWS CodeCommit repository, creating a pull request, and then reviewing recommendations created by Amazon CodeGuru Reviewer. These recommendations can supplement the existing peer review process and improve the quality of the code for your applications.

Was this tutorial helpful?

Thank you
Please let us know what you liked.
Close
Sorry to disappoint you
Is something out-of-date, confusing or inaccurate? Please help us improve this tutorial by providing feedback.
Close

Learn more

Learn more about Amazon CodeGuru Reviewer by reading the Amazon CodeGuru Reviewer User Guide.

Learn about Amazon CodeGuru Profiler

Amazon CodeGuru Profiler collects runtime performance data from your live applications, and provides recommendations that can help you fine-tune your application performance.

Explore other machine learning services tutorials