Skip to content

Commit

Permalink
Merge branch 'master' into readme-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dacbd authored Nov 1, 2022
2 parents 33823c4 + 1a2b245 commit f641e58
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 50 deletions.
39 changes: 21 additions & 18 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,7 @@ const yargs = require('yargs');
const CML = require('../src/cml').default;
const { jitsuEventPayload, send } = require('../src/analytics');

const setupOpts = (opts) => {
const legacyEnvironmentVariables = {
TB_CREDENTIALS: 'CML_TENSORBOARD_DEV_CREDENTIALS',
DOCKER_MACHINE: 'CML_RUNNER_DOCKER_MACHINE',
RUNNER_IDLE_TIMEOUT: 'CML_RUNNER_IDLE_TIMEOUT',
RUNNER_LABELS: 'CML_RUNNER_LABELS',
RUNNER_SINGLE: 'CML_RUNNER_SINGLE',
RUNNER_REUSE: 'CML_RUNNER_REUSE',
RUNNER_NO_RETRY: 'CML_RUNNER_NO_RETRY',
RUNNER_DRIVER: 'CML_RUNNER_DRIVER',
RUNNER_REPO: 'CML_RUNNER_REPO',
RUNNER_PATH: 'CML_RUNNER_PATH'
};

for (const [oldName, newName] of Object.entries(legacyEnvironmentVariables)) {
if (process.env[oldName]) process.env[newName] = process.env[oldName];
}

const aliasLegacyEnvironmentVariables = () => {
const legacyEnvironmentPrefixes = {
CML_CI: 'CML_REPO',
CML_PUBLISH: 'CML_ASSET',
Expand All @@ -46,6 +29,25 @@ const setupOpts = (opts) => {
}
}

const legacyEnvironmentVariables = {
TB_CREDENTIALS: 'CML_TENSORBOARD_CREDENTIALS',
DOCKER_MACHINE: 'CML_RUNNER_DOCKER_MACHINE',
RUNNER_IDLE_TIMEOUT: 'CML_RUNNER_IDLE_TIMEOUT',
RUNNER_LABELS: 'CML_RUNNER_LABELS',
RUNNER_SINGLE: 'CML_RUNNER_SINGLE',
RUNNER_REUSE: 'CML_RUNNER_REUSE',
RUNNER_NO_RETRY: 'CML_RUNNER_NO_RETRY',
RUNNER_DRIVER: 'CML_RUNNER_DRIVER',
RUNNER_REPO: 'CML_RUNNER_REPO',
RUNNER_PATH: 'CML_RUNNER_PATH'
};

for (const [oldName, newName] of Object.entries(legacyEnvironmentVariables)) {
if (process.env[oldName]) process.env[newName] = process.env[oldName];
}
};

const setupOpts = (opts) => {
const { markdownfile } = opts;
opts.markdownFile = markdownfile;
opts.cml = new CML(opts);
Expand Down Expand Up @@ -117,6 +119,7 @@ const handleError = (message, error) => {
};

(async () => {
aliasLegacyEnvironmentVariables();
setupLogger({ log: 'debug' });

try {
Expand Down
7 changes: 7 additions & 0 deletions bin/cml/comment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ exports.options = kebabcaseKeys({
'Avoid watermark; CML needs a watermark to be able to distinguish CML comments from others',
hidden: true,
telemetryData: 'name'
},
watermarkTitle: {
type: 'string',
description:
'Hidden comment marker (used for targeting in subsequent `cml comment update`); "{workflow}" & "{run}" are auto-replaced',
default: '',
conflicts: ['rmWatermark']
}
});
exports.DOCSURL = DOCSURL;
6 changes: 4 additions & 2 deletions bin/cml/comment/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { exec } = require('../../../src/utils');
describe('Comment integration tests', () => {
test('cml send-comment --help', async () => {
const output = await exec(`node ./bin/cml.js send-comment --help`);

expect(output).toMatchInlineSnapshot(`
"cml.js send-comment <markdown file>
Expand All @@ -30,7 +29,10 @@ describe('Comment integration tests', () => {
[string] [default: \\"https://asset.cml.dev\\"]
--publish-native, --native Uses driver's native capabilities to upload assets
instead of CML's storage; not available on GitHub
[boolean]"
[boolean]
--watermark-title Hidden comment marker (used for targeting in
subsequent \`cml comment update\`); \\"{workflow}\\" &
\\"{run}\\" are auto-replaced [string] [default: \\"\\"]"
`);
});
});
7 changes: 6 additions & 1 deletion bin/cml/repo/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ exports.builder = (yargs) =>
.options(exports.options);

exports.options = kebabcaseKeys({
fetchDepth: {
type: 'number',
description: 'Number of commits to fetch (use `0` for all branches & tags)'
},
unshallow: {
type: 'boolean',
description:
'Fetch as much as possible, converting a shallow repository to a complete one'
'Fetch as much as possible, converting a shallow repository to a complete one',
hidden: true
},
userEmail: {
type: 'string',
Expand Down
8 changes: 4 additions & 4 deletions bin/cml/repo/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe('CML e2e', () => {
--help Show help [boolean]
Options:
--unshallow Fetch as much as possible, converting a shallow repository to a
complete one [boolean]
--user-email Git user email [string] [default: \\"[email protected]\\"]
--user-name Git user name [string] [default: \\"Olivaw[bot]\\"]"
--fetch-depth Number of commits to fetch (use \`0\` for all branches & tags)
[number]
--user-email Git user email [string] [default: \\"[email protected]\\"]
--user-name Git user name [string] [default: \\"Olivaw[bot]\\"]"
`);
});
});
6 changes: 4 additions & 2 deletions bin/cml/runner/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ exports.options = kebabcaseKeys({
cloudType: {
type: 'string',
description:
'Instance type. Choices: [m, l, xl]. Also supports native types like i.e. t2.micro'
'Instance type. Choices: [m, l, xl]. Also supports native types like i.e. t2.micro',
telemetryData: 'full'
},
cloudPermissionSet: {
type: 'string',
Expand All @@ -518,7 +519,8 @@ exports.options = kebabcaseKeys({
type: 'string',
description:
'GPU type. Choices: k80, v100, or native types e.g. nvidia-tesla-t4',
coerce: (val) => (val === 'nogpu' ? undefined : val)
coerce: (val) => (val === 'nogpu' ? undefined : val),
telemetryData: 'full'
},
cloudHddSize: {
type: 'number',
Expand Down
60 changes: 48 additions & 12 deletions src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {

const { GITHUB_REPOSITORY, CI_PROJECT_URL, BITBUCKET_REPO_UUID } = process.env;

const WATERMARK_IMAGE = 'https://cml.dev/watermark.png';
const GIT_USER_NAME = 'Olivaw[bot]';
const GIT_USER_EMAIL = '[email protected]';
const GIT_REMOTE = 'origin';
Expand Down Expand Up @@ -164,26 +165,55 @@ class CML {
const triggerSha = await this.triggerSha();
const {
commitSha: inCommitSha = triggerSha,
rmWatermark,
update,
markdownFile,
pr,
publish,
publishUrl,
markdownFile,
report: testReport,
rmWatermark,
triggerFile,
update,
watch,
triggerFile
watermarkTitle
} = opts;

const drv = this.getDriver();

const commitSha =
(await this.revParse({ ref: inCommitSha })) || inCommitSha;

if (rmWatermark && update)
throw new Error('watermarks are mandatory for updateable comments');

// Create the watermark.
const genWatermark = (opts = {}) => {
const { label = '', workflow, run } = opts;
// Replace {workflow} and {run} placeholders in label with actual values.
const lbl = label.replace('{workflow}', workflow).replace('{run}', run);

let title = `CML watermark ${lbl}`.trim();
// Github appears to escape underscores and asterisks in markdown content.
// Without escaping them, the watermark content in comments retrieved
// from github will not match the input.
const patterns = [
[/_/g, '\\_'], // underscore
[/\*/g, '\\*'], // asterisk
[/\[/g, '\\['], // opening square bracket
[/</g, '\\<'] // opening angle bracket
];
title = patterns.reduce(
(label, pattern) => label.replace(pattern[0], pattern[1]),
title
);
return `![](${WATERMARK_IMAGE} "${title}")`;
};
const watermark = rmWatermark
? ''
: '![CML watermark](https://raw.githubusercontent.com/iterative/cml/master/assets/watermark.svg)';
: genWatermark({
label: watermarkTitle,
workflow: drv.workflowId,
run: drv.runId
});

let userReport = testReport;
try {
Expand All @@ -195,15 +225,17 @@ class CML {
}

let report = `${userReport}\n\n${watermark}`;
const drv = this.getDriver();

const publishLocalFiles = async (tree) => {
const nodes = [];

visit(tree, ['definition', 'image', 'link'], (node) => nodes.push(node));

const isWatermark = (node) => {
return node.title && node.title.startsWith('CML watermark');
};
const visitor = async (node) => {
if (node.url && node.alt !== 'CML watermark') {
if (node.url && !isWatermark(node)) {
const absolutePath = path.resolve(
path.dirname(markdownFile),
node.url
Expand Down Expand Up @@ -264,7 +296,7 @@ class CML {
let comment;
const updatableComment = (comments) => {
return comments.reverse().find(({ body }) => {
return body.includes('watermark.svg');
return body.includes(watermark);
});
};

Expand Down Expand Up @@ -469,15 +501,19 @@ class CML {
userName = GIT_USER_NAME,
remote = GIT_REMOTE
} = opts;
const { fetchDepth = unshallow ? 0 : undefined } = opts;

const driver = this.getDriver();
await exec(await driver.updateGitConfig({ userName, userEmail, remote }));
if (unshallow) {
if ((await exec('git rev-parse --is-shallow-repository')) === 'true') {
await exec('git fetch --unshallow');
if (fetchDepth !== undefined) {
if (fetchDepth <= 0) {
if ((await exec('git rev-parse --is-shallow-repository')) === 'true') {
return await exec('git fetch --all --unshallow');
}
} else {
return await exec(`git fetch --all --depth=${fetchDepth}`);
}
}
await exec('git fetch --all');
}

async prCreate(opts = {}) {
Expand Down
16 changes: 14 additions & 2 deletions src/drivers/bitbucket_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ const ProxyAgent = require('proxy-agent');

const { fetchUploadData, exec, gpuPresent, sleep } = require('../utils');

const { BITBUCKET_COMMIT, BITBUCKET_BRANCH, BITBUCKET_PIPELINE_UUID } =
process.env;
const {
BITBUCKET_COMMIT,
BITBUCKET_BRANCH,
BITBUCKET_PIPELINE_UUID,
BITBUCKET_BUILD_NUMBER
} = process.env;

class BitbucketCloud {
constructor(opts = {}) {
Expand Down Expand Up @@ -421,6 +425,14 @@ class BitbucketCloud {
return command;
}

get workflowId() {
return BITBUCKET_PIPELINE_UUID;
}

get runId() {
return BITBUCKET_BUILD_NUMBER;
}

get sha() {
return BITBUCKET_COMMIT;
}
Expand Down
19 changes: 14 additions & 5 deletions src/drivers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const CHECK_TITLE = 'CML Report';
process.env.RUNNER_ALLOW_RUNASROOT = 1;

const {
GITHUB_REPOSITORY,
GITHUB_SHA,
GITHUB_REF,
GITHUB_HEAD_REF,
CI,
GITHUB_EVENT_NAME,
GITHUB_HEAD_REF,
GITHUB_REF,
GITHUB_REPOSITORY,
GITHUB_RUN_ID,
GITHUB_SHA,
GITHUB_TOKEN,
CI,
GITHUB_WORKFLOW,
TPI_TASK
} = process.env;

Expand Down Expand Up @@ -705,6 +706,14 @@ class Github {
return command;
}

get workflowId() {
return GITHUB_WORKFLOW;
}

get runId() {
return GITHUB_RUN_ID;
}

warn(message) {
console.error(`::warning::${message}`);
}
Expand Down
11 changes: 10 additions & 1 deletion src/drivers/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const winston = require('winston');

const { fetchUploadData, download, gpuPresent } = require('../utils');

const { IN_DOCKER, CI_PIPELINE_ID } = process.env;
const { CI_JOB_ID, CI_PIPELINE_ID, IN_DOCKER } = process.env;

const API_VER = 'v4';
class Gitlab {
constructor(opts = {}) {
Expand Down Expand Up @@ -444,6 +445,14 @@ class Gitlab {
return command;
}

get workflowId() {
return CI_PIPELINE_ID;
}

get runId() {
return CI_JOB_ID;
}

get sha() {
return process.env.CI_COMMIT_SHA;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ const isProcRunning = async (opts) => {
};

const watermarkUri = ({ uri, type } = {}) => {
return uriParmam({ uri, param: 'cml', value: type });
return uriParam({ uri, param: 'cml', value: type });
};

const preventcacheUri = ({ uri } = {}) => {
return uriParmam({ uri, param: 'cache-bypass', value: uuid.v4() });
return uriParam({ uri, param: 'cache-bypass', value: uuid.v4() });
};

const uriParmam = (opts = {}) => {
const uriParam = (opts = {}) => {
const { uri, param, value } = opts;
const url = new URL(uri);
url.searchParams.set(param, value);
Expand Down

2 comments on commit f641e58

@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

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