Amazon CodeGuru Reviewer ã¨ã¯
æ©æ¢°å¦ç¿ã使ã£ã¦ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãã®ã»ãã¥ãªãã£èå¼±æ§ãè¦ã¤ãã«ãããã°ãç¹å®ãæ¨å¥¨äºé ãæä¾ãã¦ããããµã¼ãã¹ã§ãã2022-09 ç¾å¨ãJava 㨠Python ã«å¯¾å¿ãã¦ãã¾ãã
GitHub Actions ããå®è¡ãã¦ã¿ã
AWS ãå ¬å¼æä¾ãã以ä¸ã®ã¢ã¯ã·ã§ã³ãããã®ã§ããã¡ãã®å 容ã«æ²¿ã£ã¦å®è¡ãã¦ã¿ã¾ããèªè¨¼çãä¸é¨ã¢ã¬ã³ã¸ãå ãã¦ããé¨åãããã¾ãã
å®æå½¢ã¯ãã¡ãã®ãªãã¸ããªãåç §ãã ããã
1. GitHub Actions ã®ã¯ã¼ã¯ããã¼ãã¡ã¤ã«ãæºåãã
ã¯ã¼ã¯ããã¼ã®å 容ã¯ä»¥ä¸ã®éãã§ãã 詳細ãªæ§æçã¯å ¬å¼ã®ãªãã¡ã¬ã³ã¹ ãåç §ãã ããã
# .github/workflows/codeguru-reviewer.yml name: Review on: pull_request: workflow_dispatch: jobs: review: runs-on: ubuntu-latest permissions: security-events: write id-token: write contents: read # This is required for actions/checkout steps: # Step 1: Checkout the repository and provide your AWS credentials - name: Checkout repository uses: actions/checkout@v3 with: fetch-depth: 0 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{secrets.ROLE_ARN}} role-session-name: codeguru-reviewr-session aws-region: ${{secrets.AWS_REGION}} # Step 2: Add CodeGuru Reviewer Action - name: CodeGuru Reviewer uses: aws-actions/[email protected] with: s3_bucket: ${{secrets.BUCKET_NAME}} # S3 Bucket with "codeguru-reviewer-*" prefix # Step 3: Upload results into GitHub - name: Upload review result if: ${{ github.event_name != 'push' }} uses: github/codeql-action/upload-sarif@v2 with: sarif_file: codeguru-results.sarif.json
åã¹ãããã解説ãã¾ãã
# Step 1: Checkout the repository and provide your AWS credentials - name: Checkout repository uses: actions/checkout@v3 with: fetch-depth: 0 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v1 with: role-to-assume: ${{secrets.ROLE_ARN}} role-session-name: codeguru-reviewr-session aws-region: ${{secrets.AWS_REGION}}
CodeGuru Reviewer ã®å¯¾è±¡ã¨ãªããªãã¸ããªã®ãã§ãã¯ã¢ã¦ããåã³ CodeGuru ã¸ã¢ã¯ã»ã¹ããããã® AWS èªè¨¼æ
å ±ã®è¨å®ã§ãã
ãã¤ã³ã㯠fetch-depth: 0
ã®é¨åã§ãããã¯ãã¹ã¦ã®ãã©ã³ãã¨ã¿ã°ã®å
¨å±¥æ´ãåå¾ããè¨å®ã§ããé常㮠CI ã§ã¯ fetch-depth: 1
ã®ããã«ç´è¿ã®ã³ãããã®ã¿åå¾ãããã¨ãå¤ãã¨æãã¾ãããCodeGuru Reviewer ã使ãå ´åã¯ãã®è¨å®ãå¿
è¦ãªãã¨ã«æ³¨æã§ãã
ã¾ããåèããã¥ã¡ã³ãã§ã¯ actions/checkout@v2
㨠v2
ã使ç¨ããã¦ãã¾ããããæ¤è¨¼æç¹ã§ v3
ããªãªã¼ã¹ããã¦ãã¾ããã®ã§å¤æ´ãã¦ãã¾ããï¼ç¹ã«åé¡ãªãåä½ããæ§ã§ãï¼
AWS ã¸ã®èªè¨¼ã«ã¤ãã¦ããåèããã¥ã¡ã³ãã§ã¯ AWS_ACCESS_KEY_IDãAWS_SECRET_ACCESS_KEY ãæå®ãã¦ãã¾ãããããã»ãã¥ã¢ãªæ¹æ³ã¨ã㦠OpenID Connect ã§ã®è¨å®ã¨ãã¦ãã¾ãã詳細ã¯ä»¥ä¸ã®è¨äºãåç §ãã ããã
# Step 2: Add CodeGuru Reviewer Action - name: CodeGuru Reviewer uses: aws-actions/[email protected] with: s3_bucket: ${{secrets.BUCKET_NAME}} # S3 Bucket with "codeguru-reviewer-*" prefix
CodeGuru Reviewer ã®å
¬å¼ã¢ã¯ã·ã§ã³ãå¼ã³åºãã¾ãã
ãã¤ã³ã㯠s3_bucket
ã§ããCodeGuru Reviewer ã®ã¤ã³ãããã¨ãªãã½ã¼ã¹ã³ã¼ãããã®ãã±ããã«é
ç½®ããã¾ããå½åã«ã¼ã«ã¨ã㦠codeguru-reviewer-
ãå
é ã«ã¤ããå¿
è¦ãããã¾ãã
ãã®ä»ã®ãªãã·ã§ã³ã«ã¤ãã¦ã¯ããã¥ã¡ã³ãã§ç¶²ç¾
çã«è§£èª¬ããã¦ããªãããã以ä¸ãåç
§ãã ããã
# Step 3: Upload results into GitHub - name: Upload review result if: ${{ github.event_name != 'push' }} uses: github/codeql-action/upload-sarif@v2 with: sarif_file: codeguru-results.sarif.json
ã³ã¼ãã®è§£æçµæ㯠SARIF ã¨å¼ã°ãããã©ã¼ãããã§åå¾ã§ãã¾ãããããã GitHub ã«ã¢ãããã¼ããã¾ãã ããã«ãããGitHub ã® Code Scanning æ©è½ã¨é£æºãã¦è¨æ¸¬çµæã GitHub ãªãã¸ããªã§è¡¨ç¤ºãããã¨ãåºæ¥ã¾ãã 注æç¹ã¨ãã¦ã Code Scanning æ©è½ã¯ ãã©ã¤ãã¼ããªãã¸ããªã®å ´åãGitHub ã® Enterprise å¥ç´ãå¿ è¦ã§ãã
2. AWS å´ã«å¿ è¦ãªãªã½ã¼ã¹ãç¨æ
AWS å´ã«å¿ è¦ãªãªã½ã¼ã¹ãç¨æãã¾ãã以ä¸ã® CloudFormation ãã³ãã¬ã¼ããç¨æãã¾ããã ãã©ã¡ã¼ã¿ã®è§£èª¬ã¯åè¿°ã®ããã°ã¨åæ§ã§ãã
# template.yml Parameters: GitHubOrg: Type: String RepositoryName: Type: String OIDCProviderArn: Description: Arn for the GitHub OIDC Provider. Default: "" Type: String GitHubOIDCThumbprint: Description: GitHub OIDC thumbprint. see also https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html Default: 6938fd4d98bab03faadb97b34396831e3780aea1 Type: String Conditions: CreateOIDCProvider: !Equals - !Ref OIDCProviderArn - "" Resources: CodeGuruReviewerBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub codeguru-reviewer-${AWS::AccountId} BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: AES256 PublicAccessBlockConfiguration: BlockPublicAcls: true BlockPublicPolicy: true IgnorePublicAcls: true RestrictPublicBuckets: true CodeGuruReviewerRole: Type: AWS::IAM::Role Properties: ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonCodeGuruReviewerFullAccess AssumeRolePolicyDocument: Statement: - Effect: Allow Action: sts:AssumeRoleWithWebIdentity Principal: Federated: !If - CreateOIDCProvider - !Ref GithubOidcProvider - !Ref OIDCProviderArn Condition: StringLike: token.actions.githubusercontent.com:sub: !Sub repo:${GitHubOrg}/${RepositoryName}:* CodeGuruReviewerS3Policy: Type: AWS::IAM::Policy Properties: PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "s3:ListBucket" Resource: - !GetAtt CodeGuruReviewerBucket.Arn - Effect: Allow Action: - "s3:PutObject" - "s3:GetObject" Resource: - Fn::Join: - "/" - - !GetAtt CodeGuruReviewerBucket.Arn - "*" PolicyName: code-guru-reviewer-s3-policy Roles: - !Ref CodeGuruReviewerRole GithubOidcProvider: Type: AWS::IAM::OIDCProvider Condition: CreateOIDCProvider Properties: Url: https://token.actions.githubusercontent.com ClientIdList: - sts.amazonaws.com ThumbprintList: - !Ref GitHubOIDCThumbprint Outputs: CodeGuruReviewerBucketName: Value: !Ref CodeGuruReviewerBucket CodeGuruReviewerRoleArn: Value: !GetAtt CodeGuruReviewerRole.Arn GithubOidcProviderArn: Condition: CreateOIDCProvider Value: !GetAtt GithubOidcProvider.Arn
以ä¸ããã¤ã³ã解説ã§ãã
CodeGuruReviewerBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub codeguru-reviewer-${AWS::AccountId}
åè¿°ã®éããS3 ãã±ããã®å
é ã«ã¯ codeguru-reviewer-
ãã¤ããå¿
è¦ãããã¾ãã
CodeGuruReviewerRole: Type: AWS::IAM::Role Properties: ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonCodeGuruReviewerFullAccess # ä¸ç¥ CodeGuruReviewerS3Policy: Type: AWS::IAM::Policy Properties: PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "s3:ListBucket" Resource: - !GetAtt CodeGuruReviewerBucket.Arn - Effect: Allow Action: - "s3:PutObject" - "s3:GetObject" Resource: - Fn::Join: - "/" - - !GetAtt CodeGuruReviewerBucket.Arn - "*"
GitHub Actions å´ãã AWS ã¸ã¢ã¯ã»ã¹ããããã« AmazonCodeGuruReviewerFullAccess
㨠å種 S3 ã¸ã®ã¢ã¯ã»ã¹ããªã·ã¼ãä»ä¸ãã¦ãã¾ãã
3. ãã®ä»æºå
GitHub Actions ã® Secrets è¨å®
Action å
ã§ã${{secrets.XXXX}}
ã¨è¨è¼ãã¦ããé¨å㯠Secrets ã®è¨å® ãå¿
è¦ã§ãã
è¨å®å
容ã¯ä»¥ä¸ã®éãã§ãã
ã·ã¼ã¯ã¬ããå | è¨å®å 容 |
---|---|
ROLE_ARN | CFn ã§ä½æãã IAM Role ARN |
AWS_REGION | CodeGuru ãå®è¡ãã AWS ãªã¼ã¸ã§ã³ (e.g. ap-northeast-1) |
BUCKET_NAME | CFn ã§ä½æãã S3 ãã±ããå |
4. ãã«ãªã¯ã¨ã¹ããä½æããCodeGuru Reviwer ãå®è¡ãã
以ä¸ã®ãããªã³ã¼ãã追å ãããã«ãªã¯ã¨ã¹ããä½æãã¾ãã ããã¾ã§ CodeGuru Reviewer ã«æ¤ç¥ãããããã®ãã®ãªã®ã§ãã³ã¼ãèªä½ã«æå³ã¯ããã¾ããã
# src/hello.py print("Hello CodeGuru Reviewer!") import datetime base_datetime = datetime.datetime.now() - datetime.timedelta(days=365) print(base_datetime)
ãã«ãªã¯ã¨ã¹ããä½æããã¨ãä½æãã GitHub Actions ãèµ·åããCodeGuru Reviewer ã«ããå¢åã¬ãã¥ã¼ãå®è¡ããã¾ãã ææã¯ä»¥ä¸ã®ããã«é常ã®ã¬ãã¥ã¼ã³ã¡ã³ãã®å½¢ã§è¡¨ç¤ºããã¾ãã
ãªãããªãã¸ããªå ¨ä½ã«å¯¾ãããã«ãªãã¸ããªã¹ãã£ã³ãå®è¡å¯è½ (å½è©²ã¢ã¯ã·ã§ã³ãä»»æã®ãã©ã³ãã«å¯¾ãã¦æåå®è¡) ã§ããããã®å ´å 10 USD / å ã®è²»ç¨ããããã¾ãã®ã§ã注æãã ããã
ã¾ã¨ã
Amazon CodeGuru Reviewer ã GitHub Actions ããå®è¡ããæ¹æ³ã«ã¤ãã¦ãç´¹ä»ãã¾ããã ä¸åº¦è¨å®ãã¦ãã¾ãã°ããã«ãªã¯ã¨ã¹ãã®åº¦ã«èªåã§ã¬ãã¥ã¼ãè¡ã£ã¦ãããã®ã§ãã³ã¼ãã®å質ç¶æã®ããã«å°å ¥ãã¦ã¿ã¦ã¯ãããã§ããããã