Skip to content

Commit

Permalink
fix: Add create merge request command
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Jun 13, 2024
1 parent d7ecbb5 commit 074c008
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
44 changes: 44 additions & 0 deletions lib/cli-create.ts
Original file line number Diff line number Diff line change
@@ -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 <sourceBranch>', 'Source branch')
.option('--target-branch <targetBranch>', 'Target branch')
.option('--title <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));
}
2 changes: 1 addition & 1 deletion lib/cli-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 4 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();

0 comments on commit 074c008

Please sign in to comment.