Skip to content

Commit 1dd0a2d

Browse files
committed
feat: option to not fail when no changes to commit
1 parent a876c39 commit 1dd0a2d

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

.github/workflows/integration-tests.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ jobs:
150150
assert.strictEqual(data.commit.verification.verified, true, 'Expected commit to be verified');
151151
}
152152
153+
- name: Update checkout
154+
run: |
155+
git pull
156+
git clean -fdx
157+
158+
- name: Optionally don't fail if no changes to commit
159+
uses: ./
160+
with:
161+
fail-on-no-changes: false
162+
message: Nothing to commit
163+
ref: integration-test-playground-${{ github.run_id }}-${{ github.run_number }}
164+
token: ${{ steps.generate-token.outputs.token }}
165+
153166
- name: Clean up new ref
154167
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
155168
if: ${{ steps.generate-token.outputs.token }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ jobs:
5959
6060
### Inputs
6161
62+
- `fail-on-no-changes` - *(optional)* Whether or not to set action failure if there are no changes to commit (default: `true`)
6263
- `message` - **(required)** The commit message
6364
- `ref` - *(optional)* Git reference to associate the commit with (e.g. `main`). If it does not exist it will be created. Defaults to the the current checkout ref.
6465
- `token` - **(required)** GitHub App installation access token

__tests__/main.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RequestError } from '@octokit/request-error';
55

66
import * as lib from '../src/lib';
77
import * as main from '../src/main';
8-
import { mockGetInput } from './utils';
8+
import { mockGetBooleanInput, mockGetInput } from './utils';
99

1010
const createCommit = jest.fn();
1111
const createRef = jest.fn();
@@ -255,6 +255,7 @@ describe('action', () => {
255255

256256
it('errors if no changes to commit', async () => {
257257
mockGetInput({ message, token });
258+
mockGetBooleanInput({ 'fail-on-no-changes': true });
258259
jest.mocked(lib.getStagedFiles).mockResolvedValue([]);
259260

260261
await main.run();
@@ -266,6 +267,21 @@ describe('action', () => {
266267
);
267268
});
268269

270+
it('does not error if fail-on-no-changes is false', async () => {
271+
mockGetInput({ message, token });
272+
mockGetBooleanInput({ 'fail-on-no-changes': false });
273+
jest.mocked(lib.getStagedFiles).mockResolvedValue([]);
274+
275+
await main.run();
276+
expect(runSpy).toHaveReturned();
277+
278+
expect(core.setFailed).not.toHaveBeenCalled();
279+
expect(core.notice).toHaveBeenCalledTimes(1);
280+
expect(core.notice).toHaveBeenLastCalledWith(
281+
'No changes found to commit - skipping'
282+
);
283+
});
284+
269285
it('handles generic errors', async () => {
270286
mockGetInput({ message, token });
271287
jest.mocked(lib.getStagedFiles).mockImplementation(() => {

__tests__/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ export function mockGetInput(inputs: Record<string, string>): void {
2222
.mocked(core.getInput)
2323
.mockImplementation(makeMockInputImplementation(inputs, ''));
2424
}
25+
26+
export function mockGetBooleanInput(inputs: Record<string, boolean>): void {
27+
jest
28+
.mocked(core.getBooleanInput)
29+
.mockImplementation(makeMockInputImplementation(inputs, false));
30+
}

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ branding:
77
color: green
88

99
inputs:
10+
fail-on-no-changes:
11+
description: Whether or not to set action failure if there are no changes to commit
12+
required: false
13+
default: true
1014
message:
1115
description: The commit message
1216
required: true

dist/index.js

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

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ export async function run(): Promise<void> {
6666

6767
// Optional inputs
6868
const ref = `heads/${core.getInput('ref') || (await getHeadRef())}`;
69+
const failOnNoChanges = core.getBooleanInput('fail-on-no-changes');
6970

7071
const tree = await populateTree();
7172

7273
if (tree.length === 0) {
73-
core.setFailed('No changes found to commit');
74+
if (failOnNoChanges) {
75+
core.setFailed('No changes found to commit');
76+
} else {
77+
core.notice('No changes found to commit - skipping');
78+
}
7479
return;
7580
}
7681

0 commit comments

Comments
 (0)