forked from backstage/community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Version Bump Matrix Workflow
Signed-off-by: Andre Wanlin <awanlin@spotify.com>
- Loading branch information
Showing
3 changed files
with
101 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,115 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const fetch = require('node-fetch') | ||
const { EOL } = require('os'); | ||
/* eslint-disable @backstage/no-undeclared-imports */ | ||
/* | ||
* Copyright 2020 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
async function getBackstageVersion() { | ||
const rootPath = path.resolve(__dirname, '../backstage.json'); | ||
return fs.readJson(rootPath).then(_ => _.version); | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import fetch from 'node-fetch'; | ||
import { EOL } from 'os'; | ||
|
||
async function getBackstageVersion(workspace) { | ||
const rootPath = path.resolve(`workspaces/${workspace}/backstage.json`); | ||
if (!fs.exists(rootPath)) { | ||
return 'N/A'; | ||
} | ||
return fs.readJson(rootPath).then((_) => _.version); | ||
} | ||
|
||
async function getLatestRelease() { | ||
const response = await fetch('https://api.github.com/repos/backstage/backstage/releases/latest') | ||
const response = await fetch( | ||
'https://api.github.com/repos/backstage/backstage/releases/latest', | ||
); | ||
const json = await response.json(); | ||
return json | ||
return json; | ||
} | ||
|
||
async function getLatestPreRelease() { | ||
const response = await fetch('https://api.github.com/repos/backstage/backstage/releases') | ||
const response = await fetch( | ||
'https://api.github.com/repos/backstage/backstage/releases', | ||
); | ||
const json = await response.json(); | ||
|
||
const preReleasesOnly = json.filter(release => { | ||
return release.prerelease === true | ||
}) | ||
|
||
const latestPreRelease = preReleasesOnly.sort((a,b) => {return new Date(b.published_at) - new Date(a.published_at)})[0]; | ||
const preReleasesOnly = json.filter((release) => { | ||
return release.prerelease === true; | ||
}); | ||
|
||
const latestPreRelease = preReleasesOnly.sort((a, b) => { | ||
return new Date(b.published_at) - new Date(a.published_at); | ||
})[0]; | ||
|
||
return latestPreRelease | ||
return latestPreRelease; | ||
} | ||
|
||
async function main() { | ||
|
||
// Get the workspace | ||
const [script, workspace] = process.argv.slice(1); | ||
if (!workspace) { | ||
throw new Error(`Argument must be ${script} <workspace>`); | ||
} | ||
|
||
// Get the current Backstage version from the backstage.json file | ||
const backstageVersion = await getBackstageVersion() | ||
const backstageVersion = await getBackstageVersion(workspace); | ||
// Get the latest Backstage Release from the GitHub API | ||
const latestRelease = await getLatestRelease() | ||
const latestRelease = await getLatestRelease(); | ||
// Get the latest Backstage Pre-release from the GitHub API | ||
const latestPreRelease = await getLatestPreRelease() | ||
const latestPreRelease = await getLatestPreRelease(); | ||
|
||
console.log(`Current Backstage version is: v${backstageVersion}`) | ||
console.log(`Latest Release version is: ${latestRelease.name}, published on: ${latestRelease.published_at}`) | ||
console.log(`Latest Pre-release version is: ${latestPreRelease.name}, published on: ${latestPreRelease.published_at}`) | ||
console.log() | ||
console.log(`Current Backstage version is: v${backstageVersion}`); | ||
console.log( | ||
`Latest Release version is: ${latestRelease.name}, published on: ${latestRelease.published_at}`, | ||
); | ||
console.log( | ||
`Latest Pre-release version is: ${latestPreRelease.name}, published on: ${latestPreRelease.published_at}`, | ||
); | ||
console.log(); | ||
|
||
const latestReleaseDate = new Date(latestRelease.published_at).getTime() | ||
const latestPreReleaseDate = new Date(latestPreRelease.published_at).getTime() | ||
if (latestReleaseDate > latestPreReleaseDate){ | ||
console.log(`Latest Release is newer than latest Pre-release, using Latest Release name ${latestRelease.name}`) | ||
console.log() | ||
const latestReleaseDate = new Date(latestRelease.published_at).getTime(); | ||
const latestPreReleaseDate = new Date( | ||
latestPreRelease.published_at, | ||
).getTime(); | ||
if (latestReleaseDate > latestPreReleaseDate) { | ||
console.log( | ||
`Latest Release is newer than latest Pre-release, using Latest Release name ${latestRelease.name}`, | ||
); | ||
console.log(); | ||
|
||
await fs.appendFile(process.env.GITHUB_OUTPUT, `release_version=${latestRelease.name.substring(1)}${EOL}`); | ||
} | ||
else { | ||
console.log(`Latest Release is older than latest Pre-release, using Latest Pre-release name ${latestPreRelease.name}`) | ||
console.log() | ||
await fs.appendFile( | ||
process.env.GITHUB_OUTPUT, | ||
`release_version=${latestRelease.name.substring(1)}${EOL}`, | ||
); | ||
} else { | ||
console.log( | ||
`Latest Release is older than latest Pre-release, using Latest Pre-release name ${latestPreRelease.name}`, | ||
); | ||
console.log(); | ||
|
||
await fs.appendFile(process.env.GITHUB_OUTPUT, `release_version=${latestPreRelease.name.substring(1)}${EOL}`); | ||
await fs.appendFile( | ||
process.env.GITHUB_OUTPUT, | ||
`release_version=${latestPreRelease.name.substring(1)}${EOL}`, | ||
); | ||
} | ||
|
||
await fs.appendFile(process.env.GITHUB_OUTPUT, `current_version=${backstageVersion}${EOL}`); | ||
await fs.appendFile( | ||
process.env.GITHUB_OUTPUT, | ||
`current_version=${backstageVersion}${EOL}`, | ||
); | ||
} | ||
|
||
main().catch(error => { | ||
main().catch((error) => { | ||
console.error(error.stack); | ||
process.exit(1); | ||
}); |