From 03ec7b8082b6fb2cdf648503efefc94265a31d35 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha <weslleytato.12@gmail.com> Date: Tue, 3 Dec 2024 11:26:16 -0300 Subject: [PATCH 01/12] fix: fetch the tags and push each one individually --- src/git-utils.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/git-utils.ts b/src/git-utils.ts index 82b7bfbf..e3f2aec7 100644 --- a/src/git-utils.ts +++ b/src/git-utils.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 0b40266a1c2330a7992f228ba3d934f123cf1470 Mon Sep 17 00:00:00 2001 From: Weslley Rocha <weslley.rocha@wildlifestudios.com> Date: Tue, 3 Dec 2024 11:30:48 -0300 Subject: [PATCH 02/12] 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 e1de87ae6d6833137235664db73e663f633b7c06 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha <weslleytato.12@gmail.com> Date: Wed, 4 Dec 2024 17:00:38 +0000 Subject: [PATCH 03/12] fix: simplify pushTags function by using HEAD directly for tag retrieval --- src/git-utils.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/git-utils.ts b/src/git-utils.ts index e3f2aec7..61f13816 100644 --- a/src/git-utils.ts +++ b/src/git-utils.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 1a929f340867d52ecfa5158e402808e9191e7273 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha <weslleytato.12@gmail.com> Date: Wed, 4 Dec 2024 17:04:44 +0000 Subject: [PATCH 04/12] fix: add check for empty tag list before pushing tags to remote --- src/git-utils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/git-utils.ts b/src/git-utils.ts index 61f13816..24c97518 100644 --- a/src/git-utils.ts +++ b/src/git-utils.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 322515711fc3093d8ac97fd57de21039a67657b6 Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha <weslleytato.12@gmail.com> Date: Wed, 4 Dec 2024 19:46:28 +0000 Subject: [PATCH 05/12] fix: add early return for empty tag list in pushTags function --- src/git-utils.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/git-utils.ts b/src/git-utils.ts index 24c97518..e3f2aec7 100644 --- a/src/git-utils.ts +++ b/src/git-utils.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 38af686fead9fe1a21dc947e45c5d8be29442c8f Mon Sep 17 00:00:00 2001 From: Weslley Nascimento Rocha <weslleytato.12@gmail.com> Date: Wed, 4 Dec 2024 20:33:43 +0000 Subject: [PATCH 06/12] feat: add INPUT_PUSH_ALL_TAGS option and refactor tag pushing logic --- README.md | 1 + src/git-utils.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 46555b5c..a48e2ed9 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/git-utils.ts b/src/git-utils.ts index e3f2aec7..c62b15a7 100644 --- a/src/git-utils.ts +++ b/src/git-utils.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 bd666c25..c746531a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -85,6 +85,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 ad0a360c..3ebefe4e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -59,6 +59,7 @@ interface PublishOptions { script: string gitlabToken: string createGitlabReleases?: boolean + pushAllTags?: boolean cwd?: string } @@ -81,6 +82,7 @@ export async function runPublish({ script, gitlabToken, createGitlabReleases = true, + pushAllTags = true, cwd = process.cwd(), }: PublishOptions): Promise<PublishResult> { const api = createApi(gitlabToken) @@ -92,7 +94,9 @@ export async function runPublish({ { cwd }, ) - await gitUtils.pushTags() + if (pushAllTags) { + await gitUtils.pushTags() + } const { packages, tool } = await getPackages(cwd) const releasedPackages: Package[] = [] @@ -112,6 +116,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, @@ -141,6 +150,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 => From 26af50c7859bcf763779906d28774b037a0a0a34 Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 15:22:24 +0800 Subject: [PATCH 07/12] Update src/gitUtils.ts --- src/git-utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/git-utils.ts b/src/git-utils.ts index c62b15a7..b7f404d5 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -31,7 +31,6 @@ export const pushTags = async () => { } export const pushTag = async (tag: string) => { - console.log('Pushing tag: ' + tag) await exec('git', ['push', 'origin', tag]) } From f146e0820f18d85333963461ae3cb91b85d7f3f0 Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 16:21:43 +0800 Subject: [PATCH 08/12] chore: tiny improvements --- .changeset/mighty-chefs-serve.md | 4 ++-- README.md | 2 +- src/main.ts | 7 ++++-- src/read-changeset-state.ts | 6 +---- src/run.ts | 41 +++++++++++++------------------- src/utils.ts | 2 ++ 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/.changeset/mighty-chefs-serve.md b/.changeset/mighty-chefs-serve.md index 9c305b0a..7d61461e 100644 --- a/.changeset/mighty-chefs-serve.md +++ b/.changeset/mighty-chefs-serve.md @@ -1,5 +1,5 @@ --- -'changesets-gitlab': minor +"changesets-gitlab": minor --- -fetch the tags and push each one individually +feat: fetch the tags and push each one individually diff --git a/README.md b/README.md index a48e2ed9..528e436b 100644 --- a/README.md +++ b/README.md @@ -31,7 +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. +- `INPUT_PUSH_ALL_TAGS` - A boolean value to indicate whether to push all tags at once using `git push origin --tags`, see [#194](https://github.com/un-ts/changesets-gitlab/issues/194) for details. Default true. ### Outputs diff --git a/src/main.ts b/src/main.ts index c746531a..1e5e1155 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,6 +12,7 @@ import { runPublish, runVersion } from './run.js' import type { MainCommandOptions } from './types.js' import { execSync, + FALSY_VALUES, getOptionalInput, getUsername, TRUTHY_VALUES, @@ -84,8 +85,10 @@ export const main = async ({ const result = await runPublish({ script: publishScript, gitlabToken: GITLAB_TOKEN, - createGitlabReleases: getInput('create_gitlab_releases') !== 'false', - pushAllTags: getInput('push_all_tags') !== 'false', + createGitlabReleases: !FALSY_VALUES.has( + getInput('create_gitlab_releases'), + ), + pushAllTags: !FALSY_VALUES.has(getInput('push_all_tags')), }) if (result.published) { diff --git a/src/read-changeset-state.ts b/src/read-changeset-state.ts index c2405d91..f50f50ea 100644 --- a/src/read-changeset-state.ts +++ b/src/read-changeset-state.ts @@ -1,5 +1,5 @@ import { readPreState } from '@changesets/pre' -import _readChangesets from '@changesets/read' +import readChangesets from '@changesets/read' import type { PreState, NewChangeset } from '@changesets/types' export interface ChangesetState { @@ -7,10 +7,6 @@ export interface ChangesetState { changesets: NewChangeset[] } -// @ts-expect-error - workaround for https://github.com/atlassian/changesets/issues/622 -const readChangesets = (_readChangesets.default || - _readChangesets) as typeof _readChangesets - export default async function readChangesetState( cwd: string = process.cwd(), ): Promise<ChangesetState> { diff --git a/src/run.ts b/src/run.ts index 3ebefe4e..6d2194f9 100644 --- a/src/run.ts +++ b/src/run.ts @@ -22,7 +22,7 @@ import { sortTheThings, } from './utils.js' -const createRelease = async ( +export const createRelease = async ( api: Gitlab, { pkg, tagName }: { pkg: Package; tagName: string }, ) => { @@ -55,7 +55,7 @@ const createRelease = async ( } } -interface PublishOptions { +export interface PublishOptions { script: string gitlabToken: string createGitlabReleases?: boolean @@ -63,19 +63,14 @@ interface PublishOptions { cwd?: string } -interface PublishedPackage { +export interface PublishedPackage { name: string version: string } -type PublishResult = - | { - published: false - } - | { - published: true - publishedPackages: PublishedPackage[] - } +export type PublishResult = + | { published: false } + | { published: true; publishedPackages: PublishedPackage[] } // eslint-disable-next-line sonarjs/cognitive-complexity export async function runPublish({ @@ -116,16 +111,12 @@ export async function runPublish({ if (match) { releasedPackages.push(pkg) + const tagName = `v${pkg.packageJson.version}` if (!pushAllTags) { - await gitUtils.pushTag( - `${pkg.packageJson.name}@${pkg.packageJson.version}`, - ) + await gitUtils.pushTag(tagName) } if (createGitlabReleases) { - await createRelease(api, { - pkg, - tagName: `v${pkg.packageJson.version}`, - }) + await createRelease(api, { pkg, tagName }) } break } @@ -151,11 +142,13 @@ export async function runPublish({ releasedPackages.push(pkg) } if (!pushAllTags) { - for (const pkg of releasedPackages) { - await gitUtils.pushTag( - `${pkg.packageJson.name}@${pkg.packageJson.version}`, - ) - } + await Promise.all( + releasedPackages.map(pkg => + gitUtils.pushTag( + `${pkg.packageJson.name}@${pkg.packageJson.version}`, + ), + ), + ) } if (createGitlabReleases) { await Promise.all( @@ -197,7 +190,7 @@ const requireChangesetsCliPkgJson = (cwd: string) => { } } -interface VersionOptions { +export interface VersionOptions { script?: string gitlabToken: string cwd?: string diff --git a/src/utils.ts b/src/utils.ts index c209fcc3..075ec32d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -172,4 +172,6 @@ export const getUsername = (api: Gitlab) => { export const cjsRequire = typeof require === 'undefined' ? createRequire(import.meta.url) : require +export const FALSY_VALUES = new Set(['false', '0']) + export const TRUTHY_VALUES = new Set(['true', '1']) From 44e57a36ec14ca134c76e5e0f2ca2a691780b5b9 Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 16:28:36 +0800 Subject: [PATCH 09/12] ci: enable `--compact` flag --- .github/workflows/pkg-pr-new.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pkg-pr-new.yml b/.github/workflows/pkg-pr-new.yml index 328ee3d1..65675645 100644 --- a/.github/workflows/pkg-pr-new.yml +++ b/.github/workflows/pkg-pr-new.yml @@ -27,4 +27,4 @@ jobs: - name: Build run: yarn build - - run: yarn dlx pkg-pr-new publish + - run: yarn dlx pkg-pr-new publish --compact From 72d33b0077beb0f91d25b7456d89f67e66014b60 Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 16:52:31 +0800 Subject: [PATCH 10/12] refactor: remove unnecessary input --- README.md | 1 - src/main.ts | 1 - src/run.ts | 18 +++++++++++------- src/utils.ts | 2 ++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 528e436b..46555b5c 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ 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 [#194](https://github.com/un-ts/changesets-gitlab/issues/194) for details. Default true. ### Outputs diff --git a/src/main.ts b/src/main.ts index 1e5e1155..e830b720 100644 --- a/src/main.ts +++ b/src/main.ts @@ -88,7 +88,6 @@ export const main = async ({ createGitlabReleases: !FALSY_VALUES.has( getInput('create_gitlab_releases'), ), - pushAllTags: !FALSY_VALUES.has(getInput('push_all_tags')), }) if (result.published) { diff --git a/src/run.ts b/src/run.ts index 6d2194f9..de476120 100644 --- a/src/run.ts +++ b/src/run.ts @@ -19,6 +19,7 @@ import { getChangelogEntry, getOptionalInput, getVersionsByDirectory, + GITLAB_MAX_TAGS, sortTheThings, } from './utils.js' @@ -59,7 +60,6 @@ export interface PublishOptions { script: string gitlabToken: string createGitlabReleases?: boolean - pushAllTags?: boolean cwd?: string } @@ -77,7 +77,6 @@ export async function runPublish({ script, gitlabToken, createGitlabReleases = true, - pushAllTags = true, cwd = process.cwd(), }: PublishOptions): Promise<PublishResult> { const api = createApi(gitlabToken) @@ -89,15 +88,23 @@ export async function runPublish({ { cwd }, ) + const { packages, tool } = await getPackages(cwd) + + const pushAllTags = + packages.length <= GITLAB_MAX_TAGS || + (await api.FeatureFlags.show( + context.projectId, + 'git_push_create_all_pipelines', + ).catch(() => false)) + if (pushAllTags) { await gitUtils.pushTags() } - const { packages, tool } = await getPackages(cwd) const releasedPackages: Package[] = [] if (tool === 'root') { - if (packages.length === 0) { + if (packages.length !== 1) { throw new Error( `No package found.` + 'This is probably a bug in the action, please open an issue', @@ -112,9 +119,6 @@ export async function runPublish({ if (match) { releasedPackages.push(pkg) const tagName = `v${pkg.packageJson.version}` - if (!pushAllTags) { - await gitUtils.pushTag(tagName) - } if (createGitlabReleases) { await createRelease(api, { pkg, tagName }) } diff --git a/src/utils.ts b/src/utils.ts index 075ec32d..083930dc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -175,3 +175,5 @@ export const cjsRequire = export const FALSY_VALUES = new Set(['false', '0']) export const TRUTHY_VALUES = new Set(['true', '1']) + +export const GITLAB_MAX_TAGS = 4 From 2368909ab783688024f9baa21386c197b45ee3ae Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 16:56:23 +0800 Subject: [PATCH 11/12] Update mighty-chefs-serve.md --- .changeset/mighty-chefs-serve.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/mighty-chefs-serve.md b/.changeset/mighty-chefs-serve.md index 7d61461e..5c7d2227 100644 --- a/.changeset/mighty-chefs-serve.md +++ b/.changeset/mighty-chefs-serve.md @@ -2,4 +2,4 @@ "changesets-gitlab": minor --- -feat: fetch the tags and push each one individually +feat: fetch the tags and push each one individually based on size of `packages` to be published and `api.FeatureFlags(projectId, 'git_push_create_all_pipelines')` From e9ef014b28ce753c00eae87db5f3165802267ae8 Mon Sep 17 00:00:00 2001 From: JounQin <admin@1stg.me> Date: Tue, 1 Apr 2025 17:00:43 +0800 Subject: [PATCH 12/12] chore: change repository org in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 42f19bc0..518fbe74 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "changesets-gitlab", "version": "0.12.2", "type": "module", - "repository": "https://github.com/rx-ts/changesets-gitlab.git", + "repository": "https://github.com/un-ts/changesets-gitlab.git", "author": "JounQin (https://www.1stG.me) <admin@1stg.me>", "funding": "https://opencollective.com/changesets-gitlab", "license": "MIT",