-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11570 from dmarcoux/scm_webhook_model+validators
Create ScmWebhook model and reorganize validations for webhook events
- Loading branch information
Showing
21 changed files
with
416 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Contains the payload extracted from a SCM webhook and provides helper methods to know which webhook event we're dealing with | ||
class ScmWebhook | ||
include ActiveModel::Model | ||
|
||
attr_accessor :payload | ||
|
||
validates_with ScmWebhookEventValidator | ||
|
||
def initialize(attributes = {}) | ||
super | ||
# To safely navigate the hash and compare keys | ||
@payload = attributes[:payload].deep_symbolize_keys | ||
end | ||
|
||
def new_pull_request? | ||
(github_pull_request? && @payload[:action] == 'opened') || | ||
(gitlab_merge_request? && @payload[:action] == 'open') | ||
end | ||
|
||
def updated_pull_request? | ||
(github_pull_request? && @payload[:action] == 'synchronize') || | ||
(gitlab_merge_request? && @payload[:action] == 'update') | ||
end | ||
|
||
private | ||
|
||
def github_pull_request? | ||
@payload[:scm] == 'github' && @payload[:event] == 'pull_request' | ||
end | ||
|
||
def gitlab_merge_request? | ||
@payload[:scm] == 'gitlab' && @payload[:event] == 'Merge Request Hook' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class ScmWebhookEventValidator < ActiveModel::Validator | ||
ALLOWED_GITHUB_EVENTS = ['pull_request'].freeze | ||
ALLOWED_GITLAB_EVENTS = ['Merge Request Hook'].freeze | ||
|
||
ALLOWED_PULL_REQUEST_ACTIONS = ['opened', 'synchronize'].freeze | ||
ALLOWED_MERGE_REQUEST_ACTIONS = ['open', 'update'].freeze | ||
|
||
def validate(record) | ||
@record = record | ||
|
||
return if valid_github_event? || valid_gitlab_event? | ||
|
||
# FIXME: This error message is wrong when the SCM isn't supported. This is an edge case, somebody most probably fiddled with the payload. | ||
@record.errors.add(:base, 'Event not supported.') | ||
end | ||
|
||
private | ||
|
||
def valid_github_event? | ||
return false unless @record.payload[:scm] == 'github' | ||
return false unless ALLOWED_GITHUB_EVENTS.include?(@record.payload[:event]) | ||
|
||
case @record.payload[:event] | ||
when 'pull_request' | ||
return true if ALLOWED_PULL_REQUEST_ACTIONS.include?(@record.payload[:action]) | ||
|
||
@record.errors.add(:base, 'Pull request action not supported.') | ||
else | ||
true | ||
end | ||
end | ||
|
||
def valid_gitlab_event? | ||
return false unless @record.payload[:scm] == 'gitlab' | ||
return false unless ALLOWED_GITLAB_EVENTS.include?(@record.payload[:event]) | ||
|
||
case @record.payload[:event] | ||
when 'Merge Request Hook' | ||
return true if ALLOWED_MERGE_REQUEST_ACTIONS.include?(@record.payload[:action]) | ||
|
||
@record.errors.add(:base, 'Merge request action not supported.') | ||
else | ||
true | ||
end | ||
end | ||
end |
Oops, something went wrong.