Skip to content

Commit

Permalink
Move hook_event logic from workflow_run_row view component to model
Browse files Browse the repository at this point in the history
This logic can also be used in the workflow runs finder class and
better fits in the model itself than the view component.

Co-authored-by: Daniel Donisa <daniel.donisa@suse.com>
  • Loading branch information
krauselukas and danidoni committed Feb 4, 2022
1 parent d55a0f1 commit 56758f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/api/app/components/workflow_run_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@ 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
return payload['action'] if pull_request_with_allowed_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
Expand All @@ -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
Expand Down Expand Up @@ -85,26 +82,19 @@ 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
{ payload: 'unparseable' }
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
14 changes: 14 additions & 0 deletions src/api/app/models/workflow_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56758f6

Please sign in to comment.