Skip to content

Commit

Permalink
Remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
danidoni committed Feb 7, 2023
1 parent 341527c commit 6ddcff3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 54 deletions.
24 changes: 0 additions & 24 deletions src/api/app/models/gitea_payload.rb

This file was deleted.

25 changes: 16 additions & 9 deletions src/api/app/models/gitea_payload/pull_request.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
class GiteaPayload::PullRequest < GiteaPayload
class GiteaPayload::PullRequest < GithubPayload::PullRequest
def payload
default_payload.merge(event: 'pull_request',
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))
super.merge(scm: 'gitea',
http_url: http_url,
api_endpoint: api_endpoint)
end

private

def http_url
webhook_payload.dig(:repository, :clone_url)
end

def api_endpoint
url = URI.parse(http_url)

"#{url.scheme}://#{url.host}"
end
end
35 changes: 14 additions & 21 deletions src/api/app/models/gitea_payload/push.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
class GiteaPayload::Push < GiteaPayload
class GiteaPayload::Push < GithubPayload::Push
def payload
payload_ref = webhook_payload.fetch(:ref, '')
payload = default_payload.merge(
event: 'push',
# We need this for Workflow::Step#branch_request_content_github
source_repository_full_name: webhook_payload.dig(:repository, :full_name),
# We need this for SCMStatusReporter#call
target_repository_full_name: webhook_payload.dig(:repository, :full_name),
ref: payload_ref,
# We need this for Workflow::Step#branch_request_content_{github,gitlab}
commit_sha: webhook_payload[:after],
# We need this for Workflows::YAMLDownloader#download_url
# when the push event is for commits, we get the branch name from ref.
target_branch: payload_ref.sub('refs/heads/', '')
)
super.merge(scm: 'gitea',
http_url: http_url,
api_endpoint: api_endpoint)
end

private

def http_url
webhook_payload.dig(:repository, :clone_url)
end

return payload unless payload_ref.start_with?('refs/tags/')
def api_endpoint
url = URI.parse(http_url)

# We need this for Workflow::Step#target_package_name
# 'target_branch' will contain a commit SHA
payload.merge(tag_name: payload_ref.sub('refs/tags/', ''),
target_branch: webhook_payload.dig(:head_commit, :id),
commit_sha: webhook_payload.dig(:head_commit, :id))
"#{url.scheme}://#{url.host}"
end
end

0 comments on commit 6ddcff3

Please sign in to comment.