File size: 3,474 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  CommandKind,
  type SlashCommand,
  type OpenCustomDialogActionReturn,
} from './types.js';
import { TriageDuplicates } from '../components/triage/TriageDuplicates.js';
import { TriageIssues } from '../components/triage/TriageIssues.js';

export const oncallCommand: SlashCommand = {
  name: 'oncall',
  description: 'Oncall related commands',
  kind: CommandKind.BUILT_IN,
  autoExecute: false,
  subCommands: [
    {
      name: 'dedup',
      description: 'Triage issues labeled as status/possible-duplicate',
      kind: CommandKind.BUILT_IN,
      autoExecute: true,
      action: async (context, args): Promise<OpenCustomDialogActionReturn> => {
        const agentContext = context.services.agentContext;
        const config = agentContext?.config;
        if (!config) {
          throw new Error('Config not available');
        }

        let limit = 50;
        if (args && args.trim().length > 0) {
          const argArray = args.trim().split(/\s+/);
          const parsedLimit = parseInt(argArray[0], 10);
          if (!isNaN(parsedLimit) && parsedLimit > 0) {
            limit = parsedLimit;
          }
        }

        return {
          type: 'custom_dialog',
          component: (
            <TriageDuplicates
              config={config}
              initialLimit={limit}
              onExit={() => context.ui.removeComponent()}
            />
          ),
        };
      },
    },
    {
      name: 'audit',
      description: 'Triage issues labeled as status/need-triage',
      kind: CommandKind.BUILT_IN,
      autoExecute: true,
      action: async (context, args): Promise<OpenCustomDialogActionReturn> => {
        const agentContext = context.services.agentContext;
        const config = agentContext?.config;
        if (!config) {
          throw new Error('Config not available');
        }

        let limit = 100;
        let until: string | undefined;

        if (args && args.trim().length > 0) {
          const argArray = args.trim().split(/\s+/);
          for (let i = 0; i < argArray.length; i++) {
            const arg = argArray[i];
            if (arg === '--until') {
              if (i + 1 >= argArray.length) {
                throw new Error('Flag --until requires a value (YYYY-MM-DD).');
              }
              const val = argArray[i + 1];
              if (!/^\d{4}-\d{2}-\d{2}$/.test(val)) {
                throw new Error(
                  `Invalid date format for --until: "${val}". Expected YYYY-MM-DD.`,
                );
              }
              until = val;
              i++;
            } else if (arg.startsWith('--')) {
              throw new Error(`Unknown flag: ${arg}`);
            } else {
              const parsedLimit = parseInt(arg, 10);
              if (!isNaN(parsedLimit) && parsedLimit > 0) {
                limit = parsedLimit;
              } else {
                throw new Error(
                  `Invalid argument: "${arg}". Expected a positive number or --until flag.`,
                );
              }
            }
          }
        }

        return {
          type: 'custom_dialog',
          component: (
            <TriageIssues
              config={config}
              initialLimit={limit}
              until={until}
              onExit={() => context.ui.removeComponent()}
            />
          ),
        };
      },
    },
  ],
};