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

tests[Op#53117]: add gitlab pipeline and merge request specs

Kabiru Mwenja865ff5a4cb98
diff --git a/modules/gitlab_integration/app/models/gitlab_merge_request.rb b/modules/gitlab_integration/app/models/gitlab_merge_request.rb
index 0cd08bd2f1f..ae02a147a74 100644
--- a/modules/gitlab_integration/app/models/gitlab_merge_request.rb
+++ b/modules/gitlab_integration/app/models/gitlab_merge_request.rb
@@ -88,7 +88,7 @@ class GitlabMergeRequest < ApplicationRecord
return if labels.nil?
return if labels.all? { |label| label.keys.sort == LABEL_KEYS }
- errors.add(:labels, 'invalid schema')
+ errors.add(:labels, :invalid_schema)
end
def with_logging
diff --git a/modules/gitlab_integration/config/locales/en.yml b/modules/gitlab_integration/config/locales/en.yml
index 29eb8eaa3c9..ce7058e0c16 100644
--- a/modules/gitlab_integration/config/locales/en.yml
+++ b/modules/gitlab_integration/config/locales/en.yml
@@ -35,6 +35,10 @@ en:
attributes:
labels:
invalid_schema: "must be an array of hashes with keys: color, title"
+ gitlab_merge_request:
+ attributes:
+ labels:
+ invalid_schema: "must be an array of hashes with keys: color, title"
project_module_gitlab: "Gitlab"
permission_show_gitlab_content: "Show Gitlab content"
diff --git a/modules/gitlab_integration/spec/factories/gitlab_merge_requests.rb b/modules/gitlab_integration/spec/factories/gitlab_merge_requests.rb
new file mode 100644
index 00000000000..d8a137fde4c
--- /dev/null
+++ b/modules/gitlab_integration/spec/factories/gitlab_merge_requests.rb
@@ -0,0 +1,70 @@
+#-- 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.
+#++
+
+FactoryBot.define do
+ factory :gitlab_merge_request do
+ gitlab_user
+
+ sequence(:number)
+ sequence(:gitlab_id)
+ state { 'opened' }
+ gitlab_html_url { "https://gitlab.com/test_user/test_repo/merge/#{number}" }
+
+ labels { [] }
+ gitlab_updated_at { Time.current }
+ sequence(:title) { |n| "Title of PR #{n}" }
+ sequence(:body) { |n| "Body of PR #{n}" }
+ sequence(:repository) { |n| "test_user/repo_#{n}" }
+
+ draft { false }
+ merged { false }
+ merged_by { nil }
+ merged_at { nil }
+
+ trait :partial do
+ body { nil }
+ end
+
+ trait :draft do
+ draft { true }
+ end
+
+ trait :open
+
+ trait :closed_unmerged do
+ state { 'closed' }
+ end
+
+ trait :closed_merged do
+ state { 'merged' }
+ merged { true }
+ merged_by { association :gitlab_user }
+ merged_at { Time.current }
+ end
+ end
+end
diff --git a/modules/gitlab_integration/spec/factories/gitlab_pipelines.rb b/modules/gitlab_integration/spec/factories/gitlab_pipelines.rb
new file mode 100644
index 00000000000..ec3f1d30beb
--- /dev/null
+++ b/modules/gitlab_integration/spec/factories/gitlab_pipelines.rb
@@ -0,0 +1,82 @@
+#-- 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.
+#++
+
+FactoryBot.define do
+ factory :gitlab_pipeline do
+ gitlab_merge_request
+
+ sequence(:gitlab_id)
+ sequence(:username) { |n| "user_#{n}" }
+
+ name { gitlab_id }
+ commit_id { SecureRandom.hex[0..7] }
+ details_url { "https://gitlab.com/test_user/test_repo/commit/#{commit_id}" }
+ gitlab_html_url { "https://gitlab.com/test_user/test_repo/pipelines/#{gitlab_id}" }
+ gitlab_user_avatar_url { "https://www.gravatar.com/avatar/#{gitlab_id}/owner.jpg" }
+ status { 'pending' }
+ started_at { 1.hour.ago }
+ completed_at { nil }
+
+ ci_details do
+ # ci_details_json_file_path = File.join(File.dirname(__FILE__), 'gitlab_pipelines', 'ci_details.json')
+ # JSON.parse(File.read(ci_details_json_file_path))
+ build_list(:gitlab_pipeline_ci_detail, 3)
+ end
+
+ trait :complete do
+ status { 'success' }
+ completed_at { 1.minute.ago }
+ end
+
+ trait :recent do
+ started_at { 1.minute.ago }
+ end
+
+ trait :outdated do
+ started_at { 1.day.ago }
+ end
+ end
+
+ factory :gitlab_pipeline_ci_detail, class: 'Hash' do
+ skip_create
+
+ initialize_with { attributes }
+
+ stage { %w[test build deploy].sample }
+ sequence(:name) { |n| "job_#{n}" }
+ status { 'success' }
+ started_at { 1.hour.ago }
+ created_at { started_at }
+ finished_at { nil }
+ duration { nil }
+ queued_duration { nil }
+ failure_reason { nil }
+ manual { false }
+ allow_failure { false }
+ end
+end
diff --git a/modules/gitlab_integration/spec/factories/gitlab_pipelines/ci_details.json b/modules/gitlab_integration/spec/factories/gitlab_pipelines/ci_details.json
new file mode 100644
index 00000000000..6805a6be504
--- /dev/null
+++ b/modules/gitlab_integration/spec/factories/gitlab_pipelines/ci_details.json
@@ -0,0 +1,118 @@
+[
+ {
+ "id": 34,
+ "stage": "test",
+ "name": "unit-test-job",
+ "status": "created",
+ "created_at": "2024-03-02 09:01:05 UTC",
+ "started_at": null,
+ "finished_at": null,
+ "duration": null,
+ "queued_duration": null,
+ "failure_reason": null,
+ "when": "on_success",
+ "manual": false,
+ "allow_failure": false,
+ "user": {
+ "id": 1,
+ "name": "Administrator",
+ "username": "root",
+ "avatar_url": "https://www.gravatar.com/avatar/258d8dc916db8cea2cafb6c3cd0cb0246efe061421dbd83ec3a350428cabda4f?s=80\u0026d=identicon",
+ "email": "[REDACTED]"
+ },
+ "runner": null,
+ "artifacts_file": {
+ "filename": null,
+ "size": null
+ },
+ "environment": null
+ },
+ {
+ "id": 35,
+ "stage": "test",
+ "name": "lint-test-job",
+ "status": "created",
+ "created_at": "2024-03-02 09:01:05 UTC",
+ "started_at": null,
+ "finished_at": null,
+ "duration": null,
+ "queued_duration": null,
+ "failure_reason": null,
+ "when": "on_success",
+ "manual": false,
+ "allow_failure": false,
+ "user": {
+ "id": 1,
+ "name": "Administrator",
+ "username": "root",
+ "avatar_url": "https://www.gravatar.com/avatar/258d8dc916db8cea2cafb6c3cd0cb0246efe061421dbd83ec3a350428cabda4f?s=80\u0026d=identicon",
+ "email": "[REDACTED]"
+ },
+ "runner": null,
+ "artifacts_file": {
+ "filename": null,
+ "size": null
+ },
+ "environment": null
+ },
+ {
+ "id": 36,
+ "stage": "deploy",
+ "name": "deploy-job",
+ "status": "created",
+ "created_at": "2024-03-02 09:01:05 UTC",
+ "started_at": null,
+ "finished_at": null,
+ "duration": null,
+ "queued_duration": null,
+ "failure_reason": null,
+ "when": "on_success",
+ "manual": false,
+ "allow_failure": false,
+ "user": {
+ "id": 1,
+ "name": "Administrator",
+ "username": "root",
+ "avatar_url": "https://www.gravatar.com/avatar/258d8dc916db8cea2cafb6c3cd0cb0246efe061421dbd83ec3a350428cabda4f?s=80\u0026d=identicon",
+ "email": "[REDACTED]"
+ },
+ "runner": null,
+ "artifacts_file": {
+ "filename": null,
+ "size": null
+ },
+ "environment": {
+ "name": "production",
+ "action": "start",
+ "deployment_tier": "production"
+ }
+ },
+ {
+ "id": 33,
+ "stage": "build",
+ "name": "build-job",
+ "status": "pending",
+ "created_at": "2024-03-02 09:01:05 UTC",
+ "started_at": null,
+ "finished_at": null,
+ "duration": null,
+ "queued_duration": 399.060996359,
+ "failure_reason": null,
+ "when": "on_success",
+ "manual": false,
+ "allow_failure": false,
+ "user": {
+ "id": 1,
+ "name": "Administrator",
+ "username": "root",
+ "avatar_url": "https://www.gravatar.com/avatar/258d8dc916db8cea2cafb6c3cd0cb0246efe061421dbd83ec3a350428cabda4f?s=80\u0026d=identicon",
+ "email": "[REDACTED]"
+ },
+ "runner": null,
+ "artifacts_file": {
+ "filename": null,
+ "size": null
+ },
+ "environment": null
+ }
+]
diff --git a/modules/gitlab_integration/spec/models/gitlab_merge_request_spec.rb b/modules/gitlab_integration/spec/models/gitlab_merge_request_spec.rb
new file mode 100644
index 00000000000..76b4533956a
--- /dev/null
+++ b/modules/gitlab_integration/spec/models/gitlab_merge_request_spec.rb
@@ -0,0 +1,197 @@
+#-- 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 GitlabMergeRequest do
+ describe 'Associations' do
+ it { is_expected.to have_and_belong_to_many(:work_packages) }
+ it { is_expected.to have_many(:gitlab_pipelines).dependent(:destroy) }
+ it { is_expected.to belong_to(:gitlab_user).optional }
+ it { is_expected.to belong_to(:merged_by).class_name('GitlabUser').optional }
+ end
+
+ describe 'Validations' do
+ it { is_expected.to validate_presence_of :gitlab_html_url }
+ it { is_expected.to validate_presence_of :number }
+ it { is_expected.to validate_presence_of :repository }
+ it { is_expected.to validate_presence_of :state }
+ it { is_expected.to validate_presence_of :title }
+ it { is_expected.to validate_presence_of :gitlab_updated_at }
+
+ context 'when it is a partial merge request' do
+ let(:merge_request) { build(:gitlab_merge_request, :partial) }
+
+ it { expect(merge_request).not_to validate_presence_of :body }
+ end
+
+ context 'when it is not a partial merge_request' do
+ let(:merge_request) { build(:gitlab_merge_request) }
+
+ it { expect(merge_request).to be_valid }
+ end
+
+ describe 'labels' do
+ it { is_expected.to allow_value(nil).for(:labels) }
+ it { is_expected.to allow_value([]).for(:labels) }
+ it { is_expected.to allow_value([{ 'color' => '#666', 'title' => 'grey' }]).for(:labels) }
+
+ it 'requires both color and title' do
+ expect(subject).not_to allow_value([{ 'title' => 'grey' }]).for(:labels)
+ end
+
+ it { is_expected.not_to allow_value([{}]).for(:labels) }
+
+ it 'returns an error message when invalid' do
+ merge_request = build(:gitlab_merge_request, labels: [{ 'title' => 'grey' }])
+ merge_request.valid?
+ expect(merge_request.errors[:labels]).to include('must be an array of hashes with keys: color, title')
+ end
+ end
+ end
+
+ describe 'Enums' do
+ let(:gitlab_merge_request) { build(:gitlab_merge_request) }
+
+ it do
+ expect(gitlab_merge_request).to define_enum_for(:state)
+ .with_values(opened: 'opened', merged: 'merged', closed: 'closed')
+ .backed_by_column_of_type(:string)
+ end
+ end
+
+ describe '.without_work_package' do
+ subject { described_class.without_work_package }
+
+ let(:merge_request) { create(:gitlab_merge_request, work_packages:) }
+ let(:work_packages) { [] }
+
+ before { merge_request }
+
+ it { is_expected.to contain_exactly(merge_request) }
+
+ context 'when the pr is linked to a work_package' do
+ let(:work_packages) { create_list(:work_package, 1) }
+
+ it { is_expected.to be_empty }
+ end
+ end
+
+ describe '.find_by_gitlab_identifiers' do
+ shared_let(:merge_request) { create(:gitlab_merge_request) }
+
+ it 'raises an ArgumentError when no id or url is provided' do
+ expect { described_class.find_by_gitlab_identifiers }.to raise_error(ArgumentError, 'needs an id or an url')
+ end
+
+ context 'when the gitlab_id attribute matches' do
+ it 'finds by gitlab_id' do
+ expect(described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id)).to eql(merge_request)
+ end
+ end
+
+ context 'when the gitlab_html_url attribute matches' do
+ it 'finds by gitlab_html_url' do
+ expect(described_class.find_by_gitlab_identifiers(url: merge_request.gitlab_html_url)).to eql(merge_request)
+ end
+ end
+
+ context 'when the provided gitlab_id does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id + 1)).to be_nil
+ end
+ end
+
+ context 'when the provided gitlab_html_url does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(url: "#{merge_request.gitlab_html_url}zzzz"))
+ .to be_nil
+ end
+ end
+
+ context 'when neither match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id + 1,
+ url: "#{merge_request.gitlab_html_url}zzzz"))
+ .to be_nil
+ end
+ end
+
+ context 'when the provided gitlab_html_url does not match but the gitlab_id does' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id,
+ url: "#{merge_request.gitlab_html_url}zzzz"))
+ .to eql merge_request
+ end
+ end
+
+ context 'when the provided gitlab_html_url does match but the gitlab_id does not' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id + 1,
+ url: merge_request.gitlab_html_url))
+ .to eql merge_request
+ end
+ end
+
+ context 'when neither match but initialize is true' do
+ subject(:finder) do
+ described_class.find_by_gitlab_identifiers(id: merge_request.gitlab_id + 1,
+ url: "#{merge_request.gitlab_html_url}zzzz",
+ initialize: true)
+ end
+
+ it 'returns an merge_request' do
+ expect(finder).to be_a(described_class)
+ end
+
+ it 'returns a new record' do
+ expect(finder).to be_new_record
+ end
+
+ it 'has the provided attributes initialized' do
+ expect(finder.attributes.compact)
+ .to eql("gitlab_id" => merge_request.gitlab_id + 1,
+ "gitlab_html_url" => "#{merge_request.gitlab_html_url}zzzz")
+ end
+ end
+ end
+
+ describe '#latest_pipelines' do
+ context 'when multiple pipelines for the same merge request exist' do
+ shared_association_default(:gitlab_merge_request) { create(:gitlab_merge_request) }
+
+ shared_let(:latest_pipelines) { create_list(:gitlab_pipeline, 2, :recent, project_id: 123) }
+ shared_let(:outdated_pipelines) { create_list(:gitlab_pipeline, 2, :outdated, project_id: 123) }
+
+ it 'returns the latest pipeline ordered from most recent' do
+ expect(gitlab_merge_request.reload.latest_pipelines).to match_array(latest_pipelines + outdated_pipelines)
+ end
+ end
+ end
+end
diff --git a/modules/gitlab_integration/spec/models/gitlab_pipeline_spec.rb b/modules/gitlab_integration/spec/models/gitlab_pipeline_spec.rb
new file mode 100644
index 00000000000..4c4f9d333c7
--- /dev/null
+++ b/modules/gitlab_integration/spec/models/gitlab_pipeline_spec.rb
@@ -0,0 +1,67 @@
+#-- 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 GitlabPipeline do
+ describe 'Associations' do
+ it { is_expected.to belong_to(:gitlab_merge_request).touch(true) }
+ end
+
+ describe 'Validations' do
+ it { is_expected.to validate_presence_of :gitlab_user_avatar_url }
+ it { is_expected.to validate_presence_of :gitlab_html_url }
+ it { is_expected.to validate_presence_of :gitlab_id }
+ it { is_expected.to validate_presence_of :status }
+ it { is_expected.to validate_presence_of :name }
+ it { is_expected.to validate_presence_of :ci_details }
+ it { is_expected.to validate_presence_of :commit_id }
+ it { is_expected.to validate_presence_of :username }
+ end
+
+ describe 'Enums' do
+ let(:gitlab_pipeline) { build(:gitlab_pipeline) }
+
+ it do
+ expect(gitlab_pipeline).to define_enum_for(:status)
+ .with_values(created: 'created',
+ running: 'running',
+ success: 'success',
+ waiting: 'waiting',
+ preparing: 'preparing',
+ failed: 'failed',
+ pending: 'pending',
+ canceled: 'canceled',
+ skipped: 'skipped',
+ manual: 'manual',
+ scheduled: 'scheduled')
+ .backed_by_column_of_type(:string)
+ end
+ end
+end