From fe2544887300d393791b34fdfcb6b97ac00792de Mon Sep 17 00:00:00 2001 From: Mahmoud Mazouz Date: Thu, 21 Mar 2024 14:44:06 +0100 Subject: [PATCH] feat: Support unpublished dependencies in `publish-crates-cargo` --- .github/workflows/release-crates-cargo.yml | 23 ++++--- dist/publish-crates-cargo-main.js | 56 +++++++++-------- publish-crates-cargo/action.yml | 10 +-- src/publish-crates-cargo.ts | 72 +++++++++++++--------- 4 files changed, 93 insertions(+), 68 deletions(-) diff --git a/.github/workflows/release-crates-cargo.yml b/.github/workflows/release-crates-cargo.yml index 3cf8efb..5dd09b0 100644 --- a/.github/workflows/release-crates-cargo.yml +++ b/.github/workflows/release-crates-cargo.yml @@ -3,7 +3,7 @@ name: Release crates (Cargo) on: workflow_call: inputs: - repos: + repo: type: string required: true live-run: @@ -12,12 +12,15 @@ on: branch: type: string required: true - inter-deps-pattern: + unpublished-deps-patterns: type: string - required: true + required: false + unpublished-deps-repos: + type: string + required: false workflow_dispatch: inputs: - repos: + repo: type: string required: true live-run: @@ -26,9 +29,12 @@ on: branch: type: string required: true - inter-deps-pattern: + unpublished-deps-patterns: type: string - required: true + required: false + unpublished-deps-repos: + type: string + required: false jobs: publish: @@ -36,9 +42,10 @@ jobs: steps: - uses: eclipse-zenoh/ci/publish-crates-cargo@main with: - repos: ${{ inputs.repos }} + repo: ${{ inputs.repo }} live-run: ${{ inputs.live-run }} branch: ${{ inputs.branch }} - inter-deps-pattern: ${{ inputs.inter-deps-pattern }} + unpublished-deps-pattern: ${{ inputs.unpublished-deps-pattern }} + unpublished-deps-repos: ${{ inputs.unpublished-deps-repos }} github-token: ${{ secrets.BOT_TOKEN_WORKFLOW }} crates-io-token: ${{ secrets.CRATES_IO_TOKEN }} diff --git a/dist/publish-crates-cargo-main.js b/dist/publish-crates-cargo-main.js index 4c69993..24fe618 100644 --- a/dist/publish-crates-cargo-main.js +++ b/dist/publish-crates-cargo-main.js @@ -82304,18 +82304,20 @@ async function spawn() { function setup() { - const liveRun = lib_core.getInput("live-run"); + const liveRun = lib_core.getBooleanInput("live-run", { required: true }); const branch = lib_core.getInput("branch", { required: true }); - const repos = lib_core.getInput("repos", { required: true }); + const repo = lib_core.getInput("repo", { required: true }); const githubToken = lib_core.getInput("github-token", { required: true }); - const interDepsPattern = lib_core.getInput("inter-deps-pattern", { required: true }); + const unpublishedDepsPatterns = lib_core.getInput("unpublished-deps-patterns", { required: true }); + const unpublishedDepsRepos = lib_core.getInput("unpublished-deps-repos", { required: true }); const cratesIoToken = lib_core.getInput("crates-io-token", { required: true }); return { - liveRun: liveRun == "" ? false : lib_core.getBooleanInput("live-run"), + liveRun, branch, - repos: repos.split("\n"), + repo, githubToken, - interDepsRegExp: interDepsPattern == "" ? undefined : new RegExp(interDepsPattern), + unpublishedDepsRegExp: new RegExp(unpublishedDepsPatterns.split("\n").join("|")), + unpublishedDepsRepos: unpublishedDepsRepos.split("\n"), cratesIoToken, }; } @@ -82323,20 +82325,16 @@ async function main(input) { let registry; try { registry = await spawn(); - for (const repo of input.repos) { - lib_core.startGroup(`Publishing ${repo} to estuary`); - clone(repo, input); - await publishToEstuary(repo, input, registry); - lib_core.endGroup(); + for (const repo of input.unpublishedDepsRepos) { + await publishToEstuary(input, repo, registry, /^$/); } + await publishToEstuary(input, input.repo, registry, input.unpublishedDepsRegExp); await deleteRepos(input); if (input.liveRun) { - for (const repo of input.repos) { - lib_core.startGroup(`Publishing ${repo} to crates.io`); - clone(repo, input); - publishToCratesIo(repo, input); - lib_core.endGroup(); + for (const repo of input.unpublishedDepsRepos) { + publishToCratesIo(input, repo); } + publishToCratesIo(input, input.repo); } await cleanup(input, registry); } @@ -82360,37 +82358,41 @@ async function cleanup(input, registry) { } await deleteRepos(input); } -function clone(repo, input) { +function clone(input, repo) { const remote = `https://${input.githubToken}@github.com/${repo}.git`; sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`); } async function deleteRepos(input) { - for (const repo of input.repos) { - lib_core.info(`Deleting repository ${repoPath(repo)}`); + lib_core.info(`Deleting repository clone ${repoPath(input.repo)}`); + await (0,promises_namespaceObject.rm)(repoPath(input.repo), { recursive: true, force: true }); + for (const repo of input.unpublishedDepsRepos) { + lib_core.info(`Deleting repository clone ${repoPath(repo)}`); await (0,promises_namespaceObject.rm)(repoPath(repo), { recursive: true, force: true }); } } function repoPath(repo) { return repo.split("/").at(1); } -async function publishToEstuary(repo, input, registry) { - const path = repoPath(repo); +async function publishToEstuary(input, repo, registry, registryDepsRegExp) { + clone(input, repo); + const path = repoPath(input.repo); await configRegistry(path, registry.name, registry.index); - await setRegistry(path, input.interDepsRegExp, registry.name); + await setRegistry(path, registryDepsRegExp, registry.name); const env = { CARGO_REGISTRY_DEFAULT: registry.name, [`CARGO_REGISTRIES_${registry.name.toUpperCase()}_TOKEN`]: registry.token, }; - publish(repo, env); + publish(path, env); } -function publishToCratesIo(repo, input) { +function publishToCratesIo(input, repo) { + clone(input, repo); + const path = repoPath(input.repo); const env = { CARGO_REGISTRY_TOKEN: input.cratesIoToken, }; - publish(repo, env); + publish(path, env); } -function publish(repo, env) { - const path = repoPath(repo); +function publish(path, env) { const options = { env, cwd: path, diff --git a/publish-crates-cargo/action.yml b/publish-crates-cargo/action.yml index c83f540..6582ad9 100644 --- a/publish-crates-cargo/action.yml +++ b/publish-crates-cargo/action.yml @@ -2,17 +2,19 @@ name: Publish crates (Cargo) inputs: live-run: - required: false + required: true branch: required: true - repos: + repo: required: true github-token: required: true crates-io-token: required: true - inter-deps-pattern: - required: true + unpublished-deps-patterns: + required: false + unpublished-deps-repos: + required: false runs: using: node20 diff --git a/src/publish-crates-cargo.ts b/src/publish-crates-cargo.ts index 1aa2a4d..bf1fa1d 100644 --- a/src/publish-crates-cargo.ts +++ b/src/publish-crates-cargo.ts @@ -9,26 +9,29 @@ import { sh } from "./command"; export type Input = { liveRun: boolean; branch: string; - repos: string[]; + repo: string; githubToken: string; - interDepsRegExp: RegExp; + unpublishedDepsRegExp: RegExp; + unpublishedDepsRepos: string[]; cratesIoToken?: string; }; export function setup(): Input { - const liveRun = core.getInput("live-run"); + const liveRun = core.getBooleanInput("live-run", { required: true }); const branch = core.getInput("branch", { required: true }); - const repos = core.getInput("repos", { required: true }); + const repo = core.getInput("repo", { required: true }); const githubToken = core.getInput("github-token", { required: true }); - const interDepsPattern = core.getInput("inter-deps-pattern", { required: true }); + const unpublishedDepsPatterns = core.getInput("unpublished-deps-patterns", { required: true }); + const unpublishedDepsRepos = core.getInput("unpublished-deps-repos", { required: true }); const cratesIoToken = core.getInput("crates-io-token", { required: true }); return { - liveRun: liveRun == "" ? false : core.getBooleanInput("live-run"), + liveRun, branch, - repos: repos.split("\n"), + repo, githubToken, - interDepsRegExp: interDepsPattern == "" ? undefined : new RegExp(interDepsPattern), + unpublishedDepsRegExp: new RegExp(unpublishedDepsPatterns.split("\n").join("|")), + unpublishedDepsRepos: unpublishedDepsRepos.split("\n"), cratesIoToken, }; } @@ -37,22 +40,21 @@ export async function main(input: Input) { let registry: estuary.Estuary; try { registry = await estuary.spawn(); - for (const repo of input.repos) { - core.startGroup(`Publishing ${repo} to estuary`); - clone(repo, input); - await publishToEstuary(repo, input, registry); - core.endGroup(); + + for (const repo of input.unpublishedDepsRepos) { + await publishToEstuary(input, repo, registry, /^$/); } + await publishToEstuary(input, input.repo, registry, input.unpublishedDepsRegExp); + await deleteRepos(input); if (input.liveRun) { - for (const repo of input.repos) { - core.startGroup(`Publishing ${repo} to crates.io`); - clone(repo, input); - publishToCratesIo(repo, input); - core.endGroup(); + for (const repo of input.unpublishedDepsRepos) { + publishToCratesIo(input, repo); } + + publishToCratesIo(input, input.repo); } await cleanup(input, registry); @@ -77,14 +79,17 @@ export async function cleanup(input: Input, registry: estuary.Estuary) { await deleteRepos(input); } -function clone(repo: string, input: Input): void { +function clone(input: Input, repo: string): void { const remote = `https://${input.githubToken}@github.com/${repo}.git`; sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`); } async function deleteRepos(input: Input) { - for (const repo of input.repos) { - core.info(`Deleting repository ${repoPath(repo)}`); + core.info(`Deleting repository clone ${repoPath(input.repo)}`); + await rm(repoPath(input.repo), { recursive: true, force: true }); + + for (const repo of input.unpublishedDepsRepos) { + core.info(`Deleting repository clone ${repoPath(repo)}`); await rm(repoPath(repo), { recursive: true, force: true }); } } @@ -93,30 +98,38 @@ function repoPath(repo: string): string { return repo.split("/").at(1); } -async function publishToEstuary(repo: string, input: Input, registry: estuary.Estuary): Promise { - const path = repoPath(repo); +async function publishToEstuary( + input: Input, + repo: string, + registry: estuary.Estuary, + registryDepsRegExp: RegExp, +): Promise { + clone(input, repo); + const path = repoPath(input.repo); await cargo.configRegistry(path, registry.name, registry.index); - await cargo.setRegistry(path, input.interDepsRegExp, registry.name); + await cargo.setRegistry(path, registryDepsRegExp, registry.name); const env = { CARGO_REGISTRY_DEFAULT: registry.name, [`CARGO_REGISTRIES_${registry.name.toUpperCase()}_TOKEN`]: registry.token, }; - publish(repo, env); + publish(path, env); } -function publishToCratesIo(repo: string, input: Input) { +function publishToCratesIo(input: Input, repo: string) { + clone(input, repo); + const path = repoPath(input.repo); + const env = { CARGO_REGISTRY_TOKEN: input.cratesIoToken, }; - publish(repo, env); + publish(path, env); } -function publish(repo: string, env: NodeJS.ProcessEnv) { - const path = repoPath(repo); +function publish(path: string, env: NodeJS.ProcessEnv) { const options = { env, cwd: path, @@ -128,5 +141,6 @@ function publish(repo: string, env: NodeJS.ProcessEnv) { sh(`cargo publish --manifest-path ${package_.manifestPath}`, options); } } + sh("cargo clean", options); }