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

Telemetry (#1092) suggestion: don't repeat ourselves #1094

Merged
merged 3 commits into from
Jul 13, 2022
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
15 changes: 15 additions & 0 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const which = require('which');
const winston = require('winston');
const yargs = require('yargs');

const CML = require('../src/cml').default;
const analytics = require('../src/analytics');

let event;

const configureLogger = (level) => {
winston.configure({
format: process.stdout.isTTY
Expand Down Expand Up @@ -43,6 +48,7 @@ const runPlugin = async ({ $0: executable, command }) => {

const handleError = (message, error) => {
if (error) {
analytics.send({ error, event });
winston.error(error);
} else {
yargs.showHelp();
Expand All @@ -51,6 +57,14 @@ const handleError = (message, error) => {
process.exit(1);
};

const runTelemetry = async (opts) => {
event = await analytics.jitsuEventPayload({
action: opts._[0],
cml: new CML(opts)
});
analytics.send({ event });
};

const options = {
log: {
type: 'string',
Expand Down Expand Up @@ -82,6 +96,7 @@ yargs
.fail(handleError)
.env('CML')
.options(options)
.middleware(runTelemetry)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command('$0 <command>', false, (builder) => builder.strict(false), runPlugin)
.recommendCommands()
Expand Down
11 changes: 1 addition & 10 deletions bin/cml/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ const kebabcaseKeys = require('kebabcase-keys');

const { GIT_USER_NAME, GIT_USER_EMAIL } = require('../../src/cml');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'ci';
exports.description = 'Fixes specific CI setups';

exports.handler = async (opts) => {
const cml = new CML(opts);
const event = await analytics.jitsuEventPayload({ action: 'ci', cml });

try {
console.log((await cml.ci(opts)) || '');
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
console.log((await cml.ci(opts)) || '');
};

exports.builder = (yargs) =>
Expand Down
12 changes: 2 additions & 10 deletions bin/cml/pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ const kebabcaseKeys = require('kebabcase-keys');

const { GIT_REMOTE, GIT_USER_NAME, GIT_USER_EMAIL } = require('../../src/cml');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'pr <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const cml = new CML(opts);
const event = await analytics.jitsuEventPayload({ action: 'pr', cml });
try {
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
};

exports.builder = (yargs) =>
Expand Down
24 changes: 7 additions & 17 deletions bin/cml/publish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const winston = require('winston');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'publish <asset>';
exports.description = 'Upload an image to build a report';
Expand All @@ -19,24 +19,14 @@ exports.handler = async (opts) => {

const path = opts.asset;
const cml = new CML({ ...opts, repo: native ? repo : 'cml' });
const event = await analytics.jitsuEventPayload({
action: 'publish',
cml: new CML(opts)
});

try {
const output = await cml.publish({
...opts,
path
});
const output = await cml.publish({
...opts,
path
});

if (!file) console.log(output);
else await fs.writeFile(file, output);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
if (!file) console.log(output);
else await fs.writeFile(file, output);
};

exports.builder = (yargs) =>
Expand Down
14 changes: 1 addition & 13 deletions bin/cml/rerun-workflow.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
const kebabcaseKeys = require('kebabcase-keys');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'rerun-workflow';
exports.description = 'Reruns a workflow given the jobId or workflow Id';

exports.handler = async (opts) => {
const cml = new CML(opts);
const event = await analytics.jitsuEventPayload({
action: 'rerun-workflow',
cml
});

try {
await cml.pipelineRerun(opts);
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
await cml.pipelineRerun(opts);
};

exports.builder = (yargs) =>
Expand Down
26 changes: 7 additions & 19 deletions bin/cml/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ const kebabcaseKeys = require('kebabcase-keys');
const timestring = require('timestring');
const winston = require('winston');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');
const { randid, sleep } = require('../../src/utils');
const tf = require('../../src/terraform');

let cml;
let event;
let RUNNER;
let RUNNER_SHUTTING_DOWN = false;
let RUNNER_TIMER = 0;
Expand Down Expand Up @@ -94,8 +91,7 @@ const shutdown = async (opts) => {

await destroyTerraform();

analytics.send({ ...event });
process.exit(error ? 1 : 0);
if (error) throw new Error('failed');
};

const runCloud = async (opts) => {
Expand Down Expand Up @@ -367,8 +363,7 @@ const run = async (opts) => {
`Runner name ${name} is already in use. Please change the name or terminate the existing runner.`
);
winston.info(`Reusing existing runner named ${name}...`);
analytics.send({ ...event });
process.exit(0);
return;
}

if (
Expand All @@ -380,15 +375,14 @@ const run = async (opts) => {
winston.info(
`Reusing existing online runners with the ${labels} labels...`
);
analytics.send({ ...event });
process.exit(0);
return;
}

if (reuseIdle) {
if (driver === 'bitbucket') {
winston.error('cml runner flag --reuse-idle is unsupported by bitbucket');
analytics.send({ ...event });
process.exit(1);
throw new Error(
'cml runner flag --reuse-idle is unsupported by bitbucket'
);
}
winston.info(
`Checking for existing idle runner matching labels: ${labels}.`
Expand All @@ -399,8 +393,7 @@ const run = async (opts) => {
);
if (availableRunner) {
winston.info('Found matching idle runner.', availableRunner);
analytics.send({ ...event });
process.exit(0);
return;
}
}

Expand All @@ -422,10 +415,6 @@ exports.command = 'runner';
exports.description = 'Launch and register a self-hosted runner';

exports.handler = async (opts) => {
const { driver, repo, token } = opts;
cml = new CML({ driver, repo, token });
event = await analytics.jitsuEventPayload({ action: 'runner', cml });

if (process.env.RUNNER_NAME) {
winston.warn(
'ignoring RUNNER_NAME environment variable, use CML_RUNNER_NAME or --name instead'
Expand All @@ -435,7 +424,6 @@ exports.handler = async (opts) => {
await run(opts);
} catch (error) {
await shutdown({ ...opts, error });
analytics.send({ ...event, error: error.message });
}
};

Expand Down
15 changes: 1 addition & 14 deletions bin/cml/send-comment.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
const kebabcaseKeys = require('kebabcase-keys');

const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'send-comment <markdown file>';
exports.description = 'Comment on a commit';

exports.handler = async (opts) => {
opts.markdownFile = opts.markdownfile;

const cml = new CML(opts);
const event = await analytics.jitsuEventPayload({
action: 'send-comment',
cml
});

try {
console.log(await cml.commentCreate(opts));
analytics.send({ event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
console.log(await cml.commentCreate(opts));
};

exports.builder = (yargs) =>
Expand Down
18 changes: 3 additions & 15 deletions bin/cml/send-github-check.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const CML = require('../../src/cml').default;
const analytics = require('../../src/analytics');

exports.command = 'send-github-check <markdown file>';
exports.description = 'Create a check report';

exports.handler = async (opts) => {
const path = opts.markdownfile;
const report = await fs.readFile(path, 'utf-8');
const cml = new CML({ ...opts });
const event = await analytics.jitsuEventPayload({
action: 'send-check',
cml
});

try {
const path = opts.markdownfile;
const report = await fs.readFile(path, 'utf-8');
await cml.checkCreate({ ...opts, report });
analytics.send({ ...event });
} catch (err) {
analytics.send({ ...event, error: err.message });
throw err;
}
await cml.checkCreate({ ...opts, report });
};

exports.builder = (yargs) =>
Expand Down
Loading