-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathKnownTestsApi.swift
More file actions
253 lines (219 loc) · 9.47 KB
/
Copy pathKnownTestsApi.swift
File metadata and controls
253 lines (219 loc) · 9.47 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
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2020-Present Datadog, Inc.
*/
import Foundation
public typealias KnownTestsMap = [String: [String: [String]]]
/// Result of a Known Tests API call
public struct KnownTestsResult {
public let tests: KnownTestsMap
public let pageInfo: KnownTestsPageInfo
public init(tests: KnownTestsMap, pageInfo: KnownTestsPageInfo) {
self.tests = tests
self.pageInfo = pageInfo
}
public var isAllTests: Bool { !pageInfo.hasNext }
}
/// Pagination metadata from the Known Tests API (cursor for next page, page size, size, has_next).
public struct KnownTestsPageInfo {
public let cursor: String?
public let pageSize: Int
public let size: Int
public let hasNext: Bool
public init(cursor: String?, pageSize: Int, size: Int, hasNext: Bool) {
self.cursor = cursor
self.pageSize = pageSize
self.size = size
self.hasNext = hasNext
}
public init(pageSize: Int = 2000) {
self.pageSize = pageSize
self.size = 0
self.hasNext = false
self.cursor = nil
}
}
public protocol KnownTestsApi: APIService {
/// Fetch a single page of known tests for the provided cursor.
func tests(
service: String, env: String, repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String],
page: KnownTestsPageInfo,
observer: RequestObserver?
) async throws(APICallError) -> KnownTestsResult
/// Fetch all pages of known tests and merge them.
func tests(
service: String, env: String, repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String],
observer: PagedRequestObserver?
) async throws(APICallError) -> KnownTestsResult
}
extension KnownTestsApi {
public func tests(
service: String, env: String, repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String],
observer: PagedRequestObserver?
) async throws(APICallError) -> KnownTestsResult {
let startTime = Date().timeIntervalSince1970 * 1000
var tests: KnownTestsMap = [:]
var page: KnownTestsPageInfo = .init()
var size: Int = 0
repeat {
let result = try await self.tests(
service: service, env: env, repositoryURL: repositoryURL,
configurations: configurations, customConfigurations: customConfigurations,
page: page, observer: observer
)
tests = tests.merging(result.tests) { (current, new) in
current.merging(new) { (current, new) in
Array(Set(current).union(new))
}
}
page = result.pageInfo
size += result.pageInfo.size
} while page.hasNext
let totalFetchMs = Date().timeIntervalSince1970 * 1000 - startTime
observer?.finished(totalFetchMs: totalFetchMs)
return KnownTestsResult(tests: tests,
pageInfo: .init(cursor: page.cursor,
pageSize: page.pageSize,
size: size,
hasNext: page.hasNext))
}
/// Convenience without a telemetry observer.
@inlinable
public func tests(
service: String, env: String, repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String],
page: KnownTestsPageInfo
) async throws(APICallError) -> KnownTestsResult {
try await tests(service: service, env: env, repositoryURL: repositoryURL,
configurations: configurations, customConfigurations: customConfigurations,
page: page, observer: nil)
}
/// Convenience without a telemetry observer.
@inlinable
public func tests(
service: String, env: String, repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String]
) async throws(APICallError) -> KnownTestsResult {
try await tests(service: service, env: env, repositoryURL: repositoryURL,
configurations: configurations, customConfigurations: customConfigurations,
observer: nil)
}
}
struct KnownTestsApiService: KnownTestsApi, APIServiceConstructible {
typealias KnownTestsCall = APICall<APIDataNoMeta<TestsRequest>, APIDataNoMeta<TestsResponse>>
var endpoint: Endpoint
var headers: [HTTPHeader]
var encoder: JSONEncoder
var decoder: JSONDecoder
let httpClient: any HTTPClientType
let log: Logger
init(config: APIServiceConfig, httpClient: any HTTPClientType, log: Logger) {
self.endpoint = config.endpoint
self.httpClient = httpClient
self.log = log
self.headers = config.defaultHeaders
self.encoder = config.encoder
self.decoder = config.decoder
}
func tests(service: String, env: String,
repositoryURL: String,
configurations: [String: String],
customConfigurations: [String: String],
page: KnownTestsPageInfo,
observer: RequestObserver?) async throws(APICallError) -> KnownTestsResult
{
var configurations: [String: JSONGeneric] = configurations.mapValues { .string($0) }
configurations["custom"] = JSONGeneric(customConfigurations)
let request = TestsRequest(repositoryUrl: repositoryURL, env: env,
service: service, configurations: configurations,
pageInfo: .init(pageSize: page.pageSize, pageState: page.cursor))
let log = self.log
log.debug("Known tests request: \(request)")
let response = try await httpClient.call(KnownTestsCall.self,
url: endpoint.knownTestsURL,
data: .init(attributes: request),
headers: headers + [.contentTypeHeader(contentType: .applicationJSON)],
coders: (encoder, decoder),
observer: observer)
log.debug("Known tests response: \(response.data.attributes)")
let attrs = response.data.attributes
return KnownTestsResult(
tests: attrs.tests,
pageInfo: .init(cursor: attrs.pageInfo.cursor,
pageSize: page.pageSize,
size: attrs.pageInfo.size,
hasNext: attrs.pageInfo.hasNext)
)
}
var endpointURLs: Set<URL> { [endpoint.knownTestsURL] }
}
extension KnownTestsApiService {
struct PageInfoRequest: Encodable, CustomDebugStringConvertible {
let pageSize: Int
let pageState: String?
var debugDescription: String {
let state = pageState.map { #""\#($0)""# } ?? "null"
return #"{"page_size": \#(pageSize), "page_state": \#(state)}"#
}
}
struct PageInfoResponse: Decodable, CustomDebugStringConvertible {
let cursor: String?
let size: Int
let hasNext: Bool
var debugDescription: String {
let cursorStr = cursor.map { #""\#($0)""# } ?? "null"
return #"{"cursor": \#(cursorStr), "size": \#(size), "has_next": \#(hasNext)}"#
}
}
struct TestsRequest: Encodable, APIAttributesUUID, CustomDebugStringConvertible {
let repositoryUrl: String
let env: String
let service: String
let configurations: [String: JSONGeneric]
let pageInfo: PageInfoRequest
static var apiType: String = "ci_app_libraries_tests_request"
var debugDescription: String {
let configs = JSONGeneric.object(configurations).debugDescription
return #"{"repository_url": "\#(repositoryUrl)""#
+ #", "env": "\#(env)""#
+ #", "service": "\#(service)""#
+ #", "configurations": \#(configs)"#
+ #", "page_info": \#(pageInfo)}"#
}
}
struct TestsResponse: Decodable, APIResponseAttributesHasType,
APIResponseAttributesBrokenId, CustomDebugStringConvertible
{
let tests: KnownTestsMap
let pageInfo: PageInfoResponse
static var apiType: String = "ci_app_libraries_tests"
var debugDescription: String {
let modules = tests.map { module, suites -> String in
let suiteEntries = suites.map { suite, names -> String in
let testList = names.map { #""\#($0)""# }.joined(separator: ", ")
return #""\#(suite)": [\#(testList)]"#
}.joined(separator: ", ")
return #""\#(module)": {\#(suiteEntries)}"#
}.joined(separator: ", ")
return #"{"tests": {\#(modules)}, "page_info": \#(pageInfo)}"#
}
}
}
private extension Endpoint {
var knownTestsURL: URL {
let endpoint = "/api/v2/ci/libraries/tests"
switch self {
case let .other(testsBaseURL: url, logsBaseURL: _): return url.appendingPathComponent(endpoint)
default: return mainApi(endpoint: endpoint)!
}
}
}