Skip to content

Commit

Permalink
Handle gitea push and pull_request events
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas authored and eduardoj committed Sep 27, 2022
1 parent e3ba2cb commit b9f8cc4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/api/app/controllers/trigger_workflow_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ def create

def set_scm_event
@gitlab_event = request.env['HTTP_X_GITLAB_EVENT']
@github_event = request.env['HTTP_X_GITHUB_EVENT']
# Gitea contains the Github headers as well, so we have to check that the Gitea ones are
# not present for Github
@github_event = request.env['HTTP_X_GITHUB_EVENT'] unless request.env['HTTP_X_GITEA_EVENT']
@gitea_event = request.env['HTTP_X_GITEA_EVENT']
end

def validate_scm_event
return if @gitlab_event.present? || @github_event.present?
return if @gitlab_event.present? || @github_event.present? || @gitea_event.present?

@workflow_run.update_as_failed(
render_error(
status: 400,
errorcode: 'bad_request',
message: 'Only GitHub and GitLab are supported. Could not find the required HTTP request headers X-GitHub-Event or X-Gitlab-Event.'
message: 'Only GitHub, GitLab and Gitea are supported. Could not find the required HTTP request headers X-GitHub-Event, X-Gitlab-Event or X-Gitea-Event.'
)
)
end
Expand All @@ -48,11 +51,13 @@ def scm
'gitlab'
elsif @github_event
'github'
elsif @gitea_event
'gitea'
end
end

def event
@github_event || @gitlab_event
@github_event || @gitlab_event || @gitea_event
end

def payload
Expand Down
38 changes: 29 additions & 9 deletions src/api/app/models/scm_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SCMWebhook

validates_with SCMWebhookEventValidator

ALLOWED_PULL_REQUEST_ACTIONS = ['closed', 'opened', 'reopened', 'synchronize'].freeze
ALLOWED_PULL_REQUEST_ACTIONS = ['closed', 'opened', 'reopened', 'synchronize', 'synchronized'].freeze
ALLOWED_MERGE_REQUEST_ACTIONS = ['close', 'merge', 'open', 'reopen', 'update'].freeze

def initialize(attributes = {})
Expand All @@ -20,38 +20,42 @@ def initialize(attributes = {})

def new_pull_request?
(github_pull_request? && @payload[:action] == 'opened') ||
(gitlab_merge_request? && @payload[:action] == 'open')
(gitlab_merge_request? && @payload[:action] == 'open') ||
(gitea_pull_request? && @payload[:action] == 'opened')
end

def updated_pull_request?
(github_pull_request? && @payload[:action] == 'synchronize') ||
(gitlab_merge_request? && @payload[:action] == 'update')
(gitlab_merge_request? && @payload[:action] == 'update') ||
(gitea_pull_request? && @payload[:action] == 'synchronized')
end

def closed_merged_pull_request?
(github_pull_request? && @payload[:action] == 'closed') ||
(gitlab_merge_request? && ['close', 'merge'].include?(@payload[:action]))
(gitlab_merge_request? && ['close', 'merge'].include?(@payload[:action])) ||
(gitea_pull_request? && @payload[:action] == 'closed')
end

def reopened_pull_request?
(github_pull_request? && @payload[:action] == 'reopened') ||
(gitlab_merge_request? && @payload[:action] == 'reopen')
(gitlab_merge_request? && @payload[:action] == 'reopen') ||
(gitea_pull_request? && @payload[:action] == 'reopened')
end

def push_event?
github_push_event? || gitlab_push_event?
github_push_event? || gitlab_push_event? || gitea_push_event?
end

def tag_push_event?
github_tag_push_event? || gitlab_tag_push_event?
github_tag_push_event? || gitlab_tag_push_event? || gitea_tag_push_event?
end

def pull_request_event?
github_pull_request? || gitlab_merge_request?
github_pull_request? || gitlab_merge_request? || gitea_pull_request?
end

def ignored_pull_request_action?
ignored_github_pull_request_action? || ignored_gitlab_merge_request_action?
ignored_github_pull_request_action? || ignored_gitlab_merge_request_action? || ignored_gitea_pull_request_action?
end

private
Expand All @@ -64,6 +68,10 @@ def gitlab_push_event?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Push Hook'
end

def gitea_push_event?
@payload[:scm] == 'gitea' && @payload[:event] == 'push' && @payload.fetch(:ref, '').start_with?('refs/heads/')
end

def github_tag_push_event?
@payload[:scm] == 'github' && @payload[:event] == 'push' && @payload.fetch(:ref, '').starts_with?('refs/tags/')
end
Expand All @@ -72,6 +80,10 @@ def gitlab_tag_push_event?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Tag Push Hook'
end

def gitea_tag_push_event?
@payload[:scm] == 'gitea' && @payload[:event] == 'push' && @payload.fetch(:ref, '').starts_with?('refs/tags/')
end

def github_pull_request?
@payload[:scm] == 'github' && @payload[:event] == 'pull_request'
end
Expand All @@ -80,11 +92,19 @@ def gitlab_merge_request?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Merge Request Hook'
end

def gitea_pull_request?
@payload[:scm] == 'gitea' && @payload[:event] == 'pull_request'
end

def ignored_github_pull_request_action?
github_pull_request? && ALLOWED_PULL_REQUEST_ACTIONS.exclude?(@payload[:action])
end

def ignored_gitlab_merge_request_action?
gitlab_merge_request? && ALLOWED_MERGE_REQUEST_ACTIONS.exclude?(@payload[:action])
end

def ignored_gitea_pull_request_action?
gitea_pull_request? && ALLOWED_PULL_REQUEST_ACTIONS.exclude?(@payload[:action])
end
end

0 comments on commit b9f8cc4

Please sign in to comment.