Skip to content

Commit

Permalink
Merge pull request #5 from IntrepidPursuits/updates
Browse files Browse the repository at this point in the history
Swift Development Snapshot 8-18-a Updates
  • Loading branch information
Patrick Butkiewicz authored Aug 31, 2016
2 parents b0fa408 + ca48dd3 commit 646eae7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-07-25-a
DEVELOPMENT-SNAPSHOT-2016-08-18-a
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Jenkins-Swift
---

![Swift](http://img.shields.io/badge/swift-v3.0--dev.07.25-brightgreen.svg)
![Swift](http://img.shields.io/badge/swift-v3.0--dev.08.18-brightgreen.svg)
[![Build Status](https://travis-ci.org/IntrepidPursuits/Jenkins-swift.svg?branch=master)](https://travis-ci.org/IntrepidPursuits/Jenkins-swift)

Jenkins-Swift is a Jenkins CI client, written in Swift for MacOS. The client uses a development snapshot of Swift 3. The project will be updated to follow the latest Swift 3 snapshots as they're released.
Expand Down Expand Up @@ -207,7 +207,7 @@ ___

## License

Jenkins-Swift is free software and may be redistributed under the terms specified in the [LICENSE](https://github.com/IntrepidPursuits/Jenkins-Swift/blob/master/LICENSE) file.
Jenkins-Swift is free software and may be redistributed under the terms specified in the [LICENSE](https://github.com/IntrepidPursuits/Jenkins-swift/blob/master/license.txt) file.

___

Expand Down
21 changes: 11 additions & 10 deletions Sources/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal final class APIClient: NSObject {
self.baseURL = url
}

func get(path: URL, rawResponse: Bool = false, headers: [String : String] = [:], params: [String : AnyObject] = [:], _ handler: (AnyObject?, Error?) -> Void) {
func get(path: URL, rawResponse: Bool = false, headers: [String : String] = [:], params: [String : AnyObject] = [:], _ handler: @escaping (AnyObject?, Error?) -> Void) {
let request: URLRequest = requestFor(path, method: .GET, headers: headers, params: params, body: nil)
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: request) { data, response, error in
Expand All @@ -77,7 +77,7 @@ internal final class APIClient: NSObject {
task.resume()
}

func post(path: URL, rawResponse: Bool = false, headers: [String : String] = [:], params: [String : AnyObject] = [:], body: String? = nil, _ handler: (AnyObject?, Error?) -> Void) {
func post(path: URL, rawResponse: Bool = false, headers: [String : String] = [:], params: [String : AnyObject] = [:], body: String? = nil, _ handler: @escaping (AnyObject?, Error?) -> Void) {
let bodyData: Data? = body?.data(using: String.Encoding.utf8)
let request: URLRequest = requestFor(path, method: .POST, headers: headers, params: params, body: bodyData)
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
Expand Down Expand Up @@ -106,7 +106,7 @@ internal final class APIClient: NSObject {
if let val = $0.value as? String {
return URLQueryItem(name: $0.key, value: val)
} else {
return URLQueryItem(name: $0.key, value: String($0.value))
return URLQueryItem(name: $0.key, value: String(describing: $0.value))
}
}

Expand All @@ -117,7 +117,7 @@ internal final class APIClient: NSObject {
return request
}

private func decodeResponse(_ response: URLResponse?, rawOutput: Bool, data: Data?, error: Error?, handler: (AnyObject?, Error?) -> Void) {
private func decodeResponse(_ response: URLResponse?, rawOutput: Bool, data: Data?, error: Error?, handler: @escaping (AnyObject?, Error?) -> Void) {
guard let data = data else {
handler(nil, error)
return
Expand All @@ -136,17 +136,18 @@ internal final class APIClient: NSObject {
}
}

let retVal: AnyObject? = (rawOutput == true)
? String(data: data, encoding: String.Encoding.utf8)
: try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)

handler(retVal, nil)
if rawOutput {
handler(String(data: data, encoding: String.Encoding.utf8) as AnyObject?, nil)
} else {
let json = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
handler(json as AnyObject?, nil)
}
}

}

extension APIClient: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: (URLRequest?) -> Void) {
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
completionHandler(nil)
}
}
2 changes: 1 addition & 1 deletion Sources/Build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public enum BuildResult {
case Aborted
case Unknown

private init(type: String?) {
init(type: String?) {
guard let type = type else {
self = .Unknown
return
Expand Down
48 changes: 24 additions & 24 deletions Sources/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ extension Job : CustomStringConvertible {
// MARK: Fetch Jobs

public extension Jenkins {
func fetchJobs(_ handler: ([Job]) -> Void) {
func fetchJobs(_ handler: @escaping ([Job]) -> Void) {
guard let url = URL(string: jenkinsURL)?
.appendingPathComponent("api")
.appendingPathComponent("json") else {
Expand All @@ -160,108 +160,108 @@ public extension Jenkins {
}
}

func fetch(_ job: String, _ handler: (job: Job?) -> Void) {
func fetch(_ job: String, _ handler: @escaping (_ job: Job?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(job)
.appendingPathComponent("api")
.appendingPathComponent("json") else {
handler(job: nil)
handler(nil)
return
}

client?.get(path: url) { response, error in
guard let json = response as? JSON else {
handler(job: nil)
handler(nil)
return
}

handler(job: Job(json: json))
handler(Job(json: json))
}
}

func fetch(_ job: Job, _ handler: (job: Job?) -> Void) {
func fetch(_ job: Job, _ handler: @escaping (_ job: Job?) -> Void) {
return fetch(job.name, handler)
}
}

// MARK: Enabling and Disabling Jobs

public extension Jenkins {
func enable(_ job: Job, _ handler: (error: Error?) -> Void) {
func enable(_ job: Job, _ handler: @escaping (_ error: Error?) -> Void) {
enable(job: job.name, handler)
}

func enable(job named: String, _ handler: (error: Error?) -> Void) {
func enable(job named: String, _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(named)
.appendingPathComponent("enable") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url) { response, error in
handler(error: error)
handler(error)
}
}

func disable(_ job: Job, _ handler: (error: Error?) -> Void) {
func disable(_ job: Job, _ handler: @escaping (_ error: Error?) -> Void) {
disable(job: job.name, handler)
}

func disable(job named: String, _ handler: (error: Error?) -> Void) {
func disable(job named: String, _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(named)
.appendingPathComponent("disable") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url) { response, error in
handler(error: error)
handler(error)
}
}
}

// MARK: Build Jobs

public extension Jenkins {
func build(_ name: String, parameters: [String : String], _ handler: (error: Error?) -> Void) {
func build(_ name: String, parameters: [String : String], _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("buildWithParameters") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url, params: parameters) { response, error in
handler(error: error)
client?.post(path: url, params: parameters as [String : AnyObject]) { response, error in
handler(error)
}
}

func build(_ job: Job, parameters: [String : String], _ handler: (error: Error?) -> Void) {
func build(_ job: Job, parameters: [String : String], _ handler: @escaping (_ error: Error?) -> Void) {
build(job.name, parameters: parameters, handler)
}

func build(_ name: String, _ handler: (error: Error?) -> Void) {
func build(_ name: String, _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("build") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url) { response, error in
if let statusCode = response?.statusCode {
if statusCode == 400 {
handler(error: JenkinsError.JobRequiresParameters)
handler(JenkinsError.JobRequiresParameters)
}
}

handler(error: error)
handler(error)
}
}

func build(job: Job, _ handler: (error: Error?) -> Void) {
func build(job: Job, _ handler: @escaping (_ error: Error?) -> Void) {
build(job.name, handler)
}
}
Expand Down
58 changes: 29 additions & 29 deletions Sources/JobConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ public typealias JobConfiguration = String
// MARK: Job Configuration

public extension Jenkins {
func fetchJobConfiguration(_ job: Job, _ handler: (response: String?, error: Error?) -> Void) {
func fetchJobConfiguration(_ job: Job, _ handler: @escaping (_ response: String?, _ error: Error?) -> Void) {
fetchJobConfiguration(job.name, handler)
}

func fetchJobConfiguration(_ name: String, _ handler: (response: String?, error: Error?) -> Void) {
func fetchJobConfiguration(_ name: String, _ handler: @escaping (_ response: String?, _ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("config.xml") else {
handler(response: nil, error: JenkinsError.InvalidJenkinsURL)
handler(nil, JenkinsError.InvalidJenkinsURL)
return
}

fetchJobConfiguration(url: url, handler)
}

func fetchJobConfiguration(url: URL, _ handler: (response: String?, error: Error?) -> Void) {
func fetchJobConfiguration(url: URL, _ handler: @escaping (_ response: String?, _ error: Error?) -> Void) {
client?.get(path: url, rawResponse: true) { xml, error in

if let xml = xml as? String {
handler(response: xml, error: nil)
handler(xml, nil)
return
}

handler(response: nil, error: error)
handler(nil, error)
}
}
}
Expand All @@ -46,19 +46,19 @@ public extension Jenkins {
public extension Jenkins {
private var createCopyPath: String { return "/createItem" }

func copy(_ job: Job, to: String, _ handler: (error: Error?) -> Void) {
func copy(_ job: Job, to: String, _ handler: @escaping (_ error: Error?) -> Void) {
copy(job.name, to: to, handler)
}

func copy(_ name: String, to: String, _ handler: (error: Error?) -> Void) {
func copy(_ name: String, to: String, _ handler: @escaping (_ error: Error?) -> Void) {
guard let copyFromJob = name.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
let copyToJob = to.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

guard var url = URLComponents(string: jenkinsURL) else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

Expand All @@ -70,83 +70,83 @@ public extension Jenkins {
]

client?.post(path: url.url!) { response, error in
handler(error: error)
handler(error)
}
}

func create(_ job: Job, configuration: JobConfiguration, _ handler: (error: Error?) -> Void) {
func create(_ job: Job, configuration: JobConfiguration, _ handler: @escaping (_ error: Error?) -> Void) {
create(job.name, configuration: configuration, handler)
}

func create(_ name: String, configuration: JobConfiguration, _ handler: (error: Error?) -> Void) {
func create(_ name: String, configuration: JobConfiguration, _ handler: @escaping (_ error: Error?) -> Void) {
guard let encodedName = name.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

let queryItem = URLQueryItem(name: "name", value: encodedName)

guard var url = URLComponents(string: jenkinsURL) else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

url.path = createCopyPath
url.queryItems = [queryItem]

client?.post(path: url.url!, headers: ["Content-Type" : "text/xml"], body: configuration) { response, error in
handler(error: error)
handler(error)
}
}

func update(_ job: Job, description: String, _ handler: (error: Error?) -> Void) {
func update(_ job: Job, description: String, _ handler: @escaping (_ error: Error?) -> Void) {
update(job.name, description: description, handler)
}

func update(_ name: String, description: String, _ handler: (error: Error?) -> Void) {
func update(_ name: String, description: String, _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("description") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url, params: ["description" : description]) { response, error in
handler(error: error)
client?.post(path: url, params: ["description" : description as AnyObject]) { response, error in
handler(error)
}
}

func update(_ job: Job, configuration: JobConfiguration, _ handler: (error: Error?) -> Void) {
func update(_ job: Job, configuration: JobConfiguration, _ handler: @escaping (_ error: Error?) -> Void) {
update(job.name, configuration: configuration, handler)
}

func update(_ name: String, configuration: JobConfiguration, _ handler: (error: Error?) -> Void) {
func update(_ name: String, configuration: JobConfiguration, _ handler: @escaping (_ error: Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("config.xml") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url, body: configuration) { response, error in
handler(error: error)
handler(error)
}
}

func delete(_ job: Job, _ handler: (error: Error?) -> Void) {
func delete(_ job: Job, _ handler: @escaping (Error?) -> Void) {
delete(job.name, handler)
}

func delete(_ name: String, _ handler: (error: Error?) -> Void) {
func delete(_ name: String, _ handler: @escaping (Error?) -> Void) {
guard let url = URL(string: jobURL)?
.appendingPathComponent(name)
.appendingPathComponent("doDelete") else {
handler(error: JenkinsError.InvalidJenkinsURL)
handler(JenkinsError.InvalidJenkinsURL)
return
}

client?.post(path: url) { response, error in
handler(error: error)
handler(error)
}
}

Expand Down

0 comments on commit 646eae7

Please sign in to comment.