Add better error messages when determining merge-base

Avoid printing scary error messages to console when the current
directory is not a git repo. Instead provide a better reason for the git
failure and continue on.
This commit is contained in:
Andrew Eisenberg 2023-08-28 12:28:49 -07:00
parent 100912429f
commit d721f69753
6 changed files with 126 additions and 26 deletions

View file

@ -1,6 +1,8 @@
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as toolRunner from "@actions/exec/lib/toolrunner";
import test from "ava";
import * as sinon from "sinon";
@ -267,3 +269,50 @@ test("isAnalyzingDefaultBranch()", async (t) => {
getAdditionalInputStub.restore();
});
});
test("determineMergeBaseCommitOid non-pullrequest", async (t) => {
const stub = sinon.stub(core, "info");
process.env["GITHUB_EVENT_NAME"] = "hucairz";
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
const result = await actionsUtil.determineMergeBaseCommitOid(__dirname);
t.deepEqual(result, undefined);
t.deepEqual(0, stub.callCount);
stub.restore();
});
test("determineMergeBaseCommitOid no error", async (t) => {
const stub = sinon.stub(core, "info");
process.env["GITHUB_EVENT_NAME"] = "pull_request";
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
await actionsUtil.determineMergeBaseCommitOid(path.join(__dirname, "../.."));
t.deepEqual(1, stub.callCount);
t.assert(
stub.firstCall.args[0].startsWith(
"The checkout path provided to the action does not appear to be a git repository.",
),
);
stub.restore();
});
test("determineMergeBaseCommitOid other error", async (t) => {
const stub = sinon.stub(core, "info");
process.env["GITHUB_EVENT_NAME"] = "pull_request";
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
const result = await actionsUtil.determineMergeBaseCommitOid(
path.join(__dirname, "../../i-dont-exist"),
);
t.deepEqual(result, undefined);
t.deepEqual(1, stub.callCount);
t.assert(
stub.firstCall.args[0].startsWith(
"Failed to call git to determine merge base.",
),
);
stub.restore();
});