Skip to content

Commit 95105b0

Browse files
committed
feat: simplify branch name handling
1 parent c228c4b commit 95105b0

File tree

3 files changed

+78
-31
lines changed

3 files changed

+78
-31
lines changed

src/main.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as core from "@actions/core";
22
import { v4 as uuid } from "uuid";
33
import { ActionOutputs, getConfig } from "./action.ts";
44
import * as api from "./api.ts";
5+
import { getBranchName } from "./utils.ts";
56

67
const DISTINCT_ID = uuid();
78
const WORKFLOW_FETCH_TIMEOUT_MS = 60 * 1000;
@@ -103,6 +104,21 @@ async function run(): Promise<void> {
103104
// Dispatch the action
104105
await api.dispatchWorkflow(config.distinctId ?? DISTINCT_ID);
105106

107+
// Attempt to get the branch from config ref
108+
core.info("Attempt to extract branch name from ref...");
109+
const branch = getBranchName(config.ref);
110+
if (branch.isTag) {
111+
core.info(
112+
`Tag found for '${config.ref}', branch filtering will not be used`,
113+
);
114+
} else if (branch.branchName) {
115+
core.info(`Branch found for '${config.ref}': ${branch.branchName}`);
116+
} else {
117+
core.info(
118+
`Branch not found for '${config.ref}', branch filtering will not be used`,
119+
);
120+
}
121+
106122
const timeoutMs = config.workflowTimeoutSeconds * 1000;
107123
let attemptNo = 0;
108124
let elapsedTime = Date.now() - startTime;

src/utils.spec.ts

+33-12
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,59 @@ describe("utils", () => {
1414
});
1515

1616
describe("getBranchNameFromRef", () => {
17+
// We want to assert that the props are properly set in
18+
// the union of the return type
19+
interface BranchNameResultUnion {
20+
branchName?: string;
21+
isTag: boolean;
22+
ref: string;
23+
}
24+
1725
it("should return the branch name for a valid branch ref", () => {
1826
const branchName = "cool_feature";
1927
const ref = `/refs/heads/${branchName}`;
20-
const branch = getBranchName(ref);
28+
const branch = getBranchName(ref) as BranchNameResultUnion;
2129

22-
expect(branch).toStrictEqual(branchName);
30+
expect(branch.isTag).toStrictEqual(false);
31+
expect(branch.branchName).toStrictEqual(branchName);
32+
expect(branch.ref).toStrictEqual(ref);
2333
});
2434

2535
it("should return the branch name for a valid branch ref without a leading slash", () => {
2636
const branchName = "cool_feature";
2737
const ref = `refs/heads/${branchName}`;
28-
const branch = getBranchName(ref);
38+
const branch = getBranchName(ref) as BranchNameResultUnion;
2939

30-
expect(branch).toStrictEqual(branchName);
40+
expect(branch.isTag).toStrictEqual(false);
41+
expect(branch.branchName).toStrictEqual(branchName);
42+
expect(branch.ref).toStrictEqual(ref);
3143
});
3244

3345
it("should return undefined for an invalid branch ref", () => {
34-
const branch = getBranchName("refs/heads/");
46+
const ref = "refs/heads/";
47+
const branch = getBranchName(ref) as BranchNameResultUnion;
3548

36-
expect(branch).toBeUndefined();
49+
expect(branch.isTag).toStrictEqual(false);
50+
expect(branch.branchName).toBeUndefined();
51+
expect(branch.ref).toStrictEqual(ref);
3752
});
3853

39-
it("should return undefined if the ref is for a tag", () => {
40-
const branch = getBranchName("refs/tags/v1.0.1");
54+
it("should return isTag true if the ref is for a tag", () => {
55+
const ref = "refs/tags/v1.0.1";
56+
const branch = getBranchName(ref) as BranchNameResultUnion;
4157

42-
expect(branch).toBeUndefined();
58+
expect(branch.isTag).toStrictEqual(true);
59+
expect(branch.branchName).toBeUndefined();
60+
expect(branch.ref).toStrictEqual(ref);
4361
});
4462

45-
it("should return undefined if the ref is for an invalid tag", () => {
46-
const branch = getBranchName("refs/tags/");
63+
it("should return isTag true if the ref is for an invalid tag", () => {
64+
const ref = "refs/tags/";
65+
const branch = getBranchName(ref) as BranchNameResultUnion;
4766

48-
expect(branch).toBeUndefined();
67+
expect(branch.isTag).toStrictEqual(true);
68+
expect(branch.branchName).toBeUndefined();
69+
expect(branch.ref).toStrictEqual(ref);
4970
});
5071
});
5172
});

src/utils.ts

+29-19
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,37 @@ function isTagRef(ref: string): boolean {
1111
return new RegExp(/\/?refs\/tags\//).test(ref);
1212
}
1313

14-
export function getBranchName(ref: string): string | undefined {
15-
let branchName: string | undefined = undefined;
16-
if (!isTagRef(ref)) {
17-
/**
18-
* The listRepoWorkflows request only accepts a branch name and not a ref (for some reason).
19-
*
20-
* Attempt to filter the branch name specifically and use that.
21-
*/
22-
const branch = getBranchNameFromRef(ref);
23-
if (branch) {
24-
branchName = branch;
14+
interface RefBranch {
15+
branchName?: string;
16+
isTag: false;
17+
ref: string;
18+
}
2519

26-
core.debug(`getWorkflowRunIds: Filtered branch name: ${ref}`);
27-
} else {
28-
core.debug(
29-
`failed to get branch for ref: ${ref}, please raise an issue with this git ref.`,
30-
);
31-
}
32-
} else {
20+
interface RefTag {
21+
isTag: true;
22+
ref: string;
23+
}
24+
25+
export type BranchNameResult = RefBranch | RefTag;
26+
27+
export function getBranchName(ref: string): BranchNameResult {
28+
if (isTagRef(ref)) {
3329
core.debug(`Unable to filter branch, unsupported ref: ${ref}`);
30+
return { isTag: true, ref };
3431
}
3532

36-
return branchName;
33+
/**
34+
* The listRepoWorkflows request only accepts a branch name and not a ref (for some reason).
35+
*
36+
* Attempt to filter the branch name specifically and use that.
37+
*/
38+
const branch = getBranchNameFromRef(ref);
39+
if (branch) {
40+
core.debug(`getWorkflowRunIds: Filtered branch name: ${ref}`);
41+
} else {
42+
core.debug(
43+
`failed to get branch for ref: ${ref}, please raise an issue with this git ref.`,
44+
);
45+
}
46+
return { branchName: branch, isTag: false, ref };
3747
}

0 commit comments

Comments
 (0)