Skip to content

Commit 49035de

Browse files
committed
feat(release-trigger): determined needed jobs to trigger the release based on existing jobs
1 parent 283324c commit 49035de

File tree

7 files changed

+62
-10
lines changed

7 files changed

+62
-10
lines changed

src/semantic-release/ci-providers/github-workflows/lifter-test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import any from '@travi/any';
66
import {assert} from 'chai';
77
import sinon from 'sinon';
88

9+
import * as releaseTriggerNeeds from './release-trigger-needs';
910
import * as scaffolder from './scaffolder';
1011
import lift from './lifter';
1112

@@ -23,6 +24,7 @@ suite('github-workflows lifter for semantic-release', () => {
2324
const updatedVerificationWorkflowContents = any.string();
2425
const branchTriggers = any.listOf(any.word);
2526
const moreBranchTriggers = any.listOf(any.word);
27+
const neededJobsToTriggerRelease = any.listOf(any.word);
2628

2729
setup(() => {
2830
sandbox = sinon.createSandbox();
@@ -33,6 +35,7 @@ suite('github-workflows lifter for semantic-release', () => {
3335
sandbox.stub(jsYaml, 'dump');
3436
sandbox.stub(core, 'fileExists');
3537
sandbox.stub(scaffolder, 'default');
38+
sandbox.stub(releaseTriggerNeeds, 'default');
3639

3740
const commonVerificationWorkflowContents = any.string();
3841
fs.readFile.withArgs(`${workflowsDirectory}/node-ci.yml`, 'utf-8').resolves(commonVerificationWorkflowContents);
@@ -58,14 +61,16 @@ suite('github-workflows lifter for semantic-release', () => {
5861
});
5962

6063
test('that the legacy release job is removed', async () => {
64+
const existingJobs = {...jobs, release: legacyReleaseJob};
6165
core.fileExists.resolves(true);
6266
fs.readFile.withArgs(`${workflowsDirectory}/node-ci.yml`, 'utf-8').resolves(verificationWorkflowContents);
67+
releaseTriggerNeeds.default.withArgs(existingJobs).returns(neededJobsToTriggerRelease);
6368
jsYaml.load
6469
.withArgs(verificationWorkflowContents)
6570
.returns({
6671
...parsedVerificationWorkflowContents,
6772
on: {push: {branches: [...branchTriggers, 'alpha', 'beta', ...moreBranchTriggers]}},
68-
jobs: {...jobs, release: legacyReleaseJob}
73+
jobs: existingJobs
6974
});
7075
jsYaml.dump
7176
.withArgs({
@@ -76,7 +81,7 @@ suite('github-workflows lifter for semantic-release', () => {
7681
'trigger-release': {
7782
'runs-on': 'ubuntu-latest',
7883
if: "github.event_name == 'push'",
79-
needs: ['verify'],
84+
needs: neededJobsToTriggerRelease,
8085
steps: [{
8186
uses: 'octokit/[email protected]',
8287
with: {
@@ -103,6 +108,7 @@ suite('github-workflows lifter for semantic-release', () => {
103108
test('that the the release trigger is added when no release is configured yet', async () => {
104109
core.fileExists.resolves(true);
105110
fs.readFile.withArgs(`${workflowsDirectory}/node-ci.yml`, 'utf-8').resolves(verificationWorkflowContents);
111+
releaseTriggerNeeds.default.withArgs(jobs).returns(neededJobsToTriggerRelease);
106112
jsYaml.load
107113
.withArgs(verificationWorkflowContents)
108114
.returns({
@@ -119,7 +125,7 @@ suite('github-workflows lifter for semantic-release', () => {
119125
'trigger-release': {
120126
'runs-on': 'ubuntu-latest',
121127
if: "github.event_name == 'push'",
122-
needs: ['verify'],
128+
needs: neededJobsToTriggerRelease,
123129
steps: [{
124130
uses: 'octokit/[email protected]',
125131
with: {

src/semantic-release/ci-providers/github-workflows/lifter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {promises as fs} from 'fs';
22
import {dump, load} from 'js-yaml';
33
import {fileExists} from '@form8ion/core';
44

5+
import determineTriggerNeedsFrom from './release-trigger-needs';
56
import scaffoldReleaseWorkflow from './scaffolder';
67

78
export default async function ({projectRoot, vcs: {name: vcsProjectName, owner: vcsOwner}}) {
@@ -24,7 +25,7 @@ export default async function ({projectRoot, vcs: {name: vcsProjectName, owner:
2425
'trigger-release': {
2526
'runs-on': 'ubuntu-latest',
2627
if: "github.event_name == 'push'",
27-
needs: ['verify'],
28+
needs: determineTriggerNeedsFrom(parsedVerificationWorkflowDetails.jobs),
2829
steps: [{
2930
uses: 'octokit/[email protected]',
3031
with: {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {assert} from 'chai';
2+
import any from '@travi/any';
3+
import determineTriggerNeedsFrom from './release-trigger-needs';
4+
5+
suite('release-trigger needs', () => {
6+
test('that no jobs are listed if neither `verify` nor `verify-matrix` are present in `jobs`', async () => {
7+
assert.deepEqual(determineTriggerNeedsFrom(any.objectWithKeys(any.listOf(any.word))), []);
8+
});
9+
10+
test('that `verify` is listed if present in `jobs`', async () => {
11+
assert.deepEqual(determineTriggerNeedsFrom(any.objectWithKeys([...any.listOf(any.word), 'verify'])), ['verify']);
12+
});
13+
14+
test('that `verify-matrix` is listed if present in `jobs`', async () => {
15+
assert.deepEqual(
16+
determineTriggerNeedsFrom(any.objectWithKeys([...any.listOf(any.word), 'verify-matrix'])),
17+
['verify-matrix']
18+
);
19+
});
20+
21+
test('that `verify` and `verify-matrix` are listed if both are present in `jobs`', async () => {
22+
assert.deepEqual(
23+
determineTriggerNeedsFrom(any.objectWithKeys(['verify', ...any.listOf(any.word), 'verify-matrix'])),
24+
['verify', 'verify-matrix']
25+
);
26+
});
27+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function (jobs) {
2+
const jobNames = Object.keys(jobs);
3+
4+
return [
5+
...jobNames.includes('verify') ? ['verify'] : [],
6+
...jobNames.includes('verify-matrix') ? ['verify-matrix'] : []
7+
];
8+
}

test/integration/features/lifter.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ Feature: Lift
2020
Then the release workflow is defined
2121
And the release is not triggered until verification completes
2222

23+
Scenario: modern semantic-release in GitHub workflows verifying multiple node versions, but trigger does not wait for verification
24+
Given semantic-release is configured
25+
And modern releases are configured in a GitHub workflow
26+
And multiple node versions are verified
27+
When the project is lifted
28+
Then the release workflow is defined
29+
And the release is not triggered until verification completes
30+
2331
Scenario: no existing release
2432
Given semantic-release is configured
2533
And no release is configured in a GitHub workflow

test/integration/features/step_definitions/common-steps.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ When('the project is lifted', async function () {
4848
},
4949
jobs: {
5050
verify: {},
51-
...this.nodeCiWithReleaseJob && {
52-
release: {}
53-
},
54-
...this.nodeCiWithTriggerReleaseJob && {
55-
'trigger-release': {}
56-
}
51+
...this.multipleNodeVersionsVerified && {'verify-matrix': {}},
52+
...this.nodeCiWithReleaseJob && {release: {}},
53+
...this.nodeCiWithTriggerReleaseJob && {'trigger-release': {}}
5754
}
5855
})
5956
},

test/integration/features/step_definitions/github-workflows-steps.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Given('no release is configured in a GitHub workflow', async function () {
2929
this.betaBranchTrigger = false;
3030
});
3131

32+
Given('multiple node versions are verified', async function () {
33+
this.multipleNodeVersionsVerified = true;
34+
});
35+
3236
Given('no GitHub workflows exist', async function () {
3337
this.githubWorkflows = false;
3438
});
@@ -86,4 +90,5 @@ Then('the release is not triggered until verification completes', async function
8690
const triggerReleaseJob = verificationWorkflowDefinition.jobs['trigger-release'];
8791

8892
assert.include(triggerReleaseJob.needs, 'verify');
93+
if (this.multipleNodeVersionsVerified) assert.include(triggerReleaseJob.needs, 'verify-matrix');
8994
});

0 commit comments

Comments
 (0)