From 8a9c9022501c6b2a65c6009e5fdb577e03337c9b Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha Date: Tue, 3 Dec 2024 11:26:16 -0300 Subject: [PATCH 1/6] fix: fetch the tags and push each one individually --- src/gitUtils.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/gitUtils.ts b/src/gitUtils.ts index 82b7bfbf..e3f2aec7 100644 --- a/src/gitUtils.ts +++ b/src/gitUtils.ts @@ -27,8 +27,26 @@ export const push = async ( } export const pushTags = async () => { - await exec('git', ['push', 'origin', '--tags']) -} + // Get the commit hash + const { stdout: commitHash } = await execWithOutput("git", [ + "rev-parse", + "HEAD", + ]); + // Get the tags that contain the commit + const { stdout: tags } = await execWithOutput("git", [ + "--no-pager", + "tag", + "--contains", + commitHash, + ]); + // Separate the tags into a list + const tagList = tags.split("\n"); + // Push the tags individually to the remote + for (const tag of tagList) { + await exec("git", ["push", "origin", tag]); + } +}; + export const switchToMaybeExistingBranch = async (branch: string) => { const { stderr } = await execWithOutput('git', ['checkout', branch], { From 1037916a1d791f1e5c6ee61e3a1793bff54d05ad Mon Sep 17 00:00:00 2001 From: Weslley Rocha Date: Tue, 3 Dec 2024 11:30:48 -0300 Subject: [PATCH 2/6] fix: add changeset --- .changeset/mighty-chefs-serve.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mighty-chefs-serve.md diff --git a/.changeset/mighty-chefs-serve.md b/.changeset/mighty-chefs-serve.md new file mode 100644 index 00000000..9c305b0a --- /dev/null +++ b/.changeset/mighty-chefs-serve.md @@ -0,0 +1,5 @@ +--- +'changesets-gitlab': minor +--- + +fetch the tags and push each one individually From a521ca518f2ab76f7edde1177b53e45e4ff24905 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha Date: Wed, 4 Dec 2024 17:00:38 +0000 Subject: [PATCH 3/6] fix: simplify pushTags function by using HEAD directly for tag retrieval --- src/gitUtils.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/gitUtils.ts b/src/gitUtils.ts index e3f2aec7..61f13816 100644 --- a/src/gitUtils.ts +++ b/src/gitUtils.ts @@ -27,26 +27,19 @@ export const push = async ( } export const pushTags = async () => { - // Get the commit hash - const { stdout: commitHash } = await execWithOutput("git", [ - "rev-parse", - "HEAD", - ]); // Get the tags that contain the commit - const { stdout: tags } = await execWithOutput("git", [ - "--no-pager", - "tag", - "--contains", - commitHash, - ]); + const { stdout: tags } = await execWithOutput('git', [ + '--no-pager', + 'tag', + `HEAD`, + ]) // Separate the tags into a list - const tagList = tags.split("\n"); + const tagList = tags.split('\n') // Push the tags individually to the remote for (const tag of tagList) { - await exec("git", ["push", "origin", tag]); + await exec('git', ['push', 'origin', tag]) } -}; - +} export const switchToMaybeExistingBranch = async (branch: string) => { const { stderr } = await execWithOutput('git', ['checkout', branch], { From 71dd454e3468336cd56a34f4164f9887bf2e37b9 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha Date: Wed, 4 Dec 2024 17:04:44 +0000 Subject: [PATCH 4/6] fix: add check for empty tag list before pushing tags to remote --- src/gitUtils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gitUtils.ts b/src/gitUtils.ts index 61f13816..24c97518 100644 --- a/src/gitUtils.ts +++ b/src/gitUtils.ts @@ -35,9 +35,11 @@ export const pushTags = async () => { ]) // Separate the tags into a list const tagList = tags.split('\n') - // Push the tags individually to the remote - for (const tag of tagList) { - await exec('git', ['push', 'origin', tag]) + if (tagList.length > 0) { + // Push the tags individually to the remote + for (const tag of tagList) { + await exec('git', ['push', 'origin', tag]) + } } } From d5385944affeded62c4b22a2172b6abf85f6b29e Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha Date: Wed, 4 Dec 2024 19:46:28 +0000 Subject: [PATCH 5/6] fix: add early return for empty tag list in pushTags function --- src/gitUtils.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/gitUtils.ts b/src/gitUtils.ts index 24c97518..e3f2aec7 100644 --- a/src/gitUtils.ts +++ b/src/gitUtils.ts @@ -27,21 +27,26 @@ export const push = async ( } export const pushTags = async () => { + // Get the commit hash + const { stdout: commitHash } = await execWithOutput("git", [ + "rev-parse", + "HEAD", + ]); // Get the tags that contain the commit - const { stdout: tags } = await execWithOutput('git', [ - '--no-pager', - 'tag', - `HEAD`, - ]) + const { stdout: tags } = await execWithOutput("git", [ + "--no-pager", + "tag", + "--contains", + commitHash, + ]); // Separate the tags into a list - const tagList = tags.split('\n') - if (tagList.length > 0) { - // Push the tags individually to the remote - for (const tag of tagList) { - await exec('git', ['push', 'origin', tag]) - } + const tagList = tags.split("\n"); + // Push the tags individually to the remote + for (const tag of tagList) { + await exec("git", ["push", "origin", tag]); } -} +}; + export const switchToMaybeExistingBranch = async (branch: string) => { const { stderr } = await execWithOutput('git', ['checkout', branch], { From 0e86e4a3f223b2e29b29b2e2b2b486c353759ad7 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha Date: Wed, 4 Dec 2024 20:33:43 +0000 Subject: [PATCH 6/6] feat: add INPUT_PUSH_ALL_TAGS option and refactor tag pushing logic --- README.md | 1 + src/gitUtils.ts | 25 ++++++------------------- src/main.ts | 1 + src/run.ts | 18 +++++++++++++++++- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 50683cf2..a90c2ec2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ GitLab CI cli for [changesets](https://github.com/atlassian/changesets) like its - `INPUT_TARGET_BRANCH` -> The merge request target branch. Defaults to current branch - `INPUT_CREATE_GITLAB_RELEASES` - A boolean value to indicate whether to create Gitlab releases after publish or not. Default true. - `INPUT_LABELS` - A comma separated string of labels to be added to the version package Gitlab Merge request +- `INPUT_PUSH_ALL_TAGS` - A boolean value to indicate whether to push all tags at once using `git push origin --tags` [see](https://github.com/un-ts/changesets-gitlab/issues/194). Default true. ### Outputs diff --git a/src/gitUtils.ts b/src/gitUtils.ts index e3f2aec7..c62b15a7 100644 --- a/src/gitUtils.ts +++ b/src/gitUtils.ts @@ -27,26 +27,13 @@ export const push = async ( } export const pushTags = async () => { - // Get the commit hash - const { stdout: commitHash } = await execWithOutput("git", [ - "rev-parse", - "HEAD", - ]); - // Get the tags that contain the commit - const { stdout: tags } = await execWithOutput("git", [ - "--no-pager", - "tag", - "--contains", - commitHash, - ]); - // Separate the tags into a list - const tagList = tags.split("\n"); - // Push the tags individually to the remote - for (const tag of tagList) { - await exec("git", ["push", "origin", tag]); - } -}; + await exec('git', ['push', 'origin', '--tags']) +} +export const pushTag = async (tag: string) => { + console.log('Pushing tag: ' + tag) + await exec('git', ['push', 'origin', tag]) +} export const switchToMaybeExistingBranch = async (branch: string) => { const { stderr } = await execWithOutput('git', ['checkout', branch], { diff --git a/src/main.ts b/src/main.ts index 0f2625b4..957da0ed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -88,6 +88,7 @@ export const main = async ({ script: publishScript, gitlabToken: GITLAB_TOKEN, createGitlabReleases: getInput('create_gitlab_releases') !== 'false', + pushAllTags: getInput('push_all_tags') !== 'false', }) if (result.published) { diff --git a/src/run.ts b/src/run.ts index e6cc186e..af56e0c5 100644 --- a/src/run.ts +++ b/src/run.ts @@ -60,6 +60,7 @@ interface PublishOptions { script: string gitlabToken: string createGitlabReleases?: boolean + pushAllTags?: boolean cwd?: string } @@ -82,6 +83,7 @@ export async function runPublish({ script, gitlabToken, createGitlabReleases = true, + pushAllTags = true, cwd = process.cwd(), }: PublishOptions): Promise { const api = createApi(gitlabToken) @@ -93,7 +95,9 @@ export async function runPublish({ { cwd }, ) - await gitUtils.pushTags() + if (pushAllTags) { + await gitUtils.pushTags() + } const { packages, tool } = await getPackages(cwd) const releasedPackages: Package[] = [] @@ -113,6 +117,11 @@ export async function runPublish({ if (match) { releasedPackages.push(pkg) + if (!pushAllTags) { + await gitUtils.pushTag( + `${pkg.packageJson.name}@${pkg.packageJson.version}`, + ) + } if (createGitlabReleases) { await createRelease(api, { pkg, @@ -142,6 +151,13 @@ export async function runPublish({ } releasedPackages.push(pkg) } + if (!pushAllTags) { + for (const pkg of releasedPackages) { + await gitUtils.pushTag( + `${pkg.packageJson.name}@${pkg.packageJson.version}`, + ) + } + } if (createGitlabReleases) { await Promise.all( releasedPackages.map(pkg =>