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:
- Create a sample respository in AWS CodeCommit using AWS CloudShell
- Associate the sample repository with CodeGuru Reviewer
- Create a new code commit in AWS CodeCommit
- Create a pull request in AWS CodeCommit
- View recommendations from and provide feedback to CodeGuru Reviewer
- 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.
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"
git clone --mirror https://github.com/aws-samples/amazon-codeguru-reviewer-sample-app
cd amazon-codeguru-reviewer-sample-app.git
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.
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.
cd ~
git clone codecommit://CodeGuruReviewerRepo
cd CodeGuruReviewerRepo
git checkout -b dev
cp src/main/java/com/shipmentEvents/handlers/EventHandler.java src/main/java/com/shipmentEvents/demo/
git add --all
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
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.
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.
b. Optionally, choose the emoji reaction drop-down list to indicate whether the recommendation is helpful.
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.
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:
- Open the Amazon CodeGuru console.
- In the left navigation pane, choose Repositories.
- Choose the CodeGuruReviewerRepo then choose Disassociate repository.
Delete the CodeCommit files and repository:
- Open AWS CloudShell.
- 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.
Recommended next steps
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.