From 06750c99a97f05c673f73519dce6b541598f2b56 Mon Sep 17 00:00:00 2001 From: davidgortega Date: Sat, 15 May 2021 20:19:21 +0200 Subject: [PATCH] No git sha dependencies --- bin/cml-send-comment.js | 77 ++++++++++++++----------------- bin/cml-send-comment.test.js | 24 +++++----- bin/cml-send-github-check.js | 77 ++++++++++++++++--------------- bin/cml-send-github-check.test.js | 16 +++---- src/cml.js | 36 +++++++-------- 5 files changed, 112 insertions(+), 118 deletions(-) diff --git a/bin/cml-send-comment.js b/bin/cml-send-comment.js index 819a75449..1271f2315 100644 --- a/bin/cml-send-comment.js +++ b/bin/cml-send-comment.js @@ -4,59 +4,50 @@ console.log = console.error; const fs = require('fs').promises; const yargs = require('yargs'); +const decamelize = require('decamelize-keys'); const CML = require('../src/cml'); const run = async (opts) => { - const { - 'commit-sha': sha, - 'head-sha': head_sha, - 'rm-watermark': rm_watermark - } = opts; const path = opts._[0]; const report = await fs.readFile(path, 'utf-8'); - const cml = new CML(opts); - await cml.comment_create({ - report, - commit_sha: sha || head_sha, - rm_watermark - }); + await cml.comment_create({ ...opts, report }); }; -const argv = yargs - .strict() - .usage('Usage: $0 ') - .default('commit-sha') - .describe( - 'commit-sha', - 'Commit SHA linked to this comment. Defaults to HEAD.' - ) - .default('head-sha') - .describe('head-sha', 'Commit SHA linked to this comment. Defaults to HEAD.') - .deprecateOption('head-sha', 'Use commit-sha instead') - .boolean('rm-watermark') - .describe( - 'rm-watermark', - 'Avoid watermark. CML needs a watermark to be able to distinguish CML reports from other comments in order to provide extra functionality.' - ) - .default('repo') - .describe( - 'repo', - 'Specifies the repo to be used. If not specified is extracted from the CI ENV.' - ) - .default('token') - .describe( - 'token', - 'Personal access token to be used. If not specified in extracted from ENV REPO_TOKEN.' - ) - .default('driver') - .choices('driver', ['github', 'gitlab']) - .describe('driver', 'If not specify it infers it from the ENV.') - .help('h') - .demand(1).argv; +const opts = decamelize( + yargs + .strict() + .usage('Usage: $0 ') + .default('commit-sha') + .describe( + 'commit-sha', + 'Commit SHA linked to this comment. Defaults to HEAD.' + ) + .alias('commit-sha', 'head-sha') + .boolean('rm-watermark') + .describe( + 'rm-watermark', + 'Avoid watermark. CML needs a watermark to be able to distinguish CML reports from other comments in order to provide extra functionality.' + ) + .default('repo') + .describe( + 'repo', + 'Specifies the repo to be used. If not specified is extracted from the CI ENV.' + ) + .default('token') + .describe( + 'token', + 'Personal access token to be used. If not specified in extracted from ENV REPO_TOKEN.' + ) + .default('driver') + .choices('driver', ['github', 'gitlab']) + .describe('driver', 'If not specify it infers it from the ENV.') + .help('h') + .demand(1).argv +); -run(argv).catch((e) => { +run(opts).catch((e) => { console.error(e); process.exit(1); }); diff --git a/bin/cml-send-comment.test.js b/bin/cml-send-comment.test.js index b596b1ad8..1eb390206 100644 --- a/bin/cml-send-comment.test.js +++ b/bin/cml-send-comment.test.js @@ -19,20 +19,18 @@ describe('Comment integration tests', () => { "Usage: cml-send-comment.js Options: - --version Show version number [boolean] - --commit-sha Commit SHA linked to this comment. Defaults to HEAD. - --head-sha Commit SHA linked to this comment. Defaults to HEAD. - [deprecated: Use commit-sha instead] - --rm-watermark Avoid watermark. CML needs a watermark to be able to - distinguish CML reports from other comments in order to - provide extra functionality. [boolean] - --repo Specifies the repo to be used. If not specified is extracted - from the CI ENV. - --token Personal access token to be used. If not specified in - extracted from ENV REPO_TOKEN. - --driver If not specify it infers it from the ENV. + --version Show version number [boolean] + --commit-sha, --head-sha Commit SHA linked to this comment. Defaults to HEAD. + --rm-watermark Avoid watermark. CML needs a watermark to be able to + distinguish CML reports from other comments in order + to provide extra functionality. [boolean] + --repo Specifies the repo to be used. If not specified is + extracted from the CI ENV. + --token Personal access token to be used. If not specified + in extracted from ENV REPO_TOKEN. + --driver If not specify it infers it from the ENV. [choices: \\"github\\", \\"gitlab\\"] - -h Show help [boolean]" + -h Show help [boolean]" `); }); diff --git a/bin/cml-send-github-check.js b/bin/cml-send-github-check.js index 367be2fcd..21487bf68 100644 --- a/bin/cml-send-github-check.js +++ b/bin/cml-send-github-check.js @@ -4,52 +4,57 @@ console.log = console.error; const fs = require('fs').promises; const yargs = require('yargs'); +const decamelize = require('decamelize-keys'); const CML = require('../src/cml'); const CHECK_TITLE = 'CML Report'; const run = async (opts) => { - const { 'head-sha': head_sha, conclusion, title } = opts; const path = opts._[0]; const report = await fs.readFile(path, 'utf-8'); - - const cml = new CML({ ...opts, driver: 'github' }); - await cml.check_create({ head_sha, report, conclusion, title }); + const cml = new CML({ ...opts }); + await cml.check_create({ ...opts, report }); }; -const argv = yargs - .strict() - .usage('Usage: $0 ') - .default('head-sha') - .describe( - 'head-sha', - 'Commit sha where the comment will appear. Defaults to HEAD.' - ) - .default('conclusion', 'success', 'Sets the conclusion status of the check.') - .choices('conclusion', [ - 'success', - 'failure', - 'neutral', - 'cancelled', - 'skipped', - 'timed_out' - ]) - .default('title', CHECK_TITLE) - .describe('title', 'Sets title of the check.') - .default('repo') - .describe( - 'repo', - 'Specifies the repo to be used. If not specified is extracted from the CI ENV.' - ) - .default('token') - .describe( - 'token', - 'Personal access token to be used. If not specified in extracted from ENV REPO_TOKEN.' - ) - .help('h') - .demand(1).argv; +const opts = decamelize( + yargs + .strict() + .usage('Usage: $0 ') + .describe( + 'commit-sha', + 'Commit SHA linked to this comment. Defaults to HEAD.' + ) + .alias('commit-sha', 'head-sha') + .default( + 'conclusion', + 'success', + 'Sets the conclusion status of the check.' + ) + .choices('conclusion', [ + 'success', + 'failure', + 'neutral', + 'cancelled', + 'skipped', + 'timed_out' + ]) + .default('title', CHECK_TITLE) + .describe('title', 'Sets title of the check.') + .default('repo') + .describe( + 'repo', + 'Specifies the repo to be used. If not specified is extracted from the CI ENV.' + ) + .default('token') + .describe( + 'token', + 'Personal access token to be used. If not specified in extracted from ENV REPO_TOKEN.' + ) + .help('h') + .demand(1).argv +); -run(argv).catch((e) => { +run(opts).catch((e) => { console.error(e); process.exit(1); }); diff --git a/bin/cml-send-github-check.test.js b/bin/cml-send-github-check.test.js index ef33b3c89..7c2a2487f 100644 --- a/bin/cml-send-github-check.test.js +++ b/bin/cml-send-github-check.test.js @@ -39,14 +39,14 @@ describe('CML e2e', () => { "Usage: cml-send-github-check.js Options: - --version Show version number [boolean] - --head-sha Commit sha where the comment will appear. Defaults to HEAD. - --title Sets title of the check. [default: \\"CML Report\\"] - --repo Specifies the repo to be used. If not specified is extracted - from the CI ENV. - --token Personal access token to be used. If not specified in extracted - from ENV REPO_TOKEN. - -h Show help [boolean] + --version Show version number [boolean] + --commit-sha, --head-sha Commit SHA linked to this comment. Defaults to HEAD. + --title Sets title of the check. [default: \\"CML Report\\"] + --repo Specifies the repo to be used. If not specified is + extracted from the CI ENV. + --token Personal access token to be used. If not specified + in extracted from ENV REPO_TOKEN. + -h Show help [boolean] --conclusion[choices: \\"success\\", \\"failure\\", \\"neutral\\", \\"cancelled\\", \\"skipped\\", \\"timed_out\\"] [default: Sets the conclusion status of the check.]" `); diff --git a/src/cml.js b/src/cml.js index 8412158c9..f085603e7 100644 --- a/src/cml.js +++ b/src/cml.js @@ -21,6 +21,19 @@ const repo_from_origin = () => { return strip_auth(uri); }; +const infer_token = () => { + const { + REPO_TOKEN, + repo_token, + GITHUB_TOKEN, + GITLAB_TOKEN, + BITBUCKET_TOKEN + } = process.env; + return ( + REPO_TOKEN || repo_token || GITHUB_TOKEN || GITLAB_TOKEN || BITBUCKET_TOKEN + ); +}; + const infer_driver = (opts = {}) => { const { repo } = opts; if (repo && repo.includes('github.com')) return 'github'; @@ -48,19 +61,6 @@ const get_driver = (opts) => { throw new Error(`driver ${driver} unknown!`); }; -const infer_token = () => { - const { - REPO_TOKEN, - repo_token, - GITHUB_TOKEN, - GITLAB_TOKEN, - BITBUCKET_TOKEN - } = process.env; - return ( - REPO_TOKEN || repo_token || GITHUB_TOKEN || GITLAB_TOKEN || BITBUCKET_TOKEN - ); -}; - class CML { constructor(opts = {}) { const { driver, repo, token } = opts; @@ -78,8 +78,11 @@ class CML { } async comment_create(opts = {}) { - const sha = await this.head_sha(); - const { report: user_report, commit_sha = sha, rm_watermark } = opts; + const { + report: user_report, + commit_sha = await this.head_sha(), + rm_watermark + } = opts; const watermark = rm_watermark ? '' : ' \n\n ![CML watermark](https://raw.githubusercontent.com/iterative/cml/master/assets/watermark.svg)'; @@ -93,9 +96,6 @@ class CML { } async check_create(opts = {}) { - const sha = await this.head_sha(); - opts.head_sha = opts.head_sha || sha; - return await get_driver(this).check_create(opts); }