From 747c3a196053332c3b7e9b27bd5ce309f3bcb0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Fri, 2 Feb 2024 14:10:36 -0800 Subject: [PATCH] improve cloning, dependency updating, and PR title - add support for different base branch - add support for prefixing PR title with mod(s) - update Go mod dependencies in one command --- tools/update/config.schema.json | 8 +++++ tools/update/config.schema.ts | 8 +++++ tools/update/config.yaml | 6 ++++ tools/update/main.ts | 53 +++++++++++++++++++++++---------- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/tools/update/config.schema.json b/tools/update/config.schema.json index dbab327830..1defab67a0 100644 --- a/tools/update/config.schema.json +++ b/tools/update/config.schema.json @@ -28,6 +28,10 @@ "type": "string", "description": "The repository name" }, + "branch": { + "type": "string", + "description": "The branch name" + }, "mods": { "type": "array", "items": { @@ -44,6 +48,10 @@ "items": { "type": "string" } + }, + "prefixPRTitle": { + "type": "boolean", + "description": "Prefix the PR title with the mod(s)" } }, "additionalProperties": false, diff --git a/tools/update/config.schema.ts b/tools/update/config.schema.ts index 1cfccb9afd..f0e6d33a27 100644 --- a/tools/update/config.schema.ts +++ b/tools/update/config.schema.ts @@ -17,6 +17,10 @@ export interface Repo { * The repository name */ repo: string; + /** + * The branch name + */ + branch?: string; /** * The modules of the repository */ @@ -26,6 +30,10 @@ export interface Repo { */ needsRelease: boolean; updateLabels?: string[]; + /** + * Prefix the PR title with the mod(s) + */ + prefixPRTitle?: boolean; } export interface Mod { /** diff --git a/tools/update/config.yaml b/tools/update/config.yaml index 42d1ed12e5..42bae14bf9 100644 --- a/tools/update/config.yaml +++ b/tools/update/config.yaml @@ -9,6 +9,7 @@ repos: - repo: onflow/cadence-tools needsRelease: true + prefixPRTitle: true mods: - path: lint deps: @@ -17,6 +18,7 @@ repos: - repo: onflow/flow-go needsRelease: false + branch: feature/stable-cadence mods: - path: "" deps: @@ -42,6 +44,7 @@ repos: - repo: onflow/cadence-tools needsRelease: true + prefixPRTitle: true mods: - path: test deps: @@ -52,6 +55,8 @@ repos: - repo: onflow/flow-cli needsRelease: false + prefixPRTitle: true + branch: feature/stable-cadence mods: - path: flowkit deps: @@ -62,6 +67,7 @@ repos: - repo: onflow/cadence-tools needsRelease: true + prefixPRTitle: true mods: - path: languageserver deps: diff --git a/tools/update/main.ts b/tools/update/main.ts index 5000e3fcd5..75f169e6d6 100644 --- a/tools/update/main.ts +++ b/tools/update/main.ts @@ -37,6 +37,10 @@ function extractVersionCommit(version: string): string | null { return parts[parts.length-1] } +function capitalizeFirstLetter(string: string): string { + return string.charAt(0).toUpperCase() + string.slice(1); +} + class Updater { constructor( @@ -341,8 +345,8 @@ class Updater { const [owner, repoName] = fullRepoName.split('/') const dir = await mkdtemp(path.join(os.tmpdir(), `${owner}-${repoName}`)) - console.log(`Cloning ${fullRepoName} ...`) - await gitClone(this.protocol, fullRepoName, dir) + console.log(`Cloning ${fullRepoName} ${repo.branch ? `(branch ${repo.branch}) `: ""}...`) + await gitClone(this.protocol, fullRepoName, dir, repo.branch) process.chdir(dir) const rootFullRepoName = this.config.repo @@ -360,19 +364,25 @@ class Updater { console.log(`Updating mod ${fullModName} ...`) process.chdir(path.join(dir, mod.path)) - for (const dep of mod.deps) { + const deps = mod.deps.map((dep) => { const newVersion = this.getExpectedVersion(dep) - console.log(`Updating mod ${fullModName} dep ${dep} to version ${newVersion} ...`) - await exec(`go get github.com/${dep}@${newVersion}`) updates.set(dep, newVersion) - } + return `github.com/${dep}@${newVersion}` + }) + + console.log(`Updating mod ${fullModName} to ${deps.join(', ')} ...`) + + await exec(`go get ${deps.join(' ')}`) console.log(`Cleaning up mod ${fullModName} ...`) await exec(`go mod tidy`) } console.log(`Committing update ...`) - await exec(`git commit -a -m "auto update to ${rootFullRepoName} ${rootRepoVersion}"`) + + const message = `Update to ${capitalizeFirstLetter(rootRepoName)} ${rootRepoVersion}` + + await exec(`git commit -a -m "${message}"`) console.log(`Pushing update ...`) await exec(`git push -u origin ${branch}"`) @@ -381,15 +391,25 @@ class Updater { let updateList = '' for (const [dep, version] of updates.entries()) { - updateList += `- [${dep} ${version}](https://github.com/${dep}/releases/tag/${version})\n` + const releaseURL = isValidSemVer(version) + ? `https://github.com/${dep}/releases/tag/${version}` + : `https://github.com/${dep}/commit/${version}` + + updateList += `- [${dep} ${version}](${releaseURL})\n` + } + + let prTitle = message + if (repo.prefixPRTitle) { + const modList = repo.mods.map((mod) => mod.path).join(', ') + prTitle = `[${modList}] ${prTitle}` } const pull = await this.octokit.rest.pulls.create({ owner, repo: repoName, head: branch, - base: 'master', - title: `Auto update to ${rootFullRepoName} ${rootRepoVersion}`, + base: repo.branch || "master", + title: prTitle, body: ` ## Description @@ -474,7 +494,8 @@ class Releaser { const dir = await mkdtemp(path.join(os.tmpdir(), `${owner}-${repoName}`)) console.log(`Cloning ${this.repo} ...`) - await gitClone(this.protocol, this.repo, dir) + // TODO: add support for different branches + await gitClone(this.protocol, this.repo, dir, 'master') process.chdir(dir) console.log(`Tagging ${this.repo} version ${this.version} ...`) @@ -586,20 +607,20 @@ class Releaser { })() -async function gitClone(protocol: Protocol, fullRepoName: string, dir: string) { - let command: string +async function gitClone(protocol: Protocol, fullRepoName: string, dir: string, branch?: string) { + let prefix: string switch (protocol) { case Protocol.HTTPS: - command = `git clone https://github.com/${fullRepoName} ${dir}` + prefix = "https://github.com/" break case Protocol.SSH: - command = `git clone git@github.com:${fullRepoName} ${dir}` + prefix = 'git@github.com:' break default: console.error(`unsupported protocol: ${protocol}`) return } - await exec(command) + await exec(`git clone ${branch ? `-b ${branch} ` : ""}${prefix}${fullRepoName} ${dir}`) } async function runWithConsoleGroup(func: () => Promise): Promise {