mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 08:02:36 +09:00 
			
		
		
		
	Backport #34525 by @MarkusAmshove closes #34171 Adds a new sort option `recentclose` for issues and pull requests which will return items in a descending order of when they were closed Co-authored-by: Markus Amshove <scm@amshove.org>
This commit is contained in:
		@@ -88,6 +88,8 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
 | 
				
			|||||||
		sess.Asc("issue.created_unix").Asc("issue.id")
 | 
							sess.Asc("issue.created_unix").Asc("issue.id")
 | 
				
			||||||
	case "recentupdate":
 | 
						case "recentupdate":
 | 
				
			||||||
		sess.Desc("issue.updated_unix").Desc("issue.created_unix").Desc("issue.id")
 | 
							sess.Desc("issue.updated_unix").Desc("issue.created_unix").Desc("issue.id")
 | 
				
			||||||
 | 
						case "recentclose":
 | 
				
			||||||
 | 
							sess.Desc("issue.closed_unix").Desc("issue.created_unix").Desc("issue.id")
 | 
				
			||||||
	case "leastupdate":
 | 
						case "leastupdate":
 | 
				
			||||||
		sess.Asc("issue.updated_unix").Asc("issue.created_unix").Asc("issue.id")
 | 
							sess.Asc("issue.updated_unix").Asc("issue.created_unix").Asc("issue.id")
 | 
				
			||||||
	case "mostcomment":
 | 
						case "mostcomment":
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -152,7 +152,8 @@ func PullRequests(ctx context.Context, baseRepoID int64, opts *PullRequestsOptio
 | 
				
			|||||||
	applySorts(findSession, opts.SortType, 0)
 | 
						applySorts(findSession, opts.SortType, 0)
 | 
				
			||||||
	findSession = db.SetSessionPagination(findSession, opts)
 | 
						findSession = db.SetSessionPagination(findSession, opts)
 | 
				
			||||||
	prs := make([]*PullRequest, 0, opts.PageSize)
 | 
						prs := make([]*PullRequest, 0, opts.PageSize)
 | 
				
			||||||
	return prs, maxResults, findSession.Find(&prs)
 | 
						found := findSession.Find(&prs)
 | 
				
			||||||
 | 
						return prs, maxResults, found
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PullRequestList defines a list of pull requests
 | 
					// PullRequestList defines a list of pull requests
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,7 @@ import (
 | 
				
			|||||||
	"code.gitea.io/gitea/modules/setting"
 | 
						"code.gitea.io/gitea/modules/setting"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/require"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestPullRequest_LoadAttributes(t *testing.T) {
 | 
					func TestPullRequest_LoadAttributes(t *testing.T) {
 | 
				
			||||||
@@ -76,6 +77,47 @@ func TestPullRequestsNewest(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestPullRequests_Closed_RecentSortType(t *testing.T) {
 | 
				
			||||||
 | 
						// Issue ID | Closed At.  | Updated At
 | 
				
			||||||
 | 
						//    2     | 1707270001  | 1707270001
 | 
				
			||||||
 | 
						//    3     | 1707271000  | 1707279999
 | 
				
			||||||
 | 
						//    11    | 1707279999  | 1707275555
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							sortType             string
 | 
				
			||||||
 | 
							expectedIssueIDOrder []int64
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{"recentupdate", []int64{3, 11, 2}},
 | 
				
			||||||
 | 
							{"recentclose", []int64{11, 3, 2}},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert.NoError(t, unittest.PrepareTestDatabase())
 | 
				
			||||||
 | 
						_, err := db.Exec(db.DefaultContext, "UPDATE issue SET closed_unix = 1707270001, updated_unix = 1707270001, is_closed = true WHERE id = 2")
 | 
				
			||||||
 | 
						require.NoError(t, err)
 | 
				
			||||||
 | 
						_, err = db.Exec(db.DefaultContext, "UPDATE issue SET closed_unix = 1707271000, updated_unix = 1707279999, is_closed = true WHERE id = 3")
 | 
				
			||||||
 | 
						require.NoError(t, err)
 | 
				
			||||||
 | 
						_, err = db.Exec(db.DefaultContext, "UPDATE issue SET closed_unix = 1707279999, updated_unix = 1707275555, is_closed = true WHERE id = 11")
 | 
				
			||||||
 | 
						require.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.sortType, func(t *testing.T) {
 | 
				
			||||||
 | 
								prs, _, err := issues_model.PullRequests(db.DefaultContext, 1, &issues_model.PullRequestsOptions{
 | 
				
			||||||
 | 
									ListOptions: db.ListOptions{
 | 
				
			||||||
 | 
										Page: 1,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									State:    "closed",
 | 
				
			||||||
 | 
									SortType: test.sortType,
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								require.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if assert.Len(t, prs, len(test.expectedIssueIDOrder)) {
 | 
				
			||||||
 | 
									for i := range test.expectedIssueIDOrder {
 | 
				
			||||||
 | 
										assert.Equal(t, test.expectedIssueIDOrder[i], prs[i].IssueID)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLoadRequestedReviewers(t *testing.T) {
 | 
					func TestLoadRequestedReviewers(t *testing.T) {
 | 
				
			||||||
	assert.NoError(t, unittest.PrepareTestDatabase())
 | 
						assert.NoError(t, unittest.PrepareTestDatabase())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -73,7 +73,7 @@ func ListPullRequests(ctx *context.APIContext) {
 | 
				
			|||||||
	//   in: query
 | 
						//   in: query
 | 
				
			||||||
	//   description: Type of sort
 | 
						//   description: Type of sort
 | 
				
			||||||
	//   type: string
 | 
						//   type: string
 | 
				
			||||||
	//   enum: [oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority]
 | 
						//   enum: [oldest, recentupdate, recentclose, leastupdate, mostcomment, leastcomment, priority]
 | 
				
			||||||
	// - name: milestone
 | 
						// - name: milestone
 | 
				
			||||||
	//   in: query
 | 
						//   in: query
 | 
				
			||||||
	//   description: ID of the milestone
 | 
						//   description: ID of the milestone
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							@@ -12811,6 +12811,7 @@
 | 
				
			|||||||
            "enum": [
 | 
					            "enum": [
 | 
				
			||||||
              "oldest",
 | 
					              "oldest",
 | 
				
			||||||
              "recentupdate",
 | 
					              "recentupdate",
 | 
				
			||||||
 | 
					              "recentclose",
 | 
				
			||||||
              "leastupdate",
 | 
					              "leastupdate",
 | 
				
			||||||
              "mostcomment",
 | 
					              "mostcomment",
 | 
				
			||||||
              "leastcomment",
 | 
					              "leastcomment",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user