Skip to content

Commit

Permalink
add commitcomment event
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Nov 14, 2018
1 parent 91e9724 commit 7975653
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Sources/Swoctokit/SwoctokitWebhookClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +47,10 @@ public class SwoctokitWebhookClient {
pullRequestEventListeners.append(listener)
}

public func addCommitCommentEventListener(_ listener: CommitCommentEventListener) {
commitCommentEventListeners.append(listener)
}

}

extension SwoctokitWebhookClient: WebhookControllerDelegate {
Expand All @@ -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
}
Expand All @@ -58,4 +70,8 @@ extension SwoctokitWebhookClient: WebhookControllerDelegate {
pullRequestEventListeners.forEach { $0.pullRequestEventReceived(event) }
}

func commitCommentEventReceived(_ event: CommitCommentEvent) {
commitCommentEventListeners.forEach { $0.commitCommentEventReceived(event) }
}

}
29 changes: 29 additions & 0 deletions Sources/Swoctokit/Webhook/CommitCommentEvent.swift
Original file line number Diff line number Diff line change
@@ -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"
}

}
1 change: 1 addition & 0 deletions Sources/Swoctokit/Webhook/EventTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ import Foundation

public enum EventType: String {
case pullRequest = "pull_request"
case commitComment = "commit_comment"
}
4 changes: 4 additions & 0 deletions Sources/Swoctokit/Webhook/WebhookController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7975653

Please sign in to comment.