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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
fix 500 errors with empty MR and Issue description body, fix empty avatar_url
Aleksey Kurepin419f94b28584
diff --git a/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb b/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rbindex 42845f14e16..87aaac7d5a1 100644--- a/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb+++ b/lib/open_project/gitlab_integration/services/upsert_gitlab_user.rb@@ -1,4 +1,4 @@-#-- encoding: UTF-8+# frozen_string_literal: true#-- copyright# OpenProject is an open source project management software.@@ -27,34 +27,44 @@## See docs/COPYRIGHT.rdoc for more details.#++-module OpenProject::GitlabIntegration::Services- ##- # Takes user data coming from Gitlab webhook data and stores- # them as a `GitlabUser`.- # If the `GitlabUser` already exists, it is updated.- #- # Returns the upserted `GitlabUser`.- class UpsertGitlabUser- def call(payload)- GitlabUser.find_or_initialize_by(gitlab_id: payload.id)- .tap do |gitlab_user|- gitlab_user.update!(extract_params(payload))- end- end+module OpenProject+ module GitlabIntegration+ module Services+ ##+ # Takes user data coming from Gitlab webhook data and stores+ # them as a `GitlabUser`.+ # If the `GitlabUser` already exists, it is updated.+ #+ # Returns the upserted `GitlabUser`.+ class UpsertGitlabUser+ def call(payload)+ GitlabUser.find_or_initialize_by(gitlab_id: payload.id)+ .tap do |gitlab_user|+ gitlab_user.update!(extract_params(payload))+ end+ end++ private- private+ ##+ # Receives the input from the gitlab webhook and translates them+ # to our internal representation.+ def extract_params(payload)+ {+ gitlab_id: payload.id,+ gitlab_name: payload.name,+ gitlab_username: payload.username,+ gitlab_email: payload.email,+ gitlab_avatar_url: avatar_url(payload)+ }+ end- ##- # Receives the input from the gitlab webhook and translates them- # to our internal representation.- def extract_params(payload)- {- gitlab_id: payload.id,- gitlab_name: payload.name,- gitlab_username: payload.username,- gitlab_email: payload.email,- gitlab_avatar_url: payload.avatar_url- }+ def avatar_url(payload)+ # rubocop:disable Layout/LineLength+ payload.avatar_url.presence || "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath d='M12 2.5a5.5 5.5 0 0 1 3.096 10.047 9.005 9.005 0 0 1 5.9 8.181.75.75 0 1 1-1.499.044 7.5 7.5 0 0 0-14.993 0 .75.75 0 0 1-1.5-.045 9.005 9.005 0 0 1 5.9-8.18A5.5 5.5 0 0 1 12 2.5ZM8 8a4 4 0 1 0 8 0 4 4 0 0 0-8 0Z'%3E%3C/path%3E%3C/svg%3E"+ # rubocop:enable Layout/LineLength+ end+ endendendenddiff --git a/lib/open_project/gitlab_integration/services/upsert_issue.rb b/lib/open_project/gitlab_integration/services/upsert_issue.rbindex 402e6bee5c0..969db32a786 100644--- a/lib/open_project/gitlab_integration/services/upsert_issue.rb+++ b/lib/open_project/gitlab_integration/services/upsert_issue.rb@@ -1,4 +1,4 @@-#-- encoding: UTF-8+# frozen_string_literal: true#-- copyright# OpenProject is an open source project management software.@@ -27,58 +27,66 @@## See docs/COPYRIGHT.rdoc for more details.#++-module OpenProject::GitlabIntegration::Services- ##- # Takes merge request data coming from Gitlab webhook data and stores- # them as a `GitlabMergeRequest`.- # If the `GitlabMergeRequest` already exists, it is updated.- #- # Returns the upserted `GitlabMergeRequest`.- class UpsertIssue- def call(payload, work_packages: [])- find_or_initialize(payload).tap do |issue|- issue.update!(work_packages: issue.work_packages | work_packages, **extract_params(payload))- end- end+module OpenProject+ module GitlabIntegration+ module Services+ ##+ # Takes merge request data coming from Gitlab webhook data and stores+ # them as a `GitlabMergeRequest`.+ # If the `GitlabMergeRequest` already exists, it is updated.+ #+ # Returns the upserted `GitlabMergeRequest`.+ class UpsertIssue+ def call(payload, work_packages: [])+ find_or_initialize(payload).tap do |issue|+ issue.update!(work_packages: issue.work_packages | work_packages, **extract_params(payload))+ end+ end- private+ private- def find_or_initialize(payload)- GitlabIssue.find_by_gitlab_identifiers(id: payload.object_attributes.iid,- url: payload.object_attributes.url,- initialize: true)- end+ def find_or_initialize(payload)+ GitlabIssue.find_by_gitlab_identifiers(id: payload.object_attributes.iid,+ url: payload.object_attributes.url,+ initialize: true)+ end- # Receives the input from the gitlab webhook and translates them- # to our internal representation.- # rubocop:disable Metrics/AbcSize- def extract_params(payload)- {- gitlab_id: payload.object_attributes.iid,- gitlab_user: gitlab_user_id(payload.user),- number: payload.object_attributes.iid,- gitlab_html_url: payload.object_attributes.url,- gitlab_updated_at: payload.object_attributes.updated_at,- state: payload.object_attributes.state,- title: payload.object_attributes.title,- body: payload.object_attributes.description,- repository: payload.repository.name,- labels: payload.labels.map { |values| extract_label_values(values) }- }- end- # rubocop:enable Metrics/AbcSize+ # Receives the input from the gitlab webhook and translates them+ # to our internal representation.+ # rubocop:disable Metrics/AbcSize+ def extract_params(payload)+ {+ gitlab_id: payload.object_attributes.iid,+ gitlab_user: gitlab_user_id(payload.user),+ number: payload.object_attributes.iid,+ gitlab_html_url: payload.object_attributes.url,+ gitlab_updated_at: payload.object_attributes.updated_at,+ state: payload.object_attributes.state,+ title: payload.object_attributes.title,+ body: description(payload),+ repository: payload.repository.name,+ labels: payload.labels.map { |values| extract_label_values(values) }+ }+ end+ # rubocop:enable Metrics/AbcSize- def extract_label_values(payload)- {- title: payload['title'],- color: payload['color']- }- end+ def extract_label_values(payload)+ {+ title: payload['title'],+ color: payload['color']+ }+ end++ def gitlab_user_id(payload)+ return if payload.blank?- def gitlab_user_id(payload)- return if payload.blank?+ ::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)+ end- ::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)+ def description(payload)+ payload.object_attributes.description.presence || 'No description provided'+ end+ endendendenddiff --git a/lib/open_project/gitlab_integration/services/upsert_merge_request.rb b/lib/open_project/gitlab_integration/services/upsert_merge_request.rbindex d67a17e2701..a81176a269b 100644--- a/lib/open_project/gitlab_integration/services/upsert_merge_request.rb+++ b/lib/open_project/gitlab_integration/services/upsert_merge_request.rb@@ -1,4 +1,4 @@-#-- encoding: UTF-8+# frozen_string_literal: true#-- copyright# OpenProject is an open source project management software.@@ -27,62 +27,70 @@## See docs/COPYRIGHT.rdoc for more details.#++-module OpenProject::GitlabIntegration::Services- ##- # Takes merge request data coming from Gitlab webhook data and stores- # them as a `GitlabMergeRequest`.- # If the `GitlabMergeRequest` already exists, it is updated.- #- # Returns the upserted `GitlabMergeRequest`.- class UpsertMergeRequest- def call(payload, work_packages: [])- find_or_initialize(payload).tap do |mr|- mr.update!(work_packages: mr.work_packages | work_packages, **extract_params(payload))- end- end+module OpenProject+ module GitlabIntegration+ module Services+ ##+ # Takes merge request data coming from Gitlab webhook data and stores+ # them as a `GitlabMergeRequest`.+ # If the `GitlabMergeRequest` already exists, it is updated.+ #+ # Returns the upserted `GitlabMergeRequest`.+ class UpsertMergeRequest+ def call(payload, work_packages: [])+ find_or_initialize(payload).tap do |mr|+ mr.update!(work_packages: mr.work_packages | work_packages, **extract_params(payload))+ end+ end- private+ private- def find_or_initialize(payload)- GitlabMergeRequest.find_by_gitlab_identifiers(id: payload.object_attributes.iid,- url: payload.object_attributes.url,- initialize: true)- end+ def find_or_initialize(payload)+ GitlabMergeRequest.find_by_gitlab_identifiers(id: payload.object_attributes.iid,+ url: payload.object_attributes.url,+ initialize: true)+ end- # Receives the input from the gitlab webhook and translates them- # to our internal representation.- # rubocop:disable Metrics/AbcSize- def extract_params(payload)- {- gitlab_id: payload.object_attributes.iid,- gitlab_user: gitlab_user_id(payload.user),- number: payload.object_attributes.iid,- gitlab_html_url: payload.object_attributes.url,- gitlab_updated_at: payload.object_attributes.updated_at,- state: payload.object_attributes.state,- title: payload.object_attributes.title,- body: payload.object_attributes.description,- repository: payload.repository.name,- draft: payload.object_attributes.work_in_progress,- merged: payload.object_attributes.state == 'merged' ? true : false,- merged_by: gitlab_user_id(payload.user),- merged_at: payload.object_attributes.state == 'merged' ? payload.object_attributes.updated_at : nil,- labels: payload.labels.map { |values| extract_label_values(values) }- }- end- # rubocop:enable Metrics/AbcSize+ # Receives the input from the gitlab webhook and translates them+ # to our internal representation.+ # rubocop:disable Metrics/AbcSize+ def extract_params(payload)+ {+ gitlab_id: payload.object_attributes.iid,+ gitlab_user: gitlab_user_id(payload.user),+ number: payload.object_attributes.iid,+ gitlab_html_url: payload.object_attributes.url,+ gitlab_updated_at: payload.object_attributes.updated_at,+ state: payload.object_attributes.state,+ title: payload.object_attributes.title,+ body: description(payload),+ repository: payload.repository.name,+ draft: payload.object_attributes.work_in_progress,+ merged: payload.object_attributes.state == 'merged',+ merged_by: gitlab_user_id(payload.user),+ merged_at: payload.object_attributes.state == 'merged' ? payload.object_attributes.updated_at : nil,+ labels: payload.labels.map { |values| extract_label_values(values) }+ }+ end+ # rubocop:enable Metrics/AbcSize- def extract_label_values(payload)- {- title: payload['title'],- color: payload['color']- }- end+ def extract_label_values(payload)+ {+ title: payload['title'],+ color: payload['color']+ }+ end++ def gitlab_user_id(payload)+ return if payload.blank?- def gitlab_user_id(payload)- return if payload.blank?+ ::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)+ end- ::OpenProject::GitlabIntegration::Services::UpsertGitlabUser.new.call(payload)+ def description(payload)+ payload.object_attributes.description.presence || 'No description provided'+ end+ endendendenddiff --git a/openproject-gitlab_integration.gemspec b/openproject-gitlab_integration.gemspecindex 013dae3603e..9c4abf49d3a 100644--- a/openproject-gitlab_integration.gemspec+++ b/openproject-gitlab_integration.gemspec@@ -1,4 +1,5 @@-# encoding: UTF-8+# frozen_string_literal: true+##-- copyright# OpenProject is an open source project management software.@@ -30,16 +31,16 @@#++Gem::Specification.new do |s|- s.name = "openproject-gitlab_integration"- s.version = '2.1'- s.authors = "Ben Tey"- s.email = "[email protected]"- s.homepage = "https://github.com/btey/openproject-gitlab-integration"+ s.name = 'openproject-gitlab_integration'+ s.version = '2.1.1'+ s.authors = 'Ben Tey'+ s.email = '[email protected]'+ s.homepage = 'https://github.com/btey/openproject-gitlab-integration's.summary = 'OpenProject GitLab Integration's.description = 'Integrates OpenProject and GitLab for a better workflow's.license = 'GPLv3'- s.files = Dir["{app,config,db,frontend,lib}/**/*"] + %w(README.md)+ s.files = Dir['{app,config,db,frontend,lib}/**/*'] + %w[README.md]- s.add_dependency "openproject-webhooks"+ s.add_dependency 'openproject-webhooks'end