From aedb0bb7fdc5bd185c7e4c0ab4f894b4cdd6ac04 Mon Sep 17 00:00:00 2001
From: Rutger Wessels <rwessels@gitlab.com>
Date: Fri, 16 Feb 2024 11:56:59 +0000
Subject: [PATCH] Sort projects on contributions page by most recent activity

Changelog: changed

Related issue: https://gitlab.com/gitlab-org/gitlab/-/issues/432410
---
 app/controllers/users_controller.rb           |  2 +-
 app/finders/contributed_projects_finder.rb    |  4 ++--
 .../contributed_projects_finder_spec.rb       | 20 +++++++++++++------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 689a25d141b8..8c0da0629d90 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -224,7 +224,7 @@ def personal_projects
   end
 
   def contributed_projects
-    ContributedProjectsFinder.new(user).execute(current_user)
+    ContributedProjectsFinder.new(user).execute(current_user, order_by: 'latest_activity_desc')
   end
 
   def starred_projects
diff --git a/app/finders/contributed_projects_finder.rb b/app/finders/contributed_projects_finder.rb
index eccc7d3f2bba..2652d114424d 100644
--- a/app/finders/contributed_projects_finder.rb
+++ b/app/finders/contributed_projects_finder.rb
@@ -15,13 +15,13 @@ def initialize(user)
   #                     projects, regardless of their visibility to the current_user
   #
   # Returns an ActiveRecord::Relation.
-  def execute(current_user = nil, ignore_visibility: false)
+  def execute(current_user = nil, ignore_visibility: false, order_by: 'id_desc')
     # Do not show contributed projects if the user profile is private.
     return Project.none unless can_read_profile?(current_user)
 
     segments = all_projects(current_user, ignore_visibility)
 
-    find_union(segments, Project).with_namespace.order_id_desc
+    find_union(segments, Project).with_namespace.sort_by_attribute(order_by)
   end
 
   private
diff --git a/spec/finders/contributed_projects_finder_spec.rb b/spec/finders/contributed_projects_finder_spec.rb
index e8ce02122a11..2a19664ce91f 100644
--- a/spec/finders/contributed_projects_finder_spec.rb
+++ b/spec/finders/contributed_projects_finder_spec.rb
@@ -2,7 +2,7 @@
 
 require 'spec_helper'
 
-RSpec.describe ContributedProjectsFinder do
+RSpec.describe ContributedProjectsFinder, feature_category: :groups_and_projects do
   let(:source_user) { create(:user) }
   let(:current_user) { create(:user) }
 
@@ -12,14 +12,22 @@
   let!(:private_project) { create(:project, :private) }
   let!(:internal_project) { create(:project, :internal) }
 
+  let(:default_ordering) { [internal_project, private_project, public_project] }
+
   before do
     private_project.add_maintainer(source_user)
     private_project.add_developer(current_user)
     public_project.add_maintainer(source_user)
 
-    create(:push_event, project: public_project, author: source_user)
-    create(:push_event, project: private_project, author: source_user)
-    create(:push_event, project: internal_project, author: source_user)
+    travel_to(4.hours.from_now) { create(:push_event, project: private_project, author: source_user) }
+    travel_to(3.hours.from_now) { create(:push_event, project: internal_project, author: source_user) }
+    travel_to(2.hours.from_now) { create(:push_event, project: public_project, author: source_user) }
+  end
+
+  context 'when order_by is specified' do
+    subject { finder.execute(current_user, order_by: 'latest_activity_desc') }
+
+    it { is_expected.to eq([private_project, internal_project, public_project]) }
   end
 
   describe 'activity without a current user' do
@@ -30,14 +38,14 @@
 
     it 'does return all projects when visibility gets ignored' do
       projects = finder.execute(ignore_visibility: true)
-      expect(projects).to match_array([private_project, internal_project, public_project])
+      expect(projects).to eq(default_ordering)
     end
   end
 
   describe 'activity with a current user' do
     subject { finder.execute(current_user) }
 
-    it { is_expected.to match_array([private_project, internal_project, public_project]) }
+    it { is_expected.to eq(default_ordering) }
   end
 
   context 'user with private profile' do
-- 
GitLab