Skip to content

Commit

Permalink
Cml contexts sha (#326)
Browse files Browse the repository at this point in the history
* git sha

* commit_sha

* log

* log gh sha

* log gh sha

* show origin

* remote as repo

* remote as repo

* tests

* remove one test not working on gitlab

* space
  • Loading branch information
DavidGOrtega authored Oct 28, 2020
1 parent 2738dc9 commit dd042e2
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 237 deletions.
7 changes: 7 additions & 0 deletions bin/cml-send-comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ describe('Comment integration tests', () => {
`node ./bin/cml-send-comment.js --repo=${repo} --token=${token} --commit-sha=${sha} ${path}`
);
});

test('cml-send-comment to current repo', async () => {
const report = `## Test Comment`;

await fs.writeFile(path, report);
await exec(`node ./bin/cml-send-comment.js ${path}`);
});
});
55 changes: 55 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@actions/github": "^4.0.0",
"file-type": "^14.2.0",
"form-data": "^3.0.0",
"git-url-parse": "^11.4.0",
"is-svg": "^4.2.1",
"node-fetch": "^2.6.0",
"node-forge": "^0.10.0",
Expand Down
71 changes: 42 additions & 29 deletions src/cml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
const { execSync } = require('child_process');
const git_url_parse = require('git-url-parse');

const GitlabClient = require('./drivers/gitlab');
const GithubClient = require('./drivers/github');
const { upload } = require('./utils');
const { upload, exec } = require('./utils');

const uri_no_trailing_slash = (uri) => {
return uri.endsWith('/') ? uri.substr(0, uri.length - 1) : uri;
};

const repo_from_origin = () => {
const origin = execSync('git config --get remote.origin.url').toString(
'utf8'
);
return git_url_parse(origin).toString('https').replace('.git', '');
};

const infer_driver = (opts = {}) => {
const { repo } = opts;
if (repo && repo.includes('github.com')) return 'github';
if (repo && repo.includes('gitlab.com')) return 'gitlab';

const { GITHUB_REPOSITORY, CI_PROJECT_URL } = process.env;
if (GITHUB_REPOSITORY) return 'github';
if (CI_PROJECT_URL) return 'gitlab';
};

const env_token = () => {
const { repo_token, GITHUB_TOKEN, GITLAB_TOKEN } = process.env;
return repo_token || GITHUB_TOKEN || GITLAB_TOKEN;
};

const get_client = (opts) => {
const { driver, repo, token } = opts;
Expand All @@ -14,44 +43,28 @@ const get_client = (opts) => {

class CML {
constructor(opts = {}) {
const env_driver = () => {
const { repo } = opts;
const { GITHUB_REPOSITORY, CI_PROJECT_URL } = process.env;
const { driver, repo, token } = opts;

if (repo && repo.startsWith('https://github.com')) return 'github';
if (repo && repo.startsWith('https://gitlab.com')) return 'gitlab';

if (GITHUB_REPOSITORY) return 'github';
if (CI_PROJECT_URL) return 'gitlab';
};

const { driver = env_driver(), repo, token } = opts;
this.driver = driver;
this.repo = repo;
this.token = token;
}

env_repo() {
return get_client(this).env_repo();
}

env_token() {
return get_client(this).env_token();
}

env_is_pr() {
return get_client(this).env_is_pr();
this.repo = uri_no_trailing_slash(repo || repo_from_origin());
this.token = token || env_token();
this.driver = driver || infer_driver({ repo: this.repo });
}

env_head_sha() {
return get_client(this).env_head_sha();
async head_sha() {
return (await exec(`git rev-parse HEAD`)).replace(/(\r\n|\n|\r)/gm, '');
}

async comment_create(opts = {}) {
const sha = await this.head_sha();
opts.commit_sha = opts.commit_sha || sha;

return await get_client(this).comment_create(opts);
}

async check_create(opts = {}) {
const sha = await this.head_sha();
opts.head_sha = opts.head_sha || sha;

return await get_client(this).check_create(opts);
}

Expand Down
36 changes: 12 additions & 24 deletions src/cml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ describe('Github tests', () => {

process.env = {};
process.env.repo_token = TOKEN;
process.env.GITHUB_SHA = SHA;
process.env.GITHUB_REPOSITORY = new URL(REPO).pathname.substring(1);
});

afterAll(() => {
process.env = OLD_ENV;
});

test('env driver has to be github', async () => {
const cml = new CML();
expect(cml.driver).toBe('github');
});

test('driver has to be github', async () => {
const cml = new CML({ repo: REPO, token: TOKEN, driver: 'github' });
const cml = new CML({ repo: REPO, token: TOKEN });
expect(cml.driver).toBe('github');
});

Expand Down Expand Up @@ -62,9 +55,8 @@ describe('Github tests', () => {

test('Comment should succeed with a valid sha', async () => {
const report = '## Test comment';
const commit_sha = SHA;

await new CML().comment_create({ report, commit_sha });
await new CML({ repo: REPO }).comment_create({ report, commit_sha: SHA });
});

test('Comment should fail with a invalid sha', async () => {
Expand All @@ -73,7 +65,7 @@ describe('Github tests', () => {
const report = '## Test comment';
const commit_sha = 'invalid_sha';

await new CML().comment_create({ report, commit_sha });
await new CML({ repo: REPO }).comment_create({ report, commit_sha });
} catch (err) {
catched_err = err.message;
}
Expand All @@ -95,20 +87,13 @@ describe('Gitlab tests', () => {
jest.resetModules();

process.env = {};
process.env.CI_PROJECT_URL = REPO;
process.env.repo_token = TOKEN;
process.env.CI_COMMIT_SHA = SHA;
});

afterAll(() => {
process.env = OLD_ENV;
});

test('env driver has to be gitlab', async () => {
const cml = new CML();
expect(cml.driver).toBe('gitlab');
});

test('driver has to be gitlab', async () => {
const cml = new CML({ repo: REPO, token: TOKEN, driver: 'gitlab' });
expect(cml.driver).toBe('gitlab');
Expand All @@ -117,7 +102,10 @@ describe('Gitlab tests', () => {
test('Publish image using gl without markdown returns an url', async () => {
const path = `${__dirname}/../assets/logo.png`;

const output = await new CML().publish({ path, gitlab_uploads: true });
const output = await new CML({ repo: REPO }).publish({
path,
gitlab_uploads: true
});

expect(output.startsWith('https://')).toBe(true);
});
Expand All @@ -126,7 +114,7 @@ describe('Gitlab tests', () => {
const path = `${__dirname}/../assets/logo.png`;
const title = 'my title';

const output = await new CML().publish({
const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
Expand All @@ -141,7 +129,7 @@ describe('Gitlab tests', () => {
const path = `${__dirname}/../assets/logo.pdf`;
const title = 'my title';

const output = await new CML().publish({
const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
Expand All @@ -152,9 +140,9 @@ describe('Gitlab tests', () => {
expect(output.endsWith(')')).toBe(true);
});

test('Comment should succeed with a valid env sha', async () => {
test('Comment should succeed with a valid sha', async () => {
const report = '## Test comment';
await new CML().comment_create({ report });
await new CML({ repo: REPO }).comment_create({ report, commit_sha: SHA });
});

test('Comment should fail with a unvalid sha', async () => {
Expand All @@ -163,7 +151,7 @@ describe('Gitlab tests', () => {
const report = '## Test comment';
const commit_sha = 'invalid_sha';

await new CML().comment_create({ report, commit_sha });
await new CML({ repo: REPO }).comment_create({ report, commit_sha });
} catch (err) {
catched_err = err.message;
}
Expand Down
39 changes: 5 additions & 34 deletions src/drivers/github.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const github = require('@actions/github');

const { strip_last_chars } = require('../utils');

const GITHUB_HOST = 'https://github.com/';
const CHECK_TITLE = 'CML Report';

const owner_repo = (opts) => {
Expand All @@ -28,48 +25,22 @@ const octokit = (token) => {

class Github {
constructor(opts = {}) {
const { repo = this.env_repo(), token = this.env_token() } = opts;
const { repo, token } = opts;

if (!repo) throw new Error('repo not found');
if (!token) throw new Error('token not found');

this.repo = repo.endsWith('/')
? strip_last_chars({ str: repo, size: 1 })
: repo;
this.repo = repo;
this.token = token;
}

env_repo() {
const { GITHUB_REPOSITORY } = process.env;
if (GITHUB_REPOSITORY) return `${GITHUB_HOST}${GITHUB_REPOSITORY}`;
}

env_token() {
const { repo_token, GITHUB_TOKEN } = process.env;
return repo_token || GITHUB_TOKEN;
}

env_is_pr() {
try {
return typeof github.context.payload.pull_request !== 'undefined';
} catch (err) {
return false;
}
}

env_head_sha() {
if (this.env_is_pr()) return github.context.payload.pull_request.head.sha;

const { GITHUB_SHA } = process.env;
return GITHUB_SHA;
}

owner_repo(opts = {}) {
const { uri = this.repo } = opts;
return owner_repo({ uri });
}

async comment_create(opts = {}) {
const { report: body, commit_sha = this.env_head_sha() } = opts;
const { report: body, commit_sha } = opts;

const { url: commit_url } = await octokit(
this.token
Expand All @@ -85,7 +56,7 @@ class Github {
async check_create(opts = {}) {
const {
report,
commit_sha: head_sha = this.env_head_sha(),
head_sha,
title = CHECK_TITLE,
started_at = new Date(),
completed_at = new Date(),
Expand Down
Loading

1 comment on commit dd042e2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment

Please sign in to comment.