forked from SwiftPackageIndex/PackageList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.swift
executable file
·572 lines (444 loc) · 18 KB
/
validate.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env swift
/*
[1]: Some parts of this script have been temporarily commented out due to a Swift bug in Xcode 12 (Beta 3).
Exact can be found in this pull request: https://github.com/SwiftPackageIndex/PackageList/pull/536
https://bugs.swift.org/browse/SR-13336
*/
import Foundation
let rawGitHubBaseURL = URLComponents(string: "https://raw.githubusercontent.com")!
let existingPackageListURL = rawGitHubBaseURL.url!.appendingPathComponent("SwiftPackageIndex/PackageList/main/packages.json")
let timeoutIntervalForRequest = 3000.0
let timeoutIntervalForResource = 6000.0
let httpMaximumConnectionsPerHost = 10
let processTimeout = 50.0
// We have a special Personal Access Token (PAT) which is used to increase our rate limit allowance up to 5,000 to enable
// us to process every package.
let bearerToken = ProcessInfo.processInfo.environment["GITHUB_TOKEN"]
if bearerToken == nil {
print("Warning: Using anonymous authentication -- may run into rate limiting issues\n")
}
let config: URLSessionConfiguration = .default
config.timeoutIntervalForRequest = timeoutIntervalForRequest
config.timeoutIntervalForResource = timeoutIntervalForResource
config.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHost
let session = URLSession(configuration: config)
// MARK: - Definitions
enum SourceHost: String {
case GitHub = "github.com"
}
struct Repository: Decodable {
let default_branch: String
}
struct Product: Decodable {
let name: String
}
struct Package: Decodable {
let name: String
let products: [Product]
}
// MARK: - Error
enum ValidatorError: Error {
case invalidURL(String)
case timedOut
case noData
case networkingError(Error)
case decoderError(Error, payload: String)
case unknownGitHost(String?)
case fileSystemError(Error)
case badPackageDump(String?)
case missingProducts
/* [1]
// case rateLimitExceeded(Int)
// case packageDoesNotExist(String)
*/
case dumpTimedOut
var localizedDescription: String {
switch self {
case .invalidURL(let url):
return "Invalid URL: \(url)"
case .timedOut:
return "Request Timed Out"
case .noData:
return "No Data Received"
case .dumpTimedOut:
return "Dump Timed Out"
case let .decoderError(error, payload):
return "payload: \(payload)\n\(error.localizedDescription)"
case .networkingError(let error), .fileSystemError(let error):
return error.localizedDescription
case .unknownGitHost(let host):
return "Unknown URL host: \(host ?? "nil")"
case .badPackageDump(let output):
return "Bad Package Dump -- \(output ?? "No Output")"
case .missingProducts:
return "Missing Products"
/* [1]
case .rateLimitExceeded(let limit):
return "Rate Limit of \(limit) Exceeded"
case .packageDoesNotExist(let url):
return "Package Does Not Exist: \(url)"
*/
}
}
}
// MARK: - Networking
func downloadSync(url: String, timeout: Int = 10) -> Result<Data, ValidatorError> {
let semaphore = DispatchSemaphore(value: 0)
guard let apiURL = URL(string: url) else {
return .failure(.invalidURL(url))
}
var payload: Data?
var taskError: ValidatorError?
var request = URLRequest(url: apiURL)
if let token = bearerToken, apiURL.host?.contains(SourceHost.GitHub.rawValue) == true {
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
let task = session.dataTask(with: request) { (data, response, error) in
let httpResponse = response as? HTTPURLResponse
if let limit = httpResponse?.value(forHTTPHeaderField: "X-RateLimit-Limit").flatMap(Int.init),
let remaining = httpResponse?.value(forHTTPHeaderField: "X-RateLimit-Remaining").flatMap(Int.init),
remaining == 0 {
// [1]
print("ERROR: Rate Limit Exceeded /\(limit)") // Tem[
taskError = .noData // Temp
// taskError = .rateLimitExceeded(limit)
} else if httpResponse?.statusCode == 404 {
// [1]
print("ERROR: 404 Not Found") // Temp
taskError = .noData // Temp
// taskError = .packageDoesNotExist(apiURL.absoluteString)
} else if let error = error {
taskError = .networkingError(error)
}
payload = data
semaphore.signal()
}
task.resume()
switch semaphore.wait(timeout: .now() + .seconds(timeout)) {
case .timedOut:
return .failure(.timedOut)
case .success where payload == nil:
return .failure(taskError ?? .noData)
case .success:
return .success(payload!)
}
}
func downloadJSONSync<Payload: Decodable>(url: String, timeout: Int = 10) -> Result<Payload, ValidatorError> {
let decoder = JSONDecoder()
let result = downloadSync(url: url, timeout: timeout)
switch result {
case .failure(let error):
return .failure(error)
case .success(let data):
do {
return .success(try decoder.decode(Payload.self, from: data))
} catch {
let payload = String(decoding: data, as: UTF8.self)
return .failure(.decoderError(error, payload: payload))
}
}
}
// MARK: - Verification
func getDefaultBranch(userName: String, repositoryName: String) -> Result<String, ValidatorError> {
let result: Result<Repository, ValidatorError> = downloadJSONSync(url: "https://api.github.com/repos/\(userName)/\(repositoryName)")
return result.map { $0.default_branch }
}
func getPackageSwiftURL(url: URL) -> Result<URL, ValidatorError> {
guard let host = url.host.flatMap(SourceHost.init(rawValue:)) else {
return .failure(.unknownGitHost(url.host))
}
switch host {
case .GitHub:
let repositoryName = url.deletingPathExtension().lastPathComponent
let userName = url.deletingLastPathComponent().lastPathComponent
switch getDefaultBranch(userName: userName, repositoryName: repositoryName) {
case .success(let defaultBranch):
var rawURLComponents = rawGitHubBaseURL
rawURLComponents.path = ["", userName, repositoryName, defaultBranch, "Package.swift"].joined(separator: "/")
guard let packageURL = rawURLComponents.url else {
return .failure(.invalidURL(url.absoluteString))
}
return .success(packageURL)
case .failure(let failure):
return .failure(failure)
}
}
}
func downloadPackage(url: URL) -> Result<URL, ValidatorError> {
switch downloadSync(url: url.absoluteString) {
case .failure(let error):
return .failure(error)
case .success(let packageData):
let fileManager = FileManager.default
let outputDirectoryURL = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
do {
try fileManager.createDirectory(at: outputDirectoryURL, withIntermediateDirectories: false, attributes: nil)
try packageData.write(to: outputDirectoryURL.appendingPathComponent("Package.swift"), options: .atomic)
return .success(outputDirectoryURL)
} catch {
return .failure(.fileSystemError(error))
}
}
}
func dumpPackageProcessAt(_ packageDirectoryURL: URL, outputTo pipe: Pipe, errorsTo errorPipe: Pipe) -> Process {
let process = Process()
process.launchPath = "/usr/bin/xcrun"
process.arguments = ["swift", "package", "dump-package"]
process.currentDirectoryURL = packageDirectoryURL
process.standardOutput = pipe
process.standardError = errorPipe
return process
}
func dumpPackage(atURL url: URL, completion: @escaping (Result<Data, ValidatorError>) -> Void) {
var stdout = Data()
var stderr = Data()
let queue = DispatchQueue(label: "process-pipe-read-queue")
let stdoutPipe = Pipe { handler in
queue.async { stdout.append(handler.availableData) }
}
let stderrPipe = Pipe { handler in
queue.async { stderr.append(handler.availableData) }
}
let process = dumpPackageProcessAt(url, outputTo: stdoutPipe, errorsTo: stderrPipe)
process.terminationHandler = { process in
guard process.terminationStatus == 0 else {
if process.terminationStatus == 15 {
completion(.failure(.dumpTimedOut))
} else {
let errorDump = String(data: stderr, encoding: .utf8)
completion(.failure(.badPackageDump(errorDump)))
}
return
}
completion(.success(stdout))
}
process.launch()
DispatchQueue.main.asyncAfter(deadline: .now() + processTimeout) {
if process.isRunning {
process.terminate()
}
}
}
func verifyPackage(url: URL, completion: @escaping (Error?) -> Void) {
do {
let packageURL = try getPackageSwiftURL(url: url).get()
let localPackageURL = try downloadPackage(url: packageURL).get()
dumpPackage(atURL: localPackageURL) { result in
switch result {
case .failure(let error):
completion(error)
case .success(let data):
let decoder = JSONDecoder()
do {
let package = try decoder.decode(Package.self, from: data)
guard package.products.isEmpty == false else {
completion(ValidatorError.missingProducts)
return
}
completion(nil)
} catch {
completion(error)
}
}
}
} catch {
completion(error)
}
}
// MARK: - Redirects
class RedirectFollower: NSObject, URLSessionDataDelegate {
var lastURL: URL?
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
lastURL = request.url ?? lastURL
completionHandler(request)
}
}
extension URL {
func removingGitExtension() -> URL {
if absoluteString.hasSuffix(".git") {
let lastPath = lastPathComponent.components(separatedBy: ".").dropLast().joined(separator: ".")
return self.deletingLastPathComponent().appendingPathComponent(lastPath)
}
return self
}
func followingRedirects() -> URL? {
let semaphore = DispatchSemaphore(value: 0)
let follower = RedirectFollower()
let session = URLSession(configuration: .default, delegate: follower, delegateQueue: nil)
let task = session.dataTask(with: self) { (_, response, error) in
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .now() + 30)
if self.removingGitExtension().absoluteString == follower.lastURL?.absoluteString {
return nil
}
return follower.lastURL
}
}
// MARK: - Helpers
extension Array where Element == URL {
func findDuplicates(of url: URL) -> Array<(offset: Int, element: URL)> {
let normalise: (URL) -> String = { url in
var normalisedString = url.absoluteString.lowercased()
if normalisedString.hasSuffix(".git") {
normalisedString = normalisedString.components(separatedBy: ".").dropLast().joined(separator: ".")
}
return normalisedString
}
let normalisedSubject = normalise(url)
return enumerated().filter { tuple in
normalise(tuple.element) == normalisedSubject
}
}
}
extension Pipe {
convenience init(readabilityHandler: ((FileHandle) -> Void)?) {
self.init()
self.fileHandleForReading.readabilityHandler = readabilityHandler
}
}
// MARK: - Running Code
func url(packagesFromDirectories directoryURLs: [URL], andArguments arguments: [String]) -> URL? {
let possiblePackageURLs = arguments.dropFirst().compactMap { URL(fileURLWithPath: $0) } + directoryURLs.map { $0.appendingPathComponent("packages.json") }
return possiblePackageURLs.first(where: { FileManager.default.fileExists(atPath: $0.path) })
}
let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true)
let packagesJsonURL = url(packagesFromDirectories: [currentDirectoryURL, URL(fileURLWithPath: #file).deletingLastPathComponent()], andArguments: CommandLine.arguments)!
// 1. Download existing package list
var existingPackageList: [URL] = {
do {
return try downloadJSONSync(url: existingPackageListURL.absoluteString).get()
} catch {
print("[Error] Failed to download existing package list from GitHub")
print(error)
exit(EXIT_FAILURE)
}
}()
// 2. Load local package list
var backupLocalPackageList: Data?
var localPackageList: [URL] = {
do {
let decoder = JSONDecoder()
let data = try Data(contentsOf: packagesJsonURL)
backupLocalPackageList = data
return try decoder.decode([URL].self, from: data)
} catch {
print("[Error] Failed to load local package list")
print(error)
exit(EXIT_FAILURE)
}
}()
// 3. Calculate Differences
let difference = localPackageList.difference(from: existingPackageList)
var newURLsToValidate = [URL]()
difference.forEach { change in
switch change {
case .insert(_, let url, _):
print("+ \(url)")
newURLsToValidate.append(url)
case .remove(_, let url, _):
print("- \(url)")
}
}
// 4. Validate URLs
var errorsFound = Set<String>()
newURLsToValidate.forEach { url in
var mutableURL = url
// Handle redirects
if let newURL = mutableURL.followingRedirects() {
if let offset = localPackageList.firstIndex(of: mutableURL) {
localPackageList[offset] = newURL.appendingPathExtension("git")
errorsFound.insert("Found \"\(mutableURL.absoluteString)\" but this redirected to \"\(newURL.absoluteString)\"")
}
mutableURL = newURL
}
// URL must not be duplicated
let duplicates = localPackageList.findDuplicates(of: mutableURL)
if duplicates.count > 1 {
duplicates.map({ $0.offset }).reversed().dropLast().forEach { offset in
localPackageList.remove(at: offset)
}
errorsFound.insert("The packages.json file contained duplicate rows")
}
// URL must end in .git
if mutableURL.pathExtension != "git" {
if let offset = localPackageList.firstIndex(of: mutableURL) {
localPackageList[offset] = mutableURL.appendingPathExtension("git")
errorsFound.insert("One or more packages URLs were missing .git extensions")
}
}
}
// 5. Validate order of JSON
let localPackageListSorted = localPackageList.sorted {
$0.absoluteString.lowercased() < $1.absoluteString.lowercased()
}
let unsortedUrls = zip(localPackageList, localPackageListSorted).enumerated().filter { $0.element.0 != $0.element.1 }.map {
($0.offset, $0.element.0)
}
if unsortedUrls.isEmpty == false {
errorsFound.insert("The packages.json file was incorrectly sorted")
}
// 6. Report any problems
// We've automatically fixed them so no need to stop the script - but users should be aware that we've overriden the file
if errorsFound.isEmpty == false {
// Write the backup package list to disk to prevent users from losing data
let backupURL = packagesJsonURL.deletingLastPathComponent().appendingPathComponent("packages.backup.json")
try! backupLocalPackageList?.write(to: backupURL)
// Write the new local package list to disk
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted]
let data = try! encoder.encode(localPackageListSorted)
let str = String(data: data, encoding: .utf8)!.replacingOccurrences(of: "\\/", with: "/")
let unescapedData = str.data(using: .utf8)!
try! unescapedData.write(to: packagesJsonURL)
let errorMessage = errorsFound.map { "* \($0)" }.joined(separator: "\n")
print("""
*******************************************************************************
** IMPORTANT ******************************************************************
During validation, problem(s) were found:
\(errorMessage)
These problems have been automatically fixed, but you'll need to commit the
changes that this script has made before creating the pull request.
Thanks!
*******************************************************************************
""")
}
// 7. Validate Package
let newURLsCount = newURLsToValidate.count
var count = 0
var packageResults = [URL: Error]()
let finish = {
packageResults.forEach { url, error in
if let error = error as? ValidatorError {
print("🚨 \(url.absoluteString): \(error.localizedDescription)")
} else {
print("🚨 \(url.absoluteString): \(error)")
}
}
if errorsFound.isEmpty && packageResults.isEmpty {
print("\n\(newURLsCount) package(s) passed")
exit(EXIT_SUCCESS)
}
if packageResults.isEmpty {
print("\nPassed validation but please commit the changes made by the script before creating the pull request.")
exit(EXIT_FAILURE)
}
print("\n\(packageResults.count) package(s) out of \(newURLsCount) failed")
exit(EXIT_FAILURE)
}
func runCycle() {
if newURLsToValidate.isEmpty {
finish()
return
}
let url = newURLsToValidate.removeFirst()
verifyPackage(url: url) { error in
if let error = error {
packageResults[url] = error
}
runCycle()
}
}
runCycle()
RunLoop.main.run()