Skip to content

Commit

Permalink
Manage Jenkins - Added restarting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Kukacka committed Mar 26, 2014
1 parent 18e0306 commit 7c31d48
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 48 deletions.
122 changes: 90 additions & 32 deletions Assets/Data/ManageJenkinsTemplate.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,98 @@
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Manage plugins</string>
<key>description</key>
<string>Plugin info</string>
<key>icon</key>
<string>icon-briefcase</string>
<key>controller</key>
<string>FTManagePluginsViewController</string>
<key>loginRequired</key>
<false/>
<key>items</key>
<array>
<dict>
<key>name</key>
<string>Manage plugins</string>
<key>description</key>
<string>Plugin info</string>
<key>icon</key>
<string>icon-briefcase</string>
<key>controller</key>
<string>FTManagePluginsViewController</string>
<key>loginRequired</key>
<false/>
</dict>
<dict>
<key>name</key>
<string>Users</string>
<key>description</key>
<string>Manage users</string>
<key>icon</key>
<string>icon-group</string>
<key>controller</key>
<string>FTManageUsersViewController</string>
<key>loginRequired</key>
<true/>
</dict>
<dict>
<key>name</key>
<string>Nodes</string>
<key>description</key>
<string>Monitor nodes that Jenkins runs jobs on.</string>
<key>icon</key>
<string>icon-desktop</string>
<key>controller</key>
<string>FTManageNodesViewController</string>
<key>loginRequired</key>
<false/>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>Users</string>
<key>description</key>
<string>Manage users</string>
<key>icon</key>
<string>icon-group</string>
<key>controller</key>
<string>FTManageUsersViewController</string>
<key>loginRequired</key>
<true/>
</dict>
<dict>
<key>name</key>
<string>Nodes</string>
<key>description</key>
<string>Monitor nodes that Jenkins runs jobs on.</string>
<key>icon</key>
<string>icon-desktop</string>
<key>controller</key>
<string>FTManageNodesViewController</string>
<key>loginRequired</key>
<false/>
<key>items</key>
<array>
<dict>
<key>name</key>
<string>Safe restart</string>
<key>description</key>
<string>Restart Jenkins once no jobs are running</string>
<key>icon</key>
<string>icon-refresh</string>
<key>action</key>
<string>safeRestart</string>
<key>loginRequired</key>
<true/>
</dict>
<dict>
<key>name</key>
<string>Hard restart</string>
<key>description</key>
<string>Restart Jenkins immediately</string>
<key>icon</key>
<string>icon-exclamation-sign</string>
<key>action</key>
<string>restart</string>
<key>loginRequired</key>
<true/>
</dict>
<dict>
<key>name</key>
<string>Quiet down</string>
<key>description</key>
<string>Jenkins will enter into the &quot;quiet down&quot; mode</string>
<key>icon</key>
<string>icon-off</string>
<key>action</key>
<string>quietDown</string>
<key>loginRequired</key>
<true/>
</dict>
<dict>
<key>name</key>
<string>Cancel quiet down</string>
<key>description</key>
<string>Cancel the &quot;quiet down&quot; mode</string>
<key>icon</key>
<string>icon-off</string>
<key>action</key>
<string>cancelQuietDown</string>
<key>loginRequired</key>
<true/>
</dict>
</array>
</dict>
</array>
</plist>
6 changes: 4 additions & 2 deletions iJenkins/FTAPIRestartDataObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
typedef NS_ENUM(NSInteger, FTAPIRestartDataObjectType) {
FTAPIRestartDataObjectTypeSafeRestart,
FTAPIRestartDataObjectTypeRestart,
FTAPIRestartDataObjectTypeQuiteDown,
FTAPIRestartDataObjectTypeCancelQuiteDown
FTAPIRestartDataObjectTypeQuietDown,
FTAPIRestartDataObjectTypeCancelQuietDown
};


Expand All @@ -24,4 +24,6 @@ typedef NS_ENUM(NSInteger, FTAPIRestartDataObjectType) {
- (id)initWithRestartType:(FTAPIRestartDataObjectType)type;


+ (FTAPIRestartDataObjectType)typeWithString:(NSString *)string;

@end
25 changes: 23 additions & 2 deletions iJenkins/FTAPIRestartDataObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ - (FTHttpMethod)httpMethod {

- (NSString *)methodName {
switch (_restartType) {
case FTAPIRestartDataObjectTypeCancelQuiteDown:
case FTAPIRestartDataObjectTypeCancelQuietDown:
return @"cancelQuietDown";
break;

case FTAPIRestartDataObjectTypeQuiteDown:
case FTAPIRestartDataObjectTypeQuietDown:
return @"quietDown";
break;

Expand Down Expand Up @@ -73,4 +73,25 @@ - (id)initWithRestartType:(FTAPIRestartDataObjectType)type {
}


#pragma mark Class methods

+ (FTAPIRestartDataObjectType)typeWithString:(NSString *)string
{
FTAPIRestartDataObjectType type;

if ([string isEqualToString:@"safeRestart"]) {
type = FTAPIRestartDataObjectTypeSafeRestart;
} else if([string isEqualToString:@"restart"]) {
type = FTAPIRestartDataObjectTypeRestart;
} else if ([string isEqualToString:@"quietDown"]) {
type = FTAPIRestartDataObjectTypeQuietDown;
} else if ([string isEqualToString:@"cancelQuietDown"]) {
type = FTAPIRestartDataObjectTypeCancelQuietDown;
} else {
type = 0; // Unknown value, first item in enum by default
}

return type;
}

@end
81 changes: 69 additions & 12 deletions iJenkins/FTManageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#import "FTManageViewController.h"
#import "FTIconCell.h"
#import "FTAPIRestartDataObject.h"
#import "FTLoginAlert.h"


@interface FTManageViewController ()
@interface FTManageViewController () <UIAlertViewDelegate>

@property (nonatomic, strong) NSArray *data;

Expand Down Expand Up @@ -41,19 +43,19 @@ - (void)createAllElements {
#pragma mark Tableview delegate & datasource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
return [_data count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
return [[self itemsAtSection:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return nil;
return [(NSDictionary *)_data[section] objectForKey:@"title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand All @@ -63,7 +65,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[FTIconCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
BOOL ok = ([FTAccountsManager sharedManager].selectedAccount.username && [FTAccountsManager sharedManager].selectedAccount.username.length > 0);
NSDictionary *d = _data[indexPath.row];
NSDictionary *d = [self itemAtIndexPath:indexPath];
if (![d[@"loginRequired"] boolValue]) {
ok = YES;
}
Expand Down Expand Up @@ -97,17 +99,72 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
return;
}

NSDictionary *d = _data[indexPath.row];
NSDictionary *d = [self itemAtIndexPath:indexPath];
NSString *controllerString = d[@"controller"];
Class class = NSClassFromString(controllerString);
if (class) {
FTViewController *c = (FTViewController *)[[class alloc] init];
[c setTitle:FTLangGet(d[@"name"])];
if (c) {
[self.navigationController pushViewController:c animated:YES];
NSString *actionString = d[@"action"];

// If controller value is present, open the controller by name
if ([controllerString length] > 0) {
Class class = NSClassFromString(controllerString);
if (class) {
FTViewController *c = (FTViewController *)[[class alloc] init];
[c setTitle:FTLangGet(d[@"name"])];
if (c) {
[self.navigationController pushViewController:c animated:YES];
}
}
}
// If action value is provided, this is an action
else if([actionString length] > 0) {
FTAPIRestartDataObjectType actionType = [FTAPIRestartDataObject typeWithString:actionString];
NSString *message = [NSString stringWithFormat:FTLangGet(@"Are you sure you want to %@ Jenkins?"), cell.textLabel.text];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:cell.textLabel.text message:message delegate:self cancelButtonTitle:FTLangGet(@"Cancel") otherButtonTitles:FTLangGet(@"Confirm"), nil];
alert.tag = actionType;
[alert show];
}

}

#pragma mark UIAlertView delegate

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
[self performManagementAction:alertView.tag];
}
}

#pragma mark Helper methods

- (NSDictionary *)itemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [self itemsAtSection:indexPath.section];

return (indexPath.row < [items count] ? items[indexPath.row] : nil);
}

- (NSArray *)itemsAtSection:(NSInteger)section
{
if (section >= [_data count]) {
return nil;
}

return [(NSDictionary *)_data[section] objectForKey:@"items"];
}

- (void)performManagementAction:(FTAPIRestartDataObjectType)actionType
{
FTAPIRestartDataObject *apiObject = [[FTAPIRestartDataObject alloc] initWithRestartType:actionType];
[FTAPIConnector connectWithObject:apiObject andOnCompleteBlock:^(id<FTAPIDataAbstractObject> dataObject, NSError *error) {
if (error) {
[dFTLoginAlert showLoginDialogWithLoginBlock:^(NSString *username, NSString *password) {
[self performManagementAction:actionType];
} andCancelBlock:^{

} accordingToResponseCode:apiObject.response.statusCode];
}
}];
}

@end

0 comments on commit 7c31d48

Please sign in to comment.