diff --git a/src/api/app/models/github.rb b/src/api/app/models/github.rb new file mode 100644 index 00000000000..ca46282d08f --- /dev/null +++ b/src/api/app/models/github.rb @@ -0,0 +1,30 @@ +class Github + attr_reader :event, :webhook_payload + + def initialize(event, webhook_payload) + @event = event + @webhook_payload = webhook_payload + end + + def default_payload + { + scm: 'github', + event: event, + api_endpoint: api_endpoint + } + end + + private + + def api_endpoint + sender_url = webhook_payload.dig(:sender, :url) + return unless sender_url + + host = URI.parse(sender_url).host + if host.start_with?('api.github.com') + "https://#{host}" + else + "https://#{host}/api/v3/" + end + end +end diff --git a/src/api/app/models/github/pull_request.rb b/src/api/app/models/github/pull_request.rb new file mode 100644 index 00000000000..c6c87367334 --- /dev/null +++ b/src/api/app/models/github/pull_request.rb @@ -0,0 +1,13 @@ +class Github::PullRequest < Github + def payload + default_payload.merge( + commit_sha: webhook_payload.dig(:pull_request, :head, :sha), + pr_number: webhook_payload[:number], + source_branch: webhook_payload.dig(:pull_request, :head, :ref), + target_branch: webhook_payload.dig(:pull_request, :base, :ref), + action: webhook_payload[:action], + source_repository_full_name: webhook_payload.dig(:pull_request, :head, :repo, :full_name), + target_repository_full_name: webhook_payload.dig(:pull_request, :base, :repo, :full_name) + ) + end +end diff --git a/src/api/app/models/github_payload_extractor.rb b/src/api/app/models/github_payload_extractor.rb index 4e8b69ab00b..1a1dd4ab514 100644 --- a/src/api/app/models/github_payload_extractor.rb +++ b/src/api/app/models/github_payload_extractor.rb @@ -16,15 +16,7 @@ def payload case event when 'pull_request' - payload.merge!({ - commit_sha: webhook_payload.dig(:pull_request, :head, :sha), - pr_number: webhook_payload[:number], - source_branch: webhook_payload.dig(:pull_request, :head, :ref), - target_branch: webhook_payload.dig(:pull_request, :base, :ref), - action: webhook_payload[:action], - source_repository_full_name: webhook_payload.dig(:pull_request, :head, :repo, :full_name), - target_repository_full_name: webhook_payload.dig(:pull_request, :base, :repo, :full_name) - }) + return Github::PullRequest.new(event, webhook_payload).payload when 'push' # GitHub doesn't have different push events for commits and tags github_payload_push(payload) end