Skip to content

Commit d721f69

Browse files
committedAug 28, 2023
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.
1 parent 1009124 commit d721f69

6 files changed

+126
-26
lines changed
 

‎lib/actions-util.js

+18-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/actions-util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/actions-util.test.js

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/actions-util.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/actions-util.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4+
import * as core from "@actions/core";
5+
import * as toolRunner from "@actions/exec/lib/toolrunner";
46
import test from "ava";
57
import * as sinon from "sinon";
68

@@ -267,3 +269,50 @@ test("isAnalyzingDefaultBranch()", async (t) => {
267269
getAdditionalInputStub.restore();
268270
});
269271
});
272+
273+
test("determineMergeBaseCommitOid non-pullrequest", async (t) => {
274+
const stub = sinon.stub(core, "info");
275+
276+
process.env["GITHUB_EVENT_NAME"] = "hucairz";
277+
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
278+
const result = await actionsUtil.determineMergeBaseCommitOid(__dirname);
279+
t.deepEqual(result, undefined);
280+
t.deepEqual(0, stub.callCount);
281+
282+
stub.restore();
283+
});
284+
285+
test("determineMergeBaseCommitOid no error", async (t) => {
286+
const stub = sinon.stub(core, "info");
287+
288+
process.env["GITHUB_EVENT_NAME"] = "pull_request";
289+
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
290+
await actionsUtil.determineMergeBaseCommitOid(path.join(__dirname, "../.."));
291+
t.deepEqual(1, stub.callCount);
292+
t.assert(
293+
stub.firstCall.args[0].startsWith(
294+
"The checkout path provided to the action does not appear to be a git repository.",
295+
),
296+
);
297+
298+
stub.restore();
299+
});
300+
301+
test("determineMergeBaseCommitOid other error", async (t) => {
302+
const stub = sinon.stub(core, "info");
303+
304+
process.env["GITHUB_EVENT_NAME"] = "pull_request";
305+
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
306+
const result = await actionsUtil.determineMergeBaseCommitOid(
307+
path.join(__dirname, "../../i-dont-exist"),
308+
);
309+
t.deepEqual(result, undefined);
310+
t.deepEqual(1, stub.callCount);
311+
t.assert(
312+
stub.firstCall.args[0].startsWith(
313+
"Failed to call git to determine merge base.",
314+
),
315+
);
316+
317+
stub.restore();
318+
});

‎src/actions-util.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const getCommitOid = async function (
6363
// the merge commit, which must mean that git is available.
6464
// Even if this does go wrong, it's not a huge problem for the alerts to
6565
// reported on the merge commit.
66+
let output = "";
6667
try {
6768
let commitOid = "";
6869
await new toolrunner.ToolRunner(
@@ -75,19 +76,24 @@ export const getCommitOid = async function (
7576
commitOid += data.toString();
7677
},
7778
stderr: (data) => {
78-
process.stderr.write(data);
79+
output += data.toString();
7980
},
8081
},
8182
cwd: checkoutPath,
8283
},
8384
).exec();
8485
return commitOid.trim();
8586
} catch (e) {
86-
core.info(
87-
"Could not determine current commit SHA using git. Continuing with data from user input or environment.",
88-
);
89-
core.debug(`Reason: ${(e as Error).message}`);
90-
core.debug((e as Error).stack || "NO STACK");
87+
if (output.includes("not a git repository")) {
88+
core.info(
89+
"Could not determine current commit SHA using git. Continuing with data from user input or environment. The checkout path provided to the action does not appear to be a git repository.",
90+
);
91+
} else {
92+
core.info(
93+
`Could not determine current commit SHA using git. Continuing with data from user input or environment. Failed to call git to determine merge base. Continuing with data from environment: ${output}`,
94+
);
95+
}
96+
9197
return getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
9298
}
9399
};
@@ -96,15 +102,17 @@ export const getCommitOid = async function (
96102
* If the action was triggered by a pull request, determine the commit sha of the merge base.
97103
* Returns undefined if run by other triggers or the merge base cannot be determined.
98104
*/
99-
export const determineMergeBaseCommitOid = async function (): Promise<
100-
string | undefined
101-
> {
105+
export const determineMergeBaseCommitOid = async function (
106+
checkoutPathOverride?: string,
107+
): Promise<string | undefined> {
102108
if (getWorkflowEventName() !== "pull_request") {
103109
return undefined;
104110
}
105111

106112
const mergeSha = getRequiredEnvParam("GITHUB_SHA");
107-
const checkoutPath = getOptionalInput("checkout_path");
113+
const checkoutPath =
114+
checkoutPathOverride ?? getOptionalInput("checkout_path");
115+
let output = "";
108116

109117
try {
110118
let commitOid = "";
@@ -129,7 +137,7 @@ export const determineMergeBaseCommitOid = async function (): Promise<
129137
}
130138
},
131139
stderr: (data) => {
132-
process.stderr.write(data);
140+
output += data.toString();
133141
},
134142
},
135143
cwd: checkoutPath,
@@ -146,10 +154,15 @@ export const determineMergeBaseCommitOid = async function (): Promise<
146154
}
147155
return undefined;
148156
} catch (e) {
149-
core.info(
150-
`Failed to call git to determine merge base. Continuing with data from environment: ${e}`,
151-
);
152-
core.info((e as Error).stack || "NO STACK");
157+
if (output.includes("not a git repository")) {
158+
core.info(
159+
"The checkout path provided to the action does not appear to be a git repository. Continuing with data from environment.",
160+
);
161+
} else {
162+
core.info(
163+
`Failed to call git to determine merge base. Continuing with data from environment: ${output}`,
164+
);
165+
}
153166
return undefined;
154167
}
155168
};

0 commit comments

Comments
 (0)
Please sign in to comment.