Skip to content

Initial commit #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
29b0da1
initial commit
Dec 17, 2021
b65b0aa
fixed build logic
Dec 17, 2021
9959fc4
adding actions
Dec 17, 2021
d0d24a6
action fixes
Dec 17, 2021
3f0e2cb
more logging
Dec 17, 2021
a87c09d
switch region for self test
Dec 17, 2021
69f460f
adding deployment action
Dec 23, 2021
b248dfb
release stuff
Dec 23, 2021
afda619
trying
Dec 23, 2021
95261b6
clean up dependencies
Dec 23, 2021
e414cef
build magic
Dec 23, 2021
a18e516
work work
Dec 23, 2021
d943a34
work work
Dec 23, 2021
6c54b31
work work
Dec 23, 2021
dc82d8e
working now
Dec 23, 2021
bc29f51
working now
Dec 23, 2021
7242223
working now
Dec 23, 2021
fbe710c
looks complete
Dec 23, 2021
87e37af
working on the readme
Dec 23, 2021
e9c4d39
improved readme, clean up dependencies, build with java matrix
Dec 30, 2021
26d33e7
improvements based on xxz's comments
Dec 30, 2021
94735b7
do not fail if git remote is unknown
Jan 5, 2022
9732a40
addressing review comments
Jan 6, 2022
7416eef
addressing next round of comments
Jan 7, 2022
5e1305a
updating gitignore
Jan 7, 2022
9c43535
fix debug message
Jan 10, 2022
030823e
bug fixes and making the git folder not required for package scans
Jan 11, 2022
c71b9f4
changing long param name from repository to root-dir
Jan 11, 2022
4966c48
typo
Jan 11, 2022
c974590
bugfix
Jan 11, 2022
9b31d0a
remove unnecessary field
Jan 11, 2022
6976f2d
typo
Jan 12, 2022
de2f054
upgrading to aws sdk v2 and some bug fixes from the builder toolbox v…
Jan 19, 2022
92500e1
Better error handling if association fails. Support for KMS keys
Jan 20, 2022
2d6d824
Addressing xxz's comments
Jan 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/guru-reviewer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Created using https://github.com/aws-samples/aws-codeguru-reviewer-cicd-cdk-sample
name: Analyze with CodeGuru Reviewer

on: [push]

permissions:
id-token: write
contents: read
security-events: write

jobs:
build:
name: Run CodeGuru Reviewer
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
continue-on-error: true
id: iam-role
with:
role-to-assume: arn:aws:iam::048169001733:role/GuruGitHubCICDRole
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we double checked with security team on if it's okay to expose the role ARN in public Github repository?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security review is ongoing ... its a question in the ticket, but no answer yet

aws-region: us-west-2

- uses: actions/checkout@v2
if: steps.iam-role.outcome == 'success'
with:
fetch-depth: 0

- name: Set up JDK 1.8
if: steps.iam-role.outcome == 'success'
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build project
if: steps.iam-role.outcome == 'success'
run: ./gradlew clean installDist

- name: CodeGuru Reviewer
if: steps.iam-role.outcome == 'success'
id: codeguru
uses: aws-actions/[email protected]
continue-on-error: false
with:
s3_bucket: codeguru-reviewer-build-artifacts-048169001733-us-west-2
build_path: ./build/libs

- name: Upload review result
if: steps.iam-role.outcome == 'success' && steps.codeguru.outcome == 'success'
continue-on-error: true
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: codeguru-results.sarif.json
32 changes: 32 additions & 0 deletions .github/workflows/java-compatible.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

name: Build with different JDKs

on:
push:
branches:
- main

permissions:
id-token: write
contents: write

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '8', '11', '16', '17' ]
name: Java ${{ matrix.Java }} build

steps:
- uses: actions/checkout@v2
- name: Setup java
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Build project
run: ./gradlew clean installDist
- name: Run cli
run: ./build/install/aws-codeguru-cli/bin/aws-codeguru-cli

57 changes: 57 additions & 0 deletions .github/workflows/self-test-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Created using https://github.com/aws-samples/aws-codeguru-reviewer-cicd-cdk-sample
name: Self-test and release

on:
push:
branches:
- main

permissions:
id-token: write
contents: write

jobs:
build:
name: Build, self-test, release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build project
run: ./gradlew clean installDist distZip

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
continue-on-error: true
id: iam-role
with:
role-to-assume: arn:aws:iam::048169001733:role/GuruGitHubCICDRole
aws-region: us-west-2

- name: Self Test
if: steps.iam-role.outcome == 'success'
run: |
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli --region us-west-2 -r . -s src/main/java -b build/libs -c HEAD^:HEAD --no-prompt

- name: Get Release Version
run: |
echo "::set-output name=TAG_NAME::$(./gradlew properties -q | grep "version:" | awk '{print $2}')"
id: version

- name: Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.version.outputs.TAG_NAME }}
prerelease: false
draft: false
body: >
Version ${{ steps.version.outputs.TAG_NAME }} of the AWS CodeGuru Reviewer CLI.
files: |
./build/distributions/aws-codeguru-cli.zip
LICENSE
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build
.gradle
.vscode
.guru
code-guru
.DS_Store
.idea
test-output*
106 changes: 100 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,106 @@
## My Project
# CodeGuru Reviewer CLI Wrapper
Simple CLI wrapper for CodeGuru reviewer that provides a one-line command to scan a local clone of a repository and
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably want to let customer know that this is not stand alone, and it does make API calls toward CodeGuru Reviewer SDK to get recommendations. It might also generate metering fees.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

receive results. This CLI wraps the [AWS CLI](https://aws.amazon.com/cli/) commands to communicated with
[AWS CodeGuru Reviewer](https://aws.amazon.com/codeguru/). Using CodeGuru Reviewer may generate metering fees
in your AWS account. See the [CodeGuru Reviewer pricing](https://aws.amazon.com/codeguru/pricing/) for details.

TODO: Fill this README out!
### Before you start

Be sure to:
Before we start, let's make sure that you can access an AWS account from your computer.
Follow the credential setup process for the [AWS CLI](https://github.com/aws/aws-cli#configuration).
The credentials must have at least the following permissions:

* Change the title in this README
* Edit your repository description on GitHub
```json
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried update the test role to have following permission policy and validate if it works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh ... good that you asked; scanning still worked, but creating new associations failed. I had to add 2 more S3 permissions.

"Version": "2012-10-17",
"Statement": [
{
"Action": [
"codeguru-reviewer:ListRepositoryAssociations",
"codeguru-reviewer:AssociateRepository",
"codeguru-reviewer:DescribeRepositoryAssociation",
"codeguru-reviewer:CreateCodeReview",
"codeguru-reviewer:DescribeCodeReview",
"codeguru-reviewer:ListRecommendations"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll also need s3:CreateBucket here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

"s3:CreateBucket",
"s3:GetBucket*",
"s3:List*",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::codeguru-reviewer-cli-*",
"arn:aws:s3:::codeguru-reviewer-cli-*/*"
],
"Effect": "Allow"
}
]
}
```


### Download the CLI and scan an Example

You can download the [aws-codeguru-cli](releases/download/latest/aws-codeguru-cli.zip) from the releases section.
Download the latest version and add it to your `PATH`:
```
curl -OL https://github.com/martinschaef/aws-codeguru-cli/releases/download/latest/aws-codeguru-cli.zip
unzip aws-codeguru-cli.zip
export PATH=$PATH:./aws-codeguru-cli/bin
```

Now, lets download an example project (requires Maven):
```
git clone https://github.com/aws-samples/amazon-codeguru-reviewer-sample-app
cd amazon-codeguru-reviewer-sample-app
mvn clean compile
```
After compiling, we can run CodeGuru with:
```
aws-codeguru-cli --root-dir ./ --build target/classes --src src --output ./output
open output/codeguru-report.html
```
where `--root-dir .` specifies that the root of the project that we want to analyze. The option `--build target/classses` states that the build artifacts are located under `./target/classes` and `--src` says that we only want to analyze source files that are
located under `./src`. The option `--output ./output` specifies where CodeGuru should write its recommendations to. By default,
CodeGuru produces a Json and Html report.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. let's also log the json file path in the output. :)
  2. html report
  • html file is currently using rule id for rule field, consider adding a new field rule name and rule id to differentiate these two.
  • for line numbers, if start line and end line are the same, we can display one number; when they are different, we should show a range, instead of a single line. Example finding from amazon-codeguru-reviewer-sample-app/src/resources/setup.yml, line range should be line 9 ~ line 16.
  • is it better to sort the recommendations by severity or category? similar to what check style does.
  • Other than that, the currently html content looks good to me, we should it get it reviewed by doc writer, PM, and UX as well.
CodeGuru will preform a full repository analysis if you do not provide a commit range.
For pricing details see: https://aws.amazon.com/codeguru/pricing/
Do you want to perform a full repository analysis? (y/n): y
Starting analysis of /Users/xxz/workspace/test-repo/github-repo/amazon-codeguru-reviewer-sample-app with association arn:aws:codeguru-reviewer:us-east-1:013991436161:association:38fe98b9-05da-4980-a90b-6cbb663fc801 and S3 bucket codeguru-reviewer-cli-013991436161-us-east-1
2021-12-30 10:07:03,013 WARN - JAXB is unavailable. Will fallback to SDK implementation which may be less performant.If you are using Java 9+, you will need to include javax.xml.bind:jaxb-api as a dependency.
Started new CodeGuru Reviewer scan: https://console.aws.amazon.com/codeguru/reviewer?region=us-east-1#/codereviews/details/arn:aws:codeguru-reviewer:us-east-1:013991436161:association:38fe98b9-05da-4980-a90b-6cbb663fc801:code-review:RepositoryAnalysis-codeguru-reviewer-cli-51b316a8-7ac2-42b2-920c-36ed3d1ebb20
.........................................................................................................................................................:)
Directory ./output already exists; previous results may be overriden.

Report with 11 recommendations written to: file:///Users/xxz/workspace/test-repo/github-repo/amazon-codeguru-reviewer-sample-app/output/codeguru-report.html
Analysis finished.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address the comments above. As for:

is it better to sort the recommendations by severity or category? similar to what check style does.
my opinion is that you want all recommendations for one file next to each other, but that is subjective. If this thing catches on, we will probably spend more time on the html report and add a little JS so you can sort and filter.


You can provide your own bucket name using the `--bucket-name` option. Note that, currently, CodeGuru Reviewer only
accepts bucket names that start with the prefix `codeguru-reviewer-`.

### Running from CI/CD

You can use this CLI to run CodeGuru from inside your CI/CD pipeline. See [this action](.github/workflows/self-test-and-release.yml#L30-L41) as an example. First, you need credentials for a role with the permissions mentioned above. If you already scanned
the repository once with the CLI, the S3 bucket has been created, and the you do not need the `s3:CreateBucket*` permission anymore.

Then you can run the CLI in non-interactive mode using the `--no-prompt` option. Further, you can specify a region and
AWS profile using the `--region` and `--profile` options as needed:
```
aws-codeguru-cli --region [BUCKET REGION] --no-prompt -r ./ ...
```
obtain the commit range works differently for different CI/CD providers. For example, GitHub provides the relevant
commits via environment variables such as `${{ github.event.before }}` and `${{ github.event.after }}`.

### Build from Source

To build the project, you need Java 8 or later. Checkout this repository and run:
```
./gradlew installDist
```
and now run your local build with:
```
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli
```
you can run a self-test with:
```
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli -r . -s src/main/java -b build/libs -c HEAD^:HEAD
```

## Security

Expand All @@ -14,4 +109,3 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform
## License

This project is licensed under the Apache-2.0 License.

94 changes: 94 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.3"
}
}

/*
Applies core Gradle plugins, which are ones built into Gradle itself.
*/
plugins {
// Java for compile and unit test of Java source files. Read more at:
// https://docs.gradle.org/current/userguide/java_plugin.html
id 'java'

// Checkstyle for style checks and reports on Java source files. Read more at:
// https://docs.gradle.org/current/userguide/checkstyle_plugin.html
id 'checkstyle'

id 'application'
}

// SpotBugs for quality checks and reports of source files. Read more at:
// https://spotbugs.readthedocs.io/en/stable/gradle.html
apply plugin: 'com.github.spotbugs'

checkstyle {
sourceSets = [sourceSets.main]
ignoreFailures = false
}

spotbugs {
ignoreFailures.set(false)
}

repositories {
mavenCentral()
}

defaultTasks('installDist')

version = '0.0.1'
jar.archiveName = "${jar.baseName}.${jar.extension}"
distZip.archiveName = "${jar.baseName}.zip"

application {
mainClass = 'com.amazonaws.gurureviewercli.Main'
}

dependencies {
implementation 'software.amazon.awssdk:s3:2.17.113'
implementation 'software.amazon.awssdk:sts:2.17.113'
implementation 'software.amazon.awssdk:codegurureviewer:2.17.113'
implementation 'software.amazon.awssdk:sdk-core:2.17.113'

implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'

implementation 'com.beust:jcommander:1.81'

implementation 'org.eclipse.jgit:org.eclipse.jgit:5.13.0.202109080827-r'

implementation 'org.apache.logging.log4j:log4j-core:2.17.1'
implementation 'org.slf4j:slf4j-nop:2.0.0-alpha5'

// For Java 9+
implementation 'javax.xml.bind:jaxb-api:2.3.1'

implementation 'org.commonmark:commonmark:0.18.1'

implementation 'org.beryx:text-io:3.4.1'

implementation 'com.google.code.findbugs:jsr305:3.0.2'

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.mockito:mockito-junit-jupiter:4.2.0'
}

test {
useJUnitPlatform()
}

Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading