|
| 1 | +/****************************************************************************** |
| 2 | + * Scaleway GO SDK release script. |
| 3 | + * |
| 4 | + * This script will trigger a release process for the scaleway Go SDK. |
| 5 | + * |
| 6 | + * The script will proceed as follow: |
| 7 | + * - Create a new remote `scaleway-release` that point to main repo |
| 8 | + * - Prompt the new version number |
| 9 | + * - Create release commit |
| 10 | + * - Generate a changelog |
| 11 | + * - Update version in scw/version.go |
| 12 | + * - Ask to review changes |
| 13 | + * - Ask to merge this changes to master via PR |
| 14 | + * - Create a github release |
| 15 | + * - Ask to create a github release |
| 16 | + * - Create a post release commit |
| 17 | + * - Update scw/version.go to add +dev at the end |
| 18 | + * - Ask to merge this changes to master via PR |
| 19 | + * - Delete temporary remote `scaleway-release` that was created earlier |
| 20 | + ******************************************************************************/ |
| 21 | + |
| 22 | +const { spawnSync } = require("child_process"), |
| 23 | + readline = require("readline"), |
| 24 | + fs = require("fs") |
| 25 | +; |
| 26 | + |
| 27 | +const gitRawCommits = require("git-raw-commits"), |
| 28 | + semver = require("semver"), |
| 29 | + getStream = require("get-stream"), |
| 30 | + _ = require("colors") |
| 31 | +; |
| 32 | + |
| 33 | +const _typeReg = /(?<type>[a-zA-Z]+)/; |
| 34 | +const _scopeReg = /(\((?<scope>.*)\))?/; |
| 35 | +const _messageReg = /(?<message>[^(]*)/; |
| 36 | +const _mrReg = /(\(#(?<mr>[0-9]+)\))?/; |
| 37 | +const COMMIT_REGEX = new RegExp(`${_typeReg.source}${_scopeReg.source}: *${_messageReg.source} *${_mrReg.source}`); |
| 38 | +const CHANGELOG_PATH = "../../CHANGELOG.md"; |
| 39 | +const GO_VERSION_PATH = "../../scw/version.go"; |
| 40 | +const TMP_BRANCH = "new-release"; |
| 41 | +const TMP_REMOTE = "scaleway-release"; |
| 42 | +const REPO_URL = "[email protected]:scaleway/scaleway-sdk-go.git"; |
| 43 | + |
| 44 | +async function main() { |
| 45 | + |
| 46 | + // |
| 47 | + // Initialization |
| 48 | + // |
| 49 | + console.log("Adding temporary remote on local repo".blue); |
| 50 | + git( "remote", "add", TMP_REMOTE, REPO_URL); |
| 51 | + console.log(` Successfully created ${TMP_REMOTE} remote`.green); |
| 52 | + |
| 53 | + console.log("Make sure we are working on an up to date master".blue); |
| 54 | + git( "fetch", TMP_REMOTE); |
| 55 | + git( "checkout", `${TMP_REMOTE}/master`); |
| 56 | + console.log(` Successfully created ${TMP_REMOTE} remote`.green); |
| 57 | + |
| 58 | + console.log("Trying to find last release tag".blue); |
| 59 | + const lastSemverTag = git("tag") |
| 60 | + .trim() |
| 61 | + .split("\n") |
| 62 | + .filter(semver.valid) |
| 63 | + .sort((a, b) => semver.rcompare(semver.clean(a), semver.clean(b)))[0]; |
| 64 | + console.log(` Last found release tag was ${lastSemverTag}`.green); |
| 65 | + |
| 66 | + console.log("Listing commit since last release".blue); |
| 67 | + const commits = (await getStream.array(gitRawCommits({ from: lastSemverTag, format: "%s" }))).map(c => c.toString().trim()); |
| 68 | + commits.forEach(c => console.log(` ${c}`.grey)); |
| 69 | + console.log(` We found ${commits.length} commits since last release`.green); |
| 70 | + |
| 71 | + const newVersion = semver.clean(await prompt("Enter new version: ".magenta)); |
| 72 | + if (!newVersion) { |
| 73 | + throw new Error(`invalid version`); |
| 74 | + } |
| 75 | + |
| 76 | + // |
| 77 | + // Creating release commit |
| 78 | + // |
| 79 | + |
| 80 | + console.log(`Updating ${CHANGELOG_PATH} and ${GO_VERSION_PATH}`.blue); |
| 81 | + // Generate changelog lines by section |
| 82 | + const changelogLines = { feat: [], fix: [] }; |
| 83 | + commits.forEach(commit => { |
| 84 | + const result = COMMIT_REGEX.exec(commit); |
| 85 | + if (!result || !(result.groups.type in changelogLines)) { |
| 86 | + console.warn(`WARNING: Ignoring commit ${commit}`.yellow); |
| 87 | + return; |
| 88 | + } |
| 89 | + const stdCommit = result.groups; |
| 90 | + |
| 91 | + let line = [`*`, stdCommit.scope ? `**${stdCommit.scope}**:` : "", stdCommit.message, stdCommit.mr ? `([#${stdCommit.mr}](https://github.com/scaleway/scaleway-sdk-go/pull/${stdCommit.mr}))` : ""] |
| 92 | + .map(s => s.trim()) |
| 93 | + .filter(v => v) |
| 94 | + .join(" "); |
| 95 | + changelogLines[stdCommit.type].push(line); |
| 96 | + }); |
| 97 | + |
| 98 | + const changelogHeader = `## v${newVersion} (${new Date().toISOString().substring(0, 10)})`; |
| 99 | + const changelogSections = []; |
| 100 | + if (changelogLines.feat) { |
| 101 | + changelogSections.push("### Features\n\n" + changelogLines.feat.join("\n")); |
| 102 | + } |
| 103 | + if (changelogLines.fix) { |
| 104 | + changelogSections.push("### Fixes\n\n" + changelogLines.fix.join("\n")); |
| 105 | + } |
| 106 | + const changelogBody = changelogSections.join("\n\n"); |
| 107 | + const changelog = `${changelogHeader}\n\n${changelogBody}`; |
| 108 | + |
| 109 | + replaceInFile(CHANGELOG_PATH, "# Changelog", "# Changelog\n\n" + changelog + "\n"); |
| 110 | + replaceInFile(GO_VERSION_PATH, /const version[^\n]*\n/, `const version = "v${newVersion}"\n`); |
| 111 | + console.log(` Update success`.green); |
| 112 | + |
| 113 | + await prompt(`Please review ${CHANGELOG_PATH} and ${GO_VERSION_PATH}. When everything is fine hit enter to continue ...`.magenta); |
| 114 | + |
| 115 | + console.log(`Creating release commit`.blue); |
| 116 | + git("checkout", "-b", TMP_BRANCH); |
| 117 | + git("add", CHANGELOG_PATH, GO_VERSION_PATH); |
| 118 | + git("commit", "-m", `chore: release ${newVersion}`); |
| 119 | + git("push", "-f", "--set-upstream", TMP_REMOTE, TMP_BRANCH); |
| 120 | + |
| 121 | + await prompt(`Please create an PR here: https://github.com/scaleway/scaleway-sdk-go/pull/new/new-release . Hit enter when its merged .....`.magenta); |
| 122 | + |
| 123 | + console.log("Time to create a github release with the following info\n".blue); |
| 124 | + console.log(`Title: v${newVersion}\n\n`.gray); |
| 125 | + console.log(`${changelogBody}\n\n`.gray); |
| 126 | + await prompt(`You should create a new github release here: https://github.com/scaleway/scaleway-sdk-go/releases/new/ . Hit enter when the new release is created .....`.magenta); |
| 127 | + |
| 128 | + // |
| 129 | + // Creating post release commit |
| 130 | + // |
| 131 | + console.log("Make sure we pull the latest commit from master".blue); |
| 132 | + git("fetch", TMP_REMOTE); |
| 133 | + git("checkout", `${TMP_REMOTE}/master`); |
| 134 | + console.log(" Successfully checkout upstream/master".green); |
| 135 | + |
| 136 | + console.log(`Creating post release commit`.blue); |
| 137 | + git("branch", "-D", TMP_BRANCH); |
| 138 | + git("checkout", "-b", TMP_BRANCH); |
| 139 | + replaceInFile(GO_VERSION_PATH, /const version[^\n]*\n/, `const version = "v${newVersion}+dev"\n`); |
| 140 | + git("add", GO_VERSION_PATH); |
| 141 | + git("commit", "-m", "chore: post release commit"); |
| 142 | + git("push", "-f", "--set-upstream", TMP_REMOTE, TMP_BRANCH); |
| 143 | + git("checkout", "master"); |
| 144 | + git("branch", "-D", TMP_BRANCH); |
| 145 | + await prompt(`Please create an PR here: https://github.com/scaleway/scaleway-sdk-go/pull/new/new-release . Hit enter when its merged .....`.magenta); |
| 146 | + |
| 147 | + console.log("Make sure we pull the latest commit from master".blue); |
| 148 | + git("pull", TMP_REMOTE, "master"); |
| 149 | + console.log(" Successfully pull master".green); |
| 150 | + |
| 151 | + console.log("Remove temporary remote".blue); |
| 152 | + git("remote", "remove", TMP_REMOTE); |
| 153 | + console.log(" Successfully remove temporary remote".green); |
| 154 | + |
| 155 | + console.log(`🚀 Release Success `.green); |
| 156 | +} |
| 157 | + |
| 158 | +function git(...args) { |
| 159 | + console.log(` git ${args.join(" ")}`.grey); |
| 160 | + const { stdout, status, stderr } = spawnSync("git", args, { encoding: "utf8" }); |
| 161 | + if (status !== 0) { |
| 162 | + throw new Error(`return status ${status}\n${stderr}\n`); |
| 163 | + } |
| 164 | + return stdout; |
| 165 | +} |
| 166 | + |
| 167 | +function replaceInFile(path, oldStr, newStr) { |
| 168 | + console.log(` Editing ${path}`.grey); |
| 169 | + const content = fs.readFileSync(path, { encoding: "utf8" }).replace(oldStr, newStr); |
| 170 | + fs.writeFileSync(path, content); |
| 171 | +} |
| 172 | + |
| 173 | +function prompt(prompt) { |
| 174 | + const rl = readline.createInterface({ |
| 175 | + input: process.stdin, |
| 176 | + output: process.stdout |
| 177 | + }); |
| 178 | + return new Promise(resolve => { |
| 179 | + rl.question(prompt, answer => { |
| 180 | + resolve(answer); |
| 181 | + rl.close(); |
| 182 | + }); |
| 183 | + }); |
| 184 | +} |
| 185 | + |
| 186 | +main().catch(console.error); |
0 commit comments