/
BuildStatusChecker.m
174 lines (145 loc) · 5.63 KB
/
BuildStatusChecker.m
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
#import "BuildStatusChecker.h"
#import "Build.h"
#import "Notifications.h"
#import "WoodhouseKeychain.h"
#define DEFAULT_BUILD_UPDATE_DELAY_SECONDS 90.0
@interface BuildStatusChecker ()
- (Build *)buildFromPreviousRun:(Build *)currentBuild;
- (NSArray *)buildsThatJust:(SEL)statusSelector;
- (void)updateBuilds:(NSTimer*)theTimer;
- (void)scheduleNextCheck;
- (NSError *)parseResponseData;
- (void)notifyOfError:(NSError*)error;
- (void)makeRequest:(NSString*)url;
- (double)buildDelaySeconds;
- (void)watchForImmediateUpdateRequest;
@end
@implementation BuildStatusChecker
@synthesize builds;
- (id)init {
if (self = [super init]){
[self updateBuilds:nil];
[self watchForImmediateUpdateRequest];
runCount = 0;
}
return self;
}
- (BOOL)isFirstRun {
return runCount <= 1;
}
- (void)updateBuilds:(NSTimer*)theTimer {
[timer invalidate];
responseData = [NSMutableData data];
NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:@"Jenkins URL"];
[self makeRequest:url];
}
- (void) makeRequest:(NSString*)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void) scheduleNextCheck {
double seconds = [self buildDelaySeconds];
timer = [NSTimer timerWithTimeInterval:seconds target:self selector:@selector(updateBuilds:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (double) buildDelaySeconds {
NSString *delayFromPreferences = [[NSUserDefaults standardUserDefaults] objectForKey:@"Build Update Delay"];
return ([delayFromPreferences length] == 0) ? DEFAULT_BUILD_UPDATE_DELAY_SECONDS : [delayFromPreferences floatValue];
}
# pragma mark build diff methods
- (NSArray *)buildsThatJustAppeared {
return [self buildsThatJust:@selector(isPresent)];
}
- (NSArray *)buildsThatJustFailed {
return [self buildsThatJust:@selector(isFailure)];
}
- (NSArray *)buildsThatJustSucceeded {
return [self buildsThatJust:@selector(isSuccess)];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (NSArray *)buildsThatJust:(SEL)statusSelector {
return [builds objectsAtIndexes: [builds indexesOfObjectsPassingTest:
^BOOL(id obj, NSUInteger _idx, BOOL *_stop) {
Build *build = (Build *)obj;
BOOL matchesStatus = (BOOL)[build performSelector:statusSelector];
Build *previousBuild = [self buildFromPreviousRun:build];
BOOL statusChanged = !(BOOL)[previousBuild performSelector:statusSelector];
return matchesStatus && statusChanged;
}
]];
}
#pragma clang diagnostic pop
- (Build *)buildFromPreviousRun:(Build *)currentBuild {
for (Build *oldBuild in oldBuilds) {
if ([oldBuild isEqual: currentBuild]) {
return oldBuild;
}
}
return nil;
}
#pragma mark connection delegate methods
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0) {
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"Jenkins Username"];
NSString *password = [[WoodhouseKeychain sharedKeychain] getKeychainPasswordForUsername:nil];
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSDictionary *errorDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Check your username and password in the preferences.", NSLocalizedDescriptionKey, nil];
[self notifyOfError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorUserAuthenticationRequired userInfo:errorDict]];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (![error code] == NSURLErrorUserCancelledAuthentication) {
[self notifyOfError:error];
}
[self scheduleNextCheck];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *parsingError = [self parseResponseData];
if (parsingError) {
[self notifyOfError:parsingError];
} else {
runCount++;
[[NSNotificationCenter defaultCenter] postNotificationName:BUILDS_UPDATED object:self];
}
[self scheduleNextCheck];
}
- (NSError *) parseResponseData {
NSError *error;
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:responseData options:NSXMLDocumentTidyXML error:&error];
if (error) {return error;}
NSString *xpathQueryString = @"//Projects/Project";
NSArray *newItemsNodes = [[document rootElement] nodesForXPath:xpathQueryString error:&error];
if (error) {return error;}
oldBuilds = builds;
builds = [[NSMutableArray alloc] init];
for (NSXMLElement *node in newItemsNodes) {
[builds addObject:[[Build alloc] initFromNode:node]];
}
return nil;
}
- (void)notifyOfError:(NSError *)error {
[[NSNotificationCenter defaultCenter] postNotificationName:BUILDS_FAILED_TO_UPDATE object:error];
}
- (void) watchForImmediateUpdateRequest {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBuilds:) name:CHECK_BUILDS_NOW object:nil];
}
@end