0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Amazon CodeGuru Reviewer

Last updated at Posted at 2020-08-06
  • POCメモ

【参照情報】
https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/tutorial-github-reviewer.html

デモ用のリポジトリを自分のリポジトリへフォークする。
image.png

マネジメントコンソールにログインしてリポジトリの関連付けの設定。
image.png

関連付けされたリポジトリの確認。
image.png

ローカルにソースコードをクローンし、ブランチ作成してテスト用のソースコードをコミットしてpushする。

C:\Users\gtoru>git clone https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app.git
Cloning into 'amazon-codeguru-reviewer-sample-app'...
remote: Enumerating objects: 48, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 48 (delta 8), reused 38 (delta 5), pack-reused 0
Unpacking objects: 100% (48/48), 333.27 KiB | 233.00 KiB/s, done.
C:\Users\gtoru>cd amazon-codeguru-reviewer-sample-app
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git switch -c develop
Switched to a new branch 'develop'
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git add --all

エクスプローラでファイルコピー
コピーファイル: src/main/java/com/shipmentEvents/handlers/EventHandler.java
コピー先:src/main/java/com/shipmentEvents/demo/

C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git commit -m "new demo file"
[develop 5172a56] new demo file
 1 file changed, 174 insertions(+)
 create mode 100644 src/main/java/com/shipmentEvents/demo/EventHandler.java
C:\Users\gtoru\amazon-codeguru-reviewer-sample-app>git push --set-upstream origin develop
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 2.95 KiB | 1006.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'develop' on GitHub by visiting:
remote:      https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app/pull/new/develop
remote:
To https://github.com/t-hashi/amazon-codeguru-reviewer-sample-app.git
 * [new branch]      develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.

gitでpullリクエストしてしばらくするとレビューが完了する。
しばらくすると
image.png

image.png

Git上の表示例
image.png

image.png

  • AWS SDK以外のコード例
  • リソースリーク(DB)
package com.shipmentEvents.handlers;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class TestBean {

	public void doBusiness() {
		Connection con = null;
		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
			con = ds.getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select col1 from test where col1 = 1");
			int recordCount = 0;
			while (rs.next()) {
				System.out.println(rs.getInt("col1"));
				recordCount++;
			}
			System.out.println("recordCount:" + recordCount);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				//con.close(); リソースリークさせるコードのため、わざと処理コードを省略している。
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}

image.png

image.png

image.png

image.png

image.png

※finallyで処理コード漏れした点についてレコメンデーションしてくれた。
※実際にApache Tomcat/9.0.31で設定したコネクションプールでコネクション上限数以上は待ち状態となりSQL実行されない。

  • AWS SDK以外のコード例
  • リソースリーク(ファイル)
package com.shipmentEvents.handlers;

import java.io.File;
import java.io.FileWriter;

public class TestBean2 {

	public void doBusiness() {
		
		try {
			File file = new File("c:¥¥temp¥¥test.txt");
			FileWriter fw = new FileWriter(file);
			fw.write("test");
			//fw.close();
		} catch (Exception ex) {
		}
	}
}

image.png

※ファイルのリソースハンドリングについてのレコメンデーション。
※ただしこのレベルであれば次のようにEclipseで教えてくれる。

image.png

2020/8/4追加機能(リポジトリの分析)
image.png

ブランチ指定した例は下記のとおり
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?