forked from BranchMetrics/xcode-github
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathXGAStatusViewController.m
More file actions
483 lines (431 loc) · 17.9 KB
/
XGAStatusViewController.m
File metadata and controls
483 lines (431 loc) · 17.9 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
@file XGAStatusViewController.m
@package xcode-github-app
@brief The view controller for the status window.
@author Edward Smith
@date March 2018
@copyright Copyright © 2018 Branch. All rights reserved.
*/
#import "XGAStatusViewController.h"
#import <XcodeGitHub/XcodeGitHub.h>
#import "XGASettings.h"
#import "XGAStatusPopover.h"
#import "XGAStatusViewItem.h"
#import "BNCThreads.h"
#import "NSAttributedString+App.h"
#pragma mark XGAStatusViewController
@interface XGAStatusViewController () <NSTableViewDelegate, NSPopoverDelegate>
@property (strong) dispatch_queue_t asyncQueue;
@property (strong) dispatch_source_t statusTimer;
@property (assign, nonatomic) _Atomic(BOOL) statusIsInProgress;
@property (strong) XGAStatusPopover*statusPopover;
@property (weak) IBOutlet NSTableView *tableView;
@property (strong) IBOutlet NSArrayController *arrayController;
@property (assign) BOOL awake;
// Display update
@property (weak) IBOutlet NSProgressIndicator *updateProgessIndictor;
@property (strong) NSDate *lastUpdateDate;
@property (weak) IBOutlet NSTextField *statusTextField;
@end
#pragma mark - XGAStatusViewController
@implementation XGAStatusViewController
+ (instancetype) new {
XGAStatusViewController*controller = [[XGAStatusViewController alloc] init];
[[NSBundle mainBundle]
loadNibNamed:NSStringFromClass(self)
owner:controller
topLevelObjects:nil];
controller.window.excludedFromWindowsMenu = YES;
return controller;
}
- (void)awakeFromNib {
if (!self.awake) {
self.awake = YES;
[self.tableView setDoubleAction:@selector(showInfo:)];
self.window = self.view.window;
XGAStatusViewItem *status = [XGAStatusViewItem new];
status.statusImage = [NSImage imageNamed:@"RoundBlue"];
status.statusSummary = [APFormattedString boldText:@"< Refreshing >"];
self.arrayController.content = @[ status ];
[self startStatusUpdates];
self.tableView.delegate = self;
[self smartSort:self];
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
if (self.statusPopover.popover.isShown) [self showInfo:self.tableView];
}
- (XGAStatusViewItem*) selectedTableItem {
NSInteger idx = self.tableView.clickedRow;
if (idx >= 0 && idx < [self.arrayController.arrangedObjects count]) {
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:idx] byExtendingSelection:NO];
return [self.arrayController.arrangedObjects objectAtIndex:idx];
}
idx = self.tableView.selectedRow;
if (idx >= 0 && idx < [self.arrayController.arrangedObjects count])
return [self.arrayController.arrangedObjects objectAtIndex:idx];;
return nil;
}
#pragma mark - Actions
- (IBAction) showInfo:(id)sender {
NSInteger idx = self.tableView.selectedRow;
if (idx < 0 || idx >= [self.arrayController.arrangedObjects count]) {
[self.statusPopover close];
return;
}
XGAStatusViewItem*status = [self.arrayController.arrangedObjects objectAtIndex:idx];
if (![status isKindOfClass:XGAStatusViewItem.class]) return;
// Show the status panel:
if (!self.statusPopover) {
self.statusPopover = [[XGAStatusPopover alloc] init];
}
NSFont*font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
// Title
self.statusPopover.titleTextField.stringValue = @"";
// Status
self.statusPopover.statusImageView.image = status.statusImage;
self.statusPopover.statusTextField.attributedStringValue =
[status.statusSummary renderAttributedStringWithFont:font];
// Detail
APFormattedString*detail = nil;
if (status.repository.length == 0 || status.branchOrPRName.length == 0) {
detail = [APFormattedString boldText:@"%@\n", status.server];
} else {
detail = [APFormattedString boldText:@"%@\n%@\n\n", status.repository, status.branchOrPRName];
}
[detail append:status.statusDetail];
if (status.botName.length)
[detail italicText:@"\n\nBot: %@", status.botName];
if (status.branchOrPRName.length)
[detail italicText:@"\nRepo: %@/%@ %@",
status.bot.repoOwner, status.bot.repoName, status.bot.branch];
self.statusPopover.detailTextField.attributedStringValue =
[detail renderAttributedStringWithFont:font];
NSRect r = [self.tableView rectOfRow:idx];
if ([sender isKindOfClass:NSTableView.class]) {
NSPoint p = self.window.mouseLocationOutsideOfEventStream;
r.size.width = 20.0;
r.origin.x = p.x - 10.0;
}
[self.statusPopover showRelativeToRect:r ofView:self.tableView preferredEdge:NSRectEdgeMaxY];
}
- (IBAction) monitorRepo:(id)sender {
XGAStatusViewItem*item = [self selectedTableItem];
if (!item || !item.hasGitHubRepo || [item.botIsFromTemplate boolValue])
return;
if (item.isXGAMonitored) {
__auto_type tasks = [XGASettings shared].gitHubSyncTasks;
XGAGitHubSyncTask*taskToRemove = nil;
for (XGAGitHubSyncTask*task in tasks) {
if (item.server.length && item.bot.name.length &&
[task.xcodeServer isEqualToString:item.server] &&
[task.botNameForTemplate isEqualToString:item.bot.name]) {
taskToRemove = task;
break;
}
}
if (taskToRemove) [tasks removeObject:taskToRemove];
} else {
__auto_type task = [XGAGitHubSyncTask new];
task.xcodeServer = item.bot.server.server;
task.botNameForTemplate = item.botName;
[[XGASettings shared].gitHubSyncTasks addObject:task];
}
[[XGASettings shared] save];
[self reload:self];
}
- (IBAction) delete:(id)sender {
XGAStatusViewItem*status = [self selectedTableItem];
if (!status) return;
__auto_type alert = [[NSAlert alloc] init];
alert.messageText = [NSString stringWithFormat:@"Delete '%@'?", status.botName];
alert.informativeText =
[NSString stringWithFormat:@"Are you sure you want to delete the bot '%@'?", status.botName];
alert.alertStyle = NSAlertStyleInformational;
[[alert addButtonWithTitle:@"Delete"] setTag:NSModalResponseOK];
[[alert addButtonWithTitle:@"Cancel"] setTag:NSModalResponseCancel];
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
if (returnCode == NSModalResponseOK) {
NSError*error = [status.bot deleteBot];
if (error) {
__auto_type ea = [[NSAlert alloc] init];
ea.messageText = [NSString stringWithFormat:@"Error deleting '%@'.", status.botName];
ea.informativeText = error.localizedDescription;
ea.alertStyle = NSAlertStyleCritical;
[ea beginSheetModalForWindow:self.window completionHandler:nil];
} else {
[self updateStatusNow];
}
}
}];
}
- (IBAction) showInXcode:(id)sender {
XGAStatusViewItem*status = [self selectedTableItem];
if (!status) return;
NSString*string = nil;
if (status.botStatus.integrationID) {
string = [NSString stringWithFormat:@"xcbot://%@/botID/%@/integrationID/%@",
status.server, status.bot.botID, status.botStatus.integrationID];
} else {
string = [NSString stringWithFormat:@"xcbot://%@/botID/%@",
status.server, status.bot.botID];
}
NSURL*URL = [NSURL URLWithString:string];
[[NSWorkspace sharedWorkspace] openURL:URL];
}
- (IBAction) showInBrowser:(id)sender {
XGAStatusViewItem*status = [self selectedTableItem];
if (!status) return;
NSString*string =
[NSString stringWithFormat:@"https://%@/xcode/bots/%@",
status.server, status.botStatus.botTinyID];
NSURL*URL = [NSURL URLWithString:string];
[[NSWorkspace sharedWorkspace] openURL:URL];
}
- (IBAction) downloadAssets:(id)sender {
XGAStatusViewItem*status = [self selectedTableItem];
if (!status) return;
NSURL*URL = status.botStatus.integrationLogURL;
[[NSWorkspace sharedWorkspace] openURL:URL];
}
- (IBAction)startStopIntegration:(id)sender {
XGAStatusViewItem*item = [self selectedTableItem];
if (!item) return;
BNCPerformBlockAsync(^ {
if (item.botStatus.integrationID != nil &&
![item.botStatus.currentStep isEqualToString:@"completed"]) {
[item.bot cancelIntegrationID:item.botStatus.integrationID];
} else {
[item.bot startIntegration];
}
BNCSleepForTimeInterval(0.5);
BNCPerformBlockOnMainThreadSync(^{
[self reload:nil];
});
});
}
- (IBAction)showPullRequest:(id)sender {
XGAStatusViewItem*item = [self selectedTableItem];
if (item.bot.repoOwner == nil || item.bot.repoName == nil || item.bot.pullRequestNumber == nil)
return;
NSString*string = [NSString stringWithFormat:@"https://github.com/%@/%@/pull/%@",
item.bot.repoOwner, item.bot.repoName, item.bot.pullRequestNumber];
NSURL*URL = [NSURL URLWithString:string];
if (!URL) return;
[[NSWorkspace sharedWorkspace] openURL:URL];
}
- (IBAction)reload:(id)sender {
[self updateStatusNow];
}
- (IBAction)smartSort:(id)sender {
// Sort by repository, template bot name, botIsFromTemplate first, branchOrPRName
self.tableView.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"server"
ascending:YES selector:@selector(caseInsensitiveCompare:)],
[NSSortDescriptor sortDescriptorWithKey:@"repository"
ascending:YES selector:@selector(caseInsensitiveCompare:)],
[NSSortDescriptor sortDescriptorWithKey:@"templateBotName"
ascending:YES selector:@selector(caseInsensitiveCompare:)],
[NSSortDescriptor sortDescriptorWithKey:@"botIsFromTemplate"
ascending:YES selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"branchOrPRName"
ascending:YES selector:@selector(caseInsensitiveCompare:)],
];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem.action == @selector(reload:))
return YES;
if (menuItem.action == @selector(smartSort:)) {
menuItem.state = (self.tableView.sortDescriptors.count == 5);
return YES;
}
if (menuItem.action == @selector(monitorRepo:)) {
menuItem.state = 0;
XGAStatusViewItem*item = [self selectedTableItem];
if (item.hasGitHubRepo && ![item.botIsFromTemplate boolValue]) {
menuItem.state = item.isXGAMonitored;
return YES;
}
return NO;
}
XGAStatusViewItem*statusItem = [self selectedTableItem];
if (menuItem.action == @selector(startStopIntegration:)) {
if (!statusItem) return NO;
if (statusItem.botStatus.integrationID != nil &&
![statusItem.botStatus.currentStep isEqualToString:@"completed"])
menuItem.title = @"Cancel Integration";
else
menuItem.title = @"Start Integration";
return YES;
}
if (menuItem.action == @selector(showPullRequest:)) {
if (statusItem.bot.repoOwner == nil ||
statusItem.bot.repoName == nil ||
statusItem.bot.pullRequestNumber == nil)
return NO;
return YES;
}
SEL contextMenuItems[] = {
@selector(showInfo:),
@selector(monitorRepo:),
@selector(delete:),
@selector(showInXcode:),
@selector(showInBrowser:),
@selector(downloadAssets:),
NULL
};
SEL*item = contextMenuItems;
while (*item && *item != menuItem.action) ++item;
if (*item) return ([self selectedTableItem] != nil);
return NO;
}
#pragma mark - Status Updates
- (void) startStatusUpdates {
@synchronized (self) {
if (self.statusTimer) return;
if (!self.asyncQueue) {
self.asyncQueue = dispatch_queue_create("io.branch.status_queue", DISPATCH_QUEUE_SERIAL);
}
dispatch_source_t localStatusTimer =
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.asyncQueue);
self.statusTimer = localStatusTimer;
if (!self.statusTimer) return;
NSTimeInterval kUIRefreshInterval = 1.0;
dispatch_time_t startTime =
dispatch_time(DISPATCH_TIME_NOW, BNCNanoSecondsFromTimeInterval(kUIRefreshInterval));
dispatch_source_set_timer(
self.statusTimer,
startTime,
BNCNanoSecondsFromTimeInterval(kUIRefreshInterval),
BNCNanoSecondsFromTimeInterval(kUIRefreshInterval / 10.0)
);
__weak __typeof(self) weakSelf = self;
dispatch_source_set_event_handler(self.statusTimer, ^ {
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf)
[strongSelf updateStatus];
else
dispatch_source_cancel(localStatusTimer);
});
dispatch_resume(self.statusTimer);
dispatch_async(self.asyncQueue, ^{ [self updateStatus]; });
}
}
- (void) stopStatusUpdates {
@synchronized (self) {
if (self.statusTimer) {
dispatch_source_cancel(self.statusTimer);
self.statusTimer = nil;
self.lastUpdateDate = nil;
}
}
}
- (void) updateStatusNow {
@synchronized (self) {
self.lastUpdateDate = nil;
[self updateStatus];
}
}
- (void) updateStatus {
NSTimeInterval kStatusRefreshInterval = [XGASettings shared].refreshSeconds;
NSTimeInterval elapsed = - [self.lastUpdateDate timeIntervalSinceNow];
BNCPerformBlockOnMainThreadAsync(^{
self.updateProgessIndictor.doubleValue = elapsed / kStatusRefreshInterval * 100.0;
});
if (elapsed < kStatusRefreshInterval && self.lastUpdateDate != nil)
return;
// Prevent double status getting:
@synchronized(self) {
if (self.statusIsInProgress) return;
self.statusIsInProgress = YES;
}
BNCLogDebug(@"Start updateStatus.");
BNCPerformBlockOnMainThreadAsync(^{ self.statusTextField.stringValue = @""; });
// Create new bots as needed:
NSArray<XGAGitHubSyncTask*>* syncTasks = XGASettings.shared.gitHubSyncTasks;
NSDictionary<NSString*, XGServer*>*statusServers = XGASettings.shared.servers;
for (XGAGitHubSyncTask*task in syncTasks) {
if (task.xcodeServer.length != 0 && statusServers[task.xcodeServer] != nil)
[self updateSyncBots:task];
}
// Update the status:
NSMutableArray *statusArray = [NSMutableArray new];
for (XGAServer*server in statusServers.objectEnumerator) {
NSArray*a = [self updateXcodeServerStatus:server];
if (a) [statusArray addObjectsFromArray:a];
}
if (statusArray.count == 0) {
XGAStatusViewItem *status = [XGAStatusViewItem new];
status.statusImage = [NSImage imageNamed:@"RoundBlue"];
status.statusSummary = [APFormattedString boldText:@"< No Xcode servers added yet >"];
[statusArray addObject:status];
}
BNCPerformBlockOnMainThreadAsync(^{
self.arrayController.content = statusArray;
});
BNCLogDebug(@"End updateStatus.");
self.lastUpdateDate = [NSDate date];
BNCPerformBlockOnMainThreadAsync(^ { self.updateProgessIndictor.doubleValue = 0.0; });
// Release status lock:
self.statusIsInProgress = NO;
}
- (void) updateSyncBots:(XGAGitHubSyncTask*)syncTask {
NSError*error = nil;
if (syncTask.xcodeServer.length && syncTask.botNameForTemplate.length) {
XGAServer*server = [XGASettings shared].servers[syncTask.xcodeServer];
if (!server) {
server = [XGAServer new];
server.server = syncTask.xcodeServer;
}
XGCommandOptions*options = [XGCommandOptions new];
options.xcodeServerName = server.server;
options.xcodeServerUser = server.user;
options.xcodeServerPassword = server.password;
options.templateBotName = syncTask.botNameForTemplate;
options.githubAuthToken = XGASettings.shared.gitHubToken;
options.dryRun = XGASettings.shared.dryRun;
error = XGUpdateXcodeBotsWithGitHub(options);
if (error) {
NSMutableAttributedString*message =
[NSAttributedString stringWithStrings:
[NSAttributedString stringWithImage:
[NSImage imageNamed:@"RoundAlert"] rect:CGRectMake(0, -2, 12, 12)],
[NSAttributedString stringWithFormat:@" %@:%@ — %@",
options.xcodeServerName, options.templateBotName, error.localizedDescription],
nil];
BNCPerformBlockOnMainThreadAsync(^{
self.statusTextField.attributedStringValue = message;
});
}
}
}
- (NSArray*) updateXcodeServerStatus:(XGServer*)server {
NSError*error = nil;
NSMutableArray *statusArray = [NSMutableArray new];
if (server.server.length > 0) {
NSDictionary<NSString*, XGXcodeBot*>* bots = [XGXcodeBot botsForServer:server error:&error];
if (error) {
XGAStatusViewItem *status = [XGAStatusViewItem new];
status.server = server.server;
status.statusSummary = [APFormattedString boldText:@"Server Error"];
status.statusImage = [NSImage imageNamed:@"RoundAlert"];
status.statusDetail = [APFormattedString plainText:@"%@", error.localizedDescription];
[statusArray addObject:status];
} else {
for (XGXcodeBot *bot in bots.objectEnumerator) {
XGXcodeBotStatus*botStatus = [bot status];
__auto_type item = [XGAStatusViewItem newItemWithBot:bot status:botStatus];
if (item) [statusArray addObject:item];
}
}
}
if (statusArray.count == 0) {
XGAStatusViewItem *status = [XGAStatusViewItem new];
status.server = server.server;
status.statusImage = [NSImage imageNamed:@"RoundBlue"];
status.statusSummary = [APFormattedString boldText:@"< No Xcode bots found >"];
[statusArray addObject:status];
}
return statusArray;
}
@end