Skip to content

Commit

Permalink
Extract a gitea push event class
Browse files Browse the repository at this point in the history
  • Loading branch information
danidoni committed Feb 7, 2023
1 parent 99812ec commit bd0290a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 75 deletions.
30 changes: 30 additions & 0 deletions src/api/app/models/gitea_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class GiteaPayload
attr_reader :event, :http_url, :webhook_payload

def initialize(event, webhook_payload)
@event = event
@webhook_payload = webhook_payload
@http_url = webhook_payload.dig(:repository, :clone_url)
end

def default_payload
{
scm: 'gitea',
event: event,
api_endpoint: api_endpoint,
http_url: http_url
}
end

def payload
raise AbstractMethodCalled
end

private

def api_endpoint
url = URI.parse(http_url)

"#{url.scheme}://#{url.host}"
end
end
39 changes: 8 additions & 31 deletions src/api/app/models/gitea_payload/pull_request.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
class GiteaPayload::PullRequest
attr_reader :event, :webhook_payload

def initialize(event, webhook_payload)
@event = event
@webhook_payload = webhook_payload
end

class GiteaPayload::PullRequest < GiteaPayload
def payload
http_url = webhook_payload.dig(:repository, :clone_url)
payload = {
scm: 'gitea',
event: event,
api_endpoint: gitea_api_endpoint(http_url),
http_url: http_url
}

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

private

def gitea_api_endpoint(http_url)
url = URI.parse(http_url)

"#{url.scheme}://#{url.host}"
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
25 changes: 25 additions & 0 deletions src/api/app/models/gitea_payload/push.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class GiteaPayload::Push < GiteaPayload
def payload
payload_ref = webhook_payload.fetch(:ref, '')
payload = default_payload.merge(
# 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/', '')
)

return payload unless payload_ref.start_with?('refs/tags/')

# 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))
end
end
46 changes: 2 additions & 44 deletions src/api/app/models/gitea_payload_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,11 @@ def initialize(event, webhook_payload)
end

def payload
http_url = webhook_payload.dig(:repository, :clone_url)

payload = {
scm: 'gitea',
event: event,
api_endpoint: gitea_api_endpoint(http_url),
http_url: http_url
}

case event
when 'pull_request'
return GiteaPayload::PullRequest.new(event, webhook_payload).payload
GiteaPayload::PullRequest.new(event, webhook_payload).payload
when 'push' # GitHub doesn't have different push events for commits and tags
gitea_payload_push(payload)
GiteaPayload::Push.new(event, webhook_payload).payload
end
payload
end

private

def gitea_api_endpoint(http_url)
url = URI.parse(http_url)

"#{url.scheme}://#{url.host}"
end

def gitea_payload_push(payload)
payload_ref = webhook_payload.fetch(:ref, '')
payload.merge!({
# 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/', '')
})

return unless payload_ref.start_with?('refs/tags/')

# 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) })
end
end

0 comments on commit bd0290a

Please sign in to comment.