diff --git a/lib/cli-list.ts b/lib/cli-list.ts new file mode 100644 index 0000000..a153f1b --- /dev/null +++ b/lib/cli-list.ts @@ -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 ', '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)); +} diff --git a/lib/cli.ts b/lib/cli.ts index da88df1..8d0ae26 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -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');