mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Cache PullRequest Divergence (#10914)
* Cache PullRequest Divergence * only re-calc divergence if AddTestPullRequestTask() is exec * migrate already open pulls * finalize * take care of closed¬-merged+deleted-branch pull requests * fix nil pointer exeption Signed-off-by: 6543 <6543@obermui.de> * try this * no error its a warn * init gitea-repositories-meta * dont use gitDivergence type * CI.restart() * CI.restart() * CI.restart() * CI.restart() * check IsUserAllowedToUpdate independend from CommitsBehind
This commit is contained in:
		| @@ -12,6 +12,7 @@ import ( | |||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path" | 	"path" | ||||||
|  | 	"path/filepath" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"strings" | 	"strings" | ||||||
| @@ -25,6 +26,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/unknwon/com" | ||||||
| 	"xorm.io/xorm" | 	"xorm.io/xorm" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -54,6 +56,11 @@ func initMigrationTest(t *testing.T) func() { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	setting.NewContext() | 	setting.NewContext() | ||||||
|  |  | ||||||
|  | 	assert.True(t, len(setting.RepoRootPath) != 0) | ||||||
|  | 	assert.NoError(t, os.RemoveAll(setting.RepoRootPath)) | ||||||
|  | 	assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) | ||||||
|  |  | ||||||
| 	setting.CheckLFSVersion() | 	setting.CheckLFSVersion() | ||||||
| 	setting.InitDBConfig() | 	setting.InitDBConfig() | ||||||
| 	setting.NewLogServices(true) | 	setting.NewLogServices(true) | ||||||
|   | |||||||
| @@ -204,6 +204,8 @@ var migrations = []Migration{ | |||||||
| 	NewMigration("Refix merge base for merged pull requests", refixMergeBase), | 	NewMigration("Refix merge base for merged pull requests", refixMergeBase), | ||||||
| 	// v135 -> 136 | 	// v135 -> 136 | ||||||
| 	NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn), | 	NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn), | ||||||
|  | 	// v136 -> 137 | ||||||
|  | 	NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", addCommitDivergenceToPulls), | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetCurrentDBVersion returns the current db version | // GetCurrentDBVersion returns the current db version | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								models/migrations/v136.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								models/migrations/v136.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | // Copyright 2020 The Gitea Authors. All rights reserved. | ||||||
|  | // Use of this source code is governed by a MIT-style | ||||||
|  | // license that can be found in the LICENSE file. | ||||||
|  |  | ||||||
|  | package migrations | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  |  | ||||||
|  | 	"code.gitea.io/gitea/models" | ||||||
|  | 	"code.gitea.io/gitea/modules/log" | ||||||
|  | 	"code.gitea.io/gitea/modules/setting" | ||||||
|  | 	pull_service "code.gitea.io/gitea/services/pull" | ||||||
|  |  | ||||||
|  | 	"xorm.io/xorm" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func addCommitDivergenceToPulls(x *xorm.Engine) error { | ||||||
|  |  | ||||||
|  | 	if err := x.Sync2(new(models.PullRequest)); err != nil { | ||||||
|  | 		return fmt.Errorf("Sync2: %v", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	var last int | ||||||
|  | 	batchSize := setting.Database.IterateBufferSize | ||||||
|  | 	sess := x.NewSession() | ||||||
|  | 	defer sess.Close() | ||||||
|  | 	for { | ||||||
|  | 		if err := sess.Begin(); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		var results = make([]*models.PullRequest, 0, batchSize) | ||||||
|  | 		err := sess.Where("has_merged = ?", false).OrderBy("id").Limit(batchSize, last).Find(&results) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		if len(results) == 0 { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 		last += len(results) | ||||||
|  |  | ||||||
|  | 		for _, pr := range results { | ||||||
|  | 			divergence, err := pull_service.GetDiverging(pr) | ||||||
|  | 			if err != nil { | ||||||
|  | 				if err = pr.LoadIssue(); err != nil { | ||||||
|  | 					return fmt.Errorf("pr.LoadIssue()[%d]: %v", pr.ID, err) | ||||||
|  | 				} | ||||||
|  | 				if !pr.Issue.IsClosed { | ||||||
|  | 					return fmt.Errorf("GetDiverging: %v", err) | ||||||
|  | 				} | ||||||
|  | 				log.Warn("Could not recalculate Divergence for pull: %d", pr.ID) | ||||||
|  | 				pr.CommitsAhead = 0 | ||||||
|  | 				pr.CommitsBehind = 0 | ||||||
|  | 			} | ||||||
|  | 			if divergence != nil { | ||||||
|  | 				pr.CommitsAhead = divergence.Ahead | ||||||
|  | 				pr.CommitsBehind = divergence.Behind | ||||||
|  | 			} | ||||||
|  | 			if _, err = sess.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr); err != nil { | ||||||
|  | 				return fmt.Errorf("Update Cols: %v", err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if err := sess.Commit(); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
| @@ -42,6 +42,8 @@ type PullRequest struct { | |||||||
| 	Type            PullRequestType | 	Type            PullRequestType | ||||||
| 	Status          PullRequestStatus | 	Status          PullRequestStatus | ||||||
| 	ConflictedFiles []string `xorm:"TEXT JSON"` | 	ConflictedFiles []string `xorm:"TEXT JSON"` | ||||||
|  | 	CommitsAhead    int | ||||||
|  | 	CommitsBehind   int | ||||||
|  |  | ||||||
| 	IssueID int64  `xorm:"INDEX"` | 	IssueID int64  `xorm:"INDEX"` | ||||||
| 	Issue   *Issue `xorm:"-"` | 	Issue   *Issue `xorm:"-"` | ||||||
| @@ -615,6 +617,21 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string { | |||||||
| 	return "" | 	return "" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // UpdateCommitDivergence update Divergence of a pull request | ||||||
|  | func (pr *PullRequest) UpdateCommitDivergence(ahead, behind int) error { | ||||||
|  | 	return pr.updateCommitDivergence(x, ahead, behind) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (pr *PullRequest) updateCommitDivergence(e Engine, ahead, behind int) error { | ||||||
|  | 	if pr.ID == 0 { | ||||||
|  | 		return fmt.Errorf("pull ID is 0") | ||||||
|  | 	} | ||||||
|  | 	pr.CommitsAhead = ahead | ||||||
|  | 	pr.CommitsBehind = behind | ||||||
|  | 	_, err := e.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr) | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
| // IsSameRepo returns true if base repo and head repo is the same | // IsSameRepo returns true if base repo and head repo is the same | ||||||
| func (pr *PullRequest) IsSameRepo() bool { | func (pr *PullRequest) IsSameRepo() bool { | ||||||
| 	return pr.BaseRepoID == pr.HeadRepoID | 	return pr.BaseRepoID == pr.HeadRepoID | ||||||
|   | |||||||
| @@ -399,8 +399,6 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare | |||||||
| 	var headBranchSha string | 	var headBranchSha string | ||||||
| 	// HeadRepo may be missing | 	// HeadRepo may be missing | ||||||
| 	if pull.HeadRepo != nil { | 	if pull.HeadRepo != nil { | ||||||
| 		var err error |  | ||||||
|  |  | ||||||
| 		headGitRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath()) | 		headGitRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath()) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			ctx.ServerError("OpenRepository", err) | 			ctx.ServerError("OpenRepository", err) | ||||||
| @@ -420,19 +418,11 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if headBranchExist { | 	if headBranchExist { | ||||||
| 		allowUpdate, err := pull_service.IsUserAllowedToUpdate(pull, ctx.User) | 		ctx.Data["UpdateAllowed"], err = pull_service.IsUserAllowedToUpdate(pull, ctx.User) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			ctx.ServerError("IsUserAllowedToUpdate", err) | 			ctx.ServerError("IsUserAllowedToUpdate", err) | ||||||
| 			return nil | 			return nil | ||||||
| 		} | 		} | ||||||
| 		ctx.Data["UpdateAllowed"] = allowUpdate |  | ||||||
|  |  | ||||||
| 		divergence, err := pull_service.GetDiverging(pull) |  | ||||||
| 		if err != nil { |  | ||||||
| 			ctx.ServerError("GetDiverging", err) |  | ||||||
| 			return nil |  | ||||||
| 		} |  | ||||||
| 		ctx.Data["Divergence"] = divergence |  | ||||||
| 		ctx.Data["GetCommitMessages"] = pull_service.GetCommitMessages(pull) | 		ctx.Data["GetCommitMessages"] = pull_service.GetCommitMessages(pull) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -31,6 +31,13 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	divergence, err := GetDiverging(pr) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	pr.CommitsAhead = divergence.Ahead | ||||||
|  | 	pr.CommitsBehind = divergence.Behind | ||||||
|  |  | ||||||
| 	if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil { | 	if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -212,6 +219,15 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy | |||||||
| 						if err := models.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil { | 						if err := models.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil { | ||||||
| 							log.Error("MarkReviewsAsNotStale: %v", err) | 							log.Error("MarkReviewsAsNotStale: %v", err) | ||||||
| 						} | 						} | ||||||
|  | 						divergence, err := GetDiverging(pr) | ||||||
|  | 						if err != nil { | ||||||
|  | 							log.Error("GetDiverging: %v", err) | ||||||
|  | 						} else { | ||||||
|  | 							err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) | ||||||
|  | 							if err != nil { | ||||||
|  | 								log.Error("UpdateCommitDivergence: %v", err) | ||||||
|  | 							} | ||||||
|  | 						} | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					pr.Issue.PullRequest = pr | 					pr.Issue.PullRequest = pr | ||||||
| @@ -229,6 +245,15 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy | |||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		for _, pr := range prs { | 		for _, pr := range prs { | ||||||
|  | 			divergence, err := GetDiverging(pr) | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Error("GetDiverging: %v", err) | ||||||
|  | 			} else { | ||||||
|  | 				err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) | ||||||
|  | 				if err != nil { | ||||||
|  | 					log.Error("UpdateCommitDivergence: %v", err) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 			AddToTaskQueue(pr) | 			AddToTaskQueue(pr) | ||||||
| 		} | 		} | ||||||
| 	}) | 	}) | ||||||
|   | |||||||
| @@ -305,7 +305,7 @@ | |||||||
| 						</div> | 						</div> | ||||||
| 					{{end}} | 					{{end}} | ||||||
| 				{{end}} | 				{{end}} | ||||||
| 				{{if and .Divergence (gt .Divergence.Behind 0)}} | 				{{if gt .Issue.PullRequest.CommitsBehind 0}} | ||||||
| 				<div class="ui very compact branch-update grid"> | 				<div class="ui very compact branch-update grid"> | ||||||
| 						<div class="row"> | 						<div class="row"> | ||||||
| 						<div class="item text gray eleven wide left floated column"> | 						<div class="item text gray eleven wide left floated column"> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user