Skip to content

Commit bfaa8c5

Browse files
authored
Merge branch 'main' into feat/redirect-after-install
2 parents b10dfae + bb6377d commit bfaa8c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+967
-1124
lines changed

.github/workflows/pull-db-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818
services:
1919
pgsql:
20-
image: postgres:12
20+
image: postgres:14
2121
env:
2222
POSTGRES_DB: test
2323
POSTGRES_PASSWORD: postgres

cmd/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
hookBatchSize = 30
27+
hookBatchSize = 500
2828
)
2929

3030
var (

models/fixtures/commit_status.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
target_url: https://example.com/builds/
88
description: My awesome CI-service
99
context: ci/awesomeness
10+
context_hash: c65f4d64a3b14a3eced0c9b36799e66e1bd5ced7
1011
creator_id: 2
1112

1213
-
@@ -18,6 +19,7 @@
1819
target_url: https://example.com/converage/
1920
description: My awesome Coverage service
2021
context: cov/awesomeness
22+
context_hash: 3929ac7bccd3fa1bf9b38ddedb77973b1b9a8cfe
2123
creator_id: 2
2224

2325
-
@@ -29,6 +31,7 @@
2931
target_url: https://example.com/converage/
3032
description: My awesome Coverage service
3133
context: cov/awesomeness
34+
context_hash: 3929ac7bccd3fa1bf9b38ddedb77973b1b9a8cfe
3235
creator_id: 2
3336

3437
-
@@ -40,6 +43,7 @@
4043
target_url: https://example.com/builds/
4144
description: My awesome CI-service
4245
context: ci/awesomeness
46+
context_hash: c65f4d64a3b14a3eced0c9b36799e66e1bd5ced7
4347
creator_id: 2
4448

4549
-
@@ -51,4 +55,5 @@
5155
target_url: https://example.com/builds/
5256
description: My awesome deploy service
5357
context: deploy/awesomeness
58+
context_hash: ae9547713a6665fc4261d0756904932085a41cf2
5459
creator_id: 2

models/git/commit_status.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,24 @@ func (status *CommitStatus) HideActionsURL(ctx context.Context) {
230230

231231
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
232232
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
233+
// This function is widely used, but it is not quite right.
234+
// Ideally it should return something like "CommitStatusSummary" with properly aggregated state.
235+
// GitHub's behavior: if all statuses are "skipped", GitHub will return "success" as the combined status.
233236
var lastStatus *CommitStatus
234237
state := api.CommitStatusSuccess
235238
for _, status := range statuses {
236-
if status.State.NoBetterThan(state) {
239+
if state == status.State || status.State.HasHigherPriorityThan(state) {
237240
state = status.State
238241
lastStatus = status
239242
}
240243
}
241244
if lastStatus == nil {
242245
if len(statuses) > 0 {
246+
// FIXME: a bad case: Gitea just returns the first commit status, its status is "skipped" in this case.
243247
lastStatus = statuses[0]
244248
} else {
249+
// FIXME: another bad case: if the "statuses" slice is empty, the returned value is an invalid CommitStatus, all its fields are empty.
250+
// Frontend code (tmpl&vue) sometimes depend on the empty fields to skip rendering commit status elements (need to double check in the future)
245251
lastStatus = &CommitStatus{}
246252
}
247253
}
@@ -298,27 +304,37 @@ type CommitStatusIndex struct {
298304
MaxIndex int64 `xorm:"index"`
299305
}
300306

307+
func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) *xorm.Session {
308+
return db.GetEngine(ctx).Table(&CommitStatus{}).
309+
Where("repo_id = ?", repoID).And("sha = ?", sha)
310+
}
311+
301312
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
302-
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, int64, error) {
303-
getBase := func() *xorm.Session {
304-
return db.GetEngine(ctx).Table(&CommitStatus{}).
305-
Where("repo_id = ?", repoID).And("sha = ?", sha)
306-
}
313+
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
307314
indices := make([]int64, 0, 10)
308-
sess := getBase().Select("max( `index` ) as `index`").
309-
GroupBy("context_hash").OrderBy("max( `index` ) desc")
315+
sess := makeRepoCommitQuery(ctx, repoID, sha).
316+
Select("max( `index` ) as `index`").
317+
GroupBy("context_hash").
318+
OrderBy("max( `index` ) desc")
310319
if !listOptions.IsListAll() {
311320
sess = db.SetSessionPagination(sess, &listOptions)
312321
}
313-
count, err := sess.FindAndCount(&indices)
314-
if err != nil {
315-
return nil, count, err
322+
if err := sess.Find(&indices); err != nil {
323+
return nil, err
316324
}
317325
statuses := make([]*CommitStatus, 0, len(indices))
318326
if len(indices) == 0 {
319-
return statuses, count, nil
327+
return statuses, nil
320328
}
321-
return statuses, count, getBase().And(builder.In("`index`", indices)).Find(&statuses)
329+
err := makeRepoCommitQuery(ctx, repoID, sha).And(builder.In("`index`", indices)).Find(&statuses)
330+
return statuses, err
331+
}
332+
333+
func CountLatestCommitStatus(ctx context.Context, repoID int64, sha string) (int64, error) {
334+
return makeRepoCommitQuery(ctx, repoID, sha).
335+
Select("count(context_hash)").
336+
GroupBy("context_hash").
337+
Count()
322338
}
323339

324340
// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs

models/git/commit_status_summary.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA
5555
}
5656

5757
func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) error {
58-
commitStatuses, _, err := GetLatestCommitStatus(ctx, repoID, sha, db.ListOptionsAll)
58+
commitStatuses, err := GetLatestCommitStatus(ctx, repoID, sha, db.ListOptionsAll)
5959
if err != nil {
6060
return err
6161
}
62-
state := CalcCommitStatus(commitStatuses)
62+
// it guarantees that commitStatuses is not empty because this function is always called after a commit status is created
63+
if len(commitStatuses) == 0 {
64+
setting.PanicInDevOrTesting("no commit statuses found for repo %d and sha %s", repoID, sha)
65+
}
66+
state := CalcCommitStatus(commitStatuses) // non-empty commitStatuses is guaranteed
6367
// mysql will return 0 when update a record which state hasn't been changed which behaviour is different from other database,
6468
// so we need to use insert in on duplicate
6569
if setting.Database.Type.IsMySQL() {

models/git/commit_status_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestGetCommitStatuses(t *testing.T) {
2626

2727
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
2828

29-
sha1 := "1234123412341234123412341234123412341234"
29+
sha1 := "1234123412341234123412341234123412341234" // the mocked commit ID in test fixtures
3030

3131
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](db.DefaultContext, &git_model.CommitStatusOptions{
3232
ListOptions: db.ListOptions{Page: 1, PageSize: 50},
@@ -256,3 +256,26 @@ func TestCommitStatusesHideActionsURL(t *testing.T) {
256256
assert.Empty(t, statuses[0].TargetURL)
257257
assert.Equal(t, "https://mycicd.org/1", statuses[1].TargetURL)
258258
}
259+
260+
func TestGetCountLatestCommitStatus(t *testing.T) {
261+
assert.NoError(t, unittest.PrepareTestDatabase())
262+
263+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
264+
265+
sha1 := "1234123412341234123412341234123412341234" // the mocked commit ID in test fixtures
266+
267+
commitStatuses, err := git_model.GetLatestCommitStatus(db.DefaultContext, repo1.ID, sha1, db.ListOptions{
268+
Page: 1,
269+
PageSize: 2,
270+
})
271+
assert.NoError(t, err)
272+
assert.Len(t, commitStatuses, 2)
273+
assert.Equal(t, structs.CommitStatusFailure, commitStatuses[0].State)
274+
assert.Equal(t, "ci/awesomeness", commitStatuses[0].Context)
275+
assert.Equal(t, structs.CommitStatusError, commitStatuses[1].State)
276+
assert.Equal(t, "deploy/awesomeness", commitStatuses[1].Context)
277+
278+
count, err := git_model.CountLatestCommitStatus(db.DefaultContext, repo1.ID, sha1)
279+
assert.NoError(t, err)
280+
assert.EqualValues(t, 3, count)
281+
}

models/issues/issue_search.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
8888
sess.Asc("issue.created_unix").Asc("issue.id")
8989
case "recentupdate":
9090
sess.Desc("issue.updated_unix").Desc("issue.created_unix").Desc("issue.id")
91+
case "recentclose":
92+
sess.Desc("issue.closed_unix").Desc("issue.created_unix").Desc("issue.id")
9193
case "leastupdate":
9294
sess.Asc("issue.updated_unix").Asc("issue.created_unix").Asc("issue.id")
9395
case "mostcomment":

models/issues/issue_update.go

Lines changed: 8 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313
"code.gitea.io/gitea/models/organization"
1414
access_model "code.gitea.io/gitea/models/perm/access"
15-
project_model "code.gitea.io/gitea/models/project"
1615
repo_model "code.gitea.io/gitea/models/repo"
17-
system_model "code.gitea.io/gitea/models/system"
1816
"code.gitea.io/gitea/models/unit"
1917
user_model "code.gitea.io/gitea/models/user"
2018
"code.gitea.io/gitea/modules/git"
@@ -715,138 +713,13 @@ func UpdateReactionsMigrationsByType(ctx context.Context, gitServiceType api.Git
715713
return err
716714
}
717715

718-
// DeleteIssuesByRepoID deletes issues by repositories id
719-
func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths []string, err error) {
720-
// MariaDB has a performance bug: https://jira.mariadb.org/browse/MDEV-16289
721-
// so here it uses "DELETE ... WHERE IN" with pre-queried IDs.
722-
sess := db.GetEngine(ctx)
723-
724-
for {
725-
issueIDs := make([]int64, 0, db.DefaultMaxInSize)
726-
727-
err := sess.Table(&Issue{}).Where("repo_id = ?", repoID).OrderBy("id").Limit(db.DefaultMaxInSize).Cols("id").Find(&issueIDs)
728-
if err != nil {
729-
return nil, err
730-
}
731-
732-
if len(issueIDs) == 0 {
733-
break
734-
}
735-
736-
// Delete content histories
737-
_, err = sess.In("issue_id", issueIDs).Delete(&ContentHistory{})
738-
if err != nil {
739-
return nil, err
740-
}
741-
742-
// Delete comments and attachments
743-
_, err = sess.In("issue_id", issueIDs).Delete(&Comment{})
744-
if err != nil {
745-
return nil, err
746-
}
747-
748-
// Dependencies for issues in this repository
749-
_, err = sess.In("issue_id", issueIDs).Delete(&IssueDependency{})
750-
if err != nil {
751-
return nil, err
752-
}
753-
754-
// Delete dependencies for issues in other repositories
755-
_, err = sess.In("dependency_id", issueIDs).Delete(&IssueDependency{})
756-
if err != nil {
757-
return nil, err
758-
}
759-
760-
_, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{})
761-
if err != nil {
762-
return nil, err
763-
}
764-
765-
_, err = sess.In("issue_id", issueIDs).Delete(&Reaction{})
766-
if err != nil {
767-
return nil, err
768-
}
769-
770-
_, err = sess.In("issue_id", issueIDs).Delete(&IssueWatch{})
771-
if err != nil {
772-
return nil, err
773-
}
774-
775-
_, err = sess.In("issue_id", issueIDs).Delete(&Stopwatch{})
776-
if err != nil {
777-
return nil, err
778-
}
779-
780-
_, err = sess.In("issue_id", issueIDs).Delete(&TrackedTime{})
781-
if err != nil {
782-
return nil, err
783-
}
784-
785-
_, err = sess.In("issue_id", issueIDs).Delete(&project_model.ProjectIssue{})
786-
if err != nil {
787-
return nil, err
788-
}
789-
790-
_, err = sess.In("dependent_issue_id", issueIDs).Delete(&Comment{})
791-
if err != nil {
792-
return nil, err
793-
}
794-
795-
var attachments []*repo_model.Attachment
796-
err = sess.In("issue_id", issueIDs).Find(&attachments)
797-
if err != nil {
798-
return nil, err
799-
}
800-
801-
for j := range attachments {
802-
attachmentPaths = append(attachmentPaths, attachments[j].RelativePath())
803-
}
804-
805-
_, err = sess.In("issue_id", issueIDs).Delete(&repo_model.Attachment{})
806-
if err != nil {
807-
return nil, err
808-
}
809-
810-
_, err = sess.In("id", issueIDs).Delete(&Issue{})
811-
if err != nil {
812-
return nil, err
813-
}
814-
}
815-
816-
return attachmentPaths, err
817-
}
818-
819-
// DeleteOrphanedIssues delete issues without a repo
820-
func DeleteOrphanedIssues(ctx context.Context) error {
821-
var attachmentPaths []string
822-
err := db.WithTx(ctx, func(ctx context.Context) error {
823-
var ids []int64
824-
825-
if err := db.GetEngine(ctx).Table("issue").Distinct("issue.repo_id").
826-
Join("LEFT", "repository", "issue.repo_id=repository.id").
827-
Where(builder.IsNull{"repository.id"}).GroupBy("issue.repo_id").
828-
Find(&ids); err != nil {
829-
return err
830-
}
831-
832-
for i := range ids {
833-
paths, err := DeleteIssuesByRepoID(ctx, ids[i])
834-
if err != nil {
835-
return err
836-
}
837-
attachmentPaths = append(attachmentPaths, paths...)
838-
}
839-
840-
return nil
841-
})
842-
if err != nil {
843-
return err
844-
}
845-
846-
// Remove issue attachment files.
847-
for i := range attachmentPaths {
848-
// FIXME: it's not right, because the attachment might not be on local filesystem
849-
system_model.RemoveAllWithNotice(ctx, "Delete issue attachment", attachmentPaths[i])
716+
func GetOrphanedIssueRepoIDs(ctx context.Context) ([]int64, error) {
717+
var repoIDs []int64
718+
if err := db.GetEngine(ctx).Table("issue").Distinct("issue.repo_id").
719+
Join("LEFT", "repository", "issue.repo_id=repository.id").
720+
Where(builder.IsNull{"repository.id"}).
721+
Find(&repoIDs); err != nil {
722+
return nil, err
850723
}
851-
return nil
724+
return repoIDs, nil
852725
}

models/issues/pull_list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ func PullRequests(ctx context.Context, baseRepoID int64, opts *PullRequestsOptio
152152
applySorts(findSession, opts.SortType, 0)
153153
findSession = db.SetSessionPagination(findSession, opts)
154154
prs := make([]*PullRequest, 0, opts.PageSize)
155-
return prs, maxResults, findSession.Find(&prs)
155+
found := findSession.Find(&prs)
156+
return prs, maxResults, found
156157
}
157158

158159
// PullRequestList defines a list of pull requests

0 commit comments

Comments
 (0)