Skip to content

Commit

Permalink
Extract gitea webhook payload and recreate rubocop todos
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas authored and eduardoj committed Sep 27, 2022
1 parent b9f8cc4 commit 981aaa9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
32 changes: 15 additions & 17 deletions src/api/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100`
# on 2022-09-02 13:10:04 UTC using RuboCop version 1.36.0.
# on 2022-09-26 12:53:02 UTC using RuboCop version 1.36.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -131,7 +131,7 @@ Lint/UselessMethodDefinition:
- 'app/controllers/webui/requests/submissions_controller.rb'
- 'test/test_helper.rb'

# Offense count: 853
# Offense count: 852
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 243
Expand All @@ -150,7 +150,7 @@ Metrics/BlockNesting:
- 'lib/xpath_engine.rb'
- 'test/node_matcher.rb'

# Offense count: 80
# Offense count: 81
# Configuration parameters: CountComments, Max, CountAsOne.
Metrics/ClassLength:
Exclude:
Expand Down Expand Up @@ -202,6 +202,7 @@ Metrics/ClassLength:
- 'app/models/user_ldap_strategy.rb'
- 'app/models/workflow.rb'
- 'app/models/workflow/step.rb'
- 'app/services/trigger_controller_service/scm_extractor.rb'
- 'app/services/user_service/involved.rb'
- 'lib/xpath_engine.rb'
- 'test/functional/attributes_test.rb'
Expand Down Expand Up @@ -235,7 +236,7 @@ Metrics/ClassLength:
- 'test/unit/publish_flag_test.rb'
- 'test/unit/user_test.rb'

# Offense count: 192
# Offense count: 191
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, Max.
Metrics/CyclomaticComplexity:
Exclude:
Expand Down Expand Up @@ -313,7 +314,6 @@ Metrics/CyclomaticComplexity:
- 'app/models/user_ldap_strategy.rb'
- 'app/policies/attrib_policy.rb'
- 'app/policies/comment_policy.rb'
- 'app/services/package_controller_service/url_generator.rb'
- 'lib/memory_debugger.rb'
- 'lib/xpath_engine.rb'
- 'script/import_database.rb'
Expand Down Expand Up @@ -638,14 +638,13 @@ Performance/StringInclude:
- 'test/functional/webui/spider_test.rb'
- 'test/functional/zzz_post_consistency_test.rb'

# Offense count: 58
# Offense count: 57
RSpec/AnyInstance:
Exclude:
- 'spec/controllers/staging/excluded_requests_controller_spec.rb'
- 'spec/controllers/status_project_controller_spec.rb'
- 'spec/controllers/webui/architectures_controller_spec.rb'
- 'spec/controllers/webui/attribute_controller_spec.rb'
- 'spec/controllers/webui/package_controller/binaries.spec'
- 'spec/controllers/webui/package_controller_spec.rb'
- 'spec/controllers/webui/patchinfo_controller_spec.rb'
- 'spec/controllers/webui/project_controller_spec.rb'
Expand Down Expand Up @@ -675,13 +674,13 @@ RSpec/Capybara/SpecificMatcher:
- 'spec/features/webui/users/user_home_page_spec.rb'
- 'spec/support/shared_examples/features/bootstrap_user_tab.rb'

# Offense count: 1163
# Offense count: 1154
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Enabled: false

# Offense count: 461
# Offense count: 460
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: SkipBlocks, EnforcedStyle.
# SupportedStyles: described_class, explicit
Expand Down Expand Up @@ -784,7 +783,7 @@ RSpec/MessageSpies:
- 'spec/controllers/webui/apidocs_controller_spec.rb'
- 'spec/models/kiwi/image_spec.rb'

# Offense count: 1606
# Offense count: 1608
# Configuration parameters: AllowSubject.
RSpec/MultipleMemoizedHelpers:
Max: 28
Expand Down Expand Up @@ -877,7 +876,7 @@ Rails/CreateTableWithTimestamps:
Exclude:
- 'db/migrate/20171030143054_create_kiwi_preference_types.rb'

# Offense count: 71
# Offense count: 68
# Configuration parameters: EnforcedStyle.
# SupportedStyles: slashes, arguments
Rails/FilePath:
Expand All @@ -890,7 +889,6 @@ Rails/FilePath:
- 'app/lib/backend/test.rb'
- 'app/lib/backend/test/tasks.rb'
- 'config/environment.rb'
- 'config/initializers/git.rb'
- 'config/initializers/secret_token.rb'
- 'config/initializers/tempdir.rb'
- 'lib/tasks/extract.rake'
Expand Down Expand Up @@ -1102,7 +1100,7 @@ Style/ConditionalAssignment:
Exclude:
- 'app/models/bs_request_action_submit.rb'

# Offense count: 799
# Offense count: 797
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false
Expand All @@ -1114,7 +1112,7 @@ Style/ExplicitBlockArgument:
- 'app/lib/backend/connection.rb'
- 'app/models/project.rb'

# Offense count: 1637
# Offense count: 1635
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand Down Expand Up @@ -1244,21 +1242,21 @@ Style/SoleNestedConditional:
Style/StringConcatenation:
Enabled: false

# Offense count: 362
# Offense count: 360
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: MinSize.
# SupportedStyles: percent, brackets
Style/SymbolArray:
EnforcedStyle: brackets

# Offense count: 282
# Offense count: 276
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyle, MinSize, WordRegex.
# SupportedStyles: percent, brackets
Style/WordArray:
Enabled: false

# Offense count: 2241
# Offense count: 2244
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# URISchemes: http, https
Expand Down
57 changes: 56 additions & 1 deletion src/api/app/services/trigger_controller_service/scm_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO: Extract SCM-specific code to separate classes. This class is too big for GitHub and GitLab.
module TriggerControllerService
# NOTE: this class is coupled to GitHub pull requests events and GitLab merge requests events.
class SCMExtractor
Expand All @@ -16,6 +15,8 @@ def call
SCMWebhook.new(payload: github_extractor_payload)
when 'gitlab'
SCMWebhook.new(payload: gitlab_extractor_payload)
when 'gitea'
SCMWebhook.new(payload: gitea_extractor_payload)
end
end

Expand Down Expand Up @@ -79,6 +80,31 @@ def gitlab_extractor_payload
payload
end

def gitea_extractor_payload
http_url = @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'
payload.merge!({ commit_sha: @payload.dig(:pull_request, :head, :sha),
pr_number: @payload[:number],
source_branch: @payload.dig(:pull_request, :head, :ref),
target_branch: @payload.dig(:pull_request, :base, :ref),
action: @payload[:action],
source_repository_full_name: @payload.dig(:pull_request, :head, :repo, :full_name),
target_repository_full_name: @payload.dig(:pull_request, :base, :repo, :full_name) })
when 'push' # GitHub doesn't have different push events for commits and tags
gitea_payload_push(payload)
end
payload
end

def github_api_endpoint
sender_url = @payload.dig(:sender, :url)
return unless sender_url
Expand All @@ -91,6 +117,12 @@ def github_api_endpoint
end
end

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

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

def gitlab_api_endpoint(http_url)
return unless http_url

Expand Down Expand Up @@ -135,5 +167,28 @@ def gitlab_payload_tag(payload)
commit_sha: @payload[:after]
})
end

def gitea_payload_push(payload)
payload_ref = @payload.fetch(:ref, '')
payload.merge!({
# We need this for Workflow::Step#branch_request_content_github
source_repository_full_name: @payload.dig(:repository, :full_name),
# We need this for SCMStatusReporter#call
target_repository_full_name: @payload.dig(:repository, :full_name),
ref: payload_ref,
# We need this for Workflow::Step#branch_request_content_{github,gitlab}
commit_sha: @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: @payload[:after] })
end
end
end

0 comments on commit 981aaa9

Please sign in to comment.