Skip to content

Commit e7cbdff

Browse files
WeslleyNasRochaWeslley RochaJounQin
authored
feat: fetch the tags and push each one individually (#195)
Co-authored-by: Weslley Rocha <weslley.rocha@wildlifestudios.com> Co-authored-by: JounQin <admin@1stg.me>
1 parent b681513 commit e7cbdff

File tree

8 files changed

+52
-27
lines changed

8 files changed

+52
-27
lines changed

.changeset/mighty-chefs-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"changesets-gitlab": minor
3+
---
4+
5+
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')`

.github/workflows/pkg-pr-new.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
- name: Build
2828
run: yarn build
2929

30-
- run: yarn dlx pkg-pr-new publish
30+
- run: yarn dlx pkg-pr-new publish --compact

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "changesets-gitlab",
33
"version": "0.12.2",
44
"type": "module",
5-
"repository": "https://github.com/rx-ts/changesets-gitlab.git",
5+
"repository": "https://github.com/un-ts/changesets-gitlab.git",
66
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
77
"funding": "https://opencollective.com/changesets-gitlab",
88
"license": "MIT",

src/git-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const pushTags = async () => {
3030
await exec('git', ['push', 'origin', '--tags'])
3131
}
3232

33+
export const pushTag = async (tag: string) => {
34+
await exec('git', ['push', 'origin', tag])
35+
}
36+
3337
export const switchToMaybeExistingBranch = async (branch: string) => {
3438
const { stderr } = await execWithOutput('git', ['checkout', branch], {
3539
ignoreReturnCode: true,

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { runPublish, runVersion } from './run.js'
1212
import type { MainCommandOptions } from './types.js'
1313
import {
1414
execSync,
15+
FALSY_VALUES,
1516
getOptionalInput,
1617
getUsername,
1718
TRUTHY_VALUES,
@@ -84,7 +85,9 @@ export const main = async ({
8485
const result = await runPublish({
8586
script: publishScript,
8687
gitlabToken: GITLAB_TOKEN,
87-
createGitlabReleases: getInput('create_gitlab_releases') !== 'false',
88+
createGitlabReleases: !FALSY_VALUES.has(
89+
getInput('create_gitlab_releases'),
90+
),
8891
})
8992

9093
if (result.published) {

src/read-changeset-state.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { readPreState } from '@changesets/pre'
2-
import _readChangesets from '@changesets/read'
2+
import readChangesets from '@changesets/read'
33
import type { PreState, NewChangeset } from '@changesets/types'
44

55
export interface ChangesetState {
66
preState: PreState | undefined
77
changesets: NewChangeset[]
88
}
99

10-
// @ts-expect-error - workaround for https://github.com/atlassian/changesets/issues/622
11-
const readChangesets = (_readChangesets.default ||
12-
_readChangesets) as typeof _readChangesets
13-
1410
export default async function readChangesetState(
1511
cwd: string = process.cwd(),
1612
): Promise<ChangesetState> {

src/run.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import {
1919
getChangelogEntry,
2020
getOptionalInput,
2121
getVersionsByDirectory,
22+
GITLAB_MAX_TAGS,
2223
sortTheThings,
2324
} from './utils.js'
2425

25-
const createRelease = async (
26+
export const createRelease = async (
2627
api: Gitlab,
2728
{ pkg, tagName }: { pkg: Package; tagName: string },
2829
) => {
@@ -55,26 +56,21 @@ const createRelease = async (
5556
}
5657
}
5758

58-
interface PublishOptions {
59+
export interface PublishOptions {
5960
script: string
6061
gitlabToken: string
6162
createGitlabReleases?: boolean
6263
cwd?: string
6364
}
6465

65-
interface PublishedPackage {
66+
export interface PublishedPackage {
6667
name: string
6768
version: string
6869
}
6970

70-
type PublishResult =
71-
| {
72-
published: false
73-
}
74-
| {
75-
published: true
76-
publishedPackages: PublishedPackage[]
77-
}
71+
export type PublishResult =
72+
| { published: false }
73+
| { published: true; publishedPackages: PublishedPackage[] }
7874

7975
// eslint-disable-next-line sonarjs/cognitive-complexity
8076
export async function runPublish({
@@ -92,13 +88,23 @@ export async function runPublish({
9288
{ cwd },
9389
)
9490

95-
await gitUtils.pushTags()
96-
9791
const { packages, tool } = await getPackages(cwd)
92+
93+
const pushAllTags =
94+
packages.length <= GITLAB_MAX_TAGS ||
95+
(await api.FeatureFlags.show(
96+
context.projectId,
97+
'git_push_create_all_pipelines',
98+
).catch(() => false))
99+
100+
if (pushAllTags) {
101+
await gitUtils.pushTags()
102+
}
103+
98104
const releasedPackages: Package[] = []
99105

100106
if (tool === 'root') {
101-
if (packages.length === 0) {
107+
if (packages.length !== 1) {
102108
throw new Error(
103109
`No package found.` +
104110
'This is probably a bug in the action, please open an issue',
@@ -112,11 +118,9 @@ export async function runPublish({
112118

113119
if (match) {
114120
releasedPackages.push(pkg)
121+
const tagName = `v${pkg.packageJson.version}`
115122
if (createGitlabReleases) {
116-
await createRelease(api, {
117-
pkg,
118-
tagName: `v${pkg.packageJson.version}`,
119-
})
123+
await createRelease(api, { pkg, tagName })
120124
}
121125
break
122126
}
@@ -141,6 +145,15 @@ export async function runPublish({
141145
}
142146
releasedPackages.push(pkg)
143147
}
148+
if (!pushAllTags) {
149+
await Promise.all(
150+
releasedPackages.map(pkg =>
151+
gitUtils.pushTag(
152+
`${pkg.packageJson.name}@${pkg.packageJson.version}`,
153+
),
154+
),
155+
)
156+
}
144157
if (createGitlabReleases) {
145158
await Promise.all(
146159
releasedPackages.map(pkg =>
@@ -181,7 +194,7 @@ const requireChangesetsCliPkgJson = (cwd: string) => {
181194
}
182195
}
183196

184-
interface VersionOptions {
197+
export interface VersionOptions {
185198
script?: string
186199
gitlabToken: string
187200
cwd?: string

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,8 @@ export const getUsername = (api: Gitlab) => {
172172
export const cjsRequire =
173173
typeof require === 'undefined' ? createRequire(import.meta.url) : require
174174

175+
export const FALSY_VALUES = new Set(['false', '0'])
176+
175177
export const TRUTHY_VALUES = new Set(['true', '1'])
178+
179+
export const GITLAB_MAX_TAGS = 4

0 commit comments

Comments
 (0)