-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitHub.swift
90 lines (78 loc) · 3.6 KB
/
GitHub.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
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Rainbow
protocol URLSessionProvider {
func myUploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTaskProvider
}
protocol URLSessionUploadTaskProvider {
func resume()
}
protocol GitHubProvider {
init(git: GitProvider, token: String, urlSession: URLSessionProvider )
func createPullRequest(branchName: String, title: String) throws
}
class GitHub: GitHubProvider {
private let git: GitProvider
private let token: String
private let session: URLSessionProvider
required init(git: GitProvider, token: String = ProcessInfo.processInfo.environment["TOKEN"]!, urlSession session: URLSessionProvider = URLSession.shared) {
self.git = git
self.token = token
self.session = session
}
func createPullRequest(branchName: String, title: String) throws {
let group = DispatchGroup()
group.enter()
let parameterArray: [String: Any] = [
"head": branchName,
"base": git.baseBranch,
"title": title,
"body": "This Pull Request was automatically created using [swift-dependency-updater](https://github.com/Nef10/swift-dependency-updater). Any changes will be overriden the next time swift-dependency-updater is executed", // swiftlint:disable:this line_length
"maintainer_can_modify": true
]
var request = URLRequest(url: URL(string: "https://api.github.com/repos/\(git.slug)/pulls")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
request.setValue("token \(token)", forHTTPHeaderField: "Authorization")
let parameters = try JSONSerialization.data(withJSONObject: parameterArray, options: [])
let task = session.myUploadTask(with: request, from: parameters) { data, response, error in
self.handleCreatePullRequestResponse(data: data, response: response, error: error)
group.leave()
}
task.resume()
group.wait()
}
private func handleCreatePullRequestResponse(data: Data?, response: URLResponse?, error: Error?) {
guard let httpResponse = response as? HTTPURLResponse else {
print("Error creating Pull Request: No HTTPURLResponse".red)
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("Data: \(dataString)")
}
if let error = error {
print("Error: \(error.localizedDescription)")
}
return
}
guard httpResponse.statusCode == 201 else {
print("Error creating Pull Request: Got status code \(httpResponse.statusCode)".red)
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("Data: \(dataString)")
}
if let error = error {
print("Error: \(error.localizedDescription)")
}
return
}
print("Created Pull Request".green)
}
}
extension URLSession: URLSessionProvider {
func myUploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionUploadTaskProvider {
uploadTask(with: request, from: bodyData, completionHandler: completionHandler)
}
}
extension URLSessionUploadTask: URLSessionUploadTaskProvider {
}