Skip to content

Commit f5ece0f

Browse files
authored
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. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> * **New Features** * Introduced a new optional input to control whether assets should be pushed, with a default setting enabled. * **Bug Fixes** * Improved handling to skip pushing artifacts when the new input is set to false, with clear messaging. * **Tests** * Added and enhanced tests to verify conditional artifact pushing and improved input configuration flexibility in test setups. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 9db5d51 commit f5ece0f

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![codecov](https://codecov.io/gh/widoz/github-artifacts-action/graph/badge.svg?token=TF80AM1DUZ)](https://codecov.io/gh/widoz/github-artifacts-action)
77
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/widoz/github-artifacts-action?utm_source=oss&utm_medium=github&utm_campaign=widoz%2Fgithub-artifacts-action&labelColor=171717&color=FF570A&link=https%3A%2F%2Fcoderabbit.ai&label=CodeRabbit+Reviews)
88

9-
Assets Artifacts, is designed to compile the assets of your project and commit and push these assets back into the repository. It also moves the tags to point to the commit where the assets are pushed.
9+
Assets Artifacts is designed to compile the assets of your project and commit and push these assets back into the repository. It also moves the tags to point to the commit where the assets are pushed.
1010

1111
## What it does
1212

@@ -20,6 +20,7 @@ Assets Artifacts, is designed to compile the assets of your project and commit a
2020

2121
- `command` Pass the command the action has to use to build the artifacts. Default to `yarn build`.
2222
- `target-dir` Pass the director where the action has to store the artifacts. Default to `build`.
23+
- `can-push` Pass the boolean indicating if the assets can be pushed or not. Default to `true`.
2324

2425
## Workflow Example
2526

@@ -66,6 +67,7 @@ jobs:
6667
with:
6768
command: 'npm run build'
6869
target-dir: './dist'
70+
can-pus: 'false'
6971
```
7072
7173
In this workflow, the action is triggered on every push event that includes a tag. The workflow runs on the latest version of Ubuntu and will not run if the commit message contains `--skip-assets-artifacts`.

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)