Skip to content

Commit

Permalink
Merge pull request #449 from github/update-v1-8bd2b351
Browse files Browse the repository at this point in the history
Merge main into v1
  • Loading branch information
Andrew Eisenberg authored and GitHub committed Apr 19, 2021
2 parents af641b2 + 8bd2b35 commit 9db4c57
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/runner.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/runner.js.map

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.js.map

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions lib/upload-lib.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ program
"Checkout path. Default is the current working directory."
)
.option("--debug", "Print more verbose output", false)
// This prevents a message like: error: unknown option '--trace-process-level'
// Remove this if commander.js starts supporting hidden options.
.allowUnknownOption()
.option(
"--trace-process-name <string>",
"(Advanced, windows-only) Inject a windows tracer of this process into a process with the given process name."
)
.option(
"--trace-process-level <number>",
"(Advanced, windows-only) Inject a windows tracer of this process into a parent process <number> levels up."
)
.action(async (cmd: InitArgs) => {
const logger = getRunnerLogger(cmd.debug);
try {
Expand Down
5 changes: 2 additions & 3 deletions src/sarif_v2.1.0_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2339,9 +2339,8 @@
"description": "The language of the messages emitted into the log file during this run (expressed as an ISO 639-1 two-letter lowercase culture code) and an optional region (expressed as an ISO 3166-1 two-letter uppercase subculture code associated with a country or region). The casing is recommended but not required (in order for this data to conform to RFC5646).",
"type": "string",
"default": "en-US",
"pattern": "^[a-zA-Z]{2}|^[a-zA-Z]{2}-[a-zA-Z]{2}]?$"
"pattern": "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$"
},

"versionControlProvenance": {
"description": "Specifies the revision in version control of the artifacts that were scanned.",
"type": "array",
Expand Down Expand Up @@ -3040,7 +3039,7 @@
"description": "The language of the messages emitted into the log file during this run (expressed as an ISO 639-1 two-letter lowercase language code) and an optional region (expressed as an ISO 3166-1 two-letter uppercase subculture code associated with a country or region). The casing is recommended but not required (in order for this data to conform to RFC5646).",
"type": "string",
"default": "en-US",
"pattern": "^[a-zA-Z]{2}|^[a-zA-Z]{2}-[a-zA-Z]{2}]?$"
"pattern": "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$"
},

"contents": {
Expand Down
43 changes: 43 additions & 0 deletions src/upload-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,46 @@ test("finding SARIF files", async (t) => {
]);
});
});

test("populateRunAutomationDetails", (t) => {
let sarif = '{"runs": [{}]}';
const analysisKey = ".github/workflows/codeql-analysis.yml:analyze";

let expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"}}]}';

let modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
'{"language": "javascript", "os": "linux"}'
);
t.deepEqual(modifiedSarif, expectedSarif);

// check the environment sorting
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
'{"os": "linux", "language": "javascript"}'
);
t.deepEqual(modifiedSarif, expectedSarif);

// check that an empty environment produces the right results
expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
"{}"
);
t.deepEqual(modifiedSarif, expectedSarif);

// check that an empty environment produces the right results
sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
'{"os": "linux", "language": "javascript"}'
);
t.deepEqual(modifiedSarif, expectedSarif);
});
37 changes: 37 additions & 0 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ export function combineSarifFiles(sarifFiles: string[]): string {
return JSON.stringify(combinedSarif);
}

// Populates the run.automationDetails.id field using the analysis_key and environment
// and return an updated sarif file contents.
export function populateRunAutomationDetails(
sarifContents: string,
analysis_key: string | undefined,
environment: string | undefined
): string {
if (analysis_key === undefined) {
return sarifContents;
}
let automationID = `${analysis_key}/`;

// the id has to be deterministic so we sort the fields
if (environment !== undefined && environment !== "null") {
const environmentObject = JSON.parse(environment);
for (const entry of Object.entries(environmentObject).sort()) {
automationID += `${entry[0]}:${entry[1]}/`;
}
}

const sarif = JSON.parse(sarifContents);
for (const run of sarif.runs || []) {
if (run.automationDetails === undefined) {
run.automationDetails = {
id: automationID,
};
}
}

return JSON.stringify(sarif);
}

// Upload the given payload.
// If the request fails then this will retry a small number of times.
async function uploadPayload(
Expand Down Expand Up @@ -321,6 +353,11 @@ async function uploadFiles(
checkoutPath,
logger
);
sarifPayload = populateRunAutomationDetails(
sarifPayload,
analysisKey,
environment
);

const zippedSarif = zlib.gzipSync(sarifPayload).toString("base64");
const checkoutURI = fileUrl(checkoutPath);
Expand Down

0 comments on commit 9db4c57

Please sign in to comment.