Skip to content

Commit dcc31a0

Browse files
committed
feat: add skip-if-message-contains option
1 parent 814c0b0 commit dcc31a0

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ jobs:
118118
# E.g. 'ci.yml'. If not provided, current workflow id will be used
119119
#
120120
workflow-id: ''
121+
122+
# If an otherwise matching commit contains any of the messages in this comma separated list, do not consider the commit. Useful for conventions like [skip ci]
123+
#
124+
# Default: ""
125+
skip-if-message-contains: ''
121126
```
122127
123128
<!-- end configuration-options -->

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ inputs:
2929
default: '.'
3030
workflow-id:
3131
description: 'The ID of the workflow to track or name of the file name. E.g. ci.yml. Defaults to current workflow'
32+
skip-if-message-contains:
33+
description: 'If an otherwise matching commit contains any of the messages in this comma separated list, do not consider the commit. Useful for conventions like [skip ci]'
34+
required: false
35+
default: ''
3236

3337
outputs:
3438
base:

dist/nx-set-shas.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22713,6 +22713,8 @@ var workingDirectory = core.getInput("working-directory");
2271322713
var workflowId = core.getInput("workflow-id");
2271422714
var fallbackSHA = core.getInput("fallback-sha");
2271522715
var remote = core.getInput("remote");
22716+
var skipIfMessageContains = !core.getInput("skip-if-message-contains") ? [] : core.getInput("skip-if-message-contains").split(",").map((message) => message.trim());
22717+
var VERBOSE_LOGGING = process.env.NX_SET_SHAS_VERBOSE_LOGGING === "true";
2271622718
var defaultWorkingDirectory = ".";
2271722719
var BASE_SHA;
2271822720
(async () => {
@@ -22859,22 +22861,59 @@ async function findExistingCommit(octokit, branchName, shas) {
2285922861
}
2286022862
async function commitExists(octokit, branchName, commitSha) {
2286122863
try {
22862-
spawnSync("git", ["cat-file", "-e", commitSha], {
22863-
stdio: ["pipe", "pipe", null]
22864+
if (VERBOSE_LOGGING) {
22865+
console.log(`[Debug]: Checking if commit ${commitSha} exists in the checked out repo...`);
22866+
}
22867+
const catFileResult = spawnSync("git", ["cat-file", "-p", commitSha], {
22868+
stdio: ["pipe", "pipe", null],
22869+
encoding: "utf-8"
2286422870
});
22871+
if (catFileResult.status !== 0) {
22872+
return false;
22873+
}
22874+
if (VERBOSE_LOGGING) {
22875+
console.log(`[Debug]: Commit ${commitSha} exists in the checked out repo`);
22876+
}
22877+
if (skipIfMessageContains.length > 0) {
22878+
if (VERBOSE_LOGGING) {
22879+
console.log(`[Debug]: Checking if commit ${commitSha} contains the any of the following messages: ${skipIfMessageContains.join(", ")}`);
22880+
}
22881+
if (skipIfMessageContains.some((message) => catFileResult.stdout.includes(message))) {
22882+
if (VERBOSE_LOGGING) {
22883+
console.log(`[Debug]: Commit ${commitSha} contains the message: ${skipIfMessageContains}. Skipping commit.`);
22884+
}
22885+
return true;
22886+
}
22887+
}
22888+
if (VERBOSE_LOGGING) {
22889+
console.log(`[Debug]: Checking if commit ${commitSha} exists anywhere in the remote repo...`);
22890+
}
2286522891
await octokit.request("GET /repos/{owner}/{repo}/commits/{commit_sha}", {
2286622892
owner,
2286722893
repo,
2286822894
commit_sha: commitSha
2286922895
});
22896+
if (VERBOSE_LOGGING) {
22897+
console.log(`[Debug]:Commit ${commitSha} exists in the remote repo`);
22898+
}
22899+
if (VERBOSE_LOGGING) {
22900+
console.log(`[Debug]: Checking if commit ${commitSha} exists on the expected branch (${branchName})...`);
22901+
}
2287022902
const commits = await octokit.request("GET /repos/{owner}/{repo}/commits", {
2287122903
owner,
2287222904
repo,
2287322905
sha: branchName,
2287422906
per_page: 100
2287522907
});
22908+
if (VERBOSE_LOGGING) {
22909+
console.log(`[Debug]: Commit ${commitSha} exists on the expected branch (${branchName})`);
22910+
}
2287622911
return commits.data.some((commit) => commit.sha === commitSha);
22877-
} catch {
22912+
} catch (err) {
22913+
if (VERBOSE_LOGGING) {
22914+
console.log(`[Debug]: Commit ${commitSha} not found based on the previous check. Raw error:`);
22915+
console.log(err);
22916+
}
2287822917
return false;
2287922918
}
2288022919
}

nx-set-shas.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const workingDirectory = core.getInput('working-directory');
1919
const workflowId = core.getInput('workflow-id');
2020
const fallbackSHA = core.getInput('fallback-sha');
2121
const remote = core.getInput('remote');
22+
const skipIfMessageContains: string[] = !core.getInput(
23+
'skip-if-message-contains',
24+
)
25+
? []
26+
: core
27+
.getInput('skip-if-message-contains')
28+
.split(',')
29+
.map((message) => message.trim());
30+
31+
const VERBOSE_LOGGING = process.env.NX_SET_SHAS_VERBOSE_LOGGING === 'true';
32+
2233
const defaultWorkingDirectory = '.';
2334

2435
let BASE_SHA: string;
@@ -244,29 +255,86 @@ async function commitExists(
244255
commitSha: string,
245256
): Promise<boolean> {
246257
try {
247-
spawnSync('git', ['cat-file', '-e', commitSha], {
258+
if (VERBOSE_LOGGING) {
259+
console.log(
260+
`[Debug]: Checking if commit ${commitSha} exists in the checked out repo...`,
261+
);
262+
}
263+
const catFileResult = spawnSync('git', ['cat-file', '-p', commitSha], {
248264
stdio: ['pipe', 'pipe', null],
265+
encoding: 'utf-8',
249266
});
267+
if (catFileResult.status !== 0) {
268+
return false;
269+
}
270+
if (VERBOSE_LOGGING) {
271+
console.log(
272+
`[Debug]: Commit ${commitSha} exists in the checked out repo`,
273+
);
274+
}
275+
if (skipIfMessageContains.length > 0) {
276+
if (VERBOSE_LOGGING) {
277+
console.log(
278+
`[Debug]: Checking if commit ${commitSha} contains the any of the following messages: ${skipIfMessageContains.join(', ')}`,
279+
);
280+
}
281+
if (
282+
skipIfMessageContains.some((message) =>
283+
catFileResult.stdout.includes(message),
284+
)
285+
) {
286+
if (VERBOSE_LOGGING) {
287+
console.log(
288+
`[Debug]: Commit ${commitSha} contains the message: ${skipIfMessageContains}. Skipping commit.`,
289+
);
290+
}
291+
return true;
292+
}
293+
}
250294

251295
// Check the commit exists in general
296+
if (VERBOSE_LOGGING) {
297+
console.log(
298+
`[Debug]: Checking if commit ${commitSha} exists anywhere in the remote repo...`,
299+
);
300+
}
252301
await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
253302
owner,
254303
repo,
255304
commit_sha: commitSha,
256305
});
306+
if (VERBOSE_LOGGING) {
307+
console.log(`[Debug]:Commit ${commitSha} exists in the remote repo`);
308+
}
257309

258310
// Check the commit exists on the expected main branch (it will not in the case of a rebased main branch)
311+
if (VERBOSE_LOGGING) {
312+
console.log(
313+
`[Debug]: Checking if commit ${commitSha} exists on the expected branch (${branchName})...`,
314+
);
315+
}
259316
const commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
260317
owner,
261318
repo,
262319
sha: branchName,
263320
per_page: 100,
264321
});
322+
if (VERBOSE_LOGGING) {
323+
console.log(
324+
`[Debug]: Commit ${commitSha} exists on the expected branch (${branchName})`,
325+
);
326+
}
265327

266328
return commits.data.some(
267329
(commit: { sha: string }) => commit.sha === commitSha,
268330
);
269-
} catch {
331+
} catch (err) {
332+
if (VERBOSE_LOGGING) {
333+
console.log(
334+
`[Debug]: Commit ${commitSha} not found based on the previous check. Raw error:`,
335+
);
336+
console.log(err);
337+
}
270338
return false;
271339
}
272340
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"private": true,
3-
"version": "4.3.2",
3+
"version": "4.4.0",
44
"license": "MIT",
55
"description": "This package.json is here purely to control the version of the Action, in combination with https://github.com/JamesHenry/publish-shell-action",
66
"scripts": {

0 commit comments

Comments
 (0)