diff --git a/src/api/app/components/workflow_run_row_component.rb b/src/api/app/components/workflow_run_row_component.rb index 6d0cd92560a..53192733f92 100644 --- a/src/api/app/components/workflow_run_row_component.rb +++ b/src/api/app/components/workflow_run_row_component.rb @@ -13,13 +13,14 @@ class WorkflowRunRowComponent < ApplicationComponent 'Push Hook' => ['commits', 0, 'url'] }.freeze - attr_reader :workflow_run, :status + attr_reader :workflow_run, :status, :hook_event def initialize(workflow_run:) super @workflow_run = workflow_run @status = workflow_run.status + @hook_event = workflow_run.hook_event end def hook_action @@ -27,11 +28,6 @@ def hook_action return payload.dig('object_attributes', 'action') if merge_request_with_allowed_action end - def hook_event - parsed_request_headers['HTTP_X_GITHUB_EVENT'] || - parsed_request_headers['HTTP_X_GITLAB_EVENT'] - end - def repository_name payload.dig('repository', 'full_name') || # For GitHub payload.dig('repository', 'name') # For GitLab @@ -43,16 +39,17 @@ def repository_url end def event_source_name - path = SOURCE_NAME_PAYLOAD_MAPPING[hook_event] + path = SOURCE_NAME_PAYLOAD_MAPPING[@hook_event] payload.dig(*path) if path end def event_source_url - payload.dig(*SOURCE_URL_PAYLOAD_MAPPING[hook_event]) + mapped_source_url = SOURCE_URL_PAYLOAD_MAPPING[@hook_event] + payload.dig(*mapped_source_url) if mapped_source_url end def formatted_event_source_name - case hook_event + case @hook_event when 'pull_request', 'Merge Request Hook' "##{event_source_name}" else @@ -85,13 +82,6 @@ def status_icon private - def parsed_request_headers - workflow_run.request_headers.split("\n").each_with_object({}) do |h, headers| - k, v = h.split(':') - headers[k] = v.strip - end - end - def payload @payload ||= JSON.parse(workflow_run.request_payload) rescue JSON::ParserError @@ -99,12 +89,12 @@ def payload end def pull_request_with_allowed_action - hook_event == 'pull_request' && + @hook_event == 'pull_request' && ScmWebhookEventValidator::ALLOWED_PULL_REQUEST_ACTIONS.include?(payload['action']) end def merge_request_with_allowed_action - hook_event == 'Merge Request Hook' && + @hook_event == 'Merge Request Hook' && ScmWebhookEventValidator::ALLOWED_MERGE_REQUEST_ACTIONS.include?(payload.dig('object_attributes', 'action')) end end diff --git a/src/api/app/models/workflow_run.rb b/src/api/app/models/workflow_run.rb index 7e2463f2831..b70fd8c02b2 100644 --- a/src/api/app/models/workflow_run.rb +++ b/src/api/app/models/workflow_run.rb @@ -16,6 +16,20 @@ class WorkflowRun < ApplicationRecord def update_to_fail(message) update(response_body: message, status: 'fail') end + + def hook_event + parsed_request_headers['HTTP_X_GITHUB_EVENT'] || + parsed_request_headers['HTTP_X_GITLAB_EVENT'] + end + + private + + def parsed_request_headers + request_headers.split("\n").each_with_object({}) do |h, headers| + k, v = h.split(':') + headers[k] = v.strip + end + end end # == Schema Information