-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CLI interface to the upload-sarif action
- Loading branch information
Robert Brignull
committed
Aug 11, 2020
1 parent
bcf676e
commit 6d7a135
Showing
15 changed files
with
356 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/cli/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Command } from 'commander'; | ||
import * as path from 'path'; | ||
|
||
import { getCLILogger } from './logging'; | ||
import { parseRepositoryNwo } from './repository'; | ||
import * as upload_lib from './upload-lib'; | ||
|
||
const program = new Command(); | ||
program.version('0.0.1'); | ||
|
||
interface UploadArgs { | ||
sarifFile: string; | ||
repository: string; | ||
commit: string; | ||
ref: string; | ||
analysisKey: string; | ||
githubUrl: string; | ||
githubAuth: string; | ||
analysisName: string | undefined; | ||
checkoutPath: string | undefined; | ||
environment: string | undefined; | ||
} | ||
|
||
function parseGithubApiUrl(inputUrl: string): string { | ||
try { | ||
const url = new URL(inputUrl); | ||
|
||
// If we detect this is trying to be to github.com | ||
// then return with a fixed canonical URL. | ||
if (url.hostname === 'github.com' || url.hostname === 'api.github.com') { | ||
return 'https://api.github.com'; | ||
} | ||
|
||
// Add the API path if it's not already present. | ||
if (url.pathname.indexOf('/api/v3') === -1) { | ||
url.pathname = path.join(url.pathname, 'api', 'v3'); | ||
} | ||
|
||
return url.toString(); | ||
|
||
} catch (e) { | ||
throw new Error(`"${inputUrl}" is not a valid URL`); | ||
} | ||
} | ||
|
||
program | ||
.command('upload') | ||
.description('Uploads a SARIF file, or all SARIF files from a directory, to code scanning') | ||
.requiredOption('--sarif-file <file>', 'SARIF file to upload') | ||
.requiredOption('--repository <repository>', 'Repository name') | ||
.requiredOption('--commit <commit>', 'SHA of commit that was analyzed') | ||
.requiredOption('--ref <ref>', 'Name of ref that was analyzed') | ||
.requiredOption('--analysis-key <key>', 'Identifies the analysis, for use matching up equivalent analyses on different commits') | ||
.requiredOption('--github-url <url>', 'URL of GitHub instance') | ||
.requiredOption('--github-auth <auth>', 'GitHub Apps token, or of the form "username:token" if using a personal access token') | ||
.option('--checkout-path <path>', 'Checkout path (default: current working directory)') | ||
.option('--analysis-name <name>', 'Display name of the analysis (default: same as analysis-key') | ||
.option('--environment <env>', 'Environment (default: empty)') | ||
.action(async (cmd: UploadArgs) => { | ||
const logger = getCLILogger(); | ||
try { | ||
await upload_lib.upload( | ||
cmd.sarifFile, | ||
parseRepositoryNwo(cmd.repository), | ||
cmd.commit, | ||
cmd.ref, | ||
cmd.analysisKey, | ||
cmd.analysisName || cmd.analysisKey, | ||
undefined, | ||
cmd.checkoutPath || process.cwd(), | ||
cmd.environment, | ||
cmd.githubAuth, | ||
parseGithubApiUrl(cmd.githubUrl), | ||
'cli', | ||
logger); | ||
} catch (e) { | ||
logger.error("Upload failed"); | ||
logger.error(e); | ||
} | ||
}); | ||
|
||
program.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as core from '@actions/core'; | ||
|
||
export interface Logger { | ||
debug: (message: string) => void; | ||
info: (message: string) => void; | ||
warning: (message: string) => void; | ||
error: (message: string) => void; | ||
|
||
startGroup: (name: string) => void; | ||
endGroup: () => void; | ||
} | ||
|
||
export function getActionsLogger(): Logger { | ||
return core; | ||
} | ||
|
||
export function getCLILogger(): Logger { | ||
return { | ||
debug: console.debug, | ||
info: console.info, | ||
warning: console.warn, | ||
error: console.error, | ||
startGroup: () => undefined, | ||
endGroup: () => undefined, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// A repository name with owner, parsed into its two parts | ||
export interface RepositoryNwo { | ||
owner: string; | ||
repo: string; | ||
} | ||
|
||
export function parseRepositoryNwo(input: string): RepositoryNwo { | ||
const parts = input.split('/'); | ||
if (parts.length !== 2) { | ||
throw new Error(`"${input}" is not a valid repository name`); | ||
} | ||
return { | ||
owner: parts[0], | ||
repo: parts[1], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import test from 'ava'; | ||
|
||
import { getCLILogger } from './logging'; | ||
import {setupTests} from './testing-utils'; | ||
import * as uploadLib from './upload-lib'; | ||
|
||
setupTests(test); | ||
|
||
test('validateSarifFileSchema - valid', t => { | ||
const inputFile = __dirname + '/../src/testdata/valid-sarif.sarif'; | ||
t.notThrows(() => uploadLib.validateSarifFileSchema(inputFile)); | ||
t.notThrows(() => uploadLib.validateSarifFileSchema(inputFile, getCLILogger())); | ||
}); | ||
|
||
test('validateSarifFileSchema - invalid', t => { | ||
const inputFile = __dirname + '/../src/testdata/invalid-sarif.sarif'; | ||
t.throws(() => uploadLib.validateSarifFileSchema(inputFile)); | ||
t.throws(() => uploadLib.validateSarifFileSchema(inputFile, getCLILogger())); | ||
}); |
Oops, something went wrong.