Skip to content

Commit 049af32

Browse files
committed
Allow getJobUUID to retrieve the UUID from the environment
1 parent c7ae51b commit 049af32

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

lib/entry-points.js

Lines changed: 26 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/status-report.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,21 @@ setupTests(test);
3131
test("getJobUUID - generates valid UUIDs", async (t) => {
3232
await callee(getJobUUID)
3333
.withArgs()
34+
.logs(t, "Job run UUID is ")
3435
.passes((val) => t.true(uuid.validate(val)));
3536
});
3637

38+
test("getJobUUID - retrieves existing job UUIDs", async (t) => {
39+
const existingJobUuid = uuid.v4();
40+
await callee(getJobUUID)
41+
.withArgs()
42+
.withEnv((env) => {
43+
env.set(EnvVar.JOB_RUN_UUID, existingJobUuid);
44+
})
45+
.logs(t, `Existing job run UUID is ${existingJobUuid}.`)
46+
.passes(t.deepEqual, existingJobUuid);
47+
});
48+
3749
function setupEnvironmentAndStub(tmpDir: string) {
3850
setupActionsVars(tmpDir, tmpDir, {
3951
GITHUB_EVENT_NAME: "dynamic",

src/status-report.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as os from "os";
22

33
import * as core from "@actions/core";
4-
import { v4 as uuidV4 } from "uuid";
4+
import * as uuid from "uuid";
55

66
import type { ActionState } from "./action-common";
77
import {
@@ -62,11 +62,21 @@ export function getDisplayActionName(actionName: ActionName): string {
6262
}
6363

6464
/**
65-
* Creates a UUIDv4 for the analysis and returns it.
66-
* The generated UUID is also exported as an environment variable.
65+
* Either creates a UUIDv4 for the analysis or retrieves an existing one from the
66+
* environment and returns it.
67+
* If a new UUID is generated, it is also exported as an environment variable.
6768
*/
6869
export function getJobUUID(action: ActionState<["Logger", "ReadOnlyEnv"]>) {
69-
const jobRunUuid = uuidV4();
70+
// Check if we already have a UUID for the analysis and return it if so.
71+
const existingJobRunUuid = action.env.getOptional(EnvVar.JOB_RUN_UUID);
72+
73+
if (existingJobRunUuid !== undefined && uuid.validate(existingJobRunUuid)) {
74+
action.logger.info(`Existing job run UUID is ${existingJobRunUuid}.`);
75+
return existingJobRunUuid;
76+
}
77+
78+
// Otherwise generate a new UUID.
79+
const jobRunUuid = uuid.v4();
7080
action.logger.info(`Job run UUID is ${jobRunUuid}.`);
7181

7282
core.exportVariable(EnvVar.JOB_RUN_UUID, jobRunUuid);

0 commit comments

Comments
 (0)