/ src / commands / pr.ts
pr.ts
 1  import { Command } from 'commander';
 2  import { get } from '../http.js';
 3  
 4  export function prCommand(): Command {
 5    const pr = new Command('pr')
 6      .description('Work with AtomGit pull requests')
 7      .option('-R, --repo <repo>', 'Select repository (OWNER/REPO)')
 8      .addHelpText('after', `
 9  ARGUMENTS
10    A pull request can be supplied as argument in any of the following formats:
11    - by number, e.g. "123"
12    - by URL, e.g. "https://atomgit.com/OWNER/REPO/pulls/123"
13    - by branch name, e.g. "feature-branch"
14  
15  EXAMPLES
16    $ atomgit pr list -R owner/repo
17    $ atomgit pr view 123 -R owner/repo
18  
19  LEARN MORE
20    Use \`atomgit pr <subcommand> --help\` for more information about a command.
21  `);
22  
23    pr
24      .command('list')
25      .description('List pull requests in a repository')
26      .option('-R, --repo <repo>', 'Select repository (OWNER/REPO)')
27      .option('-s, --state <state>', 'Filter by state: open, closed, all', 'open')
28      .option('-L, --limit <number>', 'Maximum number of PRs to list', '30')
29      .action(async (options: { repo?: string; state?: string; limit?: string }) => {
30        if (!options.repo) {
31          // eslint-disable-next-line no-console
32          console.error('Repository required. Use -R owner/repo');
33          process.exitCode = 1;
34          return;
35        }
36  
37        try {
38          const params = new URLSearchParams();
39          params.set('state', options.state ?? 'open');
40          params.set('limit', options.limit ?? '30');
41  
42          const prs = await get(`/repos/${options.repo}/pulls?${params}`);
43          // eslint-disable-next-line no-console
44          console.log(JSON.stringify(prs, null, 2));
45        } catch (error) {
46          // eslint-disable-next-line no-console
47          console.error('Failed to list pull requests:', error instanceof Error ? error.message : error);
48          process.exitCode = 1;
49        }
50      });
51  
52    pr
53      .command('view')
54      .description('View pull request details')
55      .argument('<number>', 'Pull request number')
56      .option('-R, --repo <repo>', 'Select repository (OWNER/REPO)')
57      .action(async (number: string, options: { repo?: string }) => {
58        if (!options.repo) {
59          // eslint-disable-next-line no-console
60          console.error('Repository required. Use -R owner/repo');
61          process.exitCode = 1;
62          return;
63        }
64  
65        try {
66          const pr = await get(`/repos/${options.repo}/pulls/${number}`);
67          // eslint-disable-next-line no-console
68          console.log(JSON.stringify(pr, null, 2));
69        } catch (error) {
70          // eslint-disable-next-line no-console
71          console.error('Failed to view pull request:', error instanceof Error ? error.message : error);
72          process.exitCode = 1;
73        }
74      });
75  
76    return pr;
77  }