Repository navigation
Expand file tree
/
Copy pathworkflowManager.js
More file actions
241 lines (214 loc) · 7.83 KB
/
Copy pathworkflowManager.js
File metadata and controls
241 lines (214 loc) · 7.83 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
230
231
232
233
234
235
236
237
238
239
240
241
import GLib from "gi://GLib";
import { GitHubApi, isCancelled } from "./githubApi.js";
import { gettext as _ } from "resource:///org/gnome/shell/extensions/extension.js";
export class WorkflowManager {
/**
* @param {object} opts
* @param {import('gi://Soup').Session} opts.httpSession
* @param {import('gi://Gio').Settings} opts.settings
* @param {function(string, string, string, string|null): void} opts.sendNotification - Sends category, summary, body, and optional URL
* @param {function(): object[]} opts.getMonitoredRepos
* @param {function(): boolean} opts.isMenuOpen
*/
constructor({ httpSession, settings, sendNotification, getMonitoredRepos, isMenuOpen, cancellable = null }) {
this._httpSession = httpSession;
this._settings = settings;
this._sendNotification = sendNotification;
this._getMonitoredRepos = getMonitoredRepos;
this._isMenuOpen = isMenuOpen;
this._cancellable = cancellable;
// Map<repoFullName, workflowRuns[]>
this._monitoredWorkflowRuns = new Map();
this._rerunTimeout = null;
}
get monitoredWorkflowRuns() {
return this._monitoredWorkflowRuns;
}
_debugLog(...args) {
const debug = this._settings?.get_boolean("debug-mode");
if (debug) console.log(...args);
}
// Fetches workflow runs for a single repo (used by the UI on demand)
async fetchForRepo(repo, callback) {
const token = this._settings?.get_string("github-token");
if (!token) {
this._debugLog(`[GitHubTray] No token available for fetching workflow runs`);
callback([]);
return;
}
try {
const api = new GitHubApi(this._httpSession, this._settings.get_string("github-enterprise-url"));
const [owner, repoName] = repo.full_name.split("/");
const maxRuns = this._settings.get_int("workflow-runs-max-display") || 10;
this._debugLog(
`[GitHubTray] Fetching workflow runs for ${owner}/${repoName} (max: ${maxRuns})`,
);
const workflowRuns = await api.fetchRepoWorkflowRuns(
token,
owner,
repoName,
maxRuns,
this._cancellable,
);
this._debugLog(
`[GitHubTray] Fetched ${workflowRuns ? workflowRuns.length : 0} workflow runs for ${repo.full_name}`,
);
callback(workflowRuns);
} catch (error) {
if (isCancelled(error)) return;
console.error(
`[GitHubTray] Error fetching workflow runs for ${repo.full_name}:`,
error,
);
callback([]);
}
}
// Loads workflow runs for all monitored repos and triggers change detection
async loadMonitored() {
const token = this._settings?.get_string("github-token");
if (!token) return;
const monitoredRepos = this._getMonitoredRepos();
if (monitoredRepos.length === 0) return;
this._debugLog(
`[GitHubTray] Loading workflow runs for ${monitoredRepos.length} monitored repos`,
);
const api = new GitHubApi(this._httpSession, this._settings.get_string("github-enterprise-url"));
for (const repo of monitoredRepos) {
try {
const [owner, repoName] = repo.full_name.split("/");
const workflowRuns = await api.fetchRepoWorkflowRuns(
token,
owner,
repoName,
5,
this._cancellable,
);
const oldRuns = this._monitoredWorkflowRuns.get(repo.full_name) || [];
this._monitoredWorkflowRuns.set(repo.full_name, workflowRuns);
// Detect changes and notify
if (oldRuns.length > 0) {
this._detectChanges(workflowRuns, oldRuns, repo);
}
} catch (error) {
if (isCancelled(error)) return;
console.error(
`[GitHubTray] Error loading workflow runs for ${repo.full_name}:`,
error,
);
}
}
}
// Re-runs a failed workflow and refreshes after a short delay
async rerun(workflowRun, callback) {
const token = this._settings?.get_string("github-token");
if (!token) {
callback(false);
return;
}
try {
const api = new GitHubApi(this._httpSession, this._settings.get_string("github-enterprise-url"));
const [owner, repo] = workflowRun.repository_full_name.split("/");
await api.rerunWorkflow(token, owner, repo, workflowRun.id, this._cancellable);
// Refresh monitored workflow runs after a short delay
if (this._rerunTimeout) {
GLib.source_remove(this._rerunTimeout);
this._rerunTimeout = null;
}
this._rerunTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
this.loadMonitored();
this._rerunTimeout = null;
return GLib.SOURCE_REMOVE;
});
callback(true);
} catch (error) {
if (isCancelled(error)) return;
console.error(error, "GitHubTray:rerunWorkflow");
callback(false);
}
}
// Compares new and old workflow runs and sends desktop notifications for changes
_detectChanges(newRuns, oldRuns, repo) {
if (this._isMenuOpen()) return;
const oldRunsMap = new Map(oldRuns.map((run) => [run.id, run]));
for (const newRun of newRuns) {
const oldRun = oldRunsMap.get(newRun.id);
const notificationCategory = `workflow:${repo.full_name}:${newRun.id}`;
// New workflow started
if (!oldRun && newRun.status === "in_progress") {
if (this._settings.get_boolean("notify-workflow-started")) {
this._sendNotification(
notificationCategory,
_("GitHub Actions: Workflow Started"),
`${repo.name} • ${newRun.name}\n${newRun.head_branch}`,
newRun.html_url ?? null,
);
}
continue;
}
// Workflow status changed
if (oldRun && oldRun.status !== newRun.status) {
if (newRun.status === "completed" && newRun.conclusion === "success") {
if (this._settings.get_boolean("notify-workflow-success")) {
const duration = this._getDuration(newRun);
this._sendNotification(
notificationCategory,
_("GitHub Actions: Workflow Succeeded"),
`${repo.name} • ${newRun.name}\n${duration}`,
newRun.html_url ?? null,
);
}
} else if (
newRun.status === "completed" &&
newRun.conclusion === "failure"
) {
if (this._settings.get_boolean("notify-workflow-failure")) {
this._sendNotification(
notificationCategory,
_("GitHub Actions: Workflow Failed"),
`${repo.name} • ${newRun.name}\n${newRun.head_branch}`,
newRun.html_url ?? null,
);
}
} else if (
newRun.status === "completed" &&
newRun.conclusion === "cancelled"
) {
if (this._settings.get_boolean("notify-workflow-cancelled")) {
this._sendNotification(
notificationCategory,
_("GitHub Actions: Workflow Cancelled"),
`${repo.name} • ${newRun.name}`,
newRun.html_url ?? null,
);
}
}
}
}
}
// Returns a human-readable duration string for a workflow run
_getDuration(run) {
if (!run.run_started_at) return "";
const start = GLib.DateTime.new_from_iso8601(run.run_started_at, null);
const end = run.updated_at
? GLib.DateTime.new_from_iso8601(run.updated_at, null)
: GLib.DateTime.new_now_utc();
if (!start || !end) return "";
const diffSec = end.to_unix() - start.to_unix();
const minutes = Math.floor(diffSec / 60);
const seconds = diffSec % 60;
if (minutes > 0) {
return `${minutes}m ${seconds}s`;
}
return `${seconds}s`;
}
destroy() {
if (this._rerunTimeout) {
GLib.source_remove(this._rerunTimeout);
this._rerunTimeout = null;
}
this._monitoredWorkflowRuns.clear();
this._httpSession = null;
this._settings = null;
this._cancellable = null;
}
}