Skip to content

Commit

Permalink
Pull Request service and issue comment webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Nov 14, 2018
1 parent 7975653 commit 9964081
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// 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 Vapor

public class RepositoryPullRequestService {

private let token: String
private let client: Client

init(token: String, client: Client) {
self.token = token
self.client = client
}

public func getPullRequest(owner: String, repository: String, number: Int) -> Future<PullRequest> {
let url = "\(Constants.GitHubBaseURL)/repos/\(owner)/\(repository)/pulls/\(number)"

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(PullRequest.self)
}
}

}
2 changes: 2 additions & 0 deletions Sources/Swoctokit/Swoctokit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Swoctokit {

public let repository: RepositoryService
public let contents: RepositoryContentsService
public let pullRequests: RepositoryPullRequestService
public let teams: TeamsService

public init(token: String, application: Application) throws {
Expand All @@ -27,6 +28,7 @@ public class Swoctokit {
self.repository = RepositoryService(token: token, client: client)
self.teams = TeamsService(token: token, client: client)
self.contents = RepositoryContentsService(token: token, client: client)
self.pullRequests = RepositoryPullRequestService(token: token, client: client)
}

}
17 changes: 17 additions & 0 deletions Sources/Swoctokit/SwoctokitWebhookClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ public protocol CommitCommentEventListener: class {

}

public protocol IssueCommentEventListener: AnyObject {

func issueCommentEventReceived(_ event: IssueCommentEvent)

}

public class SwoctokitWebhookClient {

private let application: Application

private var pullRequestEventListeners = [PullRequestEventListener]()
private var commitCommentEventListeners = [CommitCommentEventListener]()
private var issueCommentEventListeners = [IssueCommentEventListener]()

public init(_ application: Application) throws {
self.application = application
Expand All @@ -51,6 +58,10 @@ public class SwoctokitWebhookClient {
commitCommentEventListeners.append(listener)
}

public func addIssueCommentEventListener(_ listener: IssueCommentEventListener) {
issueCommentEventListeners.append(listener)
}

}

extension SwoctokitWebhookClient: WebhookControllerDelegate {
Expand All @@ -61,6 +72,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate {
pullRequestEventReceived(event)
case let event as CommitCommentEvent:
commitCommentEventReceived(event)
case let event as IssueCommentEvent:
isuueCommentEventReceived(event)
default:
break
}
Expand All @@ -74,4 +87,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate {
commitCommentEventListeners.forEach { $0.commitCommentEventReceived(event) }
}

func isuueCommentEventReceived(_ event: IssueCommentEvent) {
issueCommentEventListeners.forEach { $0.issueCommentEventReceived(event) }
}

}
1 change: 1 addition & 0 deletions Sources/Swoctokit/Webhook/EventTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ import Foundation
public enum EventType: String {
case pullRequest = "pull_request"
case commitComment = "commit_comment"
case issueComment = "issue_comment"
}
29 changes: 29 additions & 0 deletions Sources/Swoctokit/Webhook/IssueCommentEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// IssueCommentEvent.swift
// Swoctokit
//
// Created by Franz Busch on 14.11.18.
//

import Foundation

public struct IssueCommentEvent: Decodable, WebhookEvent {

public let action: String
public let issue: Issue

}

public struct Issue: Decodable {

let number: Int

}

public struct IssueComment: Decodable {

public let id: Int
public let url: String
public let body: String

}
4 changes: 4 additions & 0 deletions Sources/Swoctokit/Webhook/WebhookController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ final class WebhookController: RouteCollection {
if let commitCommentEvent = try? req.content.syncDecode(CommitCommentEvent.self) {
delegate?.didReceive(event: commitCommentEvent)
}
case .issueComment:
if let issueCommentEvent = try? req.content.syncDecode(IssueCommentEvent.self) {
delegate?.didReceive(event: issueCommentEvent)
}
}

return .ok
Expand Down

0 comments on commit 9964081

Please sign in to comment.