Skip to content

Commit

Permalink
cml
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Jul 10, 2022
1 parent e74eda2 commit e71fec8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {

GITHUB_SERVER_URL,
GITHUB_REPOSITORY_OWNER,
// GITHUB_ACTOR,
GITHUB_ACTOR,
CI_SERVER_URL,
CI_PROJECT_ROOT_NAMESPACE,
GITLAB_USER_NAME,
Expand Down Expand Up @@ -78,12 +78,15 @@ const groupId = async () => {
return await deterministic(rawId);
};

const userId = async () => {
const userId = async ({ cml } = {}) => {
if (isCI()) {
let rawId;
const ci = guessCI();
if (ci === 'github') {
// TODO: GITHUB_ACTOR
const { name, login, id } = await cml
.getDriver()
.user({ name: GITHUB_ACTOR });
rawId = `${name || ''} ${login} ${id}`;
} else if (ci === 'gitlab') {
rawId = `${GITLAB_USER_NAME} ${GITLAB_USER_LOGIN} ${GITLAB_USER_ID}`;
} else if (ci === 'bitbucket') {
Expand Down Expand Up @@ -129,13 +132,14 @@ const OS = () => {
const jitsuEventPayload = async ({
action = '',
error = '',
extra = {}
extra = {},
cml
} = {}) => {
const { cloud: backend = '', ...extraRest } = extra;
extraRest.ci = guessCI();

return {
user_id: await userId(),
user_id: await userId({ cml }),
group_id: await groupId(),
action,
interface: 'cli',
Expand All @@ -151,22 +155,21 @@ const jitsuEventPayload = async ({
};

const send = async ({
event,
endpoint = TPI_ANALYTICS_ENDPOINT,
token = TPI_ANALYTICS_TOKEN
} = {}) => {
try {
if (ITERATIVE_DO_NOT_TRACK) return;

const payload = await jitsuEventPayload();
if (payload.id === ID_DO_NOT_TRACK) return;
if (event.id === ID_DO_NOT_TRACK) return;

await fetch(endpoint, {
method: 'POST',
headers: {
'X-Auth-Toke': token,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload),
body: JSON.stringify(event),
agent: new ProxyAgent()
});
} catch (err) {
Expand Down
11 changes: 9 additions & 2 deletions src/analytics.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const { send, jitsuEventPayload, isCI } = require('./analytics');
const CML = require('../src/cml').default;

const { TEST_GITHUB_TOKEN: TOKEN, TEST_GITHUB_REPO: REPO } = process.env;

describe('analytics tests', () => {
test('userId', async () => {
const cml = new CML({ repo: REPO, token: TOKEN });
const action = 'test';
const cloud = 'azure';
const more = { one: 1, two: 2 };
const extra = { cloud, ...more };
const error = 'Ouch!';
const regex = /\d+\.\d+\.\d+/;

const pl = await jitsuEventPayload({ action, error, extra });
const pl = await jitsuEventPayload({ action, error, extra, cml });
expect(pl.user_id.length).toBe(36);
expect(pl.action).toBe(action);
expect(pl.interface).toBe('cli');
Expand All @@ -33,7 +37,10 @@ describe('analytics tests', () => {
test('Send should never fail', async () => {
let error = null;
try {
await send({ endpoint: 'https://notfound.cml.dev' });
const cml = new CML({ repo: REPO, token: TOKEN });
const action = 'test';
const event = await jitsuEventPayload({ action, error, cml });
await send({ event, endpoint: 'https://notfound.cml.dev' });
} catch (err) {
error = err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CML {
return branch || (await exec(`git branch --show-current`));
}

driver() {
getDriver() {
return getDriver(this);
}

Expand Down

1 comment on commit e71fec8

@github-actions

This comment was marked as off-topic.

Please sign in to comment.