Skip to content

Commit c04569f

Browse files
committed
Add a canPush input to manage artifact uploads
Allow conditional artifact pushing via a new `can-push` input parameter in the configuration. Update relevant logic and tests to respect this setting.
1 parent 9db5d51 commit c04569f

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ runs:
77
inputs:
88
command:
99
description: 'Command to execute'
10+
required: false
1011
target-dir:
1112
description: 'Target directory to upload'
13+
required: false
14+
can-push:
15+
description: 'Whenever the assets shall be pushed'
16+
required: false
17+
default: 'true'

dist/index.js

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/configuration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ export class Configuration {
2020
public get isTag(): boolean {
2121
return (this.env['GITHUB_REF'] ?? '').startsWith('refs/tags/');
2222
}
23+
24+
public get canPush(): boolean {
25+
return this.read('can-push') === 'true';
26+
}
2327
}

src/model/artifacts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export class Artifacts {
5656
}
5757

5858
private async push(): Promise<void> {
59+
if (!this.configuration.canPush) {
60+
core.info('Skipping pushing artifacts.');
61+
return;
62+
}
63+
5964
const pushingResult = await this.git.push();
6065
const messages = pushingResult.remoteMessages.all.join('\n');
6166

tests/unit/model/artifacts.test.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SimpleGit } from 'simple-git';
22
import type { Tags } from '@model/tags';
3+
import { info } from '@actions/core';
34
import type { getInput } from '@actions/core';
45

56
import { it, jest, describe, expect } from '@jest/globals';
@@ -21,6 +22,13 @@ jest.mock('@model/tags', () => ({
2122
},
2223
}));
2324

25+
jest.mock('@actions/core', () => ({
26+
getInput: jest.fn(),
27+
info: jest.fn(),
28+
startGroup: jest.fn(),
29+
endGroup: jest.fn()
30+
}))
31+
2432
describe('Artifacts', () => {
2533
it('Compile the assets and Deploy when finished', async () => {
2634
const git = fromPartial<SimpleGit>({
@@ -85,6 +93,31 @@ describe('Artifacts', () => {
8593
await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to push');
8694
});
8795

96+
it('Do not push when the action is not configured to do so', async () => {
97+
const push = jest.fn();
98+
const git = fromPartial<SimpleGit>({
99+
commit: jest.fn(() =>
100+
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
101+
),
102+
push,
103+
});
104+
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });
105+
const artifacts = new Artifacts(
106+
git,
107+
tags,
108+
configuration(undefined, {
109+
'can-push': 'false',
110+
})
111+
);
112+
113+
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
114+
115+
await artifacts.update();
116+
117+
expect(push).not.toHaveBeenCalled();
118+
expect(info).toHaveBeenCalledWith('Skipping pushing artifacts.');
119+
});
120+
88121
it('Throw an error when failing to git-add', async () => {
89122
const git = fromPartial<SimpleGit>({});
90123
const tags = fromPartial<Tags>({ collect: jest.fn() });
@@ -168,7 +201,12 @@ describe('Artifacts', () => {
168201
});
169202
});
170203

171-
function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
204+
type InputsConfiguration = Readonly<Record<string, unknown>>;
205+
206+
function configuration(
207+
env?: Readonly<NodeJS.ProcessEnv>,
208+
inputsConfiguration: InputsConfiguration = {}
209+
): Configuration {
172210
let _env = env;
173211

174212
if (!_env) {
@@ -177,11 +215,19 @@ function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
177215
};
178216
}
179217

180-
return new Configuration(stubGetInput(), _env);
218+
return new Configuration(
219+
stubGetInput({
220+
command: 'yarn build',
221+
'target-dir': './build',
222+
'can-push': 'true',
223+
...inputsConfiguration,
224+
}),
225+
_env
226+
);
181227
}
182228

183-
function stubGetInput(): typeof getInput {
229+
function stubGetInput(inputsConfiguration: InputsConfiguration): typeof getInput {
184230
return jest.fn((name: string): string => {
185-
return name === 'command' ? 'yarn build' : './build';
231+
return String(Object.hasOwn(inputsConfiguration, name) ? inputsConfiguration[name] : undefined);
186232
});
187233
}

0 commit comments

Comments
 (0)