Skip to content

Commit

Permalink
no banckend in publish
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Oct 28, 2020
1 parent ccc487e commit f2ec83b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
9 changes: 6 additions & 3 deletions bin/cml-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ const argv = yargs
'gitlab-uploads',
'Uses GitLab uploads instead of CML storage. Use GitLab uploads to get around CML size limitations for hosting artifacts persistently. Only available for GitLab CI.'
)
.deprecateOption('gitlab-uploads', 'Use backend instead')
.default('backend', 'cml', 'Sets the backend used to publish the assets.')
.choices('backend', ['cml', 'gitlab'])
.deprecateOption('gitlab-uploads', 'Use native instead')
.boolean('native')
.describe(
'native',
"Uses driver's native capabilities to upload assets instead of CML's backend."
)
.default('file')
.describe(
'file',
Expand Down
9 changes: 4 additions & 5 deletions bin/cml-publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('CML e2e', () => {
--gitlab-uploads Uses GitLab uploads instead of CML storage. Use GitLab
uploads to get around CML size limitations for hosting
artifacts persistently. Only available for GitLab CI.
[deprecated: Use backend instead] [boolean]
[deprecated: Use native instead] [boolean]
--native Uses driver's native capabilities to upload assets instead
of CML's backend. [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
Expand All @@ -26,10 +28,7 @@ describe('CML e2e', () => {
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]
--backend
[choices: \\"cml\\", \\"gitlab\\"] [default: Sets the backend used to publish the
assets.]"
-h Show help [boolean]"
`);
});

Expand Down
37 changes: 18 additions & 19 deletions src/cml.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { execSync } = require('child_process');
const git_url_parse = require('git-url-parse');

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

const uri_no_trailing_slash = (uri) => {
Expand All @@ -26,27 +26,27 @@ const infer_driver = (opts = {}) => {
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 get_driver = (opts) => {
const { driver, repo, token } = opts;
if (!driver) throw new Error('driver not set');

if (driver === 'github') return new GithubClient({ repo, token });
if (driver === 'gitlab') return new GitlabClient({ repo, token });
if (driver === 'github') return new Github({ repo, token });
if (driver === 'gitlab') return new Gitlab({ repo, token });

throw new Error('driver unknown!');
};

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

class CML {
constructor(opts = {}) {
const { driver, repo, token } = opts;

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

Expand All @@ -58,23 +58,22 @@ class CML {
const sha = await this.head_sha();
opts.commit_sha = opts.commit_sha || sha;

return await get_client(this).comment_create(opts);
return await get_driver(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);
return await get_driver(this).check_create(opts);
}

async publish(opts = {}) {
const { title = '', md, gitlab_uploads, backend = 'cml' } = opts;
const { title = '', md, native, gitlab_uploads } = opts;

let mime, uri;

if (gitlab_uploads || backend !== 'cml') {
const client = get_client({ ...this, driver: 'gitlab' });
if (native || gitlab_uploads) {
const client = get_driver(this);
({ mime, uri } = await client.upload(opts));
} else {
({ mime, uri } = await upload(opts));
Expand All @@ -89,11 +88,11 @@ class CML {
}

async runner_token() {
return await get_client(this).runner_token();
return await get_driver(this).runner_token();
}

async register_runner(opts = {}) {
return await get_client(this).register_runner(opts);
return await get_driver(this).register_runner(opts);
}

log_error(e) {
Expand Down
10 changes: 5 additions & 5 deletions src/cml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,29 @@ describe('Gitlab tests', () => {
expect(output.endsWith(')')).toBe(true);
});

test('Publish a non image file using gl specifiying backend', async () => {
test('Publish a non image file using native', async () => {
const path = `${__dirname}/../assets/logo.pdf`;
const title = 'my title';

const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
backend: 'gitlab'
native: true
});

expect(output.startsWith(`[${title}](https://`)).toBe(true);
expect(output.endsWith(')')).toBe(true);
});

test('Publish should fail with an invalid backend', async () => {
test('Publish should fail with an invalid driver', async () => {
let catched_err;
try {
const path = `${__dirname}/../assets/logo.pdf`;
await new CML({ repo: REPO }).publish({
await new CML({ repo: REPO, driver: 'invalid' }).publish({
path,
md: true,
backend: 'invalid'
native: true
});
} catch (err) {
catched_err = err.message;
Expand Down

1 comment on commit f2ec83b

@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.