-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtriage.dart
166 lines (141 loc) · 4.28 KB
/
triage.dart
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:github/github.dart';
import 'package:google_generative_ai/google_generative_ai.dart';
import 'src/common.dart';
import 'src/gemini.dart';
import 'src/github.dart';
import 'src/prompts.dart';
final sdkSlug = RepositorySlug('dart-lang', 'sdk');
Future<void> triage(
int issueNumber, {
bool dryRun = false,
bool forceTriage = false,
required GithubService githubService,
required GeminiService geminiService,
required Logger logger,
}) async {
logger.log('Triaging $sdkSlug...');
logger.log('');
// retrieve the issue
final issue = await githubService.fetchIssue(sdkSlug, issueNumber);
logger.log('## issue ${issue.htmlUrl}');
logger.log('');
final existingLabels = issue.labels.map((l) => l.name).toList();
if (existingLabels.isNotEmpty) {
logger.log('labels: ${existingLabels.join(', ')}');
logger.log('');
}
logger.log('"${issue.title}"');
logger.log('');
final bodyLines =
issue.body.split('\n').where((l) => l.trim().isNotEmpty).toList();
for (final line in bodyLines.take(4)) {
logger.log(line);
}
if (bodyLines.length > 4) {
logger.log('...');
}
logger.log('');
// If the issue has any comments, retrieve and include the last comment in the
// prompt.
String? lastComment;
if (issue.hasComments) {
final comments = await githubService.fetchIssueComments(sdkSlug, issue);
final comment = comments.last;
lastComment = '''
---
Here is the last comment on the issue (by user @${comment.user?.login}):
${trimmedBody(comment.body ?? '')}
''';
}
// decide if we should triage
if (!forceTriage) {
if (issue.alreadyTriaged) {
logger.log('Exiting (issue is already triaged).');
return;
}
}
var bodyTrimmed = trimmedBody(issue.body);
// ask for the 'area-' classification
List<String> newLabels;
try {
newLabels = await geminiService.classify(
assignAreaPrompt(
title: issue.title,
body: bodyTrimmed,
lastComment: lastComment,
),
);
} on GenerativeAIException catch (e) {
// Failures here can include things like gemini safety issues, ...
stderr.writeln('gemini: $e');
exit(1);
}
// If an issue already has a `type-` label, we don't need to apply more.
if (existingLabels.any((label) => label.startsWith('type-'))) {
newLabels.removeWhere((label) => label.startsWith('type-'));
}
// ask for the summary
String summary;
try {
summary = await geminiService.summarize(
summarizeIssuePrompt(
title: issue.title,
body: bodyTrimmed,
needsInfo: newLabels.contains('needs-info'),
),
);
} on GenerativeAIException catch (e) {
// Failures here can include things like gemini safety issues, ...
stderr.writeln('gemini: $e');
exit(1);
}
logger.log('## gemini summary');
logger.log('');
logger.log(summary);
logger.log('');
logger.log('## gemini classification');
logger.log('');
logger.log(newLabels.toString());
logger.log('');
if (dryRun) {
logger.log('Exiting (dry run mode - not applying changes).');
return;
}
// perform changes
logger.log('## github comment');
logger.log('');
logger.log('labels: $newLabels');
logger.log('');
logger.log(summary);
final comment = '**Summary:** $summary\n';
// create github comment
await githubService.createComment(sdkSlug, issueNumber, comment);
final allRepoLabels = await githubService.getAllLabels(sdkSlug);
final labelAdditions =
filterLegalLabels(newLabels, allRepoLabels: allRepoLabels);
if (labelAdditions.isNotEmpty) {
labelAdditions.add('triage-automation');
}
// apply github labels
if (newLabels.isNotEmpty) {
await githubService.addLabelsToIssue(sdkSlug, issueNumber, labelAdditions);
}
logger.log('');
logger.log('---');
logger.log('');
logger.log('Triaged ${issue.htmlUrl}');
}
List<String> filterLegalLabels(
List<String> labels, {
required List<String> allRepoLabels,
}) {
final validLabels = allRepoLabels.toSet();
return [
for (var label in labels)
if (validLabels.contains(label)) label,
]..sort();
}