loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

create "shared" package to workaround import loop issues

+128 -101
+6 -99
services/automerge/automerge.go
··· 7 7 "context" 8 8 "errors" 9 9 "fmt" 10 - "strconv" 11 - "strings" 12 10 13 11 "code.gitea.io/gitea/models/db" 14 12 issues_model "code.gitea.io/gitea/models/issues" ··· 24 22 "code.gitea.io/gitea/modules/queue" 25 23 notify_service "code.gitea.io/gitea/services/notify" 26 24 pull_service "code.gitea.io/gitea/services/pull" 25 + shared_automerge "code.gitea.io/gitea/services/shared/automerge" 27 26 ) 28 - 29 - // prAutoMergeQueue represents a queue to handle update pull request tests 30 - var prAutoMergeQueue *queue.WorkerPoolQueue[string] 31 27 32 28 // Init runs the task queue to that handles auto merges 33 29 func Init() error { 34 30 notify_service.RegisterNotifier(NewNotifier()) 35 31 36 - prAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler) 37 - if prAutoMergeQueue == nil { 32 + shared_automerge.PRAutoMergeQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "pr_auto_merge", handler) 33 + if shared_automerge.PRAutoMergeQueue == nil { 38 34 return fmt.Errorf("unable to create pr_auto_merge queue") 39 35 } 40 - go graceful.GetManager().RunWithCancel(prAutoMergeQueue) 36 + go graceful.GetManager().RunWithCancel(shared_automerge.PRAutoMergeQueue) 41 37 return nil 42 38 } 43 39 ··· 55 51 return nil 56 52 } 57 53 58 - func addToQueue(pr *issues_model.PullRequest, sha string) { 59 - log.Trace("Adding pullID: %d to the pull requests patch checking queue with sha %s", pr.ID, sha) 60 - if err := prAutoMergeQueue.Push(fmt.Sprintf("%d_%s", pr.ID, sha)); err != nil { 61 - log.Error("Error adding pullID: %d to the pull requests patch checking queue %v", pr.ID, err) 62 - } 63 - } 64 - 65 54 // ScheduleAutoMerge if schedule is false and no error, pull can be merged directly 66 55 func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) { 67 56 err = db.WithTx(ctx, func(ctx context.Context) error { ··· 90 79 91 80 // StartPRCheckAndAutoMergeBySHA start an automerge check and auto merge task for all pull requests of repository and SHA 92 81 func StartPRCheckAndAutoMergeBySHA(ctx context.Context, sha string, repo *repo_model.Repository) error { 93 - pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool { 94 - return !pr.HasMerged && pr.CanAutoMerge() 95 - }) 96 - if err != nil { 97 - return err 98 - } 99 - 100 - for _, pr := range pulls { 101 - addToQueue(pr, sha) 102 - } 103 - 104 - return nil 82 + return shared_automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo) 105 83 } 106 84 107 85 // StartPRCheckAndAutoMerge start an automerge check and auto merge task for a pull request 108 86 func StartPRCheckAndAutoMerge(ctx context.Context, pull *issues_model.PullRequest) { 109 - if pull == nil || pull.HasMerged || !pull.CanAutoMerge() { 110 - return 111 - } 112 - 113 - if err := pull.LoadBaseRepo(ctx); err != nil { 114 - log.Error("LoadBaseRepo: %v", err) 115 - return 116 - } 117 - 118 - gitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo) 119 - if err != nil { 120 - log.Error("OpenRepository: %v", err) 121 - return 122 - } 123 - defer gitRepo.Close() 124 - commitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName()) 125 - if err != nil { 126 - log.Error("GetRefCommitID: %v", err) 127 - return 128 - } 129 - 130 - addToQueue(pull, commitID) 131 - } 132 - 133 - func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) { 134 - gitRepo, err := gitrepo.OpenRepository(ctx, repo) 135 - if err != nil { 136 - return nil, err 137 - } 138 - defer gitRepo.Close() 139 - 140 - refs, err := gitRepo.GetRefsBySha(sha, "") 141 - if err != nil { 142 - return nil, err 143 - } 144 - 145 - pulls := make(map[int64]*issues_model.PullRequest) 146 - 147 - for _, ref := range refs { 148 - // Each pull branch starts with refs/pull/ we then go from there to find the index of the pr and then 149 - // use that to get the pr. 150 - if strings.HasPrefix(ref, git.PullPrefix) { 151 - parts := strings.Split(ref[len(git.PullPrefix):], "/") 152 - 153 - // e.g. 'refs/pull/1/head' would be []string{"1", "head"} 154 - if len(parts) != 2 { 155 - log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo) 156 - continue 157 - } 158 - 159 - prIndex, err := strconv.ParseInt(parts[0], 10, 64) 160 - if err != nil { 161 - log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo) 162 - continue 163 - } 164 - 165 - p, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, prIndex) 166 - if err != nil { 167 - // If there is no pull request for this branch, we don't try to merge it. 168 - if issues_model.IsErrPullRequestNotExist(err) { 169 - continue 170 - } 171 - return nil, err 172 - } 173 - 174 - if filter(p) { 175 - pulls[p.ID] = p 176 - } 177 - } 178 - } 179 - 180 - return pulls, nil 87 + shared_automerge.StartPRCheckAndAutoMerge(ctx, pull) 181 88 } 182 89 183 90 // handlePullRequestAutoMerge merge the pull request if all checks are successful
+2 -2
services/repository/commitstatus/commitstatus.go
··· 19 19 "code.gitea.io/gitea/modules/json" 20 20 "code.gitea.io/gitea/modules/log" 21 21 api "code.gitea.io/gitea/modules/structs" 22 - "code.gitea.io/gitea/services/automerge" 22 + shared_automerge "code.gitea.io/gitea/services/shared/automerge" 23 23 ) 24 24 25 25 func getCacheKey(repoID int64, brancheName string) string { ··· 117 117 } 118 118 119 119 if status.State.IsSuccess() { 120 - if err := automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo); err != nil { 120 + if err := shared_automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo); err != nil { 121 121 return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err) 122 122 } 123 123 }
+120
services/shared/automerge/automerge.go
··· 1 + // Copyright 2021 Gitea. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package automerge 5 + 6 + import ( 7 + "context" 8 + "fmt" 9 + "strconv" 10 + "strings" 11 + 12 + issues_model "code.gitea.io/gitea/models/issues" 13 + repo_model "code.gitea.io/gitea/models/repo" 14 + "code.gitea.io/gitea/modules/git" 15 + "code.gitea.io/gitea/modules/gitrepo" 16 + "code.gitea.io/gitea/modules/log" 17 + "code.gitea.io/gitea/modules/queue" 18 + ) 19 + 20 + // PRAutoMergeQueue represents a queue to handle update pull request tests 21 + var PRAutoMergeQueue *queue.WorkerPoolQueue[string] 22 + 23 + func addToQueue(pr *issues_model.PullRequest, sha string) { 24 + log.Trace("Adding pullID: %d to the pull requests patch checking queue with sha %s", pr.ID, sha) 25 + if err := PRAutoMergeQueue.Push(fmt.Sprintf("%d_%s", pr.ID, sha)); err != nil { 26 + log.Error("Error adding pullID: %d to the pull requests patch checking queue %v", pr.ID, err) 27 + } 28 + } 29 + 30 + // StartPRCheckAndAutoMergeBySHA start an automerge check and auto merge task for all pull requests of repository and SHA 31 + func StartPRCheckAndAutoMergeBySHA(ctx context.Context, sha string, repo *repo_model.Repository) error { 32 + pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool { 33 + return !pr.HasMerged && pr.CanAutoMerge() 34 + }) 35 + if err != nil { 36 + return err 37 + } 38 + 39 + for _, pr := range pulls { 40 + addToQueue(pr, sha) 41 + } 42 + 43 + return nil 44 + } 45 + 46 + // StartPRCheckAndAutoMerge start an automerge check and auto merge task for a pull request 47 + func StartPRCheckAndAutoMerge(ctx context.Context, pull *issues_model.PullRequest) { 48 + if pull == nil || pull.HasMerged || !pull.CanAutoMerge() { 49 + return 50 + } 51 + 52 + if err := pull.LoadBaseRepo(ctx); err != nil { 53 + log.Error("LoadBaseRepo: %v", err) 54 + return 55 + } 56 + 57 + gitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo) 58 + if err != nil { 59 + log.Error("OpenRepository: %v", err) 60 + return 61 + } 62 + defer gitRepo.Close() 63 + commitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName()) 64 + if err != nil { 65 + log.Error("GetRefCommitID: %v", err) 66 + return 67 + } 68 + 69 + addToQueue(pull, commitID) 70 + } 71 + 72 + func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) { 73 + gitRepo, err := gitrepo.OpenRepository(ctx, repo) 74 + if err != nil { 75 + return nil, err 76 + } 77 + defer gitRepo.Close() 78 + 79 + refs, err := gitRepo.GetRefsBySha(sha, "") 80 + if err != nil { 81 + return nil, err 82 + } 83 + 84 + pulls := make(map[int64]*issues_model.PullRequest) 85 + 86 + for _, ref := range refs { 87 + // Each pull branch starts with refs/pull/ we then go from there to find the index of the pr and then 88 + // use that to get the pr. 89 + if strings.HasPrefix(ref, git.PullPrefix) { 90 + parts := strings.Split(ref[len(git.PullPrefix):], "/") 91 + 92 + // e.g. 'refs/pull/1/head' would be []string{"1", "head"} 93 + if len(parts) != 2 { 94 + log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo) 95 + continue 96 + } 97 + 98 + prIndex, err := strconv.ParseInt(parts[0], 10, 64) 99 + if err != nil { 100 + log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo) 101 + continue 102 + } 103 + 104 + p, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, prIndex) 105 + if err != nil { 106 + // If there is no pull request for this branch, we don't try to merge it. 107 + if issues_model.IsErrPullRequestNotExist(err) { 108 + continue 109 + } 110 + return nil, err 111 + } 112 + 113 + if filter(p) { 114 + pulls[p.ID] = p 115 + } 116 + } 117 + } 118 + 119 + return pulls, nil 120 + }