Skip to content

Commit

Permalink
return queue status as json (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and dmcrodrigues committed Nov 23, 2019
1 parent 48d135c commit 6fe611d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
10 changes: 8 additions & 2 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public func routes(
gitHubEventsService: GitHubEventsService
) throws {

router.get("/") { request -> String in
return String(describing: mergeService.state.value)
router.get("/") { request -> Response in
let response = Response(using: request)
if request.header(named: HTTPHeaderName.accept.description) == "application/json" {
try response.content.encode(mergeService.state.value, as: .json)
} else {
try response.content.encode(String(describing: mergeService.state.value), as: .plainText)
}
return response
}

router.get("health") { request -> HTTPResponse in
Expand Down
8 changes: 4 additions & 4 deletions Sources/Bot/Models/PullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public struct PullRequest: Equatable {
}

extension PullRequest {
public struct Author: Equatable, Decodable {
public struct Author: Equatable, Codable {
public let login: String
}
}

extension PullRequest {
public struct Label: Equatable, Decodable {
public struct Label: Equatable, Codable {
public let name: String

public init(name: String) {
Expand All @@ -26,7 +26,7 @@ extension PullRequest {
}

extension PullRequest {
public struct Branch: Equatable, Decodable {
public struct Branch: Equatable, Codable {
public let ref: String
public let sha: String
}
Expand All @@ -48,7 +48,7 @@ extension PullRequest {
}
}

extension PullRequest: Decodable {
extension PullRequest: Codable {
enum CodingKeys: String, CodingKey {
case number
case title
Expand Down
5 changes: 3 additions & 2 deletions Sources/Bot/Models/PullRequestMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension PullRequestMetadata {
// efficient than doing multiple checks to determine this
//
// Reference: https://github.com/octokit/octokit.net/pull/1764/files
public enum MergeState: String, Decodable {
public enum MergeState: String, Codable {
/// Merge conflict. Merging is blocked.
case dirty
/// Mergeability was not checked yet. Merging is blocked.
Expand All @@ -28,7 +28,7 @@ extension PullRequestMetadata {
}
}

extension PullRequestMetadata: Decodable {
extension PullRequestMetadata: Codable {
public init(from decoder: Decoder) throws {
reference = try PullRequest(from: decoder)

Expand All @@ -41,6 +41,7 @@ extension PullRequestMetadata: Decodable {
enum CodingKeys: String, CodingKey {
case isMerged = "merged"
case mergeState = "mergeable_state"
case reference
}
}

Expand Down
46 changes: 44 additions & 2 deletions Sources/Bot/Services/MergeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ extension MergeService {

extension MergeService {

public enum FailureReason: Equatable {
public enum FailureReason: String, Equatable, Encodable {
case conflicts
case mergeFailed
case synchronizationFailed
Expand All @@ -496,7 +496,7 @@ extension MergeService {

public struct State: Equatable {

public enum Status: Equatable {
public enum Status: Equatable, Encodable {
case starting
case idle
case ready
Expand All @@ -521,6 +521,35 @@ extension MergeService {
return nil
}
}

enum CodingKeys: String, CodingKey {
case status
case metadata
case error
}

public func encode(to encoder: Encoder) throws {
var values = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .starting:
try values.encode("starting", forKey: .status)
case .idle:
try values.encode("idle", forKey: .status)
case .ready:
try values.encode("ready", forKey: .status)
case let .integrating(metadata):
try values.encode("integrating", forKey: .status)
try values.encode(metadata, forKey: .metadata)
case let .runningStatusChecks(metadata):
try values.encode("runningStatusChecks", forKey: .status)
try values.encode(metadata, forKey: .metadata)
case let .integrationFailed(metadata, error):
try values.encode("integrationFailed", forKey: .status)
try values.encode(metadata, forKey: .metadata)
try values.encode(error, forKey: .error)
}

}
}

internal let integrationLabel: PullRequest.Label
Expand Down Expand Up @@ -754,6 +783,19 @@ extension MergeService.State: CustomStringConvertible {
}
}

extension MergeService.State: Encodable {
enum CodingKeys: String, CodingKey {
case status
case queue
}
public func encode(to encoder: Encoder) throws {
var values = encoder.container(keyedBy: CodingKeys.self)

try values.encode(status, forKey: .status)
try values.encode(pullRequests, forKey: .queue)
}
}

extension SignalProducer {

static func value(_ value: Value) -> SignalProducer<Value, Error> {
Expand Down

0 comments on commit 6fe611d

Please sign in to comment.