Skip to content

Commit 7aa3aae

Browse files
committed
feat: support dynamic extra semantic release plugins
Added a new input (`semanticReleasePlugins`) to allow specifying extra semantic-release plugins at runtime. These plugins are installed dynamically (with `yarn add`) based on workflow call. Added `cosmiconfig` dependency to read correct semantic release config file. Moved `executeCommand` into a separate file for reusability in `main.js` file. Related-Task: INTER-1238
1 parent 6751867 commit 7aa3aae

File tree

7 files changed

+120
-22
lines changed

7 files changed

+120
-22
lines changed

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
name: 'Semantic Release Preview'
22
description: 'Action to get next semantic release info, does not publish. export the info as output variables'
33
author: 'FingerprintJS'
4+
inputs:
5+
semanticReleasePlugins:
6+
description: 'Additional semantic release plugins'
7+
default: ''
8+
required: false
49
outputs:
510
type:
611
description: 'The part of the version incremented - major/minor/patch'

exec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const exec = require('child_process').exec;
2+
const path = require('path');
3+
4+
function executeCommand(cmd, workingDirectory = null) {
5+
return new Promise((resolve, reject) => {
6+
let options = {};
7+
if (workingDirectory) {
8+
options.cwd = path.resolve(workingDirectory);
9+
}
10+
let p = exec(cmd, options, (error, stdout, stderr) => {
11+
if (error) {
12+
reject(error);
13+
}
14+
resolve(stdout ? stdout : stderr);
15+
});
16+
17+
p.stdout.pipe(process.stdout);
18+
p.stderr.pipe(process.stderr);
19+
});
20+
}
21+
22+
module.exports.executeCommand = executeCommand;

index.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
const exec = require('child_process').exec;
2-
const path = require('path');
3-
4-
function executeCommand(cmd, workingDirectory = null) {
5-
return new Promise((resolve, reject) => {
6-
let options = {};
7-
if (workingDirectory) {
8-
options.cwd = path.resolve(workingDirectory);
9-
}
10-
let p = exec(cmd, options, (error, stdout, stderr) => {
11-
if (error) {
12-
reject(error);
13-
}
14-
resolve(stdout ? stdout : stderr);
15-
});
16-
17-
p.stdout.pipe(process.stdout);
18-
p.stderr.pipe(process.stderr);
19-
});
20-
}
1+
const { executeCommand } = require("./exec");
212

223
async function main() {
234
const currentBranch = process.env.GITHUB_HEAD_REF;

main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ delete process.env.GITHUB_ACTIONS;
22

33
const semanticRelease = require('semantic-release');
44
const core = require("@actions/core");
5+
const { executeCommand } = require("./exec");
6+
const { extractPluginName, findPluginConfig } = require("./plugin");
57

68
const plugins = [
79
[
@@ -22,6 +24,22 @@ const plugins = [
2224
async function main() {
2325
try {
2426
const currentBranch = process.env.GITHUB_HEAD_REF;
27+
28+
const extraPlugins = core.getMultilineInput('semanticReleasePlugins').filter(Boolean);
29+
if (extraPlugins.length > 0) {
30+
console.info('Installing extra plugins...');
31+
for (const extraPlugin of extraPlugins) {
32+
console.info(`Installing ${extraPlugin}...`);
33+
const pluginName = extractPluginName(extraPlugin);
34+
await executeCommand(`yarn add ${extraPlugin}`, __dirname);
35+
const pluginConfig = await findPluginConfig(pluginName);
36+
plugins.push([
37+
pluginName,
38+
pluginConfig,
39+
]);
40+
}
41+
}
42+
2543
const result = await semanticRelease({
2644
noCi: true, dryRun: true, branches: [currentBranch, 'main'],
2745
"plugins": plugins

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"@actions/core": "^1.10.1",
55
"@fingerprintjs/conventional-changelog-dx-team": "^0.1.0",
66
"conventional-changelog-conventionalcommits": "^7.0.2",
7+
"cosmiconfig": "^9.0.0",
78
"semantic-release": "^19.0.5"
89
},
910
"license": "MIT"

plugin.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const cosmiconfig = require('cosmiconfig');
2+
3+
function extractPluginName(pluginName) {
4+
const atIndex = pluginName.lastIndexOf('@');
5+
return atIndex !== -1
6+
? pluginName.substring(0, atIndex)
7+
: pluginName;
8+
}
9+
10+
async function findPluginConfig(pluginName) {
11+
const { config } = await cosmiconfig.cosmiconfig('release').search();
12+
13+
if (!config) {
14+
throw new Error(`Project semantic release config file not found.`);
15+
}
16+
17+
if (!config.plugins || !Array.isArray(config.plugins)) {
18+
throw new Error(`Project release config file doesn't have plugins field.`);
19+
}
20+
21+
const pluginConfig = config.plugins.find(plugin => {
22+
if (typeof plugin === 'string' && plugin !== pluginName) {
23+
return false;
24+
}
25+
26+
if (!Array.isArray(plugin) || plugin.length <= 1) {
27+
return false;
28+
}
29+
30+
if (plugin[0] !== pluginName) {
31+
return false;
32+
}
33+
34+
return true;
35+
});
36+
37+
return pluginConfig ? pluginConfig[1] : {};
38+
}
39+
40+
module.exports.findPluginConfig = findPluginConfig;
41+
module.exports.extractPluginName = extractPluginName;

yarn.lock

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ are-we-there-yet@^3.0.0:
566566
delegates "^1.0.0"
567567
readable-stream "^3.6.0"
568568

569+
argparse@^2.0.1:
570+
version "2.0.1"
571+
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
572+
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
573+
569574
argv-formatter@~1.0.0:
570575
version "1.0.0"
571576
resolved "https://registry.yarnpkg.com/argv-formatter/-/argv-formatter-1.0.0.tgz#a0ca0cbc29a5b73e836eebe1cbf6c5e0e4eb82f9"
@@ -914,6 +919,16 @@ cosmiconfig@^7.0.0:
914919
path-type "^4.0.0"
915920
yaml "^1.10.0"
916921

922+
cosmiconfig@^9.0.0:
923+
version "9.0.0"
924+
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-9.0.0.tgz#34c3fc58287b915f3ae905ab6dc3de258b55ad9d"
925+
integrity sha512-itvL5h8RETACmOTFc4UfIyB2RfEHi71Ax6E/PivVxq9NseKbOWpeyHEOIbmAw1rs8Ak0VursQNww7lf7YtUwzg==
926+
dependencies:
927+
env-paths "^2.2.1"
928+
import-fresh "^3.3.0"
929+
js-yaml "^4.1.0"
930+
parse-json "^5.2.0"
931+
917932
cross-spawn@^7.0.3:
918933
version "7.0.3"
919934
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@@ -1054,7 +1069,7 @@ env-ci@^5.0.0:
10541069
fromentries "^1.3.2"
10551070
java-properties "^1.0.0"
10561071

1057-
env-paths@^2.2.0:
1072+
env-paths@^2.2.0, env-paths@^2.2.1:
10581073
version "2.2.1"
10591074
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
10601075
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==
@@ -1439,6 +1454,14 @@ import-fresh@^3.2.1:
14391454
parent-module "^1.0.0"
14401455
resolve-from "^4.0.0"
14411456

1457+
import-fresh@^3.3.0:
1458+
version "3.3.1"
1459+
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.1.tgz#9cecb56503c0ada1f2741dbbd6546e4b13b57ccf"
1460+
integrity sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ==
1461+
dependencies:
1462+
parent-module "^1.0.0"
1463+
resolve-from "^4.0.0"
1464+
14421465
import-from@^4.0.0:
14431466
version "4.0.0"
14441467
resolved "https://registry.yarnpkg.com/import-from/-/import-from-4.0.0.tgz#2710b8d66817d232e16f4166e319248d3d5492e2"
@@ -1630,6 +1653,13 @@ js-tokens@^4.0.0:
16301653
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
16311654
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
16321655

1656+
js-yaml@^4.1.0:
1657+
version "4.1.0"
1658+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
1659+
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
1660+
dependencies:
1661+
argparse "^2.0.1"
1662+
16331663
16341664
version "1.1.0"
16351665
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-1.1.0.tgz#b01307cb29b618a1ed26ec79e911f803c4da0040"
@@ -2522,7 +2552,7 @@ parse-json@^4.0.0:
25222552
error-ex "^1.3.1"
25232553
json-parse-better-errors "^1.0.1"
25242554

2525-
parse-json@^5.0.0:
2555+
parse-json@^5.0.0, parse-json@^5.2.0:
25262556
version "5.2.0"
25272557
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-5.2.0.tgz#c76fc66dee54231c962b22bcc8a72cf2f99753cd"
25282558
integrity sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg==

0 commit comments

Comments
 (0)