From 074c008d761cd878e5594dc39d9f098c4f45526c Mon Sep 17 00:00:00 2001 From: MinUk Song Date: Thu, 13 Jun 2024 16:34:03 -0700 Subject: [PATCH] fix: Add create merge request command --- lib/cli-create.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/cli-merge.ts | 2 +- lib/cli.ts | 6 ++++-- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 lib/cli-create.ts diff --git a/lib/cli-create.ts b/lib/cli-create.ts new file mode 100644 index 0000000..7201742 --- /dev/null +++ b/lib/cli-create.ts @@ -0,0 +1,44 @@ +import type {Command} from "commander"; +import {newAxiosInstance, resolvePluginConfig} from "./plugin.js"; +import urlJoin from "url-join"; +import {AxiosError, type AxiosResponse} from "axios"; + +export function addCreateCommand(command: Command): void { + command + .command('create') + .description('Create merge request') + .option('--project-id [projectId]', 'Project id') + .option('--gitlab-url [gitlabUrl]', 'Gitlab url') + .option('--source-branch ', 'Source branch') + .option('--target-branch ', 'Target branch') + .option('--title ', 'Title', 'chore(release): Merge published assets [skip ci]') + .action(createAction); +} + +interface CreateOptions { + projectId?: string; + gitlabUrl?: string; + sourceBranch: string; + targetBranch: string; + title: string; +} + +export async function createAction(options: CreateOptions) { + const fakeContext = {env: process.env, cwd: process.cwd()} as any; + const config = resolvePluginConfig({projectId: options.projectId, gitlabUrl: options.gitlabUrl}, fakeContext); + const instance = newAxiosInstance(config); + + const response: AxiosResponse | AxiosError = await instance.put(urlJoin(config.gitlabBaseUrl, 'merge_requests'), { + source_branch: options.sourceBranch, + target_branch: options.targetBranch, + title: options.title, + }); + if (response instanceof AxiosError) { + throw response; + } + if (response.status !== 200) { + console.error(`Failed to create merge request: ${JSON.stringify(response.data)}, Status ${response.status}`); + throw new Error(`Failed to accept merge request`); + } + console.log(JSON.stringify(response.data)); +} diff --git a/lib/cli-merge.ts b/lib/cli-merge.ts index 1c5fd4d..cba9a8c 100644 --- a/lib/cli-merge.ts +++ b/lib/cli-merge.ts @@ -6,7 +6,7 @@ import {AxiosError, type AxiosResponse} from "axios"; export function addMergeCommand(command: Command): void { command .command('merge') - .description('Merge the pull request') + .description('Merge the request') .option('--merge-request-iid <mergeRequestIid>', 'Merge request iid') .option('--project-id [projectId]', 'Project id') .option('--gitlab-url [gitlabUrl]', 'Gitlab url') diff --git a/lib/cli.ts b/lib/cli.ts index c6d04cc..da88df1 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -2,13 +2,15 @@ import {Command} from "commander"; import {addMergeCommand} from "./cli-merge.js"; import chalk from "chalk"; +import {addCreateCommand} from "./cli-create.js"; const command = new Command(); addMergeCommand(command); +addCreateCommand(command); command.hook('preAction', (thisCommand: Command, actionCommand: Command) => { performance.mark('command-preaction'); - console.log(chalk.whiteBright.bold(`semantic-release-gitlabmonorepo-helper ${actionCommand.name()} ${actionCommand.args.join(' ')}`) + ' ' + chalk.gray(thisCommand.version())); + console.error(chalk.whiteBright.bold(`semantic-release-gitlabmonorepo-helper ${actionCommand.name()} ${actionCommand.args.join(' ')}`) + ' ' + chalk.gray(thisCommand.version())); }); command.hook('postAction', (_thisCommand: Command, _actionCommand: Command) => { performance.mark('command-postaction'); @@ -21,7 +23,7 @@ command.hook('postAction', (_thisCommand: Command, _actionCommand: Command) => { roundedDuration = result.duration.toFixed(2) + 'ms'; } // 명령을 실행하는데 걸린 시간을 로그로 남긴다. - console.log(chalk.green('Execution time') + ' ' + chalk.gray(roundedDuration)); + console.error(chalk.green('Execution time') + ' ' + chalk.gray(roundedDuration)); }); command.parse();