Skip to content

feat: fetch the tags and push each one individually #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-chefs-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changesets-gitlab": minor
---

feat: fetch the tags and push each one individually
2 changes: 1 addition & 1 deletion .github/workflows/pkg-pr-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#194](https://github.com/un-ts/changesets-gitlab/issues/194) for details. Default true.

### Outputs

Expand Down
4 changes: 4 additions & 0 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export const pushTags = async () => {
await exec('git', ['push', 'origin', '--tags'])
}

export const pushTag = async (tag: string) => {
await exec('git', ['push', 'origin', tag])
}

export const switchToMaybeExistingBranch = async (branch: string) => {
const { stderr } = await execWithOutput('git', ['checkout', branch], {
ignoreReturnCode: true,
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { runPublish, runVersion } from './run.js'
import type { MainCommandOptions } from './types.js'
import {
execSync,
FALSY_VALUES,
getOptionalInput,
getUsername,
TRUTHY_VALUES,
Expand Down Expand Up @@ -84,7 +85,10 @@ export const main = async ({
const result = await runPublish({
script: publishScript,
gitlabToken: GITLAB_TOKEN,
createGitlabReleases: getInput('create_gitlab_releases') !== 'false',
createGitlabReleases: !FALSY_VALUES.has(
getInput('create_gitlab_releases'),
),
pushAllTags: !FALSY_VALUES.has(getInput('push_all_tags')),
})

if (result.published) {
Expand Down
6 changes: 1 addition & 5 deletions src/read-changeset-state.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
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 {
preState: PreState | undefined
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> {
Expand Down
43 changes: 26 additions & 17 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
sortTheThings,
} from './utils.js'

const createRelease = async (
export const createRelease = async (
api: Gitlab,
{ pkg, tagName }: { pkg: Package; tagName: string },
) => {
Expand Down Expand Up @@ -55,32 +55,29 @@ const createRelease = async (
}
}

interface PublishOptions {
export interface PublishOptions {
script: string
gitlabToken: string
createGitlabReleases?: boolean
pushAllTags?: boolean
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({
script,
gitlabToken,
createGitlabReleases = true,
pushAllTags = true,
cwd = process.cwd(),
}: PublishOptions): Promise<PublishResult> {
const api = createApi(gitlabToken)
Expand All @@ -92,7 +89,9 @@ export async function runPublish({
{ cwd },
)

await gitUtils.pushTags()
if (pushAllTags) {
await gitUtils.pushTags()
}

const { packages, tool } = await getPackages(cwd)
const releasedPackages: Package[] = []
Expand All @@ -112,11 +111,12 @@ 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: `v${pkg.packageJson.version}`,
})
await createRelease(api, { pkg, tagName })
}
break
}
Expand All @@ -141,6 +141,15 @@ export async function runPublish({
}
releasedPackages.push(pkg)
}
if (!pushAllTags) {
await Promise.all(
releasedPackages.map(pkg =>
gitUtils.pushTag(
`${pkg.packageJson.name}@${pkg.packageJson.version}`,
),
),
)
}
if (createGitlabReleases) {
await Promise.all(
releasedPackages.map(pkg =>
Expand Down Expand Up @@ -181,7 +190,7 @@ const requireChangesetsCliPkgJson = (cwd: string) => {
}
}

interface VersionOptions {
export interface VersionOptions {
script?: string
gitlabToken: string
cwd?: string
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Loading