Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No git sha dependencies #517

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 34 additions & 43 deletions bin/cml-send-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path to markdown file>')
.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 <path to markdown file>')
.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);
});
24 changes: 11 additions & 13 deletions bin/cml-send-comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ describe('Comment integration tests', () => {
"Usage: cml-send-comment.js <path to markdown file>

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]"
`);
});

Expand Down
77 changes: 41 additions & 36 deletions bin/cml-send-github-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path to markdown file>')
.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 <path to markdown file>')
.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);
});
16 changes: 8 additions & 8 deletions bin/cml-send-github-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ describe('CML e2e', () => {
"Usage: cml-send-github-check.js <path to markdown file>

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.]"
`);
Expand Down
36 changes: 18 additions & 18 deletions src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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)';
Expand All @@ -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);
}

Expand Down