summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2023-02-03 23:11:48 +0000
committerGitHub <noreply@github.com>2023-02-03 18:11:48 -0500
commit3c5655ce18056277917092d370bbdfbcdaaa8ae6 (patch)
tree3c0f003e14a1286b56c57d52410e8fc661dca4fb /models
parent01f082287d7957ed63a0865b26e04ad23382c715 (diff)
downloadgitea-3c5655ce18056277917092d370bbdfbcdaaa8ae6.tar.gz
gitea-3c5655ce18056277917092d370bbdfbcdaaa8ae6.zip
Improve trace logging for pulls and processes (#22633)
Our trace logging is far from perfect and is difficult to follow. This PR: * Add trace logging for process manager add and remove. * Fixes an errant read file for git refs in getMergeCommit * Brings in the pullrequest `String` and `ColorFormat` methods introduced in #22568 * Adds a lot more logging in to testPR etc. Ref #22578 --------- Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'models')
-rw-r--r--models/issues/pull.go67
1 files changed, 63 insertions, 4 deletions
diff --git a/models/issues/pull.go b/models/issues/pull.go
index 6ff6502e4e..044fb5fa04 100644
--- a/models/issues/pull.go
+++ b/models/issues/pull.go
@@ -8,6 +8,7 @@ import (
"context"
"fmt"
"io"
+ "strconv"
"strings"
"code.gitea.io/gitea/models/db"
@@ -132,6 +133,27 @@ const (
PullRequestStatusAncestor
)
+func (status PullRequestStatus) String() string {
+ switch status {
+ case PullRequestStatusConflict:
+ return "CONFLICT"
+ case PullRequestStatusChecking:
+ return "CHECKING"
+ case PullRequestStatusMergeable:
+ return "MERGEABLE"
+ case PullRequestStatusManuallyMerged:
+ return "MANUALLY_MERGED"
+ case PullRequestStatusError:
+ return "ERROR"
+ case PullRequestStatusEmpty:
+ return "EMPTY"
+ case PullRequestStatusAncestor:
+ return "ANCESTOR"
+ default:
+ return strconv.Itoa(int(status))
+ }
+}
+
// PullRequestFlow the flow of pull request
type PullRequestFlow int
@@ -202,6 +224,42 @@ func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error {
return err
}
+// ColorFormat writes a colored string to identify this struct
+func (pr *PullRequest) ColorFormat(s fmt.State) {
+ if pr == nil {
+ log.ColorFprintf(s, "PR[%d]%s#%d[%s...%s:%s]",
+ log.NewColoredIDValue(0),
+ log.NewColoredValue("<nil>/<nil>"),
+ log.NewColoredIDValue(0),
+ log.NewColoredValue("<nil>"),
+ log.NewColoredValue("<nil>/<nil>"),
+ log.NewColoredValue("<nil>"),
+ )
+ return
+ }
+
+ log.ColorFprintf(s, "PR[%d]", log.NewColoredIDValue(pr.ID))
+ if pr.BaseRepo != nil {
+ log.ColorFprintf(s, "%s#%d[%s...", log.NewColoredValue(pr.BaseRepo.FullName()),
+ log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
+ } else {
+ log.ColorFprintf(s, "Repo[%d]#%d[%s...", log.NewColoredIDValue(pr.BaseRepoID),
+ log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
+ }
+ if pr.HeadRepoID == pr.BaseRepoID {
+ log.ColorFprintf(s, "%s]", log.NewColoredValue(pr.HeadBranch))
+ } else if pr.HeadRepo != nil {
+ log.ColorFprintf(s, "%s:%s]", log.NewColoredValue(pr.HeadRepo.FullName()), log.NewColoredValue(pr.HeadBranch))
+ } else {
+ log.ColorFprintf(s, "Repo[%d]:%s]", log.NewColoredIDValue(pr.HeadRepoID), log.NewColoredValue(pr.HeadBranch))
+ }
+}
+
+// String represents the pr as a simple string
+func (pr *PullRequest) String() string {
+ return log.ColorFormatAsString(pr)
+}
+
// MustHeadUserName returns the HeadRepo's username if failed return blank
func (pr *PullRequest) MustHeadUserName(ctx context.Context) string {
if err := pr.LoadHeadRepo(ctx); err != nil {
@@ -234,7 +292,8 @@ func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
return nil
}
-// LoadHeadRepo loads the head repository
+// LoadHeadRepo loads the head repository, pr.HeadRepo will remain nil if it does not exist
+// and thus ErrRepoNotExist will never be returned
func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
if !pr.isHeadRepoLoaded && pr.HeadRepo == nil && pr.HeadRepoID > 0 {
if pr.HeadRepoID == pr.BaseRepoID {
@@ -249,14 +308,14 @@ func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
pr.HeadRepo, err = repo_model.GetRepositoryByID(ctx, pr.HeadRepoID)
if err != nil && !repo_model.IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work
- return fmt.Errorf("GetRepositoryByID(head): %w", err)
+ return fmt.Errorf("pr[%d].LoadHeadRepo[%d]: %w", pr.ID, pr.HeadRepoID, err)
}
pr.isHeadRepoLoaded = true
}
return nil
}
-// LoadBaseRepo loads the target repository
+// LoadBaseRepo loads the target repository. ErrRepoNotExist may be returned.
func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
if pr.BaseRepo != nil {
return nil
@@ -274,7 +333,7 @@ func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID)
if err != nil {
- return fmt.Errorf("repo_model.GetRepositoryByID(base): %w", err)
+ return fmt.Errorf("pr[%d].LoadBaseRepo[%d]: %w", pr.ID, pr.BaseRepoID, err)
}
return nil
}