Skip to content

Commit

Permalink
Merge pull request #2 from e-Sixt/feature/add-review-event
Browse files Browse the repository at this point in the history
Add support to listen for pull_request_review webhooks
  • Loading branch information
FranzBusch authored Jul 2, 2019
2 parents 611ead7 + c8a48d1 commit e22cf5a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Swoctokit/Models/Branch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension Branch {
public struct Protection: Decodable {
public let enabled: Bool
public let requiredStatusChecks: RequiredStatusChecks
public let requiredPullRequestReviews: RequiredPullRequestReviews?
}
}

Expand All @@ -36,4 +37,8 @@ extension Branch.Protection {
public let enforcementLevel: String
public let contexts: [String]
}

public struct RequiredPullRequestReviews: Decodable {
public let requiredApprovingReviewCount: Int
}
}
19 changes: 19 additions & 0 deletions Sources/Swoctokit/Models/Review.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swoctokit open source project
//
// Copyright (c) 2018 e-Sixt
// Licensed under MIT
//
// See LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import Foundation

public struct Review: Decodable {
public let id: Int
public let user: User
public let commitId: String
public let state: String
}
17 changes: 17 additions & 0 deletions Sources/Swoctokit/Models/User.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swoctokit open source project
//
// Copyright (c) 2018 e-Sixt
// Licensed under MIT
//
// See LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import Foundation

public struct User: Decodable, Equatable, Hashable {
public let id: Int
public let login: String // Username
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public class RepositoryPullRequestService {
}
}

public func getReviews(owner: String, repository: String, number: Int) -> Future<[Review]> {
let url = "\(Constants.GitHubBaseURL)/repos/\(owner)/\(repository)/pulls/\(number)/reviews"

return client.get(url, headers: HTTPHeaders([("Authorization", "token \(token)")])).flatMap { response in
if let error = try? response.content.syncDecode(GitHubAPIErrorResponse.self) {
throw error
}

return try response.content.decode(json: [Review].self, using: .convertFromSnakeCase)
}
}

}

struct MergePullRequestResponse: Decodable {
Expand Down
17 changes: 17 additions & 0 deletions Sources/Swoctokit/SwoctokitWebhookClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public protocol CheckRunEventListener: AnyObject {

}

public protocol PullRequestReviewEventListener: AnyObject {

func pullRequestReviewEventReceived(_ event: PullRequestReviewEvent)

}

public class SwoctokitWebhookClient {

private let application: Application
Expand All @@ -44,6 +50,7 @@ public class SwoctokitWebhookClient {
private var commitCommentEventListeners = [CommitCommentEventListener]()
private var issueCommentEventListeners = [IssueCommentEventListener]()
private var checkRunEventListeners = [CheckRunEventListener]()
private var pullRequestReviewEventListeners = [PullRequestReviewEventListener]()

public init(_ application: Application) throws {
self.application = application
Expand Down Expand Up @@ -74,6 +81,10 @@ public class SwoctokitWebhookClient {
checkRunEventListeners.append(listener)
}

public func addPullRequestReviewEventListener(_ listener: PullRequestReviewEventListener) {
pullRequestReviewEventListeners.append(listener)
}

}

extension SwoctokitWebhookClient: WebhookControllerDelegate {
Expand All @@ -88,6 +99,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate {
issueCommentEventReceived(event)
case let event as CheckRunEvent:
checkRunEventReceived(event)
case let event as PullRequestReviewEvent:
pullRequestReviewEventReceived(event)
default:
break
}
Expand All @@ -109,4 +122,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate {
checkRunEventListeners.forEach { $0.checkRunEventReceived(event) }
}

func pullRequestReviewEventReceived(_ event: PullRequestReviewEvent) {
pullRequestReviewEventListeners.forEach { $0.pullRequestReviewEventReceived(event) }
}

}
1 change: 1 addition & 0 deletions Sources/Swoctokit/Webhook/PullRequestEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public struct PullRequestEvent: Decodable, WebhookEvent {
public let action: String
public let number: Int
public let pullRequest: PullRequest
public let repository: Repository

}

Expand Down
18 changes: 18 additions & 0 deletions Sources/Swoctokit/Webhook/PullRequestReviewEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swoctokit open source project
//
// Copyright (c) 2018 e-Sixt
// Licensed under MIT
//
// See LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

public struct PullRequestReviewEvent: Decodable, WebhookEvent {

public let action: String
public let pullRequest: PullRequest
public let repository: Repository

}

0 comments on commit e22cf5a

Please sign in to comment.