|
| 1 | +import type { Gh, Config } from './triage'; |
| 2 | +import { classify, fetchlabels, addlabels } from './triage'; |
| 3 | +import { createpr } from './learn'; |
| 4 | + |
| 5 | +const allowed = ['OWNER', 'MEMBER', 'COLLABORATOR']; |
| 6 | + |
| 7 | +export async function handlecomment(gh: Gh, config: Config, payload: any) { |
| 8 | + const comment = payload.comment; |
| 9 | + const body: string = comment.body?.trim() || ''; |
| 10 | + const association: string = comment.author_association || ''; |
| 11 | + |
| 12 | + if (!allowed.includes(association)) return; |
| 13 | + if (!body.toLowerCase().startsWith('@tigent')) return; |
| 14 | + |
| 15 | + const command = body.slice(7).trim().toLowerCase(); |
| 16 | + const issue = payload.issue; |
| 17 | + |
| 18 | + if (command === 'why') { |
| 19 | + await handlewhy(gh, config, issue, comment.id); |
| 20 | + } else if (command.startsWith('wrong')) { |
| 21 | + const rest = body.slice(7).trim().slice(5).trim(); |
| 22 | + const labels = parselabels(rest); |
| 23 | + if (labels.length > 0) { |
| 24 | + await handlewrong(gh, config, issue, comment.id, labels); |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +async function handlewhy( |
| 30 | + gh: Gh, |
| 31 | + config: Config, |
| 32 | + issue: any, |
| 33 | + commentid: number, |
| 34 | +) { |
| 35 | + await reactcomment(gh, commentid); |
| 36 | + |
| 37 | + const labels = await fetchlabels(gh); |
| 38 | + const result = await classify(config, labels, issue.title, issue.body || ''); |
| 39 | + |
| 40 | + const labelstr = result.labels.join(', '); |
| 41 | + const body = `**labels:** ${labelstr}\n**confidence:** ${result.confidence}\n\n${result.reasoning}`; |
| 42 | + |
| 43 | + await gh.octokit.rest.issues.createComment({ |
| 44 | + owner: gh.owner, |
| 45 | + repo: gh.repo, |
| 46 | + issue_number: issue.number, |
| 47 | + body, |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +async function handlewrong( |
| 52 | + gh: Gh, |
| 53 | + config: Config, |
| 54 | + issue: any, |
| 55 | + commentid: number, |
| 56 | + correctlabels: string[], |
| 57 | +) { |
| 58 | + await reactcomment(gh, commentid); |
| 59 | + |
| 60 | + const [repolabels, current] = await Promise.all([ |
| 61 | + fetchlabels(gh), |
| 62 | + gh.octokit.rest.issues.listLabelsOnIssue({ |
| 63 | + owner: gh.owner, |
| 64 | + repo: gh.repo, |
| 65 | + issue_number: issue.number, |
| 66 | + }), |
| 67 | + ]); |
| 68 | + |
| 69 | + const result = await classify( |
| 70 | + config, |
| 71 | + repolabels, |
| 72 | + issue.title, |
| 73 | + issue.body || '', |
| 74 | + ); |
| 75 | + |
| 76 | + const ailabels = result.labels; |
| 77 | + const existing = current.data.map(l => l.name); |
| 78 | + |
| 79 | + for (const label of ailabels) { |
| 80 | + if (existing.includes(label)) { |
| 81 | + await gh.octokit.rest.issues.removeLabel({ |
| 82 | + owner: gh.owner, |
| 83 | + repo: gh.repo, |
| 84 | + issue_number: issue.number, |
| 85 | + name: label, |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const validcorrect = correctlabels.filter(l => |
| 91 | + repolabels.some(x => x.name.toLowerCase() === l.toLowerCase()), |
| 92 | + ); |
| 93 | + const matchedlabels = validcorrect.map(l => { |
| 94 | + const match = repolabels.find( |
| 95 | + x => x.name.toLowerCase() === l.toLowerCase(), |
| 96 | + ); |
| 97 | + return match!.name; |
| 98 | + }); |
| 99 | + |
| 100 | + if (matchedlabels.length > 0) { |
| 101 | + await addlabels(gh, issue.number, matchedlabels); |
| 102 | + } |
| 103 | + |
| 104 | + await createpr(gh, issue.number, issue.title, matchedlabels); |
| 105 | +} |
| 106 | + |
| 107 | +function parselabels(input: string): string[] { |
| 108 | + const cleaned = input |
| 109 | + .replace(/^,/, '') |
| 110 | + .replace(/^should be/i, '') |
| 111 | + .trim(); |
| 112 | + if (!cleaned) return []; |
| 113 | + return cleaned |
| 114 | + .split(',') |
| 115 | + .map(s => s.trim()) |
| 116 | + .filter(Boolean); |
| 117 | +} |
| 118 | + |
| 119 | +async function reactcomment(gh: Gh, commentid: number) { |
| 120 | + await gh.octokit.rest.reactions.createForIssueComment({ |
| 121 | + owner: gh.owner, |
| 122 | + repo: gh.repo, |
| 123 | + comment_id: commentid, |
| 124 | + content: 'eyes', |
| 125 | + }); |
| 126 | +} |
0 commit comments