|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable @backstage/no-undeclared-imports */ |
| 3 | +/* |
| 4 | + * Copyright 2020 The Backstage Authors |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +// This script is used to determine whether a particular commit has changes |
| 20 | +// that should lead to a release. It is run as part of the main master build |
| 21 | +// to determine whether the release flow should be run as well. |
| 22 | +// |
| 23 | +// It has the following output which can be used later in GitHub actions: |
| 24 | +// |
| 25 | +// needs_release = 'true' | 'false' |
| 26 | + |
| 27 | +import { execFile as execFileCb } from 'child_process'; |
| 28 | +import { promises as fs } from 'fs'; |
| 29 | +import { promisify } from 'util'; |
| 30 | +import { resolve as resolvePath } from 'path'; |
| 31 | +import { EOL } from 'os'; |
| 32 | + |
| 33 | +import * as url from 'url'; |
| 34 | + |
| 35 | +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
| 36 | + |
| 37 | +const parentRef = process.env.COMMIT_SHA_BEFORE || 'HEAD^'; |
| 38 | +const targetBranch = process.env.TARGET_BRANCH || 'main'; |
| 39 | + |
| 40 | +const execFile = promisify(execFileCb); |
| 41 | + |
| 42 | +async function runPlain(cmd, ...args) { |
| 43 | + try { |
| 44 | + const { stdout } = await execFile(cmd, args, { shell: true }); |
| 45 | + return stdout.trim(); |
| 46 | + } catch (error) { |
| 47 | + if (error.stderr) { |
| 48 | + process.stderr.write(error.stderr); |
| 49 | + } |
| 50 | + if (!error.code) { |
| 51 | + throw error; |
| 52 | + } |
| 53 | + throw new Error( |
| 54 | + `Command '${[cmd, ...args].join(' ')}' failed with code ${error.code}`, |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function main() { |
| 60 | + process.cwd(resolvePath(__dirname)); |
| 61 | + |
| 62 | + if (!process.env.GITHUB_OUTPUT) { |
| 63 | + throw new Error('GITHUB_OUTPUT environment variable not set'); |
| 64 | + } |
| 65 | + |
| 66 | + // Ensure we have fetched the targetBranch |
| 67 | + await runPlain('git', 'fetch', 'origin', targetBranch); |
| 68 | + |
| 69 | + const diff = await runPlain( |
| 70 | + 'git', |
| 71 | + 'diff', |
| 72 | + '--name-only', |
| 73 | + parentRef, |
| 74 | + "'*/package.json'", // Git treats this as what would usually be **/package.json |
| 75 | + ); |
| 76 | + |
| 77 | + const packageList = diff |
| 78 | + .split('\n') |
| 79 | + .filter(path => path.match(/^(packages|plugins)\/[^/]+\/package\.json$/)); |
| 80 | + |
| 81 | + const packageVersions = await Promise.all( |
| 82 | + packageList.map(async path => { |
| 83 | + let name; |
| 84 | + let newVersion; |
| 85 | + let oldVersion; |
| 86 | + |
| 87 | + try { |
| 88 | + const data = JSON.parse( |
| 89 | + await runPlain('git', 'show', `${parentRef}:${path}`), |
| 90 | + ); |
| 91 | + name = data.name; |
| 92 | + oldVersion = data.version; |
| 93 | + } catch { |
| 94 | + oldVersion = '<none>'; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + const data = JSON.parse(await fs.readFile(path, 'utf8')); |
| 99 | + name = data.name; |
| 100 | + newVersion = data.version; |
| 101 | + } catch (error) { |
| 102 | + if (error.code === 'ENOENT') { |
| 103 | + newVersion = '<none>'; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { name, oldVersion, newVersion }; |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + const newVersions = packageVersions.filter( |
| 112 | + ({ oldVersion, newVersion }) => |
| 113 | + oldVersion !== newVersion && |
| 114 | + oldVersion !== '<none>' && |
| 115 | + newVersion !== '<none>', |
| 116 | + ); |
| 117 | + |
| 118 | + if (newVersions.length === 0) { |
| 119 | + console.log('No package version bumps detected, no release needed'); |
| 120 | + await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=false${EOL}`); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + console.log('Package version bumps detected, a new release is needed'); |
| 125 | + const maxLength = Math.max(...newVersions.map(_ => _.name.length)); |
| 126 | + for (const { name, oldVersion, newVersion } of newVersions) { |
| 127 | + console.log( |
| 128 | + ` ${name.padEnd(maxLength, ' ')} ${oldVersion} to ${newVersion}`, |
| 129 | + ); |
| 130 | + } |
| 131 | + await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=true${EOL}`); |
| 132 | +} |
| 133 | + |
| 134 | +main().catch(error => { |
| 135 | + console.error(error.stack); |
| 136 | + process.exit(1); |
| 137 | +}); |
0 commit comments