From 44849a4288dc00b9c075c8c75dde7021c628229e Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Tue, 29 Oct 2024 09:40:16 +0300 Subject: [PATCH] ci: simplify build-from-remote script --- .github/workflows/npm-publish-from-pr.yml | 9 ++- src/scripts/build-from-remote.mts | 69 +++++++++++++---------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/.github/workflows/npm-publish-from-pr.yml b/.github/workflows/npm-publish-from-pr.yml index d74cc0f..8ac52f2 100644 --- a/.github/workflows/npm-publish-from-pr.yml +++ b/.github/workflows/npm-publish-from-pr.yml @@ -4,13 +4,12 @@ on: workflow_dispatch: inputs: source: - description: 'source reference: `google/zx/main/0cba...f9`' - repoName: + description: 'source ref: `google/zx/main/0cba...f9`' + repo: description: 'source gh repo, like `google/zx`' - repoBranch: + branch: description: 'target branch' - default: 'main' - repoCommit: + commit: description: 'commit id' # buildCmd: # description: 'build cmd' diff --git a/src/scripts/build-from-remote.mts b/src/scripts/build-from-remote.mts index c8752f4..0fc18c0 100644 --- a/src/scripts/build-from-remote.mts +++ b/src/scripts/build-from-remote.mts @@ -11,12 +11,10 @@ const GH_URL = 'https://github.com' export interface TContext { cwd?: string - buildCmd?: string - repoName: string, - repoBranch: string - repoCommit: string - npmToken: string - npmRegistry: string + cmd?: string + repo: string, + branch: string + commit: string output?: string } @@ -31,41 +29,40 @@ export const createContext = (av: Record = argv, env = process.env) ...av, } - const sourceRef = input.source && parseSourceRef(input.source) + const source = input.source || `${input.repo}/${input.branch}/${input.commit}` + const sourceRef = parseSourceRef(source) const ctx: TContext = { ...input, ...sourceRef } - if (!(ctx.repoName && ctx.repoBranch && ctx.repoCommit)) throw new Error('One of `source` or `repoName, repoBranch, repoCommit` is required') - return ctx } -export const parseSourceRef = (ref: string): Pick => { - const re = /^(?:https:\/\/:)?([\w-]+\/[\w-]+)\/([\w-]+(?:\/[\w-]+)*)\/([\da-f]{40})/i - const [, repoName, repoBranch, repoCommit] = re.exec(ref) || [] +export const parseSourceRef = (ref: string): Pick => { + const re = /^(?:https:\/\/:)?([\w-]+\/[\w-]+)\/([\w-]+(?:\/[\w-]+)*)\/([\da-f]{8,40})/i + const [, repo, branch, commit] = re.exec(ref) || [] - if (!repoName) throw new Error('Invalid source ref') + if (!repo) throw new Error('Invalid source ref') return { - repoName, - repoBranch, - repoCommit + repo, + branch, + commit } } -export const fetchSource = async (ctx: Pick) => { - const repoUrl = `${GH_URL}/${ctx.repoName}` - const $$ = $({cwd: ctx.cwd, quiet: true}) - await $$`git clone -b ${ctx.repoBranch} --depth=1 ${repoUrl} .` +export const fetchSource = async ({repo, branch, commit, cwd}: Pick) => { + const repoUrl = `${GH_URL}/${repo}` + const $$ = $({cwd, quiet: true}) + await $$`git clone -b ${branch} --depth=1 ${repoUrl} .` - const commitId = (await $$`git rev-parse HEAD`).toString().trim() - if (ctx.repoCommit !== commitId) throw new Error(`Commit hash mismatch: ${ctx.repoCommit} !== ${commitId} at remote ${ctx.repoBranch} HEAD`) + const _commit = (await $$`git rev-parse HEAD`).toString().trim() + if (!_commit.startsWith(commit)) throw new Error(`Commit hash mismatch: ${commit} !== ${_commit} at remote ${branch} HEAD`) } -export const buildSource = async ({cwd, buildCmd}: Pick) => - buildCmd ? $({cwd})`${buildCmd}` : $({cwd})`exit 0` +export const buildSource = async ({cwd, cmd}: Pick) => + cmd ? $({cwd})`${cmd}` : $({cwd})`exit 0` export const buildFromRemote = async (av = argv, env = process.env)=> { protect() @@ -103,11 +100,23 @@ function test(){ const ref = parseSourceRef(source) assert.deepEqual(createContext({cwd: 'foo', source}), {cwd: 'foo', ...ref, source}) assert.deepEqual(createContext({}, {CTX: JSON.stringify({cwd: 'foo', source})}), {cwd: 'foo', ...ref, source}) - + }) + it('raises an error on empty source', () => { try { createContext({}, {}) } catch (e) { - assert.equal((e as Error).message, 'One of `source` or `repoName, repoBranch, repoCommit` is required') + assert.equal((e as Error).message, 'Invalid source ref') + } + }) + it('raises an error on invalid commit hash', () => { + try { + createContext({ + repo: 'google/zx', + branch: 'main', + commit: '0cba' + }, {}) + } catch (e) { + assert.equal((e as Error).message, 'Invalid source ref') } }) }) @@ -132,9 +141,9 @@ function test(){ const repoCtx = parseSourceRef(ref) assert.deepEqual(repoCtx, { - repoName: 'google/zx', - repoBranch: 'main', - repoCommit: '0cba54884f3084af1674118ef6299302d82daaf9' + repo: 'google/zx', + branch: 'main', + commit: '0cba54884f3084af1674118ef6299302d82daaf9' }) }) }) @@ -181,7 +190,7 @@ function test(){ const cwd = tempdir() const result = await buildSource({ cwd, - buildCmd: 'pwd' + cmd: 'pwd' }) assert.ok(result.stdout.trim().endsWith(cwd)) })