Skip to content

Commit

Permalink
Add a post step to report telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardopirovano committed Aug 14, 2024
1 parent 9d44147 commit b33ffcb
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ RUN yarn --frozen-lockfile

COPY tsconfig.base.json tsconfig.json ./

COPY scripts/post.sh /app

COPY src src

RUN yarn build
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ outputs: {}
runs:
using: "docker"
image: "docker://ghcr.io/alwaysmeticulous/report-diffs-action:main"
post-entrypoint: "/app/post.sh"
env:
API_TOKEN: ${{ inputs.api-token }}
GITHUB_TOKEN: ${{ inputs.github-token }}
Expand Down
1 change: 1 addition & 0 deletions cloud-compute/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ outputs: {}
runs:
using: node20
main: "../out/cloud-compute.entrypoint.js"
post: "../out/post.entrypoint.js"
branding:
color: purple
icon: camera
70 changes: 35 additions & 35 deletions out/cloud-compute.entrypoint.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions out/post.entrypoint.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
"license": "Unliscensed",
"private": true,
"main": "dist/main.entrypoint.mjs",
"post": "out/post.entrypoint.js",
"cloud-compute": "out/cloud-compute.entrypoint.js",
"targets": {
"main": {
"source": "src/main.entrypoint.ts",
"distDir": "dist",
"outputFormat": "esmodule"
},
"post": {
"source": "src/post.entrypoint.ts",
"includeNodeModules": true,
"distDir": "out",
"sourceMap": false,
"outputFormat": "commonjs"
},
"cloud-compute": {
"source": "src/cloud-compute.entrypoint.ts",
"includeNodeModules": true,
Expand Down
3 changes: 3 additions & 0 deletions scripts/post.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash

node /app/out/post.entrypoint.js
58 changes: 58 additions & 0 deletions src/common/run-post-step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { getInput } from "@actions/core";
import { context } from "@actions/github";
import { createClient } from "@alwaysmeticulous/client";
import { getInputFromEnv } from "./get-input-from-env";
import { getOctokitOrFail } from "./octokit";

export const runPostStep = async (): Promise<void> => {
const apiToken = getActionInput("api-token");
const githubToken = getActionInput("github-token");
const octokit = getOctokitOrFail(githubToken);
const workflow = await octokit.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
});

const values: Record<string, number> = {
"report_diffs_action.was_cancelled":
workflow.data.status === "cancelled" ? 1 : 0,
};

// This should always be true since the workflow is running, but we need to check it to make the compiler happy
if (workflow.data.run_started_at) {
const timeSinceStart =
new Date().getTime() - new Date(workflow.data.run_started_at).getTime();
values["report_diffs_action.job_duration_seconds"] = timeSinceStart / 1000;
}

if (context.payload.pull_request?.number) {
const shortSha = context.sha.slice(0, 7);
const prComments = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
});
values["report_diffs_action.saw_comment"] = prComments.data.some(
(c) =>
c.body?.includes("alwaysmeticulous/report-diffs-action") &&
c.body?.includes(shortSha)
)
? 1
: 0;
}

const client = createClient({ apiToken });
await client.post("test-runs/telemetry", { values });
};

const getActionInput = (name: string) => {
return (
getInput(name) ||
getInputFromEnv({
name: name,
required: true,
type: "string",
})
);
};
8 changes: 8 additions & 0 deletions src/post.entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { warning } from "@actions/core";
import { runPostStep } from "./common/run-post-step";

runPostStep().catch((error) => {
// We're just emitting telemetry in this post step, so log a warning but don't fail
const message = error instanceof Error ? error.message : `${error}`;
warning(message);
});

0 comments on commit b33ffcb

Please sign in to comment.