-
Notifications
You must be signed in to change notification settings - Fork 30
/
XcodeServer+Integration.swift
244 lines (189 loc) · 9.11 KB
/
XcodeServer+Integration.swift
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
//
// XcodeServer+Integration.swift
// XcodeServerSDK
//
// Created by Mateusz Zając on 01.07.2015.
// Copyright © 2015 Honza Dvorsky. All rights reserved.
//
import Foundation
import BuildaUtils
// MARK: - XcodeSever API Routes for Integrations management
extension XcodeServer {
// MARK: Bot releated integrations
/**
XCS API call for getting a list of filtered integrations for bot.
Available queries:
- **last** - find last integration for bot
- **from** - find integration based on date range
- **number** - find integration for bot by its number
- parameter botId: ID of bot.
- parameter query: Query which should be used to filter integrations.
- parameter integrations: Optional array of integrations returned from XCS.
- parameter error: Optional error.
*/
public final func getBotIntegrations(botId: String, query: [String: String], completion: (integrations: [Integration]?, error: NSError?) -> ()) {
let params = [
"bot": botId
]
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: params, query: query, body: nil) { (response, body, error) -> () in
if error != nil {
completion(integrations: nil, error: error)
return
}
if let body = (body as? NSDictionary)?["results"] as? NSArray {
let (result, error): ([Integration]?, NSError?) = unthrow {
return try XcodeServerArray(body)
}
completion(integrations: result, error: error)
} else {
completion(integrations: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}
/**
XCS API call for firing integration for specified bot.
- parameter botId: ID of the bot.
- parameter integration: Optional object of integration returned if run was successful.
- parameter error: Optional error.
*/
public final func postIntegration(botId: String, completion: (integration: Integration?, error: NSError?) -> ()) {
let params = [
"bot": botId
]
self.sendRequestWithMethod(.POST, endpoint: .Integrations, params: params, query: nil, body: nil) { (response, body, error) -> () in
if error != nil {
completion(integration: nil, error: error)
return
}
if let body = body as? NSDictionary {
let (result, error): (Integration?, NSError?) = unthrow {
return try Integration(json: body)
}
completion(integration: result, error: error)
} else {
completion(integration: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}
// MARK: General integrations methods
/**
XCS API call for retrievieng all available integrations on server.
- parameter integrations: Optional array of integrations.
- parameter error: Optional error.
*/
public final func getIntegrations(completion: (integrations: [Integration]?, error: NSError?) -> ()) {
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: nil, query: nil, body: nil) {
(response, body, error) -> () in
guard error == nil else {
completion(integrations: nil, error: error)
return
}
guard let integrationsBody = (body as? NSDictionary)?["results"] as? NSArray else {
completion(integrations: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let (result, error): ([Integration]?, NSError?) = unthrow {
return try XcodeServerArray(integrationsBody)
}
completion(integrations: result, error: error)
}
}
/**
XCS API call for retrievieng specified Integration.
- parameter integrationId: ID of integration which is about to be retrieved.
- parameter completion:
- Optional retrieved integration.
- Optional operation error.
*/
public final func getIntegration(integrationId: String, completion: (integration: Integration?, error: NSError?) -> ()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.GET, endpoint: .Integrations, params: params, query: nil, body: nil) {
(response, body, error) -> () in
guard error == nil else {
completion(integration: nil, error: error)
return
}
guard let integrationBody = body as? NSDictionary else {
completion(integration: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let (result, error): (Integration?, NSError?) = unthrow {
return try Integration(json: integrationBody)
}
completion(integration: result, error: error)
}
}
/**
XCS API call for canceling specified integration.
- parameter integrationId: ID of integration.
- parameter success: Integration cancelling success indicator.
- parameter error: Optional operation error.
*/
public final func cancelIntegration(integrationId: String, completion: (success: Bool, error: NSError?) -> ()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.POST, endpoint: .CancelIntegration, params: params, query: nil, body: nil) { (response, body, error) -> () in
if error != nil {
completion(success: false, error: error)
return
}
completion(success: true, error: nil)
}
}
/**
XCS API call for fetching all commits for specific integration.
- parameter integrationId: ID of integration.
- parameter success: Optional Integration Commits object with result.
- parameter error: Optional operation error.
*/
public final func getIntegrationCommits(integrationId: String, completion: (integrationCommits: IntegrationCommits?, error: NSError?) ->()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.GET, endpoint: .Commits, params: params, query: nil, body: nil) { (response, body, error) -> () in
guard error == nil else {
completion(integrationCommits: nil, error: error)
return
}
guard let integrationCommitsBody = (body as? NSDictionary)?["results"] as? NSArray else {
completion(integrationCommits: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let (result, error): (IntegrationCommits?, NSError?) = unthrow {
return try IntegrationCommits(json: integrationCommitsBody[0] as! NSDictionary)
}
completion(integrationCommits: result, error: error)
}
}
/**
XCS API call for fetching all commits for specific integration.
- parameter integrationId: ID of integration.
- parameter success: Optional Integration Issues object with result.
- parameter error: Optional operation error.
*/
public final func getIntegrationIssues(integrationId: String, completion: (integrationIssues: IntegrationIssues?, error: ErrorType?) ->()) {
let params = [
"integration": integrationId
]
self.sendRequestWithMethod(.GET, endpoint: .Issues, params: params, query: nil, body: nil) { (response, body, error) -> () in
guard error == nil else {
completion(integrationIssues: nil, error: error)
return
}
guard let integrationIssuesBody = body as? NSDictionary else {
completion(integrationIssues: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let (result, error): (IntegrationIssues?, NSError?) = unthrow {
return try IntegrationIssues(json: integrationIssuesBody)
}
completion(integrationIssues: result, error: error)
}
}
// TODO: Methods about to be implemented...
// public func reportQueueSizeAndEstimatedWaitingTime(integration: Integration, completion: ((queueSize: Int, estWait: Double), NSError?) -> ()) {
//TODO: we need to call getIntegrations() -> filter pending and running Integrations -> get unique bots of these integrations -> query for the average integration time of each bot -> estimate, based on the pending/running integrations, how long it will take for the passed in integration to finish
}