Skip to content

Commit

Permalink
Merge pull request #13111 from krauselukas/scm_integration/support_gitea
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas committed Sep 27, 2022
2 parents 30b029b + fa25016 commit 48b1002
Show file tree
Hide file tree
Showing 23 changed files with 664 additions and 82 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
2 changes: 2 additions & 0 deletions src/api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ gem 'octokit'
gem 'gitlab'
# Build reusable, testable & encapsulated view components in Ruby on Rails
gem 'view_component'
# Abstraction layer for HTTP requests in custom API wrapper
gem 'faraday'

# FIXME: Required by the mail gem
# See https://github.com/rails/rails/pull/44083
Expand Down
1 change: 1 addition & 0 deletions src/api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ DEPENDENCIES
down
factory_bot_rails
faker
faraday
flipper
flipper-active_record
flipper-ui
Expand Down
13 changes: 9 additions & 4 deletions src/api/app/controllers/trigger_workflow_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ def create

def set_scm_event
@gitlab_event = request.env['HTTP_X_GITLAB_EVENT']
@github_event = request.env['HTTP_X_GITHUB_EVENT']
# Gitea contains the Github headers as well, so we have to check that the Gitea ones are
# not present for Github
@github_event = request.env['HTTP_X_GITHUB_EVENT'] unless request.env['HTTP_X_GITEA_EVENT']
@gitea_event = request.env['HTTP_X_GITEA_EVENT']
end

def validate_scm_event
return if @gitlab_event.present? || @github_event.present?
return if @gitlab_event.present? || @github_event.present? || @gitea_event.present?

@workflow_run.update_as_failed(
render_error(
status: 400,
errorcode: 'bad_request',
message: 'Only GitHub and GitLab are supported. Could not find the required HTTP request headers X-GitHub-Event or X-Gitlab-Event.'
message: 'Only GitHub, GitLab and Gitea are supported. Could not find the required HTTP request headers X-GitHub-Event, X-Gitlab-Event or X-Gitea-Event.'
)
)
end
Expand All @@ -48,11 +51,13 @@ def scm
'gitlab'
elsif @github_event
'github'
elsif @gitea_event
'gitea'
end
end

def event
@github_event || @gitlab_event
@github_event || @gitlab_event || @gitea_event
end

def payload
Expand Down
69 changes: 69 additions & 0 deletions src/api/app/lib/gitea_api/v1/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module GiteaAPI
module V1
class Client
HTTP_OK_CODE = 200
HTTP_CREATED_CODE = 201
HTTP_BAD_REQUEST_CODE = 400
HTTP_UNAUTHORIZED_CODE = 401
HTTP_FORBIDDEN_CODE = 403
HTTP_NOT_FOUND_CODE = 404

GiteaApiError = Class.new(StandardError)
BadRequestError = Class.new(GiteaApiError)
UnauthorizedError = Class.new(GiteaApiError)
ForbiddenError = Class.new(GiteaApiError)
NotFoundError = Class.new(GiteaApiError)
ApiError = Class.new(GiteaApiError)

def initialize(api_endpoint:, token:)
@api_endpoint = api_endpoint + '/api/v1/'
@token = token
end

# owner: owner of the repository
# repo: name of the repository
# sha: sha of the commit
# https://try.gitea.io/api/swagger#/repository/repoCreateStatus
def create_commit_status(owner:, repo:, sha:, state:, **kwargs)
@response = client.post(
"repos/#{owner}/#{repo}/statuses/#{sha}",
{ state: state, context: kwargs[:context], description: kwargs[:description],
target_url: kwargs[:target_url] }
)
return @response.body if request_successful?

raise error_class, "HTTP Code: #{@response.status}, response: #{@response.body['message']}"
end

private

def client
@client ||= Faraday.new(@api_endpoint) do |f|
f.headers['Authorization'] = "token #{@token}"
f.request(:json)
f.response(:json) # Faraday decodes response body as JSON
f.adapter(:net_http)
end
end

def request_successful?
[HTTP_OK_CODE, HTTP_CREATED_CODE].include?(@response.status)
end

def error_class
case @response.status
when HTTP_BAD_REQUEST_CODE
BadRequestError
when HTTP_UNAUTHORIZED_CODE
UnauthorizedError
when HTTP_FORBIDDEN_CODE
ForbiddenError
when HTTP_NOT_FOUND_CODE
NotFoundError
else
ApiError
end
end
end
end
end
38 changes: 29 additions & 9 deletions src/api/app/models/scm_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SCMWebhook

validates_with SCMWebhookEventValidator

ALLOWED_PULL_REQUEST_ACTIONS = ['closed', 'opened', 'reopened', 'synchronize'].freeze
ALLOWED_PULL_REQUEST_ACTIONS = ['closed', 'opened', 'reopened', 'synchronize', 'synchronized'].freeze
ALLOWED_MERGE_REQUEST_ACTIONS = ['close', 'merge', 'open', 'reopen', 'update'].freeze

def initialize(attributes = {})
Expand All @@ -20,38 +20,42 @@ def initialize(attributes = {})

def new_pull_request?
(github_pull_request? && @payload[:action] == 'opened') ||
(gitlab_merge_request? && @payload[:action] == 'open')
(gitlab_merge_request? && @payload[:action] == 'open') ||
(gitea_pull_request? && @payload[:action] == 'opened')
end

def updated_pull_request?
(github_pull_request? && @payload[:action] == 'synchronize') ||
(gitlab_merge_request? && @payload[:action] == 'update')
(gitlab_merge_request? && @payload[:action] == 'update') ||
(gitea_pull_request? && @payload[:action] == 'synchronized')
end

def closed_merged_pull_request?
(github_pull_request? && @payload[:action] == 'closed') ||
(gitlab_merge_request? && ['close', 'merge'].include?(@payload[:action]))
(gitlab_merge_request? && ['close', 'merge'].include?(@payload[:action])) ||
(gitea_pull_request? && @payload[:action] == 'closed')
end

def reopened_pull_request?
(github_pull_request? && @payload[:action] == 'reopened') ||
(gitlab_merge_request? && @payload[:action] == 'reopen')
(gitlab_merge_request? && @payload[:action] == 'reopen') ||
(gitea_pull_request? && @payload[:action] == 'reopened')
end

def push_event?
github_push_event? || gitlab_push_event?
github_push_event? || gitlab_push_event? || gitea_push_event?
end

def tag_push_event?
github_tag_push_event? || gitlab_tag_push_event?
github_tag_push_event? || gitlab_tag_push_event? || gitea_tag_push_event?
end

def pull_request_event?
github_pull_request? || gitlab_merge_request?
github_pull_request? || gitlab_merge_request? || gitea_pull_request?
end

def ignored_pull_request_action?
ignored_github_pull_request_action? || ignored_gitlab_merge_request_action?
ignored_github_pull_request_action? || ignored_gitlab_merge_request_action? || ignored_gitea_pull_request_action?
end

private
Expand All @@ -64,6 +68,10 @@ def gitlab_push_event?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Push Hook'
end

def gitea_push_event?
@payload[:scm] == 'gitea' && @payload[:event] == 'push' && @payload.fetch(:ref, '').start_with?('refs/heads/')
end

def github_tag_push_event?
@payload[:scm] == 'github' && @payload[:event] == 'push' && @payload.fetch(:ref, '').starts_with?('refs/tags/')
end
Expand All @@ -72,6 +80,10 @@ def gitlab_tag_push_event?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Tag Push Hook'
end

def gitea_tag_push_event?
@payload[:scm] == 'gitea' && @payload[:event] == 'push' && @payload.fetch(:ref, '').starts_with?('refs/tags/')
end

def github_pull_request?
@payload[:scm] == 'github' && @payload[:event] == 'pull_request'
end
Expand All @@ -80,11 +92,19 @@ def gitlab_merge_request?
@payload[:scm] == 'gitlab' && @payload[:event] == 'Merge Request Hook'
end

def gitea_pull_request?
@payload[:scm] == 'gitea' && @payload[:event] == 'pull_request'
end

def ignored_github_pull_request_action?
github_pull_request? && ALLOWED_PULL_REQUEST_ACTIONS.exclude?(@payload[:action])
end

def ignored_gitlab_merge_request_action?
gitlab_merge_request? && ALLOWED_MERGE_REQUEST_ACTIONS.exclude?(@payload[:action])
end

def ignored_gitea_pull_request_action?
gitea_pull_request? && ALLOWED_PULL_REQUEST_ACTIONS.exclude?(@payload[:action])
end
end

0 comments on commit 48b1002

Please sign in to comment.