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

[#56609] Fix TypeError for gitlab pushes without commit message

Judith Roth6622bdaf8456
diff --git a/modules/gitlab_integration/lib/open_project/gitlab_integration/notification_handler/push_hook.rb b/modules/gitlab_integration/lib/open_project/gitlab_integration/notification_handler/push_hook.rb
index 5a8ea57ea41..50645832b56 100644
--- a/modules/gitlab_integration/lib/open_project/gitlab_integration/notification_handler/push_hook.rb
+++ b/modules/gitlab_integration/lib/open_project/gitlab_integration/notification_handler/push_hook.rb
@@ -40,7 +40,9 @@ module OpenProject::GitlabIntegration
payload.commits.each do |commit|
user = User.find_by_id(payload.open_project_user_id)
- text = commit["title"] + " - " + commit["message"]
+ text = [commit["title"], commit["message"]]
+ .select(&:present?)
+ .join(" - ")
work_packages = find_mentioned_work_packages(text, user)
notes = generate_notes(commit, payload)
comment_on_referenced_work_packages(work_packages, user, notes)
@@ -55,7 +57,7 @@ module OpenProject::GitlabIntegration
commit_id = commit["id"]
I18n.t("gitlab_integration.push_single_commit_comment",
commit_number: commit_id[0, 8],
- commit_note: commit["message"],
+ commit_note: commit["message"].presence || commit["title"],
commit_url: commit["url"],
commit_timestamp: commit["timestamp"],
repository: payload.repository.name,
diff --git a/modules/gitlab_integration/spec/lib/open_project/gitlab_integration/notification_handler/push_hook_spec.rb b/modules/gitlab_integration/spec/lib/open_project/gitlab_integration/notification_handler/push_hook_spec.rb
new file mode 100644
index 00000000000..85346989e4c
--- /dev/null
+++ b/modules/gitlab_integration/spec/lib/open_project/gitlab_integration/notification_handler/push_hook_spec.rb
@@ -0,0 +1,151 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 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
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require_module_spec_helper
+
+RSpec.describe OpenProject::GitlabIntegration::NotificationHandler::PushHook do
+ subject(:process) { handler_instance.process(payload) }
+
+ shared_let(:gitlab_system_user) { create(:admin) }
+ shared_let(:work_package) { create(:work_package) }
+
+ let(:handler_instance) { described_class.new }
+
+ let(:commit_title) { "Mentioning OP##{work_package.id}" }
+ let(:commit_message) { "Mentioning OP##{work_package.id}\n\nSome commit message\n" }
+ let(:payload) do
+ {
+ "object_kind" => "push",
+ "event_name" => "push",
+ "before" => "76e703f64c13245bdacf66737d99a52f08f3d727",
+ "after" => "a265d6b7bcf836b77ed9e32f824b231585c6a355",
+ "ref" => "refs/heads/main",
+ "ref_protected" => true,
+ "checkout_sha" => "a265d6b7bcf836b77ed9e32f824b231585c6a355",
+ "message" => nil,
+ "user_id" => 1,
+ "user_name" => "Administrator",
+ "user_username" => "root",
+ "user_email" => nil,
+ "user_avatar" => "https://www.gravatar.com/avatar/65a222b844ced567fe0ed2594c0b4abdf62efa1322a385c919c41e7bbc16d4fc?s=80&d=identicon",
+ "project_id" => 1,
+ "project" =>
+ {
+ "id" => 1,
+ "name" => "Test",
+ "description" => nil,
+ "web_url" => "http://c7e7cd2d54c3/openprojecttest/test",
+ "avatar_url" => nil,
+ "git_ssh_url" => "git@c7e7cd2d54c3:openprojecttest/test.git",
+ "git_http_url" => "http://c7e7cd2d54c3/openprojecttest/test.git",
+ "namespace" => "openprojecttest",
+ "visibility_level" => 10,
+ "path_with_namespace" => "openprojecttest/test",
+ "default_branch" => "main",
+ "ci_config_path" => nil,
+ "homepage" => "http://c7e7cd2d54c3/openprojecttest/test",
+ "url" => "git@c7e7cd2d54c3:openprojecttest/test.git",
+ "ssh_url" => "git@c7e7cd2d54c3:openprojecttest/test.git",
+ "http_url" => "http://c7e7cd2d54c3/openprojecttest/test.git"
+ },
+ "commits" => [
+ {
+ "id" => "a265d6b7bcf836b77ed9e32f824b231585c6a355",
+ "message" => commit_message,
+ "title" => commit_title,
+ "timestamp" => "2024-07-22T11:18:29+02:00",
+ "url" => "http://c7e7cd2d54c3/openprojecttest/test/-/commit/a265d6b7bcf836b77ed9e32f824b231585c6a355",
+ "author" => { "name" => "Some committer", "email" => "[email protected]" },
+ "added" => [],
+ "modified" => ["CHANGELOG"],
+ "removed" => []
+ }
+ ],
+ "total_commits_count" => 1,
+ "push_options" => {},
+ "repository" =>
+ {
+ "name" => "Test",
+ "url" => "git@c7e7cd2d54c3:openprojecttest/test.git",
+ "description" => nil,
+ "homepage" => "http://c7e7cd2d54c3/openprojecttest/test",
+ "git_http_url" => "http://c7e7cd2d54c3/openprojecttest/test.git",
+ "git_ssh_url" => "git@c7e7cd2d54c3:openprojecttest/test.git",
+ "visibility_level" => 10
+ },
+ "open_project_user_id" => gitlab_system_user.id,
+ "gitlab_event" => "push_hook"
+ }
+ end
+
+ before do
+ allow(handler_instance).to receive(:comment_on_referenced_work_packages).and_return(nil)
+ end
+
+ context "with a regular push" do
+ let(:comment) do
+ "**Pushed in MR:** [Administrator]" \
+ "(https://www.gravatar.com/avatar/65a222b844ced567fe0ed2594c0b4abdf62efa1322a385c919c41e7bbc16d4fc?s=80&d=identicon) " \
+ "pushed [a265d6b7](http://c7e7cd2d54c3/openprojecttest/test/-/commit/a265d6b7bcf836b77ed9e32f824b231585c6a355) " \
+ "to [Test](http://c7e7cd2d54c3/openprojecttest/test) at 2024-07-22T11:18:29+02:00:" \
+ "\nMentioning OP##{work_package.id}\n\nSome commit message\n\n"
+ end
+
+ it "adds a comment to the work packages" do
+ process
+ expect(handler_instance).to have_received(:comment_on_referenced_work_packages).with(
+ [work_package],
+ gitlab_system_user,
+ comment
+ )
+ end
+
+ context "when no commit message is given in the payload" do
+ before do
+ payload["commits"][0]["message"] = nil
+ end
+
+ let(:comment) do
+ "**Pushed in MR:** [Administrator]" \
+ "(https://www.gravatar.com/avatar/65a222b844ced567fe0ed2594c0b4abdf62efa1322a385c919c41e7bbc16d4fc?s=80&d=identicon) " \
+ "pushed [a265d6b7](http://c7e7cd2d54c3/openprojecttest/test/-/commit/a265d6b7bcf836b77ed9e32f824b231585c6a355) " \
+ "to [Test](http://c7e7cd2d54c3/openprojecttest/test) at 2024-07-22T11:18:29+02:00:\nMentioning OP##{work_package.id}\n"
+ end
+
+ it "does not raise (Bugfix)" do
+ process
+ expect(handler_instance).to have_received(:comment_on_referenced_work_packages).with(
+ [work_package],
+ gitlab_system_user,
+ comment
+ )
+ end
+ end
+ end
+end