Repository navigation
Expand file tree
/
Copy pathgithub-issue-creator.js
More file actions
229 lines (205 loc) · 5.69 KB
/
Copy pathgithub-issue-creator.js
File metadata and controls
229 lines (205 loc) · 5.69 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* GitHub Issue Creator
* Creates and manages GitHub issues for metrics reports
*/
class GitHubIssueCreator {
constructor(octokit) {
this.octokit = octokit;
}
/**
* Create or update metrics report issue
*/
async createMetricsIssue(
owner,
repo,
report,
period = "weekly",
options = {},
) {
const { labels = [], assignees = [] } = options;
try {
const reportDate = new Date().toISOString().split("T")[0];
const title = `[Metrics] ${period.charAt(0).toUpperCase() + period.slice(1)} Report: ${reportDate}`;
// Create issue
const issue = await this.octokit.rest.issues.create({
owner,
repo,
title,
body: report,
labels: ["type:metrics", "area:monitoring", ...labels],
assignees: assignees.length > 0 ? assignees : undefined,
});
console.log(`✅ Created metrics issue #${issue.data.number}`);
return issue.data;
} catch (error) {
console.error("Error creating metrics issue:", error.message);
throw error;
}
}
/**
* Close old metrics issues (older than specified days)
*/
async closeOldReports(owner, repo, daysOld = 90) {
try {
// Search for open metrics issues
const issues = await this.octokit.rest.issues.listForRepo({
owner,
repo,
labels: "type:metrics",
state: "open",
per_page: 100,
});
const now = Date.now();
const oldThreshold = now - daysOld * 24 * 60 * 60 * 1000;
let closedCount = 0;
for (const issue of issues.data) {
const createdTime = new Date(issue.created_at).getTime();
if (createdTime < oldThreshold) {
await this.octokit.rest.issues.update({
owner,
repo,
issue_number: issue.number,
state: "closed",
state_reason: "not_planned",
});
console.log(`✅ Closed old metrics issue #${issue.number}`);
closedCount++;
}
}
return { closedCount, totalChecked: issues.data.length };
} catch (error) {
console.error("Error closing old reports:", error.message);
throw error;
}
}
/**
* Get existing metrics issues
*/
async getMetricsIssues(owner, repo, state = "all") {
try {
const issues = await this.octokit.rest.issues.listForRepo({
owner,
repo,
labels: "type:metrics",
state,
per_page: 100,
});
return issues.data;
} catch (error) {
console.error("Error fetching metrics issues:", error.message);
throw error;
}
}
/**
* Add comment to metrics issue
*/
async addCommentToMetricsIssue(owner, repo, issueNumber, comment) {
try {
const response = await this.octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: comment,
});
console.log(`✅ Added comment to issue #${issueNumber}`);
return response.data;
} catch (error) {
console.error("Error adding comment:", error.message);
throw error;
}
}
/**
* Create weekly metrics report issue
*/
async createWeeklyMetricsIssue(owner, repo, report, options = {}) {
return this.createMetricsIssue(owner, repo, report, "weekly", {
...options,
labels: ["period:weekly", ...(options.labels || [])],
});
}
/**
* Create monthly metrics report issue
*/
async createMonthlyMetricsIssue(owner, repo, report, options = {}) {
return this.createMetricsIssue(owner, repo, report, "monthly", {
...options,
labels: ["period:monthly", ...(options.labels || [])],
});
}
/**
* Check if metrics report exists for date
*/
async reportExistsForDate(owner, repo, date) {
try {
const dateString = date.toISOString().split("T")[0];
const issues = await this.octokit.rest.issues.listForRepo({
owner,
repo,
labels: "type:metrics",
state: "all",
per_page: 100,
});
return issues.data.some((issue) => issue.title.includes(dateString));
} catch (error) {
console.error("Error checking for existing report:", error.message);
return false;
}
}
/**
* Generate metrics issue template
*/
generateIssueTemplate(reportData) {
return [
"# Metrics Report",
"",
`Generated: ${new Date().toISOString()}`,
"",
reportData,
"",
"---",
"",
"**Metadata:**",
`- Auto-generated by metrics collection system`,
`- Review the full report above for detailed analysis`,
`- Reply in this thread with questions or concerns`,
].join("\n");
}
/**
* Create issue with retry logic
*/
async createMetricsIssueWithRetry(
owner,
repo,
report,
period = "weekly",
maxRetries = 3,
) {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
console.log(
`Creating metrics issue (attempt ${attempt}/${maxRetries})...`,
);
const issue = await this.createMetricsIssue(
owner,
repo,
report,
period,
);
return issue;
} catch (error) {
lastError = error;
console.warn(`Attempt ${attempt} failed: ${error.message}`);
if (attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
console.log(`Waiting ${delay}ms before retry...`);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
throw new Error(
`Failed to create metrics issue after ${maxRetries} attempts: ${lastError.message}`,
);
}
}
module.exports = { GitHubIssueCreator };