Skip to content

Commit 2d8b1e0

Browse files
lluiscabJounQin
andauthored
feat: create MR with custom target branch (#70)
Co-authored-by: JounQin <admin@1stg.me>
1 parent cf691e2 commit 2d8b1e0

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

.changeset/seven-pugs-provide.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: merge requests target branch can now be configured through a input environment variable

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GitLab CI cli for [changesets](https://github.com/atlassian/changesets) like its
2828

2929
- published - Command executed after published
3030
- only_changesets - Command executed on only changesets detected
31+
- target_branch -> The merge request target branch. Defaults to current branch
3132

3233
### Outputs
3334

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export const main = async ({
101101
script: getOptionalInput('version'),
102102
gitlabToken: GITLAB_TOKEN!,
103103
mrTitle: getOptionalInput('title'),
104+
mrTargetBranch: getOptionalInput('target_branch'),
104105
commitMessage: getOptionalInput('commit'),
105106
hasPublishScript,
106107
})

src/run.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ interface VersionOptions {
185185
gitlabToken: string
186186
cwd?: string
187187
mrTitle?: string
188+
mrTargetBranch?: string
188189
commitMessage?: string
189190
hasPublishScript?: boolean
190191
}
@@ -194,17 +195,19 @@ export async function runVersion({
194195
gitlabToken,
195196
cwd = process.cwd(),
196197
mrTitle = 'Version Packages',
198+
mrTargetBranch = context.ref,
197199
commitMessage = 'Version Packages',
198200
hasPublishScript = false,
199201
}: VersionOptions) {
200-
const branch = context.ref
201-
const versionBranch = `changeset-release/${branch}`
202+
const currentBranch = context.ref
203+
const versionBranch = `changeset-release/${currentBranch}`
204+
202205
const api = createApi(gitlabToken)
203206
const { preState } = await readChangesetState(cwd)
204207

205208
await gitUtils.switchToMaybeExistingBranch(versionBranch)
206-
await exec('git', ['fetch', 'origin', branch])
207-
await gitUtils.reset(`origin/${branch}`)
209+
await exec('git', ['fetch', 'origin', currentBranch])
210+
await gitUtils.reset(`origin/${currentBranch}`)
208211

209212
const versionsByDirectory = await getVersionsByDirectory(cwd)
210213

@@ -228,13 +231,13 @@ export async function runVersion({
228231
hasPublishScript
229232
? 'the packages will be published to npm automatically'
230233
: 'publish to npm yourself or [setup this action to publish automatically](https://github.com/un-ts/changesets-gitlab#with-publishing)'
231-
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${branch}, this MR will be updated.
234+
}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${currentBranch}, this MR will be updated.
232235
${
233236
preState
234237
? `
235238
⚠️⚠️⚠️⚠️⚠️⚠️
236239
237-
\`${branch}\` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run \`changeset pre exit\` on \`${branch}\`.
240+
\`${currentBranch}\` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run \`changeset pre exit\` on \`${currentBranch}\`.
238241
239242
⚠️⚠️⚠️⚠️⚠️⚠️
240243
`
@@ -286,27 +289,29 @@ ${
286289
projectId: context.projectId,
287290
state: 'opened',
288291
sourceBranch: versionBranch,
289-
target_branch: branch,
292+
target_branch: mrTargetBranch,
290293
maxPages: 1,
291294
perPage: 1,
292295
})
293296
console.log(JSON.stringify(searchResult, null, 2))
294297
if (searchResult.length === 0) {
295-
console.log('creating merge request')
298+
console.log(
299+
`creating merge request from ${versionBranch} to ${mrTargetBranch}.`,
300+
)
296301
await api.MergeRequests.create(
297302
context.projectId,
298303
versionBranch,
299-
branch,
304+
mrTargetBranch,
300305
finalMrTitle,
301306
{
302307
description: await mrBodyPromise,
303308
},
304309
)
305310
} else {
311+
console.log(`updating found merge request !${searchResult[0].iid}`)
306312
await api.MergeRequests.edit(context.projectId, searchResult[0].iid, {
307313
title: finalMrTitle,
308314
description: await mrBodyPromise,
309315
})
310-
console.log('merge request found')
311316
}
312317
}

0 commit comments

Comments
 (0)