Skip to content

Commit 560c539

Browse files
authored
Merge pull request #23 from martinschaef/suppression
Support aws-codeguru-reviewer.yml files
2 parents 3d593bb + 94dfad1 commit 560c539

File tree

10 files changed

+88
-12
lines changed

10 files changed

+88
-12
lines changed

.github/workflows/self-test-and-release.yml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,28 @@ jobs:
4343
- name: Run Code Guru
4444
if: steps.iam-role.outcome == 'success'
4545
run: |
46-
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli --region us-west-2 -r python-code --no-prompt -c "c5f9c93d5605bfe3a4de02896a26b7b91eb7c007:a574f23f6ea3fe607a6d33cc6c8fe28d81ab37a2"
46+
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli --region us-west-2 -r python-code --no-prompt -c "5a0c9579b6c9b0ea7a5f5c1b9328bb7100c7a16e:a574f23f6ea3fe607a6d33cc6c8fe28d81ab37a2"
4747
48-
- name: Print Summary of Findings Linux
48+
- name: Upload findings artifact
4949
if: steps.iam-role.outcome == 'success'
50+
uses: actions/upload-artifact@v1
51+
with:
52+
name: recommendations_python_${{ matrix.os }}
53+
path: code-guru/
54+
55+
- name: Print Summary of Findings on ${{ matrix.os }}
56+
if: steps.iam-role.outcome == 'success' && matrix.os != 'windows-latest' # cat and jq don't work the same way on windows
5057
run: |
5158
echo "detector,filePath,lineNumber" > summary-of-results.csv
5259
jq -r '.runs[0].results[] | {ruleId: .ruleId, firstFile: .locations[0].physicalLocation.artifactLocation.uri, firstLine: .locations[0].physicalLocation.region.startLine} | join(",")' \
5360
code-guru/recommendations.sarif.json | sort >> summary-of-results.csv
5461
cat summary-of-results.csv
5562
56-
- name: Validate Findings Linux
63+
- name: Validate Findings ${{ matrix.os }}
5764
if: steps.iam-role.outcome == 'success'
5865
shell: bash
5966
env:
60-
EXPECTED: 112
67+
EXPECTED: 98
6168
run: |
6269
[[ $(jq -r '.runs[0].results[].ruleId' code-guru/recommendations.sarif.json | wc -l) -eq $EXPECTED ]] || { echo "Expected $EXPECTED recommendations but got $(jq -r '.runs[0].results[].ruleId' code-guru/recommendations.sarif.json | wc -l)"; exit 1; }
6370
@@ -78,7 +85,7 @@ jobs:
7885
matrix:
7986
os: [ubuntu-latest, windows-latest]
8087
name: Self Test Java on ${{ matrix.os }}
81-
steps:
88+
steps:
8289
- uses: actions/checkout@v2
8390
- name: Set up JDK 1.8
8491
uses: actions/setup-java@v1
@@ -115,15 +122,22 @@ jobs:
115122
run: |
116123
./build/install/aws-codeguru-cli/bin/aws-codeguru-cli --region us-west-2 -r amazon-codeguru-samples -s amazon-codeguru-samples/src/main/java -b amazon-codeguru-samples/build/libs --no-prompt
117124
118-
- name: Print Summary of Findings Linux
125+
- name: Upload findings artifact
119126
if: steps.iam-role.outcome == 'success'
127+
uses: actions/upload-artifact@v1
128+
with:
129+
name: recommendations_java_${{ matrix.os }}
130+
path: code-guru/
131+
132+
- name: Print Summary of Findings on ${{ matrix.os }}
133+
if: steps.iam-role.outcome == 'success' && matrix.os != 'windows-latest' # cat and jq don't work the same way on windows
120134
run: |
121135
echo "detector,filePath,lineNumber" > summary-of-results.csv
122136
jq -r '.runs[0].results[] | {ruleId: .ruleId, firstFile: .locations[0].physicalLocation.artifactLocation.uri, firstLine: .locations[0].physicalLocation.region.startLine} | join(",")' \
123137
code-guru/recommendations.sarif.json | sort >> summary-of-results.csv
124138
cat summary-of-results.csv
125139
126-
- name: Validate Findings Linux
140+
- name: Validate Findings ${{ matrix.os }}
127141
if: steps.iam-role.outcome == 'success'
128142
shell: bash
129143
env:
@@ -158,7 +172,7 @@ jobs:
158172
uses: actions/download-artifact@v1
159173
with:
160174
name: pass_status_windows-latest
161-
- name: Set the statuses of the windows and linux job as output parameters
175+
- name: Set the statuses of the windows and ${{ matrix.os }} job as output parameters
162176
id: set_outputs
163177
run: |
164178
echo "::set-output name=status_ubuntu-latest::$(<pass_status_ubuntu-latest/status_ubuntu-latest.txt)"

aws-codeguru-reviewer.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1.0
2+
3+
# Sample YAML file to suppress recommendations in test code.
4+
# https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/recommendation-suppression.html
5+
6+
excludeFiles:
7+
- "tst/**"
8+
- "test-data/**"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repositories {
2323

2424
defaultTasks 'clean', 'check', 'installDist'
2525

26-
version = '0.0.4'
26+
version = '0.0.5'
2727
jar.archiveName = "${jar.baseName}.${jar.extension}"
2828
distZip.archiveName = "${jar.baseName}.zip"
2929

src/main/java/com/amazonaws/gurureviewercli/util/ZipUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public static void pack(final List<Path> sourceDirPaths,
6565
final String zipFilePath) throws IOException {
6666

6767
val files = getFilesInDirectories(sourceDirPaths);
68+
val codeGuruConfigFile = relativeRoot.resolve("aws-codeguru-reviewer.yml");
69+
if (codeGuruConfigFile != null && codeGuruConfigFile.toFile().isFile()) {
70+
files.add(codeGuruConfigFile);
71+
}
6872
packFiles(files, relativeRoot, Paths.get(zipFilePath));
6973
}
7074

src/test/java/com/amazonaws/gurureviewercli/adapter/ArtifactAdapterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ public void test_zipAndUpload_happyCaseBuildDir() throws Exception {
162162
// only include files from the util dir.
163163
val repoDir = Paths.get("./test-data/fake-repo");
164164
val buildArtifacts = repoDir.resolve("build-dir/lib");
165-
final List<Path> buildDirs = Arrays.asList(buildArtifacts);
166165

167166
val config = Configuration.builder()
168167
.s3Client(s3client)

src/test/java/com/amazonaws/gurureviewercli/util/ZipUtilsTest.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
package com.amazonaws.gurureviewercli.util;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.nio.file.Files;
6+
import java.nio.file.Path;
57
import java.nio.file.Paths;
68
import java.util.Arrays;
9+
import java.util.Comparator;
710
import java.util.Enumeration;
811
import java.util.HashSet;
912
import java.util.zip.ZipEntry;
1013
import java.util.zip.ZipFile;
1114

1215
import lombok.val;
16+
import org.junit.jupiter.api.AfterEach;
1317
import org.junit.jupiter.api.Assertions;
18+
import org.junit.jupiter.api.BeforeEach;
1419
import org.junit.jupiter.api.Test;
1520

1621
public class ZipUtilsTest {
1722

23+
private Path workDir;
24+
25+
@BeforeEach
26+
void beforeEach() throws IOException {
27+
workDir = Files.createTempDirectory("zip-files");
28+
}
29+
30+
@AfterEach
31+
void afterEach() throws IOException {
32+
Files.walk(workDir)
33+
.sorted(Comparator.reverseOrder())
34+
.map(Path::toFile)
35+
.forEach(File::delete);
36+
}
37+
1838
@Test
19-
public void test_packUnpack() throws IOException {
39+
void test_packUnpack() throws IOException {
2040
val testDir = Paths.get("test-data/fake-repo");
21-
val zipName = Files.createTempDirectory("zip-files").resolve("test.zip").toString();
41+
val zipName = workDir.resolve("test.zip").toString();
2242
ZipUtils.pack(Arrays.asList(testDir), testDir, zipName);
2343
try (ZipFile zipFile = new ZipFile(zipName)) {
2444
Enumeration<? extends ZipEntry> entries = zipFile.entries();
@@ -35,4 +55,28 @@ public void test_packUnpack() throws IOException {
3555
Assertions.assertTrue(expectedFileNames.isEmpty());
3656
}
3757
}
58+
59+
/*
60+
If a aws-codeguru-reviewer.yml file is present, it has to be included in the zip file even if the root folder
61+
is not mentioned in the list of source directories.
62+
*/
63+
@Test
64+
void test_packUnpackWithConfig() throws IOException {
65+
val testDir = Paths.get("test-data/fake-repo-with-config");
66+
val zipName = workDir.resolve("test.zip").toString();
67+
ZipUtils.pack(Arrays.asList(testDir.resolve("src-dir")), testDir, zipName);
68+
try (ZipFile zipFile = new ZipFile(zipName)) {
69+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
70+
val expectedFileNames =
71+
new HashSet<String>(Arrays.asList("src-dir/included-src.txt",
72+
"aws-codeguru-reviewer.yml"));
73+
while (entries.hasMoreElements()) {
74+
ZipEntry entry = entries.nextElement();
75+
Assertions.assertFalse(entry.toString().contains("\\"), "Unexpected zip entry " + entry);
76+
Assertions.assertTrue(expectedFileNames.contains(entry.toString()));
77+
expectedFileNames.remove(entry.toString());
78+
}
79+
Assertions.assertTrue(expectedFileNames.isEmpty());
80+
}
81+
}
3882
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 1.0
2+
# See https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/recommendation-suppression.html
3+
excludeFiles:
4+
- "tst/**"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdf

0 commit comments

Comments
 (0)