Skip to content

Commit 0d50b75

Browse files
authored
Merge branch 'go-gitea:main' into main
2 parents bfa2d10 + bb6377d commit 0d50b75

32 files changed

+332
-508
lines changed

models/git/commit_status.go

Lines changed: 7 additions & 1 deletion
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
}

models/git/commit_status_summary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) er
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/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
}

modules/actions/workflows.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ func IsWorkflow(path string) bool {
4343
return strings.HasPrefix(path, ".gitea/workflows") || strings.HasPrefix(path, ".github/workflows")
4444
}
4545

46-
func ListWorkflows(commit *git.Commit) (git.Entries, error) {
47-
tree, err := commit.SubTree(".gitea/workflows")
46+
func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
47+
rpath := ".gitea/workflows"
48+
tree, err := commit.SubTree(rpath)
4849
if _, ok := err.(git.ErrNotExist); ok {
49-
tree, err = commit.SubTree(".github/workflows")
50+
rpath = ".github/workflows"
51+
tree, err = commit.SubTree(rpath)
5052
}
5153
if _, ok := err.(git.ErrNotExist); ok {
52-
return nil, nil
54+
return "", nil, nil
5355
}
5456
if err != nil {
55-
return nil, err
57+
return "", nil, err
5658
}
5759

5860
entries, err := tree.ListEntriesRecursiveFast()
5961
if err != nil {
60-
return nil, err
62+
return "", nil, err
6163
}
6264

6365
ret := make(git.Entries, 0, len(entries))
@@ -66,7 +68,7 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
6668
ret = append(ret, entry)
6769
}
6870
}
69-
return ret, nil
71+
return rpath, ret, nil
7072
}
7173

7274
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
@@ -102,7 +104,7 @@ func DetectWorkflows(
102104
payload api.Payloader,
103105
detectSchedule bool,
104106
) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
105-
entries, err := ListWorkflows(commit)
107+
_, entries, err := ListWorkflows(commit)
106108
if err != nil {
107109
return nil, nil, err
108110
}
@@ -147,7 +149,7 @@ func DetectWorkflows(
147149
}
148150

149151
func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
150-
entries, err := ListWorkflows(commit)
152+
_, entries, err := ListWorkflows(commit)
151153
if err != nil {
152154
return nil, err
153155
}

modules/structs/commit_status.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const (
1818
CommitStatusFailure CommitStatusState = "failure"
1919
// CommitStatusWarning is for when the CommitStatus is Warning
2020
CommitStatusWarning CommitStatusState = "warning"
21+
// CommitStatusSkipped is for when CommitStatus is Skipped
22+
CommitStatusSkipped CommitStatusState = "skipped"
2123
)
2224

2325
var commitStatusPriorities = map[CommitStatusState]int{
@@ -26,25 +28,17 @@ var commitStatusPriorities = map[CommitStatusState]int{
2628
CommitStatusWarning: 2,
2729
CommitStatusPending: 3,
2830
CommitStatusSuccess: 4,
31+
CommitStatusSkipped: 5,
2932
}
3033

3134
func (css CommitStatusState) String() string {
3235
return string(css)
3336
}
3437

35-
// NoBetterThan returns true if this State is no better than the given State
36-
// This function only handles the states defined in CommitStatusPriorities
37-
func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool {
38-
// NoBetterThan only handles the 5 states above
39-
if _, exist := commitStatusPriorities[css]; !exist {
40-
return false
41-
}
42-
43-
if _, exist := commitStatusPriorities[css2]; !exist {
44-
return false
45-
}
46-
47-
return commitStatusPriorities[css] <= commitStatusPriorities[css2]
38+
// HasHigherPriorityThan returns true if this state has higher priority than the other
39+
// Undefined states are considered to have the highest priority like CommitStatusError(0)
40+
func (css CommitStatusState) HasHigherPriorityThan(other CommitStatusState) bool {
41+
return commitStatusPriorities[css] < commitStatusPriorities[other]
4842
}
4943

5044
// IsPending represents if commit status state is pending

0 commit comments

Comments
 (0)