aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/check-recipe-version-bump/index.js
blob: 606c1bddce1bc0c6e2f963be4a7d312f20154b2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import core from '@actions/core';
import exec from '@actions/exec';

function isSemverGreater(a, b) {
  console.log(
    'comparing ',
    a,
    'and',
    b,
    ': ',
    a.localeCompare(b, undefined, { numeric: true }),
  );
  return a.localeCompare(b, undefined, { numeric: true }) === 1;
}

async function readFileFromGitBranch(branch, filename) {
  let output = '';

  const options = {
    silent: true,
    listeners: {
      stdout: data => {
        output += data.toString();
      },
    },
  };

  await exec.exec('git', ['show', `${branch}:${filename}`], options);

  return output;
}

try {
  const changedFilesString = core.getInput('changed-files');
  const changedFiles = JSON.parse(changedFilesString);

  const changedFilesInRecipes = changedFiles
    .filter(filename => filename.startsWith('recipes/'))
    .map(filename => filename.slice('recipes/'.length));

  const changedRecipes = [
    ...new Set(
      changedFilesInRecipes
        .map(filename => filename.replace(/\/(.*)$/, ''))
        .sort(),
    ),
  ];

  const notBumpedUpRecipes = {};
  for (const recipe of changedRecipes) {
    const packageJsonPath = `recipes/${recipe}/package.json`;

    if (!changedFiles.includes(packageJsonPath)) {
      notBumpedUpRecipes[recipe] = 'package.json not updated';
      continue;
    }

    // Check differences
    const packageMain = JSON.parse(
      await readFileFromGitBranch('origin/main', packageJsonPath),
    );
    const packageCurrent = JSON.parse(
      await readFileFromGitBranch('HEAD', packageJsonPath),
    );

    if (!isSemverGreater(packageCurrent.version, packageMain.version)) {
      notBumpedUpRecipes[
        recipe
      ] = `${packageCurrent.version} is not greater than ${packageMain.version}`;
      continue;
    }
  }

  if (Object.keys(notBumpedUpRecipes).length !== 0) {
    core.setFailed(
      'The following recipes should have their version bumped: ' +
      Object.keys(notBumpedUpRecipes).join(', ') +
        '. Please check the contributing guide: https://github.com/ferdium/ferdium-recipes/blob/main/docs/updating.md' +
        '\n' +
        JSON.stringify(notBumpedUpRecipes, undefined, 2),
    );
  }
} catch (error) {
  core.setFailed(error.message);
}