Skip to content

Commit 955d001

Browse files
committed
Extract runGitCommand()
1 parent 8aba5f2 commit 955d001

File tree

6 files changed

+114
-126
lines changed

6 files changed

+114
-126
lines changed

lib/actions-util.js

Lines changed: 43 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ test("determineMergeBaseCommitOid non-pullrequest", async (t) => {
281281
infoStub.restore();
282282
});
283283

284-
test("determineMergeBaseCommitOid no error", async (t) => {
284+
test("determineMergeBaseCommitOid not git repository", async (t) => {
285285
const infoStub = sinon.stub(core, "info");
286286

287287
process.env["GITHUB_EVENT_NAME"] = "pull_request";
@@ -292,11 +292,12 @@ test("determineMergeBaseCommitOid no error", async (t) => {
292292
});
293293

294294
t.deepEqual(1, infoStub.callCount);
295-
t.assert(
296-
infoStub.firstCall.args[0].startsWith(
295+
t.deepEqual(
296+
infoStub.firstCall.args[0],
297+
"git call failed. Will calculate the base branch SHA on the server. Error: " +
297298
"The checkout path provided to the action does not appear to be a git repository.",
298-
),
299299
);
300+
300301
infoStub.restore();
301302
});
302303

@@ -312,7 +313,12 @@ test("determineMergeBaseCommitOid other error", async (t) => {
312313
t.deepEqual(1, infoStub.callCount);
313314
t.assert(
314315
infoStub.firstCall.args[0].startsWith(
315-
"Failed to call git to determine merge base.",
316+
"git call failed. Will calculate the base branch SHA on the server. Error: ",
317+
),
318+
);
319+
t.assert(
320+
!infoStub.firstCall.args[0].endsWith(
321+
"The checkout path provided to the action does not appear to be a git repository.",
316322
),
317323
);
318324

src/actions-util.ts

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ export function getTemporaryDirectory(): string {
4949
: getRequiredEnvParam("RUNNER_TEMP");
5050
}
5151

52+
async function runGitCommand(
53+
checkoutPath: string | undefined,
54+
args: string[],
55+
customErrorMessage: string,
56+
): Promise<string> {
57+
let stdout = "";
58+
let stderr = "";
59+
try {
60+
await new toolrunner.ToolRunner(await safeWhich.safeWhich("git"), args, {
61+
silent: true,
62+
listeners: {
63+
stdout: (data) => {
64+
stdout += data.toString();
65+
},
66+
stderr: (data) => {
67+
stderr += data.toString();
68+
},
69+
},
70+
cwd: checkoutPath,
71+
}).exec();
72+
return stdout;
73+
} catch (error) {
74+
let reason = stderr;
75+
if (stderr.includes("not a git repository")) {
76+
reason =
77+
"The checkout path provided to the action does not appear to be a git repository.";
78+
}
79+
core.info(`git call failed. ${customErrorMessage} Error: ${reason}`);
80+
throw error;
81+
}
82+
}
83+
5284
/**
5385
* Gets the SHA of the commit that is currently checked out.
5486
*/
@@ -63,38 +95,14 @@ export const getCommitOid = async function (
6395
// the merge commit, which must mean that git is available.
6496
// Even if this does go wrong, it's not a huge problem for the alerts to
6597
// reported on the merge commit.
66-
let stderr = "";
6798
try {
68-
let commitOid = "";
69-
await new toolrunner.ToolRunner(
70-
await safeWhich.safeWhich("git"),
99+
const stdout = await runGitCommand(
100+
checkoutPath,
71101
["rev-parse", ref],
72-
{
73-
silent: true,
74-
listeners: {
75-
stdout: (data) => {
76-
commitOid += data.toString();
77-
},
78-
stderr: (data) => {
79-
stderr += data.toString();
80-
},
81-
},
82-
cwd: checkoutPath,
83-
},
84-
).exec();
85-
return commitOid.trim();
102+
"Continuing with commit SHA from user input or environment.",
103+
);
104+
return stdout.trim();
86105
} catch {
87-
if (stderr.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. " +
90-
"The checkout path provided to the action does not appear to be a git repository.",
91-
);
92-
} else {
93-
core.info(
94-
`Could not determine current commit SHA using git. Continuing with data from user input or environment. ${stderr}`,
95-
);
96-
}
97-
98106
return getOptionalInput("sha") || getRequiredEnvParam("GITHUB_SHA");
99107
}
100108
};
@@ -113,37 +121,29 @@ export const determineMergeBaseCommitOid = async function (
113121
const mergeSha = getRequiredEnvParam("GITHUB_SHA");
114122
const checkoutPath =
115123
checkoutPathOverride ?? getOptionalInput("checkout_path");
116-
let stderr = "";
117124

118125
try {
119126
let commitOid = "";
120127
let baseOid = "";
121128
let headOid = "";
122129

123-
await new toolrunner.ToolRunner(
124-
await safeWhich.safeWhich("git"),
130+
const stdout = await runGitCommand(
131+
checkoutPath,
125132
["show", "-s", "--format=raw", mergeSha],
126-
{
127-
silent: true,
128-
listeners: {
129-
stdline: (data) => {
130-
if (data.startsWith("commit ") && commitOid === "") {
131-
commitOid = data.substring(7);
132-
} else if (data.startsWith("parent ")) {
133-
if (baseOid === "") {
134-
baseOid = data.substring(7);
135-
} else if (headOid === "") {
136-
headOid = data.substring(7);
137-
}
138-
}
139-
},
140-
stderr: (data) => {
141-
stderr += data.toString();
142-
},
143-
},
144-
cwd: checkoutPath,
145-
},
146-
).exec();
133+
"Will calculate the base branch SHA on the server.",
134+
);
135+
136+
for (const data of stdout.split("\n")) {
137+
if (data.startsWith("commit ") && commitOid === "") {
138+
commitOid = data.substring(7);
139+
} else if (data.startsWith("parent ")) {
140+
if (baseOid === "") {
141+
baseOid = data.substring(7);
142+
} else if (headOid === "") {
143+
headOid = data.substring(7);
144+
}
145+
}
146+
}
147147

148148
// Let's confirm our assumptions: We had a merge commit and the parsed parent data looks correct
149149
if (
@@ -155,17 +155,6 @@ export const determineMergeBaseCommitOid = async function (
155155
}
156156
return undefined;
157157
} catch {
158-
if (stderr.includes("not a git repository")) {
159-
core.info(
160-
"The checkout path provided to the action does not appear to be a git repository. " +
161-
"Will calculate the merge base on the server.",
162-
);
163-
} else {
164-
core.info(
165-
`Failed to call git to determine merge base. Will calculate the merge base on ` +
166-
`the server. Reason: ${stderr}`,
167-
);
168-
}
169158
return undefined;
170159
}
171160
};

0 commit comments

Comments
 (0)