Skip to content

Commit

Permalink
Merge pull request #3070 from onflow/bastian/improve-update-script
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Feb 6, 2024
2 parents 55925d1 + 747c3a1 commit 7be52fc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
8 changes: 8 additions & 0 deletions tools/update/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"type": "string",
"description": "The repository name"
},
"branch": {
"type": "string",
"description": "The branch name"
},
"mods": {
"type": "array",
"items": {
Expand All @@ -44,6 +48,10 @@
"items": {
"type": "string"
}
},
"prefixPRTitle": {
"type": "boolean",
"description": "Prefix the PR title with the mod(s)"
}
},
"additionalProperties": false,
Expand Down
8 changes: 8 additions & 0 deletions tools/update/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface Repo {
* The repository name
*/
repo: string;
/**
* The branch name
*/
branch?: string;
/**
* The modules of the repository
*/
Expand All @@ -26,6 +30,10 @@ export interface Repo {
*/
needsRelease: boolean;
updateLabels?: string[];
/**
* Prefix the PR title with the mod(s)
*/
prefixPRTitle?: boolean;
}
export interface Mod {
/**
Expand Down
6 changes: 6 additions & 0 deletions tools/update/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:

- repo: onflow/cadence-tools
needsRelease: true
prefixPRTitle: true
mods:
- path: lint
deps:
Expand All @@ -17,6 +18,7 @@ repos:

- repo: onflow/flow-go
needsRelease: false
branch: feature/stable-cadence
mods:
- path: ""
deps:
Expand All @@ -42,6 +44,7 @@ repos:

- repo: onflow/cadence-tools
needsRelease: true
prefixPRTitle: true
mods:
- path: test
deps:
Expand All @@ -52,6 +55,8 @@ repos:

- repo: onflow/flow-cli
needsRelease: false
prefixPRTitle: true
branch: feature/stable-cadence
mods:
- path: flowkit
deps:
Expand All @@ -62,6 +67,7 @@ repos:

- repo: onflow/cadence-tools
needsRelease: true
prefixPRTitle: true
mods:
- path: languageserver
deps:
Expand Down
53 changes: 37 additions & 16 deletions tools/update/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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}"`)
Expand All @@ -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
Expand Down Expand Up @@ -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} ...`)
Expand Down Expand Up @@ -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 = '[email protected]:'
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<boolean>): Promise<boolean> {
Expand Down

0 comments on commit 7be52fc

Please sign in to comment.