Skip to content

Commit

Permalink
feat: simplify branch name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 2, 2024
1 parent c228c4b commit 95105b0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 31 deletions.
16 changes: 16 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "@actions/core";
import { v4 as uuid } from "uuid";
import { ActionOutputs, getConfig } from "./action.ts";
import * as api from "./api.ts";
import { getBranchName } from "./utils.ts";

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

// Attempt to get the branch from config ref
core.info("Attempt to extract branch name from ref...");
const branch = getBranchName(config.ref);
if (branch.isTag) {
core.info(
`Tag found for '${config.ref}', branch filtering will not be used`,
);
} else if (branch.branchName) {
core.info(`Branch found for '${config.ref}': ${branch.branchName}`);
} else {
core.info(
`Branch not found for '${config.ref}', branch filtering will not be used`,
);
}

const timeoutMs = config.workflowTimeoutSeconds * 1000;
let attemptNo = 0;
let elapsedTime = Date.now() - startTime;
Expand Down
45 changes: 33 additions & 12 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,59 @@ describe("utils", () => {
});

describe("getBranchNameFromRef", () => {
// We want to assert that the props are properly set in
// the union of the return type
interface BranchNameResultUnion {
branchName?: string;
isTag: boolean;
ref: string;
}

it("should return the branch name for a valid branch ref", () => {
const branchName = "cool_feature";
const ref = `/refs/heads/${branchName}`;
const branch = getBranchName(ref);
const branch = getBranchName(ref) as BranchNameResultUnion;

expect(branch).toStrictEqual(branchName);
expect(branch.isTag).toStrictEqual(false);
expect(branch.branchName).toStrictEqual(branchName);
expect(branch.ref).toStrictEqual(ref);
});

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

expect(branch).toStrictEqual(branchName);
expect(branch.isTag).toStrictEqual(false);
expect(branch.branchName).toStrictEqual(branchName);
expect(branch.ref).toStrictEqual(ref);
});

it("should return undefined for an invalid branch ref", () => {
const branch = getBranchName("refs/heads/");
const ref = "refs/heads/";
const branch = getBranchName(ref) as BranchNameResultUnion;

expect(branch).toBeUndefined();
expect(branch.isTag).toStrictEqual(false);
expect(branch.branchName).toBeUndefined();
expect(branch.ref).toStrictEqual(ref);
});

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

expect(branch).toBeUndefined();
expect(branch.isTag).toStrictEqual(true);
expect(branch.branchName).toBeUndefined();
expect(branch.ref).toStrictEqual(ref);
});

it("should return undefined if the ref is for an invalid tag", () => {
const branch = getBranchName("refs/tags/");
it("should return isTag true if the ref is for an invalid tag", () => {
const ref = "refs/tags/";
const branch = getBranchName(ref) as BranchNameResultUnion;

expect(branch).toBeUndefined();
expect(branch.isTag).toStrictEqual(true);
expect(branch.branchName).toBeUndefined();
expect(branch.ref).toStrictEqual(ref);
});
});
});
48 changes: 29 additions & 19 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,37 @@ function isTagRef(ref: string): boolean {
return new RegExp(/\/?refs\/tags\//).test(ref);
}

export function getBranchName(ref: string): string | undefined {
let branchName: string | undefined = undefined;
if (!isTagRef(ref)) {
/**
* The listRepoWorkflows request only accepts a branch name and not a ref (for some reason).
*
* Attempt to filter the branch name specifically and use that.
*/
const branch = getBranchNameFromRef(ref);
if (branch) {
branchName = branch;
interface RefBranch {
branchName?: string;
isTag: false;
ref: string;
}

core.debug(`getWorkflowRunIds: Filtered branch name: ${ref}`);
} else {
core.debug(
`failed to get branch for ref: ${ref}, please raise an issue with this git ref.`,
);
}
} else {
interface RefTag {
isTag: true;
ref: string;
}

export type BranchNameResult = RefBranch | RefTag;

export function getBranchName(ref: string): BranchNameResult {
if (isTagRef(ref)) {
core.debug(`Unable to filter branch, unsupported ref: ${ref}`);
return { isTag: true, ref };
}

return branchName;
/**
* The listRepoWorkflows request only accepts a branch name and not a ref (for some reason).
*
* Attempt to filter the branch name specifically and use that.
*/
const branch = getBranchNameFromRef(ref);
if (branch) {
core.debug(`getWorkflowRunIds: Filtered branch name: ${ref}`);
} else {
core.debug(
`failed to get branch for ref: ${ref}, please raise an issue with this git ref.`,
);
}
return { branchName: branch, isTag: false, ref };
}

0 comments on commit 95105b0

Please sign in to comment.