Skip to content

Commit

Permalink
Align SCMWebhook payload data
Browse files Browse the repository at this point in the history
Gitlab payload now also contains the source/target repository name
with the same key names at Github/Gitea.
  • Loading branch information
hennevogel committed Mar 13, 2024
1 parent 17be7c6 commit 47863a1
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/api/app/models/gitlab_payload/merge_request.rb
Expand Up @@ -8,6 +8,8 @@ def payload
target_branch: webhook_payload.dig(:object_attributes, :target_branch),
action: webhook_payload.dig(:object_attributes, :action),
project_id: webhook_payload.dig(:object_attributes, :source_project_id),
path_with_namespace: webhook_payload.dig(:project, :path_with_namespace))
path_with_namespace: webhook_payload.dig(:project, :path_with_namespace),
target_repository_full_name: webhook_payload.dig(:project, :path_with_namespace),
source_repository_full_name: webhook_payload.dig(:source, :path_with_namespace))
end
end
4 changes: 4 additions & 0 deletions src/api/app/models/gitlab_payload/push.rb
Expand Up @@ -7,6 +7,10 @@ def payload
target_branch: webhook_payload[:ref].sub('refs/heads/', ''),
# We need this for Workflows::YAMLDownloader#download_url
path_with_namespace: webhook_payload.dig(:project, :path_with_namespace),
# We need this for Workflow::Step#branch_request_content_github
source_repository_full_name: webhook_payload.dig(:project, :path_with_namespace),
# We need this for SCMStatusReporter#call
target_repository_full_name: webhook_payload.dig(:project, :path_with_namespace),
# We need this for SCMStatusReporter#call
project_id: webhook_payload[:project_id],
# We need this for SCMWebhookEventValidator#valid_push_event
Expand Down
2 changes: 2 additions & 0 deletions src/api/app/models/gitlab_payload/tag_push.rb
Expand Up @@ -9,6 +9,8 @@ def payload
target_branch: webhook_payload[:after],
# We need this for Workflows::YAMLDownloader#download_url
path_with_namespace: webhook_payload.dig(:project, :path_with_namespace),
source_repository_full_name: webhook_payload.dig(:project, :path_with_namespace),
target_repository_full_name: webhook_payload.dig(:project, :path_with_namespace),
# We need this for SCMWebhookEventValidator#valid_push_event
ref: webhook_payload[:ref],
# We need this for Workflow::Step#branch_request_content_{github,gitlab}
Expand Down
6 changes: 1 addition & 5 deletions src/api/app/models/workflow/step.rb
Expand Up @@ -26,11 +26,7 @@ def target_project_name

return nil unless scm_webhook.pull_request_event?

pr_subproject_name = if %w[github gitea].include?(scm_webhook.payload[:scm])
scm_webhook.payload[:target_repository_full_name]&.tr('/', ':')
else
scm_webhook.payload[:path_with_namespace]&.tr('/', ':')
end
pr_subproject_name = scm_webhook.payload[:target_repository_full_name]&.tr('/', ':')

"#{target_project_base_name}:#{pr_subproject_name}:PR-#{scm_webhook.payload[:pr_number]}"
end
Expand Down
7 changes: 4 additions & 3 deletions src/api/app/models/workflow/step/branch_package_step.rb
Expand Up @@ -105,6 +105,10 @@ def create_target_project
project.store
end

# FIXME: Just because the tar_scm service accepts different formats for the _branch_request file, we don't need to have code
# to generate those different formats. We can just generate one format, should be the gitlab format because it provides more
# flexibility regarding the URL.
# https://github.com/openSUSE/obs-service-tar_scm/blob/2319f50e741e058ad599a6890ac5c710112d5e48/TarSCM/tasks.py#L145
def branch_request_content
case scm_webhook.payload[:scm]
when 'github'
Expand All @@ -118,9 +122,6 @@ def branch_request_content

def branch_request_content_github
{
# TODO: change to scm_webhook.payload[:action]
# when check_for_branch_request method in obs-service-tar_scm accepts other actions than 'opened'
# https://github.com/openSUSE/obs-service-tar_scm/blob/2319f50e741e058ad599a6890ac5c710112d5e48/TarSCM/tasks.py#L145
action: 'opened',
pull_request: {
head: {
Expand Down
6 changes: 1 addition & 5 deletions src/api/app/models/workflow/step/set_flags.rb
Expand Up @@ -59,11 +59,7 @@ def target_project_name(project_name:)

return nil unless scm_webhook.pull_request_event?

pr_subproject_name = if scm_webhook.payload[:scm] == 'github'
scm_webhook.payload[:target_repository_full_name]&.tr('/', ':')
else
scm_webhook.payload[:path_with_namespace]&.tr('/', ':')
end
pr_subproject_name = scm_webhook.payload[:target_repository_full_name]&.tr('/', ':')

"#{project_name}:#{pr_subproject_name}:PR-#{scm_webhook.payload[:pr_number]}"
end
Expand Down
3 changes: 1 addition & 2 deletions src/api/app/services/workflows/yaml_to_workflows_service.rb
Expand Up @@ -35,8 +35,7 @@ def create_workflows
end

def parse_workflow_configuration(workflow_configuration)
target_repository_full_name = @scm_webhook.payload.values_at(:target_repository_full_name, :path_with_namespace).compact.first
scm_organization_name, scm_repository_name = target_repository_full_name.split('/')
scm_organization_name, scm_repository_name = @scm_webhook.payload.fetch(:target_repository_full_name).split('/')

# The PR number is only present in webhook events for pull requests, so we have a default value in case someone doesn't use
# this correctly. Here, we cannot inform users about this since we're processing the whole workflows file
Expand Down

0 comments on commit 47863a1

Please sign in to comment.