Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending TanukiKit for CI Builds (experiment) #8

Closed
tiferrei opened this issue May 29, 2016 · 3 comments
Closed

Extending TanukiKit for CI Builds (experiment) #8

tiferrei opened this issue May 29, 2016 · 3 comments

Comments

@tiferrei
Copy link
Contributor

tiferrei commented May 29, 2016

As an experiment I was trying to extend TanukiKit to get the builds for a project, the API structure for it is as follows:

  • All builds from project:
    /projects/(the id of the project)/builds
    This gets a JSON array with all the builds, pretty similar to getting the project list. Example response:

screen shot 2016-05-29 at 13 06 40

- To get a specific build from the project: `/projects/(the id of the project)/builds/(the id of the specific build)` This gets a JSON with the info from the build. Example response:

screen shot 2016-05-29 at 13 07 29

Due to the similar response to a projects get, I tried to adapt it to get the builds, here is the code I have tried:

import Foundation
import RequestKit

@objc public class apiBuildClass: NSObject {
    public let id: Int
    public let user: apiUserClass
    public var status: String?
    public var stage: String?
    public var ref: String?
    public var tag: Bool?
    public var coverage: Double?
    public var createdAt: String?
    public var startedAt: String?
    public var finishedAt: String?

    public init(_ json: [String: AnyObject]) {
        user = apiUserClass(json["user"] as? [String: AnyObject] ?? [:])
        if let id = json["id"] as? Int {
            self.id = id
            status = json["status"] as? String
            stage = json["stage"] as? String
            ref = json["ref"] as? String
            tag = json["tag"] as? Bool
            coverage = json["coverage"] as? Double
            createdAt = json["created_at"] as? String
            startedAt = json["started_at"] as? String
            finishedAt = json["finished_at"] as? String
        } else {
            id = -1
        }
    }
}

public extension TanukiKit {

    /**
     Fetches the Builds for the specified Project
     - parameter Project: The Project to get the builds from.
     - parameter Build: The specific build to get, if nil, gives a list of all the builds available.
     - parameter completion: Callback for the outcome of the fetch.
     */

    public func builds(project: String, build: String, completion: (response: Response<[apiBuildClass]>) -> Void) {
        let router = BuildRouter.ReadProjectBuilds(configuration, project, build)
        router.loadJSON([[String: AnyObject]].self) { json, error in
            if let error = error {
                completion(response: Response.Failure(error))
            }
            if let json = json {
                let repos = json.map { apiBuildClass($0) }
                completion(response: Response.Success(repos))
            }
        }
    }
}

// MARK: Router

enum BuildRouter: Router {
    case ReadProjectBuilds(Configuration, String, String)

    var configuration: Configuration {
        switch self {
        case .ReadProjectBuilds(let config, _, _): return config
        }
    }

    var method: HTTPMethod {
        return .GET
    }

    var encoding: HTTPEncoding {
        return .URL
    }

    var params: [String: String] {
        switch self {
        case .ReadProjectBuilds(_, let project, let build):
            return ["project": project, "build": build]
        }
    }

    var path: String {
        switch self {
        case .ReadProjectBuilds:
            return "/projects/\(params["project"])/builds/\(params["build"])"
        }
    }
}

And this should work by calling it using:

TanukiKit(config).builds("2", build: "47") { response in
            switch response {
            case .Success(let apiBuild):
                print("Number of builds: \(apiBuild.count)")
            case .Failure(let error):
                print("OOPS: \(error)")
                authFailedWarning(self)
            }
        }

Everything builds and compiles perfectly, the public function builds is recognised but nothing outputs when I run it, not even the error. Can you give me hand on this @pietbrauer?

Thanks!

@pietbrauer
Copy link
Member

Why not open a PullRequest? Much easier than an issue as I could comment on the code.

@tiferrei
Copy link
Contributor Author

Of course, just a second and I'll create the pull request

@pietbrauer
Copy link
Member

Cool, closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants