Skip to content

Commit

Permalink
Merge pull request #3441 from onflow/bastian/improve-update-Tool-7
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jun 27, 2024
2 parents c0fa37d + 79878f5 commit 92c8ee8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
1 change: 0 additions & 1 deletion tools/update/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ repos:

- repo: onflow/flow-evm-gateway
needsRelease: false
branch: main
mods:
- path: ""
deps:
Expand Down
64 changes: 49 additions & 15 deletions tools/update/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,27 @@ function capitalizeFirstLetter(string: string): string {

class Updater {

private versions = new Map<string, string>()

constructor(
public versions: Map<string, string>,
public config: CadenceUpdateToolConfigSchema,
public octokit: Octokit,
public protocol: Protocol
) {}

public setVersion(name: string, version: string) {
this.versions.set(name, version)
const match = version.match(/^v(\d+)/)
if (!match) {
return
}
const major = match[1]
if (major == "0" || major == "1") {
return
}
this.versions.set(`${name}/v${major}`, version)
}

async update(repoName: string | undefined): Promise<void> {
const rootFullRepoName = this.config.repo
const rootRepoVersion = this.getExpectedVersion(rootFullRepoName)
Expand Down Expand Up @@ -98,8 +112,8 @@ class Updater {

const [owner, repoName] = fullRepoName.split('/')

const defaultBranch = repo.branch ||
(await this.octokit.rest.repos.get({owner, repo: repoName})).data.default_branch
const defaultBranch = repo.branch || await this.getDefaultBranch(owner, repoName)

console.log(`> Default branch of repo ${fullRepoName}: ${defaultBranch}`)

const defaultRefResponse = await this.octokit.rest.git.getRef({
Expand Down Expand Up @@ -227,7 +241,7 @@ class Updater {

console.log(`✓ All deps of mod ${fullModName} at repo version ${version} are up-to-date`)

this.versions.set(fullModName, version)
this.setVersion(fullModName, version)

return true
}
Expand Down Expand Up @@ -418,11 +432,13 @@ class Updater {
prTitle = `[${modList}] ${prTitle}`
}

const base = repo.branch || (await this.getDefaultBranch(owner, repoName))

const pull = await this.octokit.rest.pulls.create({
owner,
repo: repoName,
head: branch,
base: repo.branch || "master",
base: base,
title: prTitle,
body: `
## Description
Expand Down Expand Up @@ -450,6 +466,11 @@ ${updateList}
console.log(`Cleaning up clone of ${fullRepoName} ...`)
await rm(dir, { recursive: true, force: true })
}

async getDefaultBranch(owner: string, repoName: string): Promise<string> {
const response = await this.octokit.rest.repos.get({owner, repo: repoName})
return response.data.default_branch
}
}

async function authenticate(): Promise<Octokit> {
Expand Down Expand Up @@ -509,7 +530,10 @@ class Releaser {
const dir = await mkdtemp(path.join(os.tmpdir(), `${owner}-${repoName}`))

console.log(`Cloning ${this.repo} ...`)
await gitClone(this.protocol, this.repo, dir, this.branch || 'master')

const branch = this.branch || (await this.getDefaultBranch(owner, repoName))

await gitClone(this.protocol, this.repo, dir, branch)
process.chdir(dir)

console.log(`Tagging ${this.repo} version ${this.version} ...`)
Expand All @@ -527,6 +551,11 @@ class Releaser {

console.log(`Now create a GitHub release: https://github.com/${this.repo}/releases/new?tag=${tag}`)
}

async getDefaultBranch(owner: string, repoName: string): Promise<string> {
const response = await this.octokit.rest.repos.get({owner, repo: repoName})
return response.data.default_branch
}
}

(async () => {
Expand Down Expand Up @@ -569,19 +598,24 @@ class Releaser {
async (args) => {
const config = await loadConfig(args.config)

const versions = new Map([
[config.repo, args.version],
])
const protocol = args.useSSH ? Protocol.SSH : Protocol.HTTPS

const updater = new Updater(config, octokit, protocol)

const {version, versions} = args;

for (const versionEntry of args.versions.split(',')) {
const [repo, version] = versionEntry.split('@')
versions.set(repo, version)
if (version) {
updater.setVersion(config.repo, version)
}

const protocol = args.useSSH ? Protocol.SSH : Protocol.HTTPS
if (versions) {
for (const versionEntry of versions.split(',')) {
const [repo, version] = versionEntry.split('@')
updater.setVersion(repo, version)
}
}

await new Updater(versions, config, octokit, protocol)
.update(args.repo)
await updater.update(args.repo)
}
)
.command(
Expand Down

0 comments on commit 92c8ee8

Please sign in to comment.