Skip to content

Commit

Permalink
chore[Op#53019]: rubocop amend modules/gitlab_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Feb 28, 2024
1 parent 0ff9861 commit a2ad065
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 95 deletions.
2 changes: 1 addition & 1 deletion modules/gitlab_integration/app/models/gitlab_issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GitlabIssue < ApplicationRecord
unless: :partial?
validate :validate_labels_schema

scope :without_work_package, -> { left_outer_joins(:work_packages).where(work_packages: { id: nil }) }
scope :without_work_package, -> { where.missing(:work_packages) }

def self.find_by_gitlab_identifiers(id: nil, url: nil, initialize: false)
raise ArgumentError, "needs an id or an url" if id.nil? && url.blank?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GitlabMergeRequest < ApplicationRecord
unless: :partial?
validate :validate_labels_schema

scope :without_work_package, -> { left_outer_joins(:work_packages).where(work_packages: { id: nil }) }
scope :without_work_package, -> { where.missing(:work_packages) }

def self.find_by_gitlab_identifiers(id: nil, url: nil, initialize: false)
raise ArgumentError, "needs an id or an url" if id.nil? && url.blank?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def change
t.references :gitlab_user
t.references :merged_by

t.bigint :gitlab_id, unique: true
t.bigint :gitlab_id, unique: true
t.integer :number, null: false
t.string :gitlab_html_url, null: false, unique: true
t.string :state, null: false
Expand All @@ -45,6 +45,7 @@ def change
t.boolean :merged
t.datetime :merged_at
t.json :labels # [{name, color}]

t.timestamps
end

Expand All @@ -55,7 +56,6 @@ def change
name: "unique_index_gl_mrs_wps_on_gl_mr_id_and_wp_id"
end


create_table :gitlab_users do |t|
t.bigint :gitlab_id, null: false, unique: true
t.string :gitlab_name, null: false
Expand All @@ -66,14 +66,13 @@ def change
t.timestamps
end


create_table :gitlab_pipelines do |t|
t.references :gitlab_merge_request, null: false

t.bigint :gitlab_id, null: false, unique: true
t.string :gitlab_html_url, null: false
t.bigint :project_id, null: false
t.string :gitlab_user_avatar_url, null: false
t.bigint :project_id, null: false
t.string :gitlab_user_avatar_url, null: false
t.string :status, null: false
t.string :name, null: false
t.string :details_url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
class AddGitlabIssues < ActiveRecord::Migration[7.0]
# rubocop:disable Metrics/AbcSize
def change
create_table :gitlab_issues do |t|
t.references :gitlab_user

t.bigint :gitlab_id, unique: true
t.bigint :gitlab_id, unique: true
t.integer :number, null: false
t.string :gitlab_html_url, null: false, unique: true
t.string :state, null: false
Expand All @@ -41,6 +40,7 @@ def change
t.string :title
t.text :body
t.json :labels # [{name, color}]

t.timestamps
end

Expand All @@ -50,7 +50,5 @@ def change
unique: true,
name: "unique_index_gl_issues_wps_on_gl_issue_id_and_wp_id"
end

end
# rubocop:enable Metrics/AbcSize
end
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GitlabIssueRepresenter < ::API::Decorators::Single
def initialize(model, current_user:, **_opts)
# We force `embed_links` so that github_user and github_check_runs
# are embedded and we can avoid having separate endpoints.
super(model, current_user: current_user, embed_links: true)
super(model, current_user:, embed_links: true)
end

cached_representer key_parts: %i[gitlab_user]
Expand All @@ -61,7 +61,7 @@ def initialize(model, current_user:, **_opts)

date_time_property :gitlab_updated_at,
render_nil: true,
setter: ->(*) { nil }
setter: ->(*) {}

property :title,
render_nil: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GitlabUserRepresenter < ::API::Decorators::Single
include API::Caching::CachedRepresenter

self_link id_attribute: :id,
title_getter: ->(*) { nil }
title_getter: ->(*) {}

property :gitlab_name, as: :login
property :gitlab_email, as: :email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GitlabMergeRequestRepresenter < ::API::Decorators::Single
def initialize(model, current_user:, **_opts)
# We force `embed_links` so that github_user and github_check_runs
# are embedded and we can avoid having separate endpoints.
super(model, current_user: current_user, embed_links: true)
super(model, current_user:, embed_links: true)
end

cached_representer key_parts: %i[gitlab_user merged_by]
Expand All @@ -61,7 +61,7 @@ def initialize(model, current_user:, **_opts)

date_time_property :gitlab_updated_at,
render_nil: true,
setter: ->(*) { nil }
setter: ->(*) {}

property :title,
render_nil: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GitlabPipelineRepresenter < ::API::Decorators::Single
include API::Decorators::DateProperty

self_link id_attribute: :id,
title_getter: ->(*) { nil }
title_getter: ->(*) {}

property :gitlab_html_url, as: :htmlUrl
property :gitlab_user_avatar_url, as: :userAvatarUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GitlabUserRepresenter < ::API::Decorators::Single
include API::Caching::CachedRepresenter

self_link id_attribute: :id,
title_getter: ->(*) { nil }
title_getter: ->(*) {}

property :gitlab_name, as: :login
property :gitlab_email, as: :email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

require 'open_project/plugins'

require_relative './patches/api/work_package_representer'
require_relative './notification_handlers'
require_relative './hook_handler'
require_relative './services'
require_relative 'patches/api/work_package_representer'
require_relative 'notification_handlers'
require_relative 'hook_handler'
require_relative 'services'

module OpenProject::GitlabIntegration
class Engine < ::Rails::Engine
Expand All @@ -41,12 +41,12 @@ class Engine < ::Rails::Engine
include OpenProject::Plugins::ActsAsOpEngine

register 'openproject-gitlab_integration',
:author_url => 'https://github.com/btey/openproject',
author_url: 'https://github.com/btey/openproject',
bundled: true do
project_module(:gitlab, dependencies: :work_package_tracking) do
permission(:show_gitlab_content,
{},
permissible_on: %i[work_package project])
{},
permissible_on: %i[work_package project])
end
end

Expand Down Expand Up @@ -74,7 +74,7 @@ class Engine < ::Rails::Engine
end

extend_api_response(:v3, :work_packages, :work_package,
&::OpenProject::GitlabIntegration::Patches::API::WorkPackageRepresenter.extension)
&::OpenProject::GitlabIntegration::Patches::API::WorkPackageRepresenter.extension)

add_api_path :gitlab_merge_requests_by_work_package do |id|
"#{work_package(id)}/gitlab_merge_requests"
Expand Down Expand Up @@ -104,6 +104,5 @@ class Engine < ::Rails::Engine
# Register the cron job to clean up old gitlab merge requests
::Cron::CronJob.register! ::Cron::ClearOldMergeRequestsJob
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module OpenProject::GitlabIntegration
class HookHandler
# List of the gitlab events we can handle.
KNOWN_EVENTS = %w[
push_hook
issue_hook
note_hook
push_hook
issue_hook
note_hook
merge_request_hook
pipeline_hook
system_hook
Expand All @@ -42,12 +42,12 @@ class HookHandler
# A gitlab webhook happened.
# We need to check validity of the data and send a Notification
# which we process in our NotificationHandler.
def process(hook, request, params, user)
def process(_hook, request, params, user)
event_type = request.env['HTTP_X_GITLAB_EVENT']
event_type.gsub!(' ','_')
event_type.tr!(' ', '_')
event_type = event_type.to_s.downcase

Rails.logger.debug "Received gitlab webhook #{event_type}"
Rails.logger.debug { "Received gitlab webhook #{event_type}" }

return 404 unless KNOWN_EVENTS.include?(event_type)
return 403 unless user.present?
Expand All @@ -58,9 +58,9 @@ def process(hook, request, params, user)
.merge('open_project_user_id' => user.id,
'gitlab_event' => event_type)

OpenProject::Notifications.send("gitlab.#{event_type}", payload)
OpenProject::Notifications.send(:"gitlab.#{event_type}", payload)

Check failure

Code scanning / CodeQL

Code injection Critical

This code execution depends on a
user-provided value
.

return 200
200
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module NotificationHandler
# Handles Gitlab issue notifications.
class IssueHook
include OpenProject::GitlabIntegration::NotificationHandler::Helper

def process(payload_params)
@payload = wrap_payload(payload_params)
user = User.find_by_id(payload.open_project_user_id)
Expand All @@ -58,14 +58,15 @@ def generate_notes(payload)
}[payload.object_attributes.action]

return nil unless accepted_actions.include? payload.object_attributes.action

I18n.t("gitlab_integration.issue_#{key_action}_referenced_comment",
:issue_number => payload.object_attributes.iid,
:issue_title => payload.object_attributes.title,
:issue_url => payload.object_attributes.url,
:repository => payload.repository.name,
:repository_url => payload.repository.homepage,
:gitlab_user => payload.user.name,
:gitlab_user_url => payload.user.avatar_url)
issue_number: payload.object_attributes.iid,
issue_title: payload.object_attributes.title,
issue_url: payload.object_attributes.url,
repository: payload.repository.name,
repository_url: payload.repository.homepage,
gitlab_user: payload.user.name,
gitlab_user_url: payload.user.avatar_url)
end

def gitlab_issue
Expand All @@ -77,9 +78,10 @@ def gitlab_issue

def upsert_issue(work_packages)
return if work_packages.empty? && gitlab_issue.nil?

OpenProject::GitlabIntegration::Services::UpsertIssue.new.call(payload,
work_packages: work_packages)
work_packages:)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ def generate_notes(payload)

return nil unless key

I18n.t("gitlab_integration.merge_request_#{key_action ? key_action : key}_comment",
:mr_number => payload.object_attributes.iid,
:mr_title => payload.object_attributes.title,
:mr_url => payload.object_attributes.url,
:repository => payload.repository.name,
:repository_url => payload.repository.url,
:gitlab_user => payload.user.name,
:gitlab_user_url => payload.user.avatar_url)
I18n.t("gitlab_integration.merge_request_#{key_action || key}_comment",
mr_number: payload.object_attributes.iid,
mr_title: payload.object_attributes.title,
mr_url: payload.object_attributes.url,
repository: payload.repository.name,
repository_url: payload.repository.url,
gitlab_user: payload.user.name,
gitlab_user_url: payload.user.avatar_url)
end

def merge_request
Expand All @@ -102,10 +102,10 @@ def merge_request

def upsert_merge_request(work_packages)
return if work_packages.empty? && merge_request.nil?

OpenProject::GitlabIntegration::Services::UpsertMergeRequest.new.call(payload,
work_packages: work_packages)
work_packages:)
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ module NotificationHandler
# Handles Gitlab commit notifications.
class PushHook
include OpenProject::GitlabIntegration::NotificationHandler::Helper

def process(payload_params)
@payload = wrap_payload(payload_params)
return nil unless payload.object_kind == 'push'

payload.commits.each do |commit|
user = User.find_by_id(payload.open_project_user_id)
text = commit['title'] + " - " + commit['message']
Expand All @@ -53,15 +54,15 @@ def process(payload_params)
def generate_notes(commit, payload)
commit_id = commit['id']
I18n.t("gitlab_integration.push_single_commit_comment",
:commit_number => commit_id[0, 8],
:commit_note => commit['message'],
:commit_url => commit['url'],
:commit_timestamp => commit['timestamp'],
:repository => payload.repository.name,
:repository_url => payload.repository.homepage,
:gitlab_user => payload.user_name,
:gitlab_user_url => payload.user_avatar)
commit_number: commit_id[0, 8],
commit_note: commit['message'],
commit_url: commit['url'],
commit_timestamp: commit['timestamp'],
repository: payload.repository.name,
repository_url: payload.repository.homepage,
gitlab_user: payload.user_name,
gitlab_user_url: payload.user_avatar)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ module NotificationHandler
# Handles Gitlab commit notifications.
class SystemHook
include OpenProject::GitlabIntegration::NotificationHandler::Helper

def process(payload_params)
@payload = wrap_payload(payload_params)
return nil unless payload.object_kind == 'push'

payload.commits.each do |commit|
user = User.find_by_id(payload.open_project_user_id)
text = commit['title'] + " - " + commit['message']
Expand All @@ -53,14 +54,14 @@ def process(payload_params)
def generate_notes(commit, payload)
commit_id = commit['id']
I18n.t("gitlab_integration.push_single_commit_comment",
:commit_number => commit_id[0, 8],
:commit_note => commit['message'],
:commit_url => commit['url'],
:commit_timestamp => commit['timestamp'],
:repository => payload.repository.name,
:repository_url => payload.repository.homepage,
:gitlab_user => payload.user_name,
:gitlab_user_url => payload.user_avatar)
commit_number: commit_id[0, 8],
commit_note: commit['message'],
commit_url: commit['url'],
commit_timestamp: commit['timestamp'],
repository: payload.repository.name,
repository_url: payload.repository.homepage,
gitlab_user: payload.user_name,
gitlab_user_url: payload.user_avatar)
end
end
end
Expand Down
Loading

0 comments on commit a2ad065

Please sign in to comment.