Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

services refactoring

Aleksey Kurepina13763905884
diff --git a/lib/open_project/gitlab_integration/services/params_helper.rb b/lib/open_project/gitlab_integration/services/params_helper.rb
new file mode 100644
index 00000000000..ef7a9355c03
--- /dev/null
+++ b/lib/open_project/gitlab_integration/services/params_helper.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2023 Ben Tey
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+module OpenProject
+ module GitlabIntegration
+ module Services
+ module ParamsHelper
+ private
+
+ EMPTY_AVATAR_URL = 'https://www.gravatar.com/avatar/?d=mp'
+ EMPTY_DESCRIPTION = 'No description provided'
+
+ def avatar_url(raw_url)
+ raw_url.presence || EMPTY_AVATAR_URL
+ end
+
+ def description(raw_description)
+ raw_description.presence || EMPTY_DESCRIPTION
+ end
+ end
+ end
+ end
+end
diff --git a/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb b/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb
index 23c2531d316..e9f70e340e4 100644
--- a/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb
+++ b/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb
@@ -38,6 +38,8 @@ module OpenProject
#
# Returns the upserted `GitlabUser`.
class UpsertGitlabUser
+ include ParamsHelper
+
def call(payload)
GitlabUser.find_or_initialize_by(gitlab_id: payload.id)
.tap do |gitlab_user|
@@ -56,13 +58,9 @@ module OpenProject
gitlab_name: payload.name,
gitlab_username: payload.username,
gitlab_email: payload.email,
- gitlab_avatar_url: avatar_url(payload)
+ gitlab_avatar_url: avatar_url(payload.avatar_url)
}
end
-
- def avatar_url(payload)
- payload.avatar_url.presence || 'https://www.gravatar.com/avatar/?d=mp'
- end
end
end
end
diff --git a/lib/open_project/gitlab_integration/services/upsert_issue.rb b/lib/open_project/gitlab_integration/services/upsert_issue.rb
index f222ab385d2..b0e50f8f1d3 100644
--- a/lib/open_project/gitlab_integration/services/upsert_issue.rb
+++ b/lib/open_project/gitlab_integration/services/upsert_issue.rb
@@ -31,7 +31,6 @@
module OpenProject
module GitlabIntegration
module Services
-
class UpsertIssue
def call(payload, work_packages: [])
find_or_initialize(payload).tap do |issue|
@@ -59,7 +58,7 @@ module OpenProject
gitlab_updated_at: payload.object_attributes.updated_at,
state: payload.object_attributes.state,
title: payload.object_attributes.title,
- body: description(payload),
+ body: description(payload.object_attributes.description),
repository: payload.repository.name,
labels: payload.labels.map { |values| extract_label_values(values) }
}
@@ -78,10 +77,6 @@ module OpenProject
::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)
end
-
- def description(payload)
- payload.object_attributes.description.presence || 'No description provided'
- end
end
end
end
diff --git a/lib/open_project/gitlab_integration/services/upsert_issue_note.rb b/lib/open_project/gitlab_integration/services/upsert_issue_note.rb
index 72cb4400253..eb58b8e83e3 100644
--- a/lib/open_project/gitlab_integration/services/upsert_issue_note.rb
+++ b/lib/open_project/gitlab_integration/services/upsert_issue_note.rb
@@ -31,8 +31,9 @@
module OpenProject
module GitlabIntegration
module Services
-
class UpsertIssueNote
+ include ParamsHelper
+
def call(payload, work_packages: [])
find_or_initialize(payload).tap do |issue|
issue.update!(work_packages: issue.work_packages | work_packages, **extract_params(payload))
@@ -43,8 +44,8 @@ module OpenProject
def find_or_initialize(payload)
GitlabIssue.find_by_gitlab_identifiers(id: payload.issue.iid,
- url: payload.issue.url,
- initialize: true)
+ url: payload.issue.url,
+ initialize: true)
end
# Receives the input from the gitlab webhook and translates them
@@ -59,7 +60,7 @@ module OpenProject
gitlab_updated_at: payload.issue.updated_at,
state: payload.issue.state,
title: payload.issue.title,
- body: description(payload),
+ body: description(payload.object_attributes.description),
repository: payload.repository.name,
labels: payload.issue.labels.map { |values| extract_label_values(values) }
}
@@ -78,10 +79,6 @@ module OpenProject
::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)
end
-
- def description(payload)
- payload.object_attributes.description.presence || 'No description provided'
- end
end
end
end
diff --git a/lib/open_project/gitlab_integration/services/upsert_merge_request.rb b/lib/open_project/gitlab_integration/services/upsert_merge_request.rb
index cec74aaa6ef..78c8c0ccfa0 100644
--- a/lib/open_project/gitlab_integration/services/upsert_merge_request.rb
+++ b/lib/open_project/gitlab_integration/services/upsert_merge_request.rb
@@ -31,8 +31,9 @@
module OpenProject
module GitlabIntegration
module Services
-
class UpsertMergeRequest
+ include ParamsHelper
+
def call(payload, work_packages: [])
find_or_initialize(payload).tap do |mr|
mr.update!(work_packages: mr.work_packages | work_packages, **extract_params(payload))
@@ -59,7 +60,7 @@ module OpenProject
gitlab_updated_at: payload.object_attributes.updated_at,
state: payload.object_attributes.state,
title: payload.object_attributes.title,
- body: description(payload),
+ body: description(payload.object_attributes.description),
repository: payload.repository.name,
draft: payload.object_attributes.work_in_progress,
merged: payload.object_attributes.state == 'merged',
@@ -82,10 +83,6 @@ module OpenProject
::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)
end
-
- def description(payload)
- payload.object_attributes.description.presence || 'No description provided'
- end
end
end
end
diff --git a/lib/open_project/gitlab_integration/services/upsert_pipeline.rb b/lib/open_project/gitlab_integration/services/upsert_pipeline.rb
index 859d74070a5..0fbdd9ecf7e 100644
--- a/lib/open_project/gitlab_integration/services/upsert_pipeline.rb
+++ b/lib/open_project/gitlab_integration/services/upsert_pipeline.rb
@@ -31,8 +31,9 @@
module OpenProject
module GitlabIntegration
module Services
-
class UpsertPipeline
+ include ParamsHelper
+
def call(payload, merge_request:)
GitlabPipeline.find_or_initialize_by(gitlab_id: payload.object_attributes.iid)
.tap do |pipeline|
@@ -49,7 +50,7 @@ module OpenProject
gitlab_id: payload.object_attributes.iid,
gitlab_html_url: "#{payload.project.web_url}/-/pipelines/#{payload.object_attributes.iid}",
project_id: payload.project.id,
- gitlab_user_avatar_url: payload.user.avatar_url,
+ gitlab_user_avatar_url: avatar_url(payload.user.avatar_url),
name: payload.object_attributes.iid,
status: payload.object_attributes.status,
details_url: "#{payload.project.web_url}/-/commit/#{payload.object_attributes.sha[0..7]}",
@@ -63,4 +64,4 @@ module OpenProject
end
end
end
-end
\ No newline at end of file
+end