Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaofficial/main'
Browse files Browse the repository at this point in the history
* giteaofficial/main:
  [skip ci] Updated translations via Crowdin
  Show correct SSL Mode on "install page" (go-gitea#25818)
  Fix incorrect oldest sort in project list (go-gitea#25806)
  Fix the error message when the token is incorrect (go-gitea#25701)
  Exclude default branch from pushed branch hint (go-gitea#25795)
  For API attachments, use API URL (go-gitea#25639)
  • Loading branch information
zjjhot committed Jul 12, 2023
2 parents 28f689d + d1e066f commit 252b54e
Show file tree
Hide file tree
Showing 32 changed files with 453 additions and 128 deletions.
8 changes: 8 additions & 0 deletions models/fixtures/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
creator_id: 2
board_type: 1
type: 2
created_unix: 1688973030
updated_unix: 1688973030

-
id: 2
Expand All @@ -17,6 +19,8 @@
creator_id: 3
board_type: 1
type: 2
created_unix: 1688973010
updated_unix: 1688973010

-
id: 3
Expand All @@ -27,6 +31,8 @@
creator_id: 5
board_type: 1
type: 2
created_unix: 1688973020
updated_unix: 1688973020

-
id: 4
Expand All @@ -37,3 +43,5 @@
creator_id: 2
board_type: 1
type: 2
created_unix: 1688973000
updated_unix: 1688973000
4 changes: 3 additions & 1 deletion models/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
}

// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (BranchList, error) {
// except the indicate branch
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, excludeBranchName string) (BranchList, error) {
branches := make(BranchList, 0, 2)
subQuery := builder.Select("head_branch").From("pull_request").
InnerJoin("issue", "issue.id = pull_request.issue_id").
Expand All @@ -392,6 +393,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (B
})
err := db.GetEngine(ctx).
Where("pusher_id=? AND is_deleted=?", userID, false).
And("name <> ?", excludeBranchName).
And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()).
NotIn("name", subQuery).
OrderBy("branch.updated_unix DESC").
Expand Down
28 changes: 15 additions & 13 deletions models/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ type SearchOptions struct {
RepoID int64
Page int
IsClosed util.OptionalBool
SortType string
OrderBy db.SearchOrderBy
Type Type
}

Expand Down Expand Up @@ -226,26 +226,28 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) {
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project))
}

func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy {
switch sortType {
case "oldest":
return db.SearchOrderByOldest
case "recentupdate":
return db.SearchOrderByRecentUpdated
case "leastupdate":
return db.SearchOrderByLeastUpdated
default:
return db.SearchOrderByNewest
}
}

// FindProjects returns a list of all projects that have been created in the repository
func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds())
e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String())
projects := make([]*Project, 0, setting.UI.IssuePagingNum)

if opts.Page > 0 {
e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
}

switch opts.SortType {
case "oldest":
e.Desc("created_unix")
case "recentupdate":
e.Desc("updated_unix")
case "leastupdate":
e.Asc("updated_unix")
default:
e.Asc("created_unix")
}

count, err := e.FindAndCount(&projects)
return projects, count, err
}
Expand Down
39 changes: 39 additions & 0 deletions models/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,42 @@ func TestProject(t *testing.T) {

assert.True(t, projectFromDB.IsClosed)
}

func TestProjectsSort(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

tests := []struct {
sortType string
wants []int64
}{
{
sortType: "default",
wants: []int64{1, 3, 2, 4},
},
{
sortType: "oldest",
wants: []int64{4, 2, 3, 1},
},
{
sortType: "recentupdate",
wants: []int64{1, 3, 2, 4},
},
{
sortType: "leastupdate",
wants: []int64{4, 2, 3, 1},
},
}

for _, tt := range tests {
projects, count, err := FindProjects(db.DefaultContext, SearchOptions{
OrderBy: GetSearchOrderByBySortType(tt.sortType),
})
assert.NoError(t, err)
assert.EqualValues(t, int64(4), count)
if assert.Len(t, projects, 4) {
for i := range projects {
assert.EqualValues(t, tt.wants[i], projects[i].ID)
}
}
}
}
7 changes: 0 additions & 7 deletions models/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ func (a *Attachment) DownloadURL() string {
return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
}

// _____ __ __ .__ __
// / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_
// / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\
// / | \ | | | / __ \\ \___| Y \ Y Y \ ___/| | \ |
// \____|__ /__| |__| (____ /\___ >___| /__|_| /\___ >___| /__|
// \/ \/ \/ \/ \/ \/ \/

// ErrAttachmentNotExist represents a "AttachmentNotExist" kind of error.
type ErrAttachmentNotExist struct {
ID int64
Expand Down
Loading

0 comments on commit 252b54e

Please sign in to comment.