Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions __tests__/revision.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
checkedOutRevision,
firstParent,
resolveBuildId,
resolveMergeBaseBranch,
resolveParentRevision,
resolveRevision,
upstreamMergeBase,
Expand Down Expand Up @@ -243,3 +244,25 @@ describe("resolveBuildId", () => {
});
});
});

describe("resolveMergeBaseBranch", () => {
test("a pull request's base branch", () => {
const pr = { head: { ref: "topic", sha: SHA_A }, base: { ref: "dev" } };
expect(
resolveMergeBaseBranch({
pull_request: pr,
repository: { default_branch: "trunk" },
}),
).toEqual("dev");
});

test("the repository's default branch outside a pull request", () => {
expect(
resolveMergeBaseBranch({ repository: { default_branch: "trunk" } }),
).toEqual("trunk");
});

test("main when the payload names no repository", () => {
expect(resolveMergeBaseBranch({})).toEqual("main");
});
});
14 changes: 12 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function getConfig() {
revisionSource,
parentRevision,
parentRevisionSource,
mergeBaseBranchName: eventPullRequest ? eventPullRequest.base.ref : "main",
mergeBaseBranchName: (0, revision_1.resolveMergeBaseBranch)(event),
};
}
/**
Expand Down Expand Up @@ -397,7 +397,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.resolveBuildId = exports.upstreamMergeBase = exports.resolveParentRevision = exports.firstParent = exports.resolveRevision = exports.checkedOutRevision = void 0;
exports.resolveMergeBaseBranch = exports.resolveBuildId = exports.upstreamMergeBase = exports.resolveParentRevision = exports.firstParent = exports.resolveRevision = exports.checkedOutRevision = void 0;
const child_process_1 = __nccwpck_require__(2081);
/**
* The commit the workspace actually has checked out. Never throws: when `cwd`
Expand Down Expand Up @@ -563,6 +563,16 @@ function resolveBuildId(explicit, mergeBase) {
return { buildId: "", source: "none" };
}
exports.resolveBuildId = resolveBuildId;
/**
* The branch a run is compared against: a pull request's base, else the
* repository's default branch (in every event payload that names the
* repository), else "main".
*/
function resolveMergeBaseBranch(event) {
var _a, _b;
return (((_a = event.pull_request) === null || _a === void 0 ? void 0 : _a.base.ref) || ((_b = event.repository) === null || _b === void 0 ? void 0 : _b.default_branch) || "main");
}
exports.resolveMergeBaseBranch = resolveMergeBaseBranch;


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* The parts of the event payload (`GITHUB_EVENT_PATH`) the action reads. Each
* object is required when present by GitHub's webhook payload schemas, but
* absent on events that lack it (`pull_request` outside pull requests;
* `repository` is not guaranteed on `schedule`).
*/
export interface GitHubEvent {
pull_request?: {
head: { ref: string; sha: string };
base: { ref: string };
};
repository?: {
default_branch: string;
};
}
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
ParentRevisionSource,
resolveParentRevision,
resolveBuildId,
resolveMergeBaseBranch,
resolveRevision,
RevisionSource,
upstreamMergeBase,
} from "./revision";
import { GitHubEvent } from "./github";
import { mayhemProjectSlug } from "./project";

const mayhemUrl: string =
Expand Down Expand Up @@ -95,7 +97,7 @@ function getConfig(): Config {

const ghRepo = `${process.env["GITHUB_SERVER_URL"]}:443/${repo}/`;
const eventPath = process.env["GITHUB_EVENT_PATH"] || "event.json";
const event = JSON.parse(readFileSync(eventPath, "utf-8")) || {};
const event: GitHubEvent = JSON.parse(readFileSync(eventPath, "utf-8")) || {};
const eventPullRequest = event.pull_request;

// Optional typed run duration (in seconds). When set it must be a positive
Expand Down Expand Up @@ -159,7 +161,7 @@ function getConfig(): Config {
revisionSource,
parentRevision,
parentRevisionSource,
mergeBaseBranchName: eventPullRequest ? eventPullRequest.base.ref : "main",
mergeBaseBranchName: resolveMergeBaseBranch(event),
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/revision.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFileSync } from "child_process";
import { GitHubEvent } from "./github";

/** Where the revision recorded on a Mayhem run came from, for the action log. */
export type RevisionSource =
Expand Down Expand Up @@ -208,3 +209,14 @@ export function resolveBuildId(
}
return { buildId: "", source: "none" };
}

/**
* The branch a run is compared against: a pull request's base, else the
* repository's default branch (in every event payload that names the
* repository), else "main".
*/
export function resolveMergeBaseBranch(event: GitHubEvent): string {
return (
event.pull_request?.base.ref || event.repository?.default_branch || "main"
);
}
Loading