From 0b458f05aeb1794601dc2ff783db254bb8b323b5 Mon Sep 17 00:00:00 2001 From: Herman Jensen Date: Mon, 12 May 2025 12:59:22 +0200 Subject: [PATCH 1/2] fix: create tags implicitly through release api --- .changeset/nervous-eggs-bathe.md | 5 +++++ src/git.ts | 12 ++---------- src/run.ts | 1 + 3 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 .changeset/nervous-eggs-bathe.md diff --git a/.changeset/nervous-eggs-bathe.md b/.changeset/nervous-eggs-bathe.md new file mode 100644 index 00000000..128b5af4 --- /dev/null +++ b/.changeset/nervous-eggs-bathe.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": minor +--- + +Reduce the number of API calls when releasing with `commitMode: github-api` diff --git a/src/git.ts b/src/git.ts index de313afc..afbb719c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -77,16 +77,8 @@ export class Git { async pushTag(tag: string) { if (this.octokit) { - return this.octokit.rest.git - .createRef({ - ...github.context.repo, - ref: `refs/tags/${tag}`, - sha: github.context.sha, - }) - .catch((err) => { - // Assuming tag was manually pushed in custom publish script - core.warning(`Failed to create tag ${tag}: ${err.message}`); - }); + // a tag will be created automatically when creating a release + return; } await exec("git", ["push", "origin", tag], { cwd: this.cwd }); } diff --git a/src/run.ts b/src/run.ts index 60dba996..71421fb7 100644 --- a/src/run.ts +++ b/src/run.ts @@ -50,6 +50,7 @@ const createRelease = async ( await octokit.rest.repos.createRelease({ name: tagName, tag_name: tagName, + target_commitish: github.context.sha, body: changelogEntry.content, prerelease: pkg.packageJson.version.includes("-"), ...github.context.repo, From 6a328cae3985582e7a3047a30b658dbcf409a5ab Mon Sep 17 00:00:00 2001 From: Herman Jensen Date: Mon, 12 May 2025 14:59:02 +0200 Subject: [PATCH 2/2] fix: only pass target_commitish in github-api commit mode --- src/index.ts | 3 ++- src/run.ts | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index c5b0dc92..bbee81f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; } const git = new Git({ octokit: commitMode === "github-api" ? octokit : undefined, - cwd + cwd, }); let setupGitUser = core.getBooleanInput("setupGitUser"); @@ -104,6 +104,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; git, octokit, createGithubReleases: core.getBooleanInput("createGithubReleases"), + commitMode, cwd, }); diff --git a/src/run.ts b/src/run.ts index 71421fb7..652a7f03 100644 --- a/src/run.ts +++ b/src/run.ts @@ -26,7 +26,11 @@ const MAX_CHARACTERS_PER_MESSAGE = 60000; const createRelease = async ( octokit: Octokit, - { pkg, tagName }: { pkg: Package; tagName: string } + { + pkg, + tagName, + commitMode, + }: { pkg: Package; tagName: string; commitMode: "github-api" | "git-cli" } ) => { let changelog; try { @@ -47,20 +51,28 @@ const createRelease = async ( ); } - await octokit.rest.repos.createRelease({ + let createReleaseParams: Parameters< + typeof octokit.rest.repos.createRelease + >[0] = { name: tagName, tag_name: tagName, - target_commitish: github.context.sha, body: changelogEntry.content, prerelease: pkg.packageJson.version.includes("-"), ...github.context.repo, - }); + }; + + if (commitMode === "github-api") { + createReleaseParams.target_commitish = github.context.sha; + } + + await octokit.rest.repos.createRelease(createReleaseParams); }; type PublishOptions = { script: string; octokit: Octokit; createGithubReleases: boolean; + commitMode: "github-api" | "git-cli"; git: Git; cwd: string; }; @@ -81,6 +93,7 @@ export async function runPublish({ git, octokit, createGithubReleases, + commitMode, cwd, }: PublishOptions): Promise { let [publishCommand, ...publishArgs] = script.split(/\s+/); @@ -119,7 +132,7 @@ export async function runPublish({ releasedPackages.map(async (pkg) => { const tagName = `${pkg.packageJson.name}@${pkg.packageJson.version}`; await git.pushTag(tagName); - await createRelease(octokit, { pkg, tagName }); + await createRelease(octokit, { pkg, tagName, commitMode }); }) ); } @@ -141,7 +154,7 @@ export async function runPublish({ if (createGithubReleases) { const tagName = `v${pkg.packageJson.version}`; await git.pushTag(tagName); - await createRelease(octokit, { pkg, tagName }); + await createRelease(octokit, { pkg, tagName, commitMode }); } break; }