diff options
Diffstat (limited to '.github/workflows/check-recipe-version-bump/index.js')
-rw-r--r-- | .github/workflows/check-recipe-version-bump/index.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/.github/workflows/check-recipe-version-bump/index.js b/.github/workflows/check-recipe-version-bump/index.js new file mode 100644 index 0000000..606c1bd --- /dev/null +++ b/.github/workflows/check-recipe-version-bump/index.js | |||
@@ -0,0 +1,85 @@ | |||
1 | import core from '@actions/core'; | ||
2 | import exec from '@actions/exec'; | ||
3 | |||
4 | function isSemverGreater(a, b) { | ||
5 | console.log( | ||
6 | 'comparing ', | ||
7 | a, | ||
8 | 'and', | ||
9 | b, | ||
10 | ': ', | ||
11 | a.localeCompare(b, undefined, { numeric: true }), | ||
12 | ); | ||
13 | return a.localeCompare(b, undefined, { numeric: true }) === 1; | ||
14 | } | ||
15 | |||
16 | async function readFileFromGitBranch(branch, filename) { | ||
17 | let output = ''; | ||
18 | |||
19 | const options = { | ||
20 | silent: true, | ||
21 | listeners: { | ||
22 | stdout: data => { | ||
23 | output += data.toString(); | ||
24 | }, | ||
25 | }, | ||
26 | }; | ||
27 | |||
28 | await exec.exec('git', ['show', `${branch}:${filename}`], options); | ||
29 | |||
30 | return output; | ||
31 | } | ||
32 | |||
33 | try { | ||
34 | const changedFilesString = core.getInput('changed-files'); | ||
35 | const changedFiles = JSON.parse(changedFilesString); | ||
36 | |||
37 | const changedFilesInRecipes = changedFiles | ||
38 | .filter(filename => filename.startsWith('recipes/')) | ||
39 | .map(filename => filename.slice('recipes/'.length)); | ||
40 | |||
41 | const changedRecipes = [ | ||
42 | ...new Set( | ||
43 | changedFilesInRecipes | ||
44 | .map(filename => filename.replace(/\/(.*)$/, '')) | ||
45 | .sort(), | ||
46 | ), | ||
47 | ]; | ||
48 | |||
49 | const notBumpedUpRecipes = {}; | ||
50 | for (const recipe of changedRecipes) { | ||
51 | const packageJsonPath = `recipes/${recipe}/package.json`; | ||
52 | |||
53 | if (!changedFiles.includes(packageJsonPath)) { | ||
54 | notBumpedUpRecipes[recipe] = 'package.json not updated'; | ||
55 | continue; | ||
56 | } | ||
57 | |||
58 | // Check differences | ||
59 | const packageMain = JSON.parse( | ||
60 | await readFileFromGitBranch('origin/main', packageJsonPath), | ||
61 | ); | ||
62 | const packageCurrent = JSON.parse( | ||
63 | await readFileFromGitBranch('HEAD', packageJsonPath), | ||
64 | ); | ||
65 | |||
66 | if (!isSemverGreater(packageCurrent.version, packageMain.version)) { | ||
67 | notBumpedUpRecipes[ | ||
68 | recipe | ||
69 | ] = `${packageCurrent.version} is not greater than ${packageMain.version}`; | ||
70 | continue; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | if (Object.keys(notBumpedUpRecipes).length !== 0) { | ||
75 | core.setFailed( | ||
76 | 'The following recipes should have their version bumped: ' + | ||
77 | Object.keys(notBumpedUpRecipes).join(', ') + | ||
78 | '. Please check the contributing guide: https://github.com/ferdium/ferdium-recipes/blob/main/docs/updating.md' + | ||
79 | '\n' + | ||
80 | JSON.stringify(notBumpedUpRecipes, undefined, 2), | ||
81 | ); | ||
82 | } | ||
83 | } catch (error) { | ||
84 | core.setFailed(error.message); | ||
85 | } | ||