Repository navigation
Expand file tree
/
Copy pathprefs.js
More file actions
379 lines (338 loc) · 14.7 KB
/
Copy pathprefs.js
File metadata and controls
379 lines (338 loc) · 14.7 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-FileCopyrightText: 2026 Daniel Arndt <dan@arndt.ca>
// SPDX-License-Identifier: GPL-2.0-or-later
import Adw from 'gi://Adw';
import Gtk from 'gi://Gtk?version=4.0';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import { ExtensionPreferences } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
import * as keyring from './src/api/keyring.js';
import { notifyTokenChanged } from './src/api/tokenSync.js';
import { GithubClient, RequestCancelledError } from './src/api/githubClient.js';
import { getDebugLogs } from './src/utils/debugLogs.js';
export const GITHUB_NEW_TOKEN_URL =
'https://github.com/settings/tokens/new?description=GitHub%20PR%20Tracker&scopes=repo';
export default class GitHubPRExtensionPreferences extends ExtensionPreferences {
/**
* Token storage used by the window; tests replace this to avoid touching
* the real keyring.
* @returns {{storeToken: Function, loadToken: Function, deleteToken: Function}}
*/
getKeyring() {
return keyring;
}
/**
* Diagnostic logs retriever used by the window; tests replace this to avoid
* querying the real system journal.
* @param {Object} metadata
* @returns {Promise<string>}
*/
getDebugLogs(metadata) {
return getDebugLogs(metadata);
}
fillPreferencesWindow(window) {
const settings = this.getSettings();
const { storeToken, loadToken, deleteToken } = this.getKeyring();
const client = new GithubClient();
const page = new Adw.PreferencesPage({
title: 'General',
icon_name: 'preferences-other-symbolic',
});
window.add(page);
// --- Group 1: Authentication ---
const authGroup = new Adw.PreferencesGroup({
title: 'Authentication',
description: 'Access token is securely stored in your GNOME Keyring.',
});
page.add(authGroup);
const tokenRow = new Adw.PasswordEntryRow({
title: 'Personal Access Token',
});
const saveBtn = new Gtk.Button({
label: 'Save',
valign: Gtk.Align.CENTER,
});
saveBtn.add_css_class('suggested-action');
const clearBtn = new Gtk.Button({
label: 'Clear',
valign: Gtk.Align.CENTER,
});
clearBtn.add_css_class('destructive-action');
tokenRow.add_suffix(saveBtn);
tokenRow.add_suffix(clearBtn);
authGroup.add(tokenRow);
const statusRow = new Adw.ActionRow({
title: 'Connection Status',
subtitle: 'Checking keyring...',
icon_name: 'dialog-information-symbolic',
// Subtitle shows GitHub display names and error messages verbatim
use_markup: false,
});
window.connect('close-request', () => {
client.destroy();
});
const testBtn = new Gtk.Button({
label: 'Test Connection',
valign: Gtk.Align.CENTER,
});
statusRow.add_suffix(testBtn);
authGroup.add(statusRow);
const generateTokenRow = new Adw.ActionRow({
title: 'Generate Token',
subtitle: 'Open GitHub to create a classic token with required scopes pre-populated',
activatable: true,
});
const linkBtn = new Gtk.LinkButton({
uri: GITHUB_NEW_TOKEN_URL,
icon_name: 'adw-external-link-symbolic',
valign: Gtk.Align.CENTER,
focusable: false,
});
generateTokenRow.add_suffix(linkBtn);
generateTokenRow.connect('activated', () => {
try {
Gio.AppInfo.launch_default_for_uri(GITHUB_NEW_TOKEN_URL, null);
} catch (err) {
console.error(`Failed to open token URL: ${err.message}`);
}
});
authGroup.add(generateTokenRow);
// Load token from keyring on open
loadToken().then(token => {
if (token) {
tokenRow.set_text(token);
statusRow.set_icon_name('dialog-information-symbolic');
statusRow.set_subtitle('Token loaded from keyring. Click "Test Connection" to verify.');
} else {
statusRow.set_icon_name('dialog-warning-symbolic');
statusRow.set_subtitle('No token saved. Please enter a Personal Access Token.');
}
}).catch(err => {
statusRow.set_icon_name('dialog-error-symbolic');
statusRow.set_subtitle(`Keyring error: ${err.message}`);
});
// Test connection helper
const verifyCurrentToken = async (token) => {
if (!token || token.trim().length === 0) {
statusRow.set_icon_name('dialog-warning-symbolic');
statusRow.set_subtitle('Please enter a valid token first.');
return;
}
statusRow.set_icon_name('dialog-information-symbolic');
statusRow.set_subtitle('Verifying token with GitHub API...');
testBtn.set_sensitive(false);
try {
const user = await client.verifyToken(token);
if (user && user.login) {
const nameStr = user.name ? ` (${user.name})` : '';
statusRow.set_icon_name('emblem-ok-symbolic');
statusRow.set_subtitle(`Connected as @${user.login}${nameStr}`);
settings.set_string('last-username', user.login);
} else {
statusRow.set_icon_name('dialog-warning-symbolic');
statusRow.set_subtitle('Connected, but could not determine user login.');
}
} catch (err) {
// Superseded by a newer verification, which will set the status.
if (err instanceof RequestCancelledError) return;
statusRow.set_icon_name('dialog-error-symbolic');
statusRow.set_subtitle(`Error: ${err.message}`);
} finally {
testBtn.set_sensitive(true);
}
};
saveBtn.connect('clicked', async () => {
const token = tokenRow.get_text().trim();
if (!token) {
statusRow.set_icon_name('dialog-warning-symbolic');
statusRow.set_subtitle('Token cannot be empty.');
return;
}
saveBtn.set_sensitive(false);
try {
await storeToken(token);
notifyTokenChanged(settings);
await verifyCurrentToken(token);
} catch (err) {
statusRow.set_icon_name('dialog-error-symbolic');
statusRow.set_subtitle(`Failed to store token: ${err.message}`);
} finally {
saveBtn.set_sensitive(true);
}
});
clearBtn.connect('clicked', async () => {
clearBtn.set_sensitive(false);
try {
await deleteToken();
notifyTokenChanged(settings);
tokenRow.set_text('');
statusRow.set_icon_name('dialog-information-symbolic');
statusRow.set_subtitle('Token removed from keyring.');
} catch (err) {
statusRow.set_icon_name('dialog-error-symbolic');
statusRow.set_subtitle(`Failed to clear token: ${err.message}`);
} finally {
clearBtn.set_sensitive(true);
}
});
testBtn.connect('clicked', () => {
const token = tokenRow.get_text().trim();
verifyCurrentToken(token);
});
// --- Group 2: Repository Filtering ---
const filterGroup = new Adw.PreferencesGroup({
title: 'Repository Filters',
description: 'Optionally restrict which repositories or organizations are tracked.',
});
page.add(filterGroup);
const includeRow = new Adw.EntryRow({
title: 'Include Repositories / Orgs',
});
settings.bind('include-repos', includeRow, 'text', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(includeRow);
const excludeRow = new Adw.EntryRow({
title: 'Exclude Repositories',
});
settings.bind('exclude-repos', excludeRow, 'text', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(excludeRow);
const archivedRow = new Adw.SwitchRow({
title: 'Ignore Archived Repositories',
subtitle: 'Exclude pull requests in read-only/archived repos',
});
settings.bind('ignore-archived', archivedRow, 'active', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(archivedRow);
const forksRow = new Adw.SwitchRow({
title: 'Ignore Forked Repositories',
subtitle: 'Exclude pull requests in repository forks',
});
settings.bind('ignore-forks', forksRow, 'active', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(forksRow);
const teamReviewsRow = new Adw.SwitchRow({
title: 'Include Team Review Requests',
subtitle: 'Include pull requests requested from teams you belong to, in addition to direct requests',
});
settings.bind('include-team-reviews', teamReviewsRow, 'active', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(teamReviewsRow);
const assignedPrsRow = new Adw.SwitchRow({
title: 'Include Assigned Pull Requests',
subtitle: 'Include pull requests assigned to you',
});
settings.bind('include-assigned-prs', assignedPrsRow, 'active', Gio.SettingsBindFlags.DEFAULT);
filterGroup.add(assignedPrsRow);
// --- Group 3: Polling & Behavior ---
const behaviorGroup = new Adw.PreferencesGroup({
title: 'Behavior and Polling',
});
page.add(behaviorGroup);
const spinAdjustment = new Gtk.Adjustment({
lower: 1,
upper: 60,
step_increment: 1,
page_increment: 5,
value: settings.get_int('refresh-interval') || 5,
});
const refreshRow = new Adw.SpinRow({
title: 'Refresh Interval (minutes)',
subtitle: 'Time between automatic background updates',
adjustment: spinAdjustment,
});
settings.bind('refresh-interval', refreshRow, 'value', Gio.SettingsBindFlags.DEFAULT);
behaviorGroup.add(refreshRow);
const notifyRow = new Adw.SwitchRow({
title: 'Notify on Category Changes',
subtitle: 'Show a desktop notification when a pull request moves between categories',
});
settings.bind('notify-on-transition', notifyRow, 'active', Gio.SettingsBindFlags.DEFAULT);
behaviorGroup.add(notifyRow);
// --- Group 4: Troubleshooting ---
const debugGroup = new Adw.PreferencesGroup({
title: 'Troubleshooting',
description: 'Export diagnostic logs to share in bug reports. Sensitive tokens and credentials are automatically redacted.',
});
page.add(debugGroup);
const exportRow = new Adw.ActionRow({
title: 'Export Logs',
subtitle: 'Save extension log messages from the system journal to a file or clipboard',
});
const copyLogsBtn = new Gtk.Button({
label: 'Copy',
valign: Gtk.Align.CENTER,
tooltip_text: 'Copy logs to clipboard',
});
const exportLogsBtn = new Gtk.Button({
label: 'Export…',
valign: Gtk.Align.CENTER,
tooltip_text: 'Save logs to a file',
});
exportRow.add_suffix(copyLogsBtn);
exportRow.add_suffix(exportLogsBtn);
debugGroup.add(exportRow);
copyLogsBtn.connect('clicked', async () => {
copyLogsBtn.set_sensitive(false);
exportLogsBtn.set_sensitive(false);
try {
const logs = await this.getDebugLogs(this.metadata);
window.get_clipboard().set(logs);
window.add_toast(new Adw.Toast({
title: 'Logs copied to clipboard',
}));
} catch (err) {
window.add_toast(new Adw.Toast({
title: `Failed to copy logs: ${err.message}`,
}));
} finally {
copyLogsBtn.set_sensitive(true);
exportLogsBtn.set_sensitive(true);
}
});
exportLogsBtn.connect('clicked', async () => {
copyLogsBtn.set_sensitive(false);
exportLogsBtn.set_sensitive(false);
try {
const logs = await this.getDebugLogs(this.metadata);
const fileDialog = new Gtk.FileDialog({
title: 'Export Extension Logs',
initial_name: 'github-pr-tracker.log',
});
fileDialog.save(window, null, (dlg, res) => {
try {
const file = dlg.save_finish(res);
const encoder = new TextEncoder();
const bytes = new GLib.Bytes(encoder.encode(logs));
file.replace_contents_bytes_async(
bytes,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null,
(f, writeRes) => {
try {
f.replace_contents_finish(writeRes);
window.add_toast(new Adw.Toast({
title: 'Logs exported successfully',
}));
} catch (writeErr) {
window.add_toast(new Adw.Toast({
title: `Failed to save file: ${writeErr.message}`,
}));
}
}
);
} catch (dialogErr) {
if (!dialogErr.matches(Gtk.DialogError, Gtk.DialogError.DISMISSED)) {
window.add_toast(new Adw.Toast({
title: `Failed to export logs: ${dialogErr.message}`,
}));
}
}
});
} catch (err) {
window.add_toast(new Adw.Toast({
title: `Failed to export logs: ${err.message}`,
}));
} finally {
copyLogsBtn.set_sensitive(true);
exportLogsBtn.set_sensitive(true);
}
});
}
}