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

Cml contexts #314

Merged
merged 22 commits into from
Oct 19, 2020
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
6 changes: 6 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ on: [push, pull_request]

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }}
TEST_GITHUB_REPO: ${{ secrets.TEST_GITHUB_REPO }}
TEST_GITHUB_SHA: ${{ secrets.TEST_GITHUB_SHA }}
TEST_GITLAB_TOKEN: ${{ secrets.TEST_GITLAB_TOKEN }}
TEST_GITLAB_REPO: ${{ secrets.TEST_GITLAB_REPO }}
TEST_GITLAB_SHA: ${{ secrets.TEST_GITLAB_SHA }}

jobs:
test_and_deploy:
Expand Down
13 changes: 13 additions & 0 deletions bin/cml-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ const argv = yargs
'Append the output to the given file. Create it if does not exist.'
)
.alias('file', 'f')
.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 or GITLAB_TOKEN.'
)
.default('driver')
.choices('driver', ['github', 'gitlab'])
.describe('driver', 'If not specify it infers it from the ENV.')
.help('h')
.demand(data ? 0 : 1).argv;

Expand Down
16 changes: 16 additions & 0 deletions bin/cml-publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ describe('CML e2e', () => {
[boolean]
--file, -f Append the output to the given file. Create it if does not
exist.
--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 or GITLAB_TOKEN.
--driver If not specify it infers it from the ENV.
[choices: \\"github\\", \\"gitlab\\"]
-h Show help [boolean]"
`);
});
Expand Down Expand Up @@ -86,4 +92,14 @@ describe('CML e2e', () => {
expect(fs.existsSync(file)).toBe(true);
await fs.promises.unlink(file);
});

test('cml-publish assets/test.svg in Gitlab storage', async () => {
const { TEST_GITLAB_REPO: repo, TEST_GITLAB_TOKEN: token } = process.env;

const output = await exec(
`echo none | node ./bin/cml-publish.js --repo=${repo} --token=${token} --gitlab-uploads assets/test.svg`
);

expect(output.startsWith('https://')).toBe(true);
});
});
16 changes: 15 additions & 1 deletion bin/cml-send-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const yargs = require('yargs');
const CML = require('../src/cml');

const run = async (opts) => {
console.error(process.env);
const { 'commit-sha': sha, 'head-sha': head_sha } = opts;
const path = opts._[0];
const report = await fs.readFile(path, 'utf-8');
Expand All @@ -24,8 +25,21 @@ const argv = yargs
'Commit SHA linked to this comment. Defaults to HEAD.'
)
.default('head-sha')
.describe('head-sha', 'Commit SHA linked to this comment. Defaults to HEAD')
.describe('head-sha', 'Commit SHA linked to this comment. Defaults to HEAD.')
.deprecateOption('head-sha', 'Use commit-sha instead')
.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;

Expand Down
22 changes: 18 additions & 4 deletions bin/cml-send-comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,30 @@ describe('Comment integration tests', () => {
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
--head-sha Commit SHA linked to this comment. Defaults to HEAD.
[deprecated: Use commit-sha instead]
--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]"
`);
});

test('cml-send-comment', async () => {
const report = `## Test Comment Report`;
test('cml-send-comment to specific repo', async () => {
const {
TEST_GITHUB_REPO: repo,
TEST_GITHUB_TOKEN: token,
TEST_GITHUB_SHA: sha
} = process.env;

const report = `## Test Comment Report specific`;

await fs.writeFile(path, report);
await exec(`node ./bin/cml-send-comment.js ${path}`);
await exec(
`node ./bin/cml-send-comment.js --repo=${repo} --token=${token} --commit-sha=${sha} ${path}`
);
});
});
13 changes: 13 additions & 0 deletions bin/cml-send-github-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ const argv = yargs
])
.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.'
)
.default('driver')
.choices('driver', ['github', 'gitlab'])
.describe('driver', 'If not specify it infers it from the ENV.')
.help('h')
.demand(1).argv;

Expand Down
6 changes: 6 additions & 0 deletions bin/cml-send-github-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ describe('CML e2e', () => {
--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.
--driver If not specify it infers it from the ENV.
[choices: \\"github\\", \\"gitlab\\"]
-h Show help [boolean]
--conclusion[choices: \\"success\\", \\"failure\\", \\"neutral\\", \\"cancelled\\", \\"skipped\\",
\\"timed_out\\"] [default: Sets the conclusion status of the check.]"
Expand Down
8 changes: 3 additions & 5 deletions src/GithubClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ const CHECK_TITLE = 'CML Report';
const owner_repo = (opts) => {
let owner, repo;
const { uri } = opts;
const { GITHUB_REPOSITORY } = process.env;

if (uri) {
const { pathname } = new URL(uri);
[owner, repo] = pathname.substr(1).split('/');
}

const { GITHUB_REPOSITORY } = process.env;
if (GITHUB_REPOSITORY) {
} else if (GITHUB_REPOSITORY) {
[owner, repo] = GITHUB_REPOSITORY.split('/');
}

Expand Down Expand Up @@ -87,13 +85,13 @@ class GithubClient {
report,
commit_sha: head_sha = this.env_head_sha(),
title = CHECK_TITLE,
name = CHECK_TITLE,
started_at = new Date(),
completed_at = new Date(),
conclusion = 'success',
status = 'completed'
} = opts;

const name = title;
return await octokit(this.token).checks.create({
...owner_repo({ uri: this.repo }),
head_sha,
Expand Down
83 changes: 32 additions & 51 deletions src/GithubClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,40 @@ jest.setTimeout(20000);

const GithubClient = require('./GithubClient');

const TOKEN = process.env.GITHUB_TOKEN || process.env.repo_token;
const GITHUB_REPOSITORY = 'DavidGOrtega/3_tensorboard';
const REPO = `https://github.com/${GITHUB_REPOSITORY}`;
const SHA = 'ee672b3b35c21b440c6fe6890de2fe769fbdbcee';
const {
TEST_GITHUB_TOKEN: TOKEN,
TEST_GITHUB_REPO: REPO,
TEST_GITHUB_SHA: SHA
} = process.env;

describe('Non Enviromental tests', () => {
const gh_client = new GithubClient({ repo: REPO, token: TOKEN });
const client = new GithubClient({ repo: REPO, token: TOKEN });

test('test repo and token', async () => {
expect(gh_client.repo).toBe(REPO);
expect(gh_client.token).toBe(TOKEN);
expect(client.repo).toBe(REPO);
expect(client.token).toBe(TOKEN);

const { owner, repo } = gh_client.owner_repo();
const parts = GITHUB_REPOSITORY.split('/');
expect(owner).toBe(parts[0]);
expect(repo).toBe(parts[1]);
const { owner, repo } = client.owner_repo();
const parts = REPO.split('/');
expect(owner).toBe(parts[parts.length - 2]);
expect(repo).toBe(parts[parts.length - 1]);
});

test('Comment', async () => {
const report = '## Test comment';
const commit_sha = SHA;

await gh_client.comment_create({ report, commit_sha });
});

test('Check', async () => {
const report = '## Hi this check should be neutral';
const title = 'CML neutral test';
const conclusion = `neutral`;

const output = await gh_client.check_create({ report, title, conclusion });
expect(output.startsWith('https://')).toBe(true);
await client.comment_create({ report, commit_sha });
});

test('Publish', async () => {
await expect(gh_client.publish()).rejects.toThrow(
await expect(client.publish()).rejects.toThrow(
'Github does not support publish!'
);
});

test('Runner token', async () => {
const output = await gh_client.runner_token();
const output = await client.runner_token();
expect(output.length).toBe(29);
});
});
Expand All @@ -54,61 +46,50 @@ describe('Enviromental tests', () => {
beforeEach(() => {
jest.resetModules();

process.env = { ...OLD_ENV };

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

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

test('Env', async () => {
const gh_client = new GithubClient({});
const client = new GithubClient({});

expect(gh_client.env_is_pr()).toBe(false);
expect(gh_client.env_head_sha()).toBe(SHA);
expect(gh_client.env_repo()).toBe(REPO);
expect(gh_client.env_token()).toBe(TOKEN);
expect(client.env_is_pr()).toBe(false);
expect(client.env_head_sha()).toBe(SHA);
expect(client.env_repo()).toBe(REPO);
expect(client.env_token()).toBe(TOKEN);

expect(gh_client.repo).toBe(REPO);
expect(gh_client.token).toBe(TOKEN);
expect(client.repo).toBe(REPO);
expect(client.token).toBe(TOKEN);

const { owner, repo } = gh_client.owner_repo();
const parts = GITHUB_REPOSITORY.split('/');
const { owner, repo } = client.owner_repo();
const parts = process.env.GITHUB_REPOSITORY.split('/');
expect(owner).toBe(parts[0]);
expect(repo).toBe(parts[1]);
});

test('Comment', async () => {
const gh_client = new GithubClient({});
const client = new GithubClient({});
const report = '## Test comment';

await gh_client.comment_create({ report });
});

test('Check', async () => {
const gh_client = new GithubClient({});
const report = '## Hi this check should be neutral';
const title = 'CML neutral test';
const conclusion = `neutral`;

const output = await gh_client.check_create({ report, title, conclusion });
expect(output.startsWith('https://')).toBe(true);
await client.comment_create({ report });
});

test('Publish', async () => {
const gh_client = new GithubClient({});
await expect(gh_client.publish()).rejects.toThrow(
const client = new GithubClient({});
await expect(client.publish()).rejects.toThrow(
'Github does not support publish!'
);
});

test('Runner token', async () => {
const gh_client = new GithubClient({});
const output = await gh_client.runner_token();
const client = new GithubClient({});
const output = await client.runner_token();
expect(output.length).toBe(29);
});
});
2 changes: 2 additions & 0 deletions src/GitlabClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class GitlabClient {
const url = `${api_v4}${endpoint}`;
const response = await fetch(url, { method, headers, body });

if (response.status > 300) throw new Error(response.statusText);

return await response.json();
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/GitlabClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ jest.setTimeout(20000);

const GitlabClient = require('./GitlabClient');

const TOKEN = process.env.GITLAB_TOKEN || process.env.repo_token;
const REPO = 'https://gitlab.com/DavidGOrtega/3_tensorboard';
const SHA = '2dc250a9449728356913e1f0e30758a44da65a12';
const {
TEST_GITLAB_TOKEN: TOKEN,
TEST_GITLAB_REPO: REPO,
TEST_GITLAB_SHA: SHA
} = process.env;

describe('Non Enviromental tests', () => {
const client = new GitlabClient({ repo: REPO, token: TOKEN });
Expand All @@ -19,6 +21,7 @@ describe('Non Enviromental tests', () => {
const commit_sha = SHA;

const { created_at } = await client.comment_create({ report, commit_sha });

expect(created_at).not.toBeUndefined();
});

Expand All @@ -32,12 +35,12 @@ describe('Non Enviromental tests', () => {
const path = `${__dirname}/../assets/logo.png`;
const { uri } = await client.publish({ path });

console.log(uri);
expect(uri).not.toBeUndefined();
});

test('Runner token', async () => {
const output = await client.runner_token();

expect(output.length).toBe(20);
});
});
Expand All @@ -48,8 +51,7 @@ describe('Enviromental tests', () => {
beforeEach(() => {
jest.resetModules();

process.env = { ...OLD_ENV };

process.env = {};
process.env.repo_token = TOKEN;
process.env.CI_PROJECT_URL = REPO;
process.env.CI_COMMIT_SHA = SHA;
Expand Down Expand Up @@ -97,6 +99,7 @@ describe('Enviromental tests', () => {
test('Runner token', async () => {
const client = new GitlabClient();
const output = await client.runner_token();

expect(output.length).toBe(20);
});
});
Loading