From 7975653b98c9c1c84dff7b66d43b2795ca67ee1d Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Wed, 14 Nov 2018 12:43:27 +0100 Subject: [PATCH] add commitcomment event --- .../Swoctokit/SwoctokitWebhookClient.swift | 18 +++++++++++- .../Webhook/CommitCommentEvent.swift | 29 +++++++++++++++++++ Sources/Swoctokit/Webhook/EventTypes.swift | 1 + .../Swoctokit/Webhook/WebhookController.swift | 4 +++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 Sources/Swoctokit/Webhook/CommitCommentEvent.swift diff --git a/Sources/Swoctokit/SwoctokitWebhookClient.swift b/Sources/Swoctokit/SwoctokitWebhookClient.swift index 2931c67..b075e2c 100644 --- a/Sources/Swoctokit/SwoctokitWebhookClient.swift +++ b/Sources/Swoctokit/SwoctokitWebhookClient.swift @@ -17,12 +17,18 @@ public protocol PullRequestEventListener: class { } +public protocol CommitCommentEventListener: class { + + func commitCommentEventReceived(_ commitCommentEvent: CommitCommentEvent) + +} + public class SwoctokitWebhookClient { private let application: Application private var pullRequestEventListeners = [PullRequestEventListener]() - + private var commitCommentEventListeners = [CommitCommentEventListener]() public init(_ application: Application) throws { self.application = application @@ -41,6 +47,10 @@ public class SwoctokitWebhookClient { pullRequestEventListeners.append(listener) } + public func addCommitCommentEventListener(_ listener: CommitCommentEventListener) { + commitCommentEventListeners.append(listener) + } + } extension SwoctokitWebhookClient: WebhookControllerDelegate { @@ -49,6 +59,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate { switch event { case let event as PullRequestEvent: pullRequestEventReceived(event) + case let event as CommitCommentEvent: + commitCommentEventReceived(event) default: break } @@ -58,4 +70,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate { pullRequestEventListeners.forEach { $0.pullRequestEventReceived(event) } } + func commitCommentEventReceived(_ event: CommitCommentEvent) { + commitCommentEventListeners.forEach { $0.commitCommentEventReceived(event) } + } + } diff --git a/Sources/Swoctokit/Webhook/CommitCommentEvent.swift b/Sources/Swoctokit/Webhook/CommitCommentEvent.swift new file mode 100644 index 0000000..42ae5b6 --- /dev/null +++ b/Sources/Swoctokit/Webhook/CommitCommentEvent.swift @@ -0,0 +1,29 @@ +// +// CommitCommentEvent.swift +// Swoctokit +// +// Created by Franz Busch on 14.11.18. +// + +public struct CommitCommentEvent: Decodable, WebhookEvent { + + public let action: String + public let comment: CommitComment + +} + +public struct CommitComment: Decodable { + + public let id: Int + public let url: String + public let body: String + public let commitId: String + + private enum CodingKeys: String, CodingKey { + case id + case url + case body + case commitId = "commit_id" + } + +} diff --git a/Sources/Swoctokit/Webhook/EventTypes.swift b/Sources/Swoctokit/Webhook/EventTypes.swift index aec1fcb..2474a67 100644 --- a/Sources/Swoctokit/Webhook/EventTypes.swift +++ b/Sources/Swoctokit/Webhook/EventTypes.swift @@ -13,4 +13,5 @@ import Foundation public enum EventType: String { case pullRequest = "pull_request" + case commitComment = "commit_comment" } diff --git a/Sources/Swoctokit/Webhook/WebhookController.swift b/Sources/Swoctokit/Webhook/WebhookController.swift index 8123107..4dce55e 100644 --- a/Sources/Swoctokit/Webhook/WebhookController.swift +++ b/Sources/Swoctokit/Webhook/WebhookController.swift @@ -46,6 +46,10 @@ final class WebhookController: RouteCollection { if let pullRequestEvent = try? req.content.syncDecode(PullRequestEvent.self) { delegate?.didReceive(event: pullRequestEvent) } + case .commitComment: + if let commitCommentEvent = try? req.content.syncDecode(CommitCommentEvent.self) { + delegate?.didReceive(event: commitCommentEvent) + } } return .ok