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
add lifecycle filter with `on` operator
ulfertsa834a4958926
diff --git a/app/components/projects/projects_filters_component.rb b/app/components/projects/projects_filters_component.rbindex 0dbde255ca6..8f107d34561 100644--- a/app/components/projects/projects_filters_component.rb+++ b/app/components/projects/projects_filters_component.rb@@ -56,7 +56,8 @@ class Projects::ProjectsFiltersComponent < Filter::FilterComponentQueries::Projects::Filters::ProjectStatusFilter,Queries::Projects::Filters::PublicFilter,Queries::Projects::Filters::TemplatedFilter,- Queries::Projects::Filters::TypeFilter+ Queries::Projects::Filters::TypeFilter,+ Queries::Projects::Filters::AnyStageOrGateFilter]allowlist.any? { |clazz| filter.is_a? clazz }diff --git a/app/models/queries/projects.rb b/app/models/queries/projects.rbindex 147e04f4b33..2f0bf4e2b51 100644--- a/app/models/queries/projects.rb+++ b/app/models/queries/projects.rb@@ -49,6 +49,7 @@ module Queries::Projectsfilter Filters::UserActionFilterfilter Filters::VisibleFilterfilter Filters::FavoredFilter+ filter Filters::AnyStageOrGateFilterorder Orders::DefaultOrderorder Orders::LatestActivityAtOrderdiff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rbnew file mode 100644index 00000000000..dfda6f59f03--- /dev/null+++ b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb@@ -0,0 +1,81 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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.+# ++++class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filters::Base+ def type+ :date+ end++ def available_operators+ [::Queries::Operators::OnDate]+ end++ def default_operator+ ::Queries::Operators::OnDate+ end++ def available?+ OpenProject::FeatureDecisions.stages_and_gates_active? &&+ User.current.allowed_in_any_project?(:view_project_stages_and_gates)+ end++ def human_name+ I18n.t("project.filters.any_stage_or_gate")+ end++ def where+ case operator+ when "=d"+ stage_where+ .or(gate_where)+ .arel+ .exists+ else+ raise "Unknown operator #{operator}"+ end+ end++ def stage_where+ date = Date.parse(values.first)++ Project::LifeCycleStep+ .where("project_id = #{Project.table_name}.id")+ .where(type: Project::Stage.name)+ .where("start_date <= ? AND end_date >= ?", date, date)+ end++ def gate_where+ # On gates, only the start_date is set.+ date = Date.parse(values.first)++ Project::LifeCycleStep+ .where("project_id = #{Project.table_name}.id")+ .where(type: Project::Gate.name)+ .where(start_date: date)+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex d61c17a6dea..1d8ae6b842e 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -3488,6 +3488,8 @@ en:project_verification: "Enter the project's name %{name} to verify the deletion."subprojects_confirmation: "Its subproject(s): %{value} will also be deleted."title: "Delete the project %{name}"+ filters:+ any_stage_or_gate: "Any stage or gate"identifier:warning_one: Members of the project will have to relocate the project's repositories.warning_two: Existing links to the project will no longer work.diff --git a/spec/features/projects/lists/filters_spec.rb b/spec/features/projects/lists/filters_spec.rbindex 11886c79854..bae51821ac9 100644--- a/spec/features/projects/lists/filters_spec.rb+++ b/spec/features/projects/lists/filters_spec.rb@@ -616,4 +616,45 @@ RSpec.describe "Projects list filters", :js, with_settings: { login_required?: fprojects_page.expect_columns("Name", "Created on", "Status")endend++ describe "filtering for lifecycle" do+ shared_let(:stage) do+ create(:project_stage, project:, start_date: Time.zone.today + 5.days, end_date: Time.zone.today + 10.days)+ end+ shared_let(:gate) { create(:project_gate, project: public_project, date: Time.zone.today + 8.days) }++ context "with the feature flag disabled", with_flag: { stages_and_gates: false } do+ it "does not have any lifecycle filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Any stage or gate")+ end+ end++ context "with the feature flag enabled", with_flag: { stages_and_gates: true } do+ context "with the necessary permissions" do+ before do+ project_role.add_permission!(:view_project_stages_and_gates)+ end++ it "allows filtering the projects by life cycle elements" do+ load_and_open_filters manager++ projects_page.expect_filter_available("Any stage or gate")++ projects_page.set_filter("any_stage_or_gate", "Any stage or gate", "on", [Time.zone.today + 8.days])++ projects_page.expect_projects_in_order(project, public_project)+ end+ end++ context "without the necessary permissions" do+ it "does not have any lifecycle filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Any stage or gate")+ end+ end+ end+ endenddiff --git a/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rb b/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rbnew file mode 100644index 00000000000..f9230904b3c--- /dev/null+++ b/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rb@@ -0,0 +1,93 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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"++RSpec.describe Queries::Projects::Filters::AnyStageOrGateFilter do+ it_behaves_like "basic query filter" do+ let(:class_key) { :any_stage_or_gate }+ let(:type) { :date }+ let(:model) { Project }+ let(:attribute) { :created_at }+ let(:values) { ["3"] }++ describe "human_name" do+ it "is 'Any stage or gate'" do+ expect(instance.human_name)+ .to eql I18n.t("project.filters.any_stage_or_gate")+ end+ end++ describe "default_operator" do+ it "is 'on'" do+ expect(instance.default_operator)+ .to eql Queries::Operators::OnDate+ end+ end++ describe "#available?" do+ let(:project) { build_stubbed(:project) }+ let(:user) { build_stubbed(:user) }++ current_user { user }++ before do+ mock_permissions_for(user) do |mock|+ mock.allow_in_project(*permissions, project:)+ end+ end++ context "for a user with the necessary permission and the feature flag on", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is true" do+ expect(instance)+ .to be_available+ end+ end++ context "for a user with the necessary permission and the feature flag off", with_flag: { stages_and_gates: false } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end++ context "for a user without the necessary permission", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end+ end+ end+enddiff --git a/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rbnew file mode 100644index 00000000000..6b18c526208--- /dev/null+++ b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb@@ -0,0 +1,129 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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"++RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do+ let(:instance) { described_class.new }++ shared_let(:view_role) { create(:project_role, permissions: %i[view_project_stages_and_gates]) }++ shared_let(:stage_start_date) { Time.zone.today - 5.days }+ shared_let(:stage_end_date) { Time.zone.today + 5.days }+ shared_let(:project_with_stage) do+ create(:project, name: "Project with stage") do |project|+ create(:project_stage, project:, start_date: stage_start_date, end_date: stage_end_date)+ end+ end++ shared_let(:gate_date) { Time.zone.today + 10.days }+ shared_let(:project_with_gate) do+ create(:project, name: "Project with gate") do |project|+ create(:project_gate, project:, date: gate_date)+ end+ end++ shared_let(:user) do+ create(:user, member_with_permissions: {+ project_with_stage => %i[view_project_stages_and_gates],+ project_with_gate => %i[view_project_stages_and_gates]+ })+ end++ current_user { user }++ context "with a =d (on) operator" do+ before do+ instance.where("any_stage_or_gate", "=d", values)+ end++ context "when filtering in the middle of the stage" do+ let(:values) { [stage_start_date + ((stage_end_date - stage_start_date) / 2)] }++ it "returns the project whose stage is covering an interval including the date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering on the first day of the stage" do+ let(:values) { [stage_start_date] }++ it "returns the project whose stage begins on that date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering on the last day of the stage" do+ let(:values) { [stage_end_date] }++ it "returns the project whose stage ends on that date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering before the stage" do+ let(:values) { [stage_start_date - 1.day] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering after the stage" do+ let(:values) { [stage_end_date + 1.day] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering on the day of the gate" do+ let(:values) { [gate_date] }++ it "returns the project whose gate is on the date of the value" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when filtering before the gate" do+ let(:values) { [gate_date - 1.day] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering after the gate" do+ let(:values) { [gate_date + 1.day] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end+ end+enddiff --git a/spec/models/queries/projects/project_query_results_spec.rb b/spec/models/queries/projects/project_query_results_spec.rbindex 4ed5a92d40c..4f1488b569d 100644--- a/spec/models/queries/projects/project_query_results_spec.rb+++ b/spec/models/queries/projects/project_query_results_spec.rb@@ -30,7 +30,6 @@ require "spec_helper"RSpec.describe ProjectQuery, "results" dolet(:instance) { described_class.new }- let(:base_scope) { Project.order(id: :desc) }shared_let(:view_role) { create(:project_role, permissions: %i[view_project]) }shared_let(:non_member_role) { create(:non_member, permissions: %i[view_project]) }diff --git a/spec/support/pages/projects/index.rb b/spec/support/pages/projects/index.rbindex 52fa4beb5b1..e58db025fe5 100644--- a/spec/support/pages/projects/index.rb+++ b/spec/support/pages/projects/index.rb@@ -39,11 +39,6 @@ module Pages"/projects"end- def expect_listed(*users)- rows = page.all "td.username"- expect(rows.map(&:text)).to eq(users.map(&:login))- end-def expect_projects_listed(*projects, archived: false)within_table doprojects.each do |project|@@ -182,6 +177,14 @@ module Pagesexpect(page).to have_css('[data-test-selector="filters-button-counter"]', text: count)end+ def expect_filter_available(filter_name)+ expect(page).to have_select("add_filter_select", with_options: [filter_name])+ end++ def expect_filter_not_available(filter_name)+ expect(page).to have_no_select("add_filter_select", with_options: [filter_name])+ end+def expect_no_project_create_buttonexpect(page).to have_no_css('[data-test-selector="project-new-button"]')end@@ -278,11 +281,12 @@ module Pageselsif autocomplete_filter?(selected_filter)select(human_operator, from: "operator")set_autocomplete_filter(values)- elsif name == "created_at"- select(human_operator, from: "operator")- set_created_at_filter(human_operator, values, send_keys:)elsif date_filter?(selected_filter) && human_operator == "on"- set_date_filter(values, send_keys)+ select(human_operator, from: "operator")+ set_date_filter(human_operator, values, send_keys:)+ elsif /cf_\d+/.match?(name)+ select(human_operator, from: "operator")+ set_custom_field_filter(selected_filter, human_operator, values)endendend@@ -336,7 +340,7 @@ module Pagesendend- def set_created_at_filter(human_operator, values, send_keys: false)+ def set_date_filter(human_operator, values, send_keys: false)case human_operatorwhen "on", "less than days ago", "more than days ago", "days ago"if send_keys@@ -346,7 +350,7 @@ module Pagesendwhen "between"if send_keys- find_field("from_value").send_keysvalues.first+ find_field("from_value").send_keys values.firstfind_field("to_value").send_keys values.secondelsefill_in "from_value", with: values.first@@ -372,14 +376,6 @@ module Pagesvalue_select.select values.firstend- def set_date_filter(values, send_keys)- if send_keys- find_field("value").send_keys values.first- else- fill_in "value", with: values.first- end- end-def open_filtersretry_block dotoggle_filters_section