Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: tag version on codegen server (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Nov 23, 2022
1 parent d7fcd39 commit 428f3f2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 109 deletions.
55 changes: 50 additions & 5 deletions codegen/server/capi_repo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PermanentMemo, TimedMemo } from "../../util/memo.ts"
import { SHA_ABBREV_LENGTH } from "./git_utils.ts"
import { getFullSha, getSha, SHA_ABBREV_LENGTH } from "./git_utils.ts"
import { CodegenServer } from "./server.ts"

const TAGS_TTL = 60_000 // 1 minute
Expand All @@ -13,6 +13,7 @@ export const R_SHA_VERSION = /^sha:([0-9a-f]+)$/

export abstract class CapiCodegenServer extends CodegenServer {
async normalizeVersion(version: string) {
if (version === this.mainVersion) return version
const tagMatch = R_TAG_VERSION.exec(version)
if (tagMatch) return "v" + tagMatch[1]
if (R_REF_VERSION.test(version)) {
Expand All @@ -28,11 +29,16 @@ export abstract class CapiCodegenServer extends CodegenServer {
throw this.e404()
}

moduleFileUrl(version: string, path: string) {
if (this.local && version === this.version) {
async canHandleVersion(version: string): Promise<boolean> {
return version === this.mainVersion
|| (await this.versionSha(version)) === (await this.versionSha(this.mainVersion))
}

async moduleFileUrl(version: string, path: string) {
if (this.local && (await this.canHandleVersion(version))) {
return new URL("../.." + path, import.meta.url).toString()
}
if (R_REF_VERSION.test(version)) {
if (R_TAG_VERSION.test(version)) {
return `https://deno.land/x/capi@${version}${path}`
}
const shaMatch = R_SHA_VERSION.exec(version)
Expand All @@ -46,7 +52,7 @@ export abstract class CapiCodegenServer extends CodegenServer {
async versionSuggestions(): Promise<string[]> {
return [
...new Set((await Promise.all([
this.local ? [this.version] : [],
this.local ? [this.mainVersion] : [],
this.tags(),
this.branches().then(Object.keys),
])).flat()),
Expand Down Expand Up @@ -76,6 +82,45 @@ export abstract class CapiCodegenServer extends CodegenServer {
})
}

async versionSha(version: string) {
if (version === "local") {
return getSha()
}
if (R_TAG_VERSION.test(version)) {
return (await this.tagSha(version)).slice(0, SHA_ABBREV_LENGTH)
}
const shaMatch = R_SHA_VERSION.exec(version)
if (shaMatch) {
return shaMatch[1]!
}
throw new Error("expected normalized version")
}

async versionFullSha(version: string) {
if (version === "local") {
return getFullSha()
}
if (R_TAG_VERSION.test(version)) {
return this.tagSha(version)
}
const shaMatch = R_SHA_VERSION.exec(version)
if (shaMatch) {
return await this.fullSha(shaMatch[1]!)
}
throw new Error("expected normalized version")
}

tagShaMemo = new PermanentMemo<string, string>()
tagSha(tag: string) {
return this.fullShaMemo.run(tag, async () => {
const refs: GithubRef[] = await json(
`${GITHUB_API_REPO}/git/matching-refs/tags/${tag}`,
)
if (!refs[0]) throw this.e404()
return refs[0].object.sha
})
}

fullShaMemo = new PermanentMemo<string, string>()
fullSha(sha: string) {
return this.fullShaMemo.run(sha, async () => {
Expand Down
37 changes: 4 additions & 33 deletions codegen/server/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { PermanentMemo } from "../../util/memo.ts"
import {
CapiCodegenServer,
GITHUB_API_REPO,
GithubRef,
json,
R_REF_VERSION,
R_SHA_VERSION,
} from "./capi_repo.ts"
import { CapiCodegenServer, GITHUB_API_REPO, json } from "./capi_repo.ts"
import { S3Cache } from "./s3.ts"

const DENO_DEPLOY_USER_ID = 75045203
Expand All @@ -23,7 +16,7 @@ export class DenoDeployCodegenServer extends CapiCodegenServer {
}, this.abortController.signal)
local = false

constructor(readonly version: string, moduleIndex: string[]) {
constructor(readonly mainVersion: string, moduleIndex: string[]) {
super()
this.moduleIndex = async () => moduleIndex
}
Expand All @@ -32,12 +25,12 @@ export class DenoDeployCodegenServer extends CapiCodegenServer {
if (new URL(request.url).host === PRODUCTION_HOST) {
return (await this.tags())[0]!
}
return this.version
return this.mainVersion
}

deploymentUrlMemo = new PermanentMemo<string, string>()
async deploymentUrl(version: string) {
const fullSha = await this.versionSha(version)
const fullSha = await this.versionFullSha(version)
return this.deploymentUrlMemo.run(fullSha, async () => {
const deployments: GithubDeployment[] = await json(
`${GITHUB_API_REPO}/deployments?sha=${fullSha}`,
Expand All @@ -50,28 +43,6 @@ export class DenoDeployCodegenServer extends CapiCodegenServer {
return url
})
}

async versionSha(version: string) {
if (R_REF_VERSION.test(version)) {
return this.tagSha(version)
}
const shaMatch = R_SHA_VERSION.exec(version)
if (shaMatch) {
return await this.fullSha(shaMatch[1]!)
}
throw new Error("expected normalized version")
}

tagShaMemo = new PermanentMemo<string, string>()
tagSha(tag: string) {
return this.fullShaMemo.run(tag, async () => {
const refs: GithubRef[] = await json(
`${GITHUB_API_REPO}/git/matching-refs/tags/${tag}`,
)
if (!refs[0]) throw this.e404()
return refs[0].object.sha
})
}
}

interface GithubDeployment {
Expand Down
8 changes: 6 additions & 2 deletions codegen/server/git_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ export async function getModuleIndex() {
return output.split("\n").filter((x) => x.endsWith(".ts"))
}

export async function getSha() {
export async function getFullSha() {
const cmd = Deno.run({
cmd: ["git", "rev-parse", "@"],
stdout: "piped",
})
if (!(await cmd.status()).success) throw new Error("git rev-parse failed")
const output = new TextDecoder().decode(await cmd.output())
return output.slice(0, SHA_ABBREV_LENGTH)
return output
}

export async function getSha() {
return (await getFullSha()).slice(0, SHA_ABBREV_LENGTH)
}
8 changes: 4 additions & 4 deletions codegen/server/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const R_DENO_LAND_URL = /^https:\/\/deno\.land\/x\/capi@(v[^\/]+)\//
const R_GITHUB_URL = /^https:\/\/raw\.githubusercontent\.com\/paritytech\/capi\/([0-9a-f]+)\//

export class LocalCapiCodegenServer extends CapiCodegenServer {
version
mainVersion
cache: Cache = new FsCache("target/codegen", this.abortController.signal)
local = true

constructor(version?: string) {
super()
this.version = version ?? this.detectVersion()
this.mainVersion = version ?? this.detectVersion()
}

detectVersion() {
Expand All @@ -33,13 +33,13 @@ export class LocalCapiCodegenServer extends CapiCodegenServer {
}

async defaultVersion() {
return this.version
return this.mainVersion
}

deploymentUrlMemo = new PermanentMemo<string, string>()
async deploymentUrl(version: string) {
return this.deploymentUrlMemo.run(version, async () => {
const mod = await import(this.moduleFileUrl(version, "/codegen/server/local.ts"))
const mod = await import(await this.moduleFileUrl(version, "/codegen/server/local.ts"))
const Server = mod.LocalCapiCodegenServer as typeof LocalCapiCodegenServer
const server = new Server(version)
this.abortController.signal.addEventListener("abort", () => {
Expand Down
Loading

0 comments on commit 428f3f2

Please sign in to comment.