Skip to content

Commit cf0c093

Browse files
Add test for failed workflow (#46)
1 parent f793f8b commit cf0c093

12 files changed

+519
-87
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This action exports Github CI/CD workflows to any endpoint compatible with OpenT
88
This is a fork of [otel-export-trace-action](https://github.com/inception-health/otel-export-trace-action) with more features and better support.
99

1010
Compliant with OpenTelemetry [CICD semconv](https://opentelemetry.io/docs/specs/semconv/attributes-registry/cicd/).
11-
Look at [Sample OpenTelemetry Output](./src/__assets__/output.txt) for the list of attributes and their values.
11+
Look at [Sample OpenTelemetry Output](./src/__assets__/output_success.txt) for the list of attributes and their values.
1212

1313
![Example](./docs/honeycomb-example.png)
1414

dist/index.js

+43-30
Original file line numberDiff line numberDiff line change
@@ -33914,8 +33914,8 @@ var esm$4 = /*#__PURE__*/Object.freeze({
3391433914
trace: trace
3391533915
});
3391633916

33917-
const tracer$2 = trace.getTracer("otel-cicd-action");
3391833917
async function traceStep(step) {
33918+
const tracer = trace.getTracer("otel-cicd-action");
3391933919
if (!step.completed_at || !step.started_at) {
3392033920
coreExports.info(`Step ${step.name} is not completed yet.`);
3392133921
return;
@@ -33927,7 +33927,7 @@ async function traceStep(step) {
3392733927
const startTime = new Date(step.started_at);
3392833928
const completedTime = new Date(step.completed_at);
3392933929
const attributes = stepToAttributes(step);
33930-
await tracer$2.startActiveSpan(step.name, { attributes, startTime }, async (span) => {
33930+
await tracer.startActiveSpan(step.name, { attributes, startTime }, async (span) => {
3393133931
const code = step.conclusion === "failure" ? SpanStatusCode.ERROR : SpanStatusCode.OK;
3393233932
span.setStatus({ code });
3393333933
// Some skipped and post jobs return completed_at dates that are older than started_at
@@ -33946,8 +33946,8 @@ function stepToAttributes(step) {
3394633946
};
3394733947
}
3394833948

33949-
const tracer$1 = trace.getTracer("otel-cicd-action");
3395033949
async function traceJob(job, annotations) {
33950+
const tracer = trace.getTracer("otel-cicd-action");
3395133951
if (!job.completed_at) {
3395233952
coreExports.info(`Job ${job.id} is not completed yet`);
3395333953
return;
@@ -33958,7 +33958,7 @@ async function traceJob(job, annotations) {
3395833958
...jobToAttributes(job),
3395933959
...annotationsToAttributes(annotations),
3396033960
};
33961-
await tracer$1.startActiveSpan(job.name, { attributes, startTime }, async (span) => {
33961+
await tracer.startActiveSpan(job.name, { attributes, startTime }, async (span) => {
3396233962
const code = job.conclusion === "failure" ? SpanStatusCode.ERROR : SpanStatusCode.OK;
3396333963
span.setStatus({ code });
3396433964
for (const step of job.steps ?? []) {
@@ -34024,8 +34024,8 @@ function annotationsToAttributes(annotations) {
3402434024
return attributes;
3402534025
}
3402634026

34027-
const tracer = trace.getTracer("otel-cicd-action");
3402834027
async function traceWorkflowRun(workflowRun, jobs, jobAnnotations, prLabels) {
34028+
const tracer = trace.getTracer("otel-cicd-action");
3402934029
const startTime = new Date(workflowRun.run_started_at ?? workflowRun.created_at);
3403034030
const attributes = workflowRunToAttributes(workflowRun, prLabels);
3403134031
return await tracer.startActiveSpan(workflowRun.name ?? workflowRun.display_title, { attributes, root: true, startTime }, async (rootSpan) => {
@@ -86103,6 +86103,42 @@ class DeterministicIdGenerator {
8610386103
}
8610486104
}
8610586105

86106+
async function fetchGithub(token, runId) {
86107+
const octokit = githubExports.getOctokit(token);
86108+
coreExports.info(`Get workflow run for ${runId}`);
86109+
const workflowRun = await getWorkflowRun(githubExports.context, octokit, runId);
86110+
coreExports.info("Get jobs");
86111+
const jobs = await listJobsForWorkflowRun(githubExports.context, octokit, runId);
86112+
coreExports.info("Get job annotations");
86113+
const jobsId = (jobs ?? []).map((job) => job.id);
86114+
let jobAnnotations = {};
86115+
try {
86116+
jobAnnotations = await getJobsAnnotations(githubExports.context, octokit, jobsId);
86117+
}
86118+
catch (error) {
86119+
if (error instanceof RequestError) {
86120+
coreExports.info(`Failed to get job annotations: ${error.message}}`);
86121+
}
86122+
else {
86123+
throw error;
86124+
}
86125+
}
86126+
coreExports.info("Get PRs labels");
86127+
const prNumbers = (workflowRun.pull_requests ?? []).map((pr) => pr.number);
86128+
let prLabels = {};
86129+
try {
86130+
prLabels = await getPRsLabels(githubExports.context, octokit, prNumbers);
86131+
}
86132+
catch (error) {
86133+
if (error instanceof RequestError) {
86134+
coreExports.info(`Failed to get PRs labels: ${error.message}}`);
86135+
}
86136+
else {
86137+
throw error;
86138+
}
86139+
}
86140+
return { workflowRun, jobs, jobAnnotations, prLabels };
86141+
}
8610686142
async function run() {
8610786143
try {
8610886144
const otlpEndpoint = coreExports.getInput("otlpEndpoint");
@@ -86111,31 +86147,8 @@ async function run() {
8611186147
const runId = Number.parseInt(coreExports.getInput("runId") || `${githubExports.context.runId}`);
8611286148
const extraAttributes = stringToRecord(coreExports.getInput("extraAttributes"));
8611386149
const ghToken = coreExports.getInput("githubToken") || process.env["GITHUB_TOKEN"] || "";
86114-
const octokit = githubExports.getOctokit(ghToken);
86115-
coreExports.info(`Get workflow run for ${runId}`);
86116-
const workflowRun = await getWorkflowRun(githubExports.context, octokit, runId);
86117-
coreExports.info("Get jobs");
86118-
const jobs = await listJobsForWorkflowRun(githubExports.context, octokit, runId);
86119-
coreExports.info("Get job annotations");
86120-
const jobsId = (jobs ?? []).map((job) => job.id);
86121-
let jobAnnotations = {};
86122-
try {
86123-
jobAnnotations = await getJobsAnnotations(githubExports.context, octokit, jobsId);
86124-
}
86125-
catch (error) {
86126-
const message = error instanceof Error ? error.message : JSON.stringify(error);
86127-
coreExports.info(`Failed to get job annotations: ${message}}`);
86128-
}
86129-
coreExports.info("Get PRs labels");
86130-
const prNumbers = (workflowRun.pull_requests ?? []).map((pr) => pr.number);
86131-
let prLabels = {};
86132-
try {
86133-
prLabels = await getPRsLabels(githubExports.context, octokit, prNumbers);
86134-
}
86135-
catch (error) {
86136-
const message = error instanceof Error ? error.message : JSON.stringify(error);
86137-
coreExports.info(`Failed to get PRs labels: ${message}}`);
86138-
}
86150+
coreExports.info("Use Github API to fetch workflow data");
86151+
const { workflowRun, jobs, jobAnnotations, prLabels } = await fetchGithub(ghToken, runId);
8613986152
coreExports.info(`Create tracer provider for ${otlpEndpoint}`);
8614086153
const attributes = {
8614186154
[ATTR_SERVICE_NAME]: otelServiceName || workflowRun.name || `${workflowRun.workflow_id}`,

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)