Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ runs:
inputs:
command:
description: 'Command to execute'
required: false
target-dir:
description: 'Target directory to upload'
required: false
can-push:
description: 'Whenever the assets shall be pushed'
required: false
default: 'true'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export class Configuration {
public get isTag(): boolean {
return (this.env['GITHUB_REF'] ?? '').startsWith('refs/tags/');
}

public get canPush(): boolean {
return this.read('can-push') === 'true';
}
}
5 changes: 5 additions & 0 deletions src/model/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export class Artifacts {
}

private async push(): Promise<void> {
if (!this.configuration.canPush) {
core.info('Skipping pushing artifacts.');
return;
}

const pushingResult = await this.git.push();
const messages = pushingResult.remoteMessages.all.join('\n');

Expand Down
54 changes: 50 additions & 4 deletions tests/unit/model/artifacts.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SimpleGit } from 'simple-git';
import type { Tags } from '@model/tags';
import { info } from '@actions/core';
import type { getInput } from '@actions/core';

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

jest.mock('@actions/core', () => ({
getInput: jest.fn(),
info: jest.fn(),
startGroup: jest.fn(),
endGroup: jest.fn()
}))

describe('Artifacts', () => {
it('Compile the assets and Deploy when finished', async () => {
const git = fromPartial<SimpleGit>({
Expand Down Expand Up @@ -85,6 +93,31 @@ describe('Artifacts', () => {
await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to push');
});

it('Do not push when the action is not configured to do so', async () => {
const push = jest.fn();
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push,
});
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });
const artifacts = new Artifacts(
git,
tags,
configuration(undefined, {
'can-push': 'false',
})
);

jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));

await artifacts.update();

expect(push).not.toHaveBeenCalled();
expect(info).toHaveBeenCalledWith('Skipping pushing artifacts.');
});

it('Throw an error when failing to git-add', async () => {
const git = fromPartial<SimpleGit>({});
const tags = fromPartial<Tags>({ collect: jest.fn() });
Expand Down Expand Up @@ -168,7 +201,12 @@ describe('Artifacts', () => {
});
});

function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
type InputsConfiguration = Readonly<Record<string, unknown>>;

function configuration(
env?: Readonly<NodeJS.ProcessEnv>,
inputsConfiguration: InputsConfiguration = {}
): Configuration {
let _env = env;

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

return new Configuration(stubGetInput(), _env);
return new Configuration(
stubGetInput({
command: 'yarn build',
'target-dir': './build',
'can-push': 'true',
...inputsConfiguration,
}),
_env
);
}

function stubGetInput(): typeof getInput {
function stubGetInput(inputsConfiguration: InputsConfiguration): typeof getInput {
return jest.fn((name: string): string => {
return name === 'command' ? 'yarn build' : './build';
return String(Object.hasOwn(inputsConfiguration, name) ? inputsConfiguration[name] : undefined);
});
}