Skip to content

Commit

Permalink
fix: add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Jun 14, 2024
1 parent e98da71 commit fbedb66
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/cli-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type {Command} from "commander";
import {newAxiosInstance, resolvePluginConfig} from "./plugin.js";
import urlJoin from "url-join";
import {AxiosError, type AxiosResponse} from "axios";

export function addListCommand(command: Command): void {
command
.command('list')
.description('get merge request')
.option('--project-id [projectId]', 'Project id')
.option('--gitlab-url [gitlabUrl]', 'Gitlab url')
.option('--source-branch <sourceBranch>', 'Source branch')
.action(listAction);
}

interface ListOptions {
projectId?: string;
gitlabUrl?: string;
sourceBranch: string;
}

export async function listAction(options: ListOptions) {
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.get(urlJoin(config.gitlabBaseUrl, 'merge_requests'), {
params: {
source_branch: options.sourceBranch,
}
});
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: 2 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {Command} from "commander";
import {addMergeCommand} from "./cli-merge.js";
import chalk from "chalk";
import {addCreateCommand} from "./cli-create.js";
import {addListCommand} from "./cli-list.js";

const command = new Command();
addMergeCommand(command);
addCreateCommand(command);
addListCommand(command);

command.hook('preAction', (thisCommand: Command, actionCommand: Command) => {
performance.mark('command-preaction');
Expand Down

0 comments on commit fbedb66

Please sign in to comment.