|
| 1 | +import { Command } from 'commander'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +import { getCLILogger } from './logging'; |
| 5 | +import { parseRepositoryNwo } from './repository'; |
| 6 | +import * as upload_lib from './upload-lib'; |
| 7 | + |
| 8 | +const program = new Command(); |
| 9 | +program.version('0.0.1'); |
| 10 | + |
| 11 | +interface UploadArgs { |
| 12 | + sarifFile: string; |
| 13 | + repository: string; |
| 14 | + commit: string; |
| 15 | + ref: string; |
| 16 | + analysisKey: string; |
| 17 | + githubUrl: string; |
| 18 | + githubAuth: string; |
| 19 | + analysisName: string | undefined; |
| 20 | + checkoutPath: string | undefined; |
| 21 | + environment: string | undefined; |
| 22 | +} |
| 23 | + |
| 24 | +function parseGithubApiUrl(inputUrl: string): string { |
| 25 | + try { |
| 26 | + const url = new URL(inputUrl); |
| 27 | + |
| 28 | + // If we detect this is trying to be to github.com |
| 29 | + // then return with a fixed canonical URL. |
| 30 | + if (url.hostname === 'github.com' || url.hostname === 'api.github.com') { |
| 31 | + return 'https://api.github.com'; |
| 32 | + } |
| 33 | + |
| 34 | + // Add the API path if it's not already present. |
| 35 | + if (url.pathname.indexOf('/api/v3') === -1) { |
| 36 | + url.pathname = path.join(url.pathname, 'api', 'v3'); |
| 37 | + } |
| 38 | + |
| 39 | + return url.toString(); |
| 40 | + |
| 41 | + } catch (e) { |
| 42 | + throw new Error(`"${inputUrl}" is not a valid URL`); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +program |
| 47 | + .command('upload') |
| 48 | + .description('Uploads a SARIF file, or all SARIF files from a directory, to code scanning') |
| 49 | + .requiredOption('--sarif-file <file>', 'SARIF file to upload') |
| 50 | + .requiredOption('--repository <repository>', 'Repository name') |
| 51 | + .requiredOption('--commit <commit>', 'SHA of commit that was analyzed') |
| 52 | + .requiredOption('--ref <ref>', 'Name of ref that was analyzed') |
| 53 | + .requiredOption('--analysis-key <key>', 'Identifies the analysis, for use matching up equivalent analyses on different commits') |
| 54 | + .requiredOption('--github-url <url>', 'URL of GitHub instance') |
| 55 | + .requiredOption('--github-auth <auth>', 'GitHub Apps token, or of the form "username:token" if using a personal access token') |
| 56 | + .option('--checkout-path <path>', 'Checkout path (default: current working directory)') |
| 57 | + .option('--analysis-name <name>', 'Display name of the analysis (default: same as analysis-key') |
| 58 | + .option('--environment <env>', 'Environment (default: empty)') |
| 59 | + .action(async (cmd: UploadArgs) => { |
| 60 | + const logger = getCLILogger(); |
| 61 | + try { |
| 62 | + await upload_lib.upload( |
| 63 | + cmd.sarifFile, |
| 64 | + parseRepositoryNwo(cmd.repository), |
| 65 | + cmd.commit, |
| 66 | + cmd.ref, |
| 67 | + cmd.analysisKey, |
| 68 | + cmd.analysisName || cmd.analysisKey, |
| 69 | + undefined, |
| 70 | + cmd.checkoutPath || process.cwd(), |
| 71 | + cmd.environment, |
| 72 | + cmd.githubAuth, |
| 73 | + parseGithubApiUrl(cmd.githubUrl), |
| 74 | + 'cli', |
| 75 | + logger); |
| 76 | + } catch (e) { |
| 77 | + logger.error("Upload failed"); |
| 78 | + logger.error(e); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | +program.parse(process.argv); |
0 commit comments