diff --git a/Sources/Swoctokit/Repository/PullRequests/RepositoryPullRequestService.swift b/Sources/Swoctokit/Repository/PullRequests/RepositoryPullRequestService.swift new file mode 100644 index 0000000..1ed1eff --- /dev/null +++ b/Sources/Swoctokit/Repository/PullRequests/RepositoryPullRequestService.swift @@ -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 { + 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) + } + } + +} diff --git a/Sources/Swoctokit/Swoctokit.swift b/Sources/Swoctokit/Swoctokit.swift index b522c6c..70661dd 100644 --- a/Sources/Swoctokit/Swoctokit.swift +++ b/Sources/Swoctokit/Swoctokit.swift @@ -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 { @@ -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) } } diff --git a/Sources/Swoctokit/SwoctokitWebhookClient.swift b/Sources/Swoctokit/SwoctokitWebhookClient.swift index b075e2c..b34b49a 100644 --- a/Sources/Swoctokit/SwoctokitWebhookClient.swift +++ b/Sources/Swoctokit/SwoctokitWebhookClient.swift @@ -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 @@ -51,6 +58,10 @@ public class SwoctokitWebhookClient { commitCommentEventListeners.append(listener) } + public func addIssueCommentEventListener(_ listener: IssueCommentEventListener) { + issueCommentEventListeners.append(listener) + } + } extension SwoctokitWebhookClient: WebhookControllerDelegate { @@ -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 } @@ -74,4 +87,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate { commitCommentEventListeners.forEach { $0.commitCommentEventReceived(event) } } + func isuueCommentEventReceived(_ event: IssueCommentEvent) { + issueCommentEventListeners.forEach { $0.issueCommentEventReceived(event) } + } + } diff --git a/Sources/Swoctokit/Webhook/EventTypes.swift b/Sources/Swoctokit/Webhook/EventTypes.swift index 2474a67..54c930f 100644 --- a/Sources/Swoctokit/Webhook/EventTypes.swift +++ b/Sources/Swoctokit/Webhook/EventTypes.swift @@ -14,4 +14,5 @@ import Foundation public enum EventType: String { case pullRequest = "pull_request" case commitComment = "commit_comment" + case issueComment = "issue_comment" } diff --git a/Sources/Swoctokit/Webhook/IssueCommentEvent.swift b/Sources/Swoctokit/Webhook/IssueCommentEvent.swift new file mode 100644 index 0000000..8385c2b --- /dev/null +++ b/Sources/Swoctokit/Webhook/IssueCommentEvent.swift @@ -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 + +} diff --git a/Sources/Swoctokit/Webhook/WebhookController.swift b/Sources/Swoctokit/Webhook/WebhookController.swift index 4dce55e..2beb3b1 100644 --- a/Sources/Swoctokit/Webhook/WebhookController.swift +++ b/Sources/Swoctokit/Webhook/WebhookController.swift @@ -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