Skip to content

Commit a8c5987

Browse files
authored
Generate Artifacts Outside the Tags Context (#15)
This pull request introduces changes to improve the handling of GitHub Actions workflows by adding support for tag-based operations and refactoring the configuration setup. Additionally, it includes updates to testing configurations and test cases to align with these changes. ### GitHub Actions Workflow Enhancements: * [`src/configuration.ts`](diffhunk://#diff-0931cf958317ce57b36bdf11695eaa9ce4ebeec0f9ae067343159a85d75732dfL7-R10): Added a new `env` parameter to the `Configuration` class and a method `isTag` to determine if the workflow is triggered by a tag reference. [[1]](diffhunk://#diff-0931cf958317ce57b36bdf11695eaa9ce4ebeec0f9ae067343159a85d75732dfL7-R10) [[2]](diffhunk://#diff-0931cf958317ce57b36bdf11695eaa9ce4ebeec0f9ae067343159a85d75732dfR19-R22) * [`src/main.ts`](diffhunk://#diff-4fab5baaca5c14d2de62d8d2fceef376ddddcc8e9509d86cfa5643f51b89ce3dL9-R20): Updated the `main` function to conditionally execute temporary branch creation and deletion based on the `isTag` property. * [`src/model/artifacts.ts`](diffhunk://#diff-5595f77bb2b2f49522043f8dcd486390018618b047f219418459269c756ecc03L19-R21): Modified the `Artifacts` class to conditionally execute tag-related tasks (`collect` and `move`) based on the `isTag` property. ### Testing Updates: * [`tests/unit/model/artifacts.test.ts`](diffhunk://#diff-7038a68cfc252210ef35773ee922184899920f50ea4663f60096442511db507eR138-R180): Added a new test case to verify that tag-related tasks are skipped when the action is not triggered by a tag. Updated the `configuration` function to accept an optional `env` parameter for testing purposes. ### Configuration and File Structure Adjustments: * [`jest.config.ts`](diffhunk://#diff-860bd1f15d1e0bafcfc6f62560524f588e6d6bf56d4ab1b0f6f8146461558730L7-R10): Updated paths in `moduleNameMapper` and `setupFilesAfterEnv` to reflect the new file structure after renaming `tests/jest.config.ts` to `jest.config.ts`. * [`package.json`](diffhunk://#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L33-R33): Simplified the Jest configuration in the `test` script to use the default `jest.config.ts`. * [`tests/tsconfig.json`](diffhunk://#diff-9ef891bf14629264f353ee51ca7dad4267563a5d90ad7e3c62030eba140bf5c9L12-R12): Adjusted the `include` paths to match the updated file structure.
1 parent 9024e90 commit a8c5987

File tree

8 files changed

+61
-14
lines changed

8 files changed

+61
-14
lines changed

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.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module.exports = {
44
preset: 'ts-jest',
55
moduleDirectories: ['node_modules'],
66
moduleNameMapper: {
7-
'^@/(.*)$': '<rootDir>/../src/$1',
8-
'^@model/(.*)$': '<rootDir>/../src/model/$1',
7+
'^@/(.*)$': '<rootDir>/src/$1',
8+
'^@model/(.*)$': '<rootDir>/src/model/$1',
99
},
10-
setupFilesAfterEnv: ['<rootDir>/setup-tests.ts'],
10+
setupFilesAfterEnv: ['<rootDir>/tests/setup-tests.ts'],
1111
maxWorkers: 8,
1212
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"lint": "eslint ./src --ext .ts",
3131
"lint:test": "eslint --config ./tests/.eslintrc.json ./tests --ext .ts",
3232
"prepare": "husky",
33-
"test": "jest --config ./tests/jest.config.ts",
33+
"test": "jest",
3434
"qa": "yarn lint && yarn lint:test && yarn test"
3535
}
3636
}

src/configuration.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ export class Configuration {
44
private static readonly COMMAND = 'yarn build';
55
private static readonly TARGET_DIR = './build';
66

7-
constructor(private readonly read: typeof getInput) {}
7+
constructor(
8+
private readonly read: typeof getInput,
9+
private readonly env: Readonly<NodeJS.ProcessEnv>
10+
) {}
811

912
public get command(): string {
1013
return this.read('command') || Configuration.COMMAND;
@@ -13,4 +16,8 @@ export class Configuration {
1316
public get targetDir(): string {
1417
return this.read('target-dir') || Configuration.TARGET_DIR;
1518
}
19+
20+
public get isTag(): boolean {
21+
return (this.env['GITHUB_REF'] ?? '').startsWith('refs/tags/');
22+
}
1623
}

src/main.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import { createGit } from './create-git';
66
import { Configuration } from './configuration';
77

88
async function main(): Promise<void> {
9-
const configuration = new Configuration(core.getInput.bind(core));
9+
const configuration = new Configuration(core.getInput.bind(core), process.env);
1010

1111
const git = await createGit();
1212
const tags = new Tags(git);
1313
const artifacts = new Artifacts(git, tags, configuration);
1414
const temporaryBranch = new TemporaryBranch(git);
15+
const { isTag } = configuration;
1516

1617
Promise.resolve()
17-
.then(() => temporaryBranch.create())
18+
.then(() => (isTag ? temporaryBranch.create() : null))
1819
.then(() => artifacts.update())
19-
.then(() => temporaryBranch.delete())
20+
.then(() => (isTag ? temporaryBranch.delete() : null))
2021

2122
.catch((error) => {
2223
core.setFailed(`Failed to create and push artifacts: ${error}`);

src/model/artifacts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export class Artifacts {
1616

1717
try {
1818
await this.compile();
19-
await this.tags.collect();
19+
this.configuration.isTag && (await this.tags.collect());
2020
await this.deploy();
21-
await this.tags.move();
21+
this.configuration.isTag && (await this.tags.move());
2222
} catch (error: unknown) {
2323
core.endGroup();
2424
const message = String(error instanceof Error ? error.message : error);

tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
},
1010
"rootDir": "./"
1111
},
12-
"include": ["../src/**/*.ts", "./unit/**/*.ts", "jest.config.ts", "setup-tests.ts"],
12+
"include": ["../src/**/*.ts", "./unit/**/*.ts", "../jest.config.ts", "setup-tests.ts"],
1313
"exclude": ["../node_modules"]
1414
}

tests/unit/model/artifacts.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,49 @@ describe('Artifacts', () => {
135135

136136
expect(collect.mock.invocationCallOrder[0]).toBeLessThan(move.mock.invocationCallOrder[0] ?? 0);
137137
});
138+
139+
it('Do not perform any tasks associated to tags when the action is not running for tags', async () => {
140+
const git = fromPartial<SimpleGit>({
141+
commit: jest.fn(() =>
142+
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
143+
),
144+
push: jest.fn(() =>
145+
Promise.resolve({
146+
remoteMessages: {
147+
all: [''],
148+
},
149+
})
150+
),
151+
});
152+
153+
const collect = jest.fn();
154+
const move = jest.fn();
155+
const tags = fromPartial<Tags>({ collect, move });
156+
157+
const _configuration = configuration({
158+
GITHUB_REF: 'refs/heads/main',
159+
});
160+
const artifacts = new Artifacts(git, tags, _configuration);
161+
162+
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
163+
164+
await artifacts.update();
165+
166+
expect(collect).not.toHaveBeenCalled();
167+
expect(move).not.toHaveBeenCalled();
168+
});
138169
});
139170

140-
function configuration(): Configuration {
141-
return new Configuration(stubGetInput());
171+
function configuration(env?: Readonly<NodeJS.ProcessEnv>): Configuration {
172+
let _env = env;
173+
174+
if (!_env) {
175+
_env = {
176+
GITHUB_REF: 'refs/tags/v1.0.0',
177+
};
178+
}
179+
180+
return new Configuration(stubGetInput(), _env);
142181
}
143182

144183
function stubGetInput(): typeof getInput {

0 commit comments

Comments
 (0)