-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a post step to report telemetry (#492)
Adds a post step to both our Actions (with shared code) to report some telemetry.
- Loading branch information
Showing
10 changed files
with
185 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#! /bin/bash | ||
|
||
node /app/out/post-step.entrypoint.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { getInput } from "@actions/core"; | ||
import { context } from "@actions/github"; | ||
import { createClient, emitTelemetry } 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, | ||
}; | ||
|
||
// workflow.data.run_started_at should always be set since the workflow is running, but we need to make the compiler happy | ||
const workflowStartTime = new Date(); | ||
if (workflow.data.run_started_at) { | ||
workflowStartTime.setTime(new Date(workflow.data.run_started_at).getTime()); | ||
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 prComments = await octokit.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.payload.pull_request.number, | ||
per_page: 1000, | ||
}); | ||
values["report_diffs_action.saw_comment"] = prComments.data.some( | ||
(c) => | ||
c.body?.includes( | ||
"<!--- alwaysmeticulous/report-diffs-action/status-comment" | ||
) && new Date(c.updated_at).getTime() >= workflowStartTime.getTime() | ||
) | ||
? 1 | ||
: 0; | ||
} | ||
|
||
const client = createClient({ apiToken }); | ||
await emitTelemetry({ client, values }); | ||
}; | ||
|
||
/** | ||
* This method checks the input from the action in two different ways because we don't know if we are the post step | ||
* for the main action or the cloud-compute one so we need to support both. | ||
*/ | ||
const getActionInput = (name: string) => { | ||
return ( | ||
getInput(name) || | ||
getInputFromEnv({ | ||
name: name, | ||
required: true, | ||
type: "string", | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters