下記で、Cloud9上でJava(Amazon Corretto 11)のSAMアプリケーションをCodeCommitで管理し、”CodeGuru Reviewer“でコードレビューする準備ができました。



“CodeGuru Reviewer”とは?
そもそも”CodeGuru Reviewer”とは下記の通りで、今回は、CodeCommit上のJavaコード(SAMアプリケーション)をレビューします。(言語はJavaのみ対応)
Q: Amazon CodeGuru Reviewer とは何ですか?
Amazon CodeGuru Reviewer は、Java コードに関する重大な欠陥や、コーディングのベストプラクティスからの逸脱を識別するための、自動コードレビューサービスです。プルリクエスト内のコード行をスキャンし、主要なオープンソースプロジェクトおよび Amazon コードベースから取り入れた標準に基づいた、インテリジェントな推奨事項を提供します。Amazon CodeGuru Reviewer は、GitHub、GitHub Enterprise、Bitbucket、および AWS CodeCommit など、広く使用されているソース管理システムにある既存のコードレビューワークフローとシームレスに統合し、コード品質を改善するための実用的な推奨事項を提供します。
Amazon CodeGuru のよくある質問 – アマゾン ウェブ サービス
“GodeGuru Reviewer”でコードレビュー
レビューするコード
下記のリソースリークを起こしているコードをレビューします。
private String getPageContents(String address) throws IOException{
URL url = new URL(address);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}Code language: JavaScript (javascript)
コードレビュー
上述した記事で作成したCodeCommitのリポジトリに”GodeGuru Reviewer”が関連付けられていることが確認できます。

今回は「リポジトリの分析」でコードレビューします。

対象のリポジトリとブランチを指定します。

リポジトリが分析されると「コードレビュー」が作成されます。(1件の推奨事項があります)

推奨事項を確認します。想定どおりリソースリークの指摘がされ、try-with-resources文でのFix方法も教えてくれます。

Fixして再レビュー
推奨されたtry-with-resources文を用いて、下記のようにコードをFixして再レビューします。
private String getPageContents(String address) throws IOException{
URL url = new URL(address);
try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}
}Code language: JavaScript (javascript)
推奨事項を確認すると、今度は0になりました。

ちなみに差分は下記の通りです。

提供される推奨事項の種類
推奨事項は下記の種類で提供されています。
- AWS best practices
- Concurrency
- Resource leak prevention
- Sensitive information leak prevention
- Common coding best practices
- Refactoring
- Input validation
推奨事項の種類の詳細をドキュメントから引用しておきます。
AWS best practices
AWS APIs contain a rich set of features to ensure performance and stability of software. For example, usage patterns such as batching and waiters lead to enhanced performance and more efficient, maintainable code. Use of pagination is often required to ensure correctness of code. Developers might fail to use the right constructs when using AWS APIs, and this leads to problems in production. AWS best practices provide recommendations on correct use of AWS APIs, which leads to availability and performance gains.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Concurrency
CodeGuru Reviewer identifies problems with implementations of concurrency in multithreaded code. Concurrency defects are often subtle and escape even expert programmers. Incorrect implementations of concurrency can lead to incorrect code or performance issues. CodeGuru Reviewer identifies atomicity violations that might result in correctness problems, and it identifies excessive synchronizations that might result in performance problems.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Resource leak prevention
CodeGuru Reviewer looks for lines of code where resource leaks might be occurring. Resource leaks can cause latency issues and outages. CodeGuru Reviewer can point to code where this might be occurring and suggest handling the resources in a different way.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Sensitive information leak prevention
Sensitive information in code should not be shared with unauthorized parties. CodeGuru Reviewer looks for lines of code where sensitive information might be leaking, and suggests different ways to handle the data.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Common coding best practices
CodeGuru Reviewer checks parameters and looks for lines of code that could create bugs. There are many common coding errors that cause bugs to happen, such as forgetting to check whether an object is null before setting it, reassigning a synchronized object, or forgetting to initialize a variable along an exception path. CodeGuru Reviewer can point to the location of those errors and other sources of problems in code.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Refactoring
CodeGuru Reviewer looks for lines of code that appear to be duplicated or similar enough to be refactored. Refactoring can help improve code maintainability.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
Input validation
It’s important to detect unexpected input that arrives at a computation, and to apply appropriate validation before the computation starts. Input validation is an important layer of defense against unintentional errors, such as client component changes, and malicious attacks, such as code injection or denial of service. CodeGuru Reviewer looks for lines of code that process input data and suggests additional validation where it’s needed.
Recommendations in Amazon CodeGuru Reviewer – Amazon CodeGuru Reviewer
最後に
これで、Cloud9上でJava(Amazon Corretto 11)のSAMアプリケーションを作成し、CodeCommitでソースを管理することで、”CodeGuru Reviewer“でコードレビューする環境が整いました。しかし、そもそもCloud9でJavaコードを書くことが現実的ではありませんでした。何故ならコード補完が無いからです。ということで開発環境ネタは、まだまだ続きます!


