Skip to content

Commit cb7ea00

Browse files
author
Loïc Dachary
committed
Revert "Improve trace logging for pulls and processes (go-gitea#22633) (go-gitea#22812)"
This reverts commit e423858. Refs: https://codeberg.org/forgejo/forgejo/issues/395
1 parent cd299fc commit cb7ea00

File tree

9 files changed

+144
-227
lines changed

9 files changed

+144
-227
lines changed

models/issues/pull.go

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12-
"strconv"
1312
"strings"
1413

1514
"code.gitea.io/gitea/models/db"
@@ -134,27 +133,6 @@ const (
134133
PullRequestStatusAncestor
135134
)
136135

137-
func (status PullRequestStatus) String() string {
138-
switch status {
139-
case PullRequestStatusConflict:
140-
return "CONFLICT"
141-
case PullRequestStatusChecking:
142-
return "CHECKING"
143-
case PullRequestStatusMergeable:
144-
return "MERGEABLE"
145-
case PullRequestStatusManuallyMerged:
146-
return "MANUALLY_MERGED"
147-
case PullRequestStatusError:
148-
return "ERROR"
149-
case PullRequestStatusEmpty:
150-
return "EMPTY"
151-
case PullRequestStatusAncestor:
152-
return "ANCESTOR"
153-
default:
154-
return strconv.Itoa(int(status))
155-
}
156-
}
157-
158136
// PullRequestFlow the flow of pull request
159137
type PullRequestFlow int
160138

@@ -226,42 +204,6 @@ func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error {
226204
return err
227205
}
228206

229-
// ColorFormat writes a colored string to identify this struct
230-
func (pr *PullRequest) ColorFormat(s fmt.State) {
231-
if pr == nil {
232-
log.ColorFprintf(s, "PR[%d]%s#%d[%s...%s:%s]",
233-
log.NewColoredIDValue(0),
234-
log.NewColoredValue("<nil>/<nil>"),
235-
log.NewColoredIDValue(0),
236-
log.NewColoredValue("<nil>"),
237-
log.NewColoredValue("<nil>/<nil>"),
238-
log.NewColoredValue("<nil>"),
239-
)
240-
return
241-
}
242-
243-
log.ColorFprintf(s, "PR[%d]", log.NewColoredIDValue(pr.ID))
244-
if pr.BaseRepo != nil {
245-
log.ColorFprintf(s, "%s#%d[%s...", log.NewColoredValue(pr.BaseRepo.FullName()),
246-
log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
247-
} else {
248-
log.ColorFprintf(s, "Repo[%d]#%d[%s...", log.NewColoredIDValue(pr.BaseRepoID),
249-
log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
250-
}
251-
if pr.HeadRepoID == pr.BaseRepoID {
252-
log.ColorFprintf(s, "%s]", log.NewColoredValue(pr.HeadBranch))
253-
} else if pr.HeadRepo != nil {
254-
log.ColorFprintf(s, "%s:%s]", log.NewColoredValue(pr.HeadRepo.FullName()), log.NewColoredValue(pr.HeadBranch))
255-
} else {
256-
log.ColorFprintf(s, "Repo[%d]:%s]", log.NewColoredIDValue(pr.HeadRepoID), log.NewColoredValue(pr.HeadBranch))
257-
}
258-
}
259-
260-
// String represents the pr as a simple string
261-
func (pr *PullRequest) String() string {
262-
return log.ColorFormatAsString(pr)
263-
}
264-
265207
// MustHeadUserName returns the HeadRepo's username if failed return blank
266208
func (pr *PullRequest) MustHeadUserName() string {
267209
if err := pr.LoadHeadRepo(); err != nil {
@@ -313,7 +255,7 @@ func (pr *PullRequest) LoadHeadRepoCtx(ctx context.Context) (err error) {
313255

314256
pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID)
315257
if err != nil && !repo_model.IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work
316-
return fmt.Errorf("pr[%d].LoadHeadRepo[%d]: %w", pr.ID, pr.HeadRepoID, err)
258+
return fmt.Errorf("getRepositoryByID(head): %w", err)
317259
}
318260
pr.isHeadRepoLoaded = true
319261
}
@@ -348,7 +290,7 @@ func (pr *PullRequest) LoadBaseRepoCtx(ctx context.Context) (err error) {
348290

349291
pr.BaseRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.BaseRepoID)
350292
if err != nil {
351-
return fmt.Errorf("pr[%d].LoadBaseRepo[%d]: %w", pr.ID, pr.BaseRepoID, err)
293+
return fmt.Errorf("repo_model.GetRepositoryByID(base): %w", err)
352294
}
353295
return nil
354296
}

modules/log/colors.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,6 @@ func (cv *ColoredValue) Format(s fmt.State, c rune) {
384384
s.Write(*cv.resetBytes)
385385
}
386386

387-
// ColorFormatAsString returns the result of the ColorFormat without the color
388-
func ColorFormatAsString(colorVal ColorFormatted) string {
389-
s := new(strings.Builder)
390-
_, _ = ColorFprintf(&protectedANSIWriter{w: s, mode: removeColor}, "%-v", colorVal)
391-
return s.String()
392-
}
393-
394387
// SetColorBytes will allow a user to set the colorBytes of a colored value
395388
func (cv *ColoredValue) SetColorBytes(colorBytes []byte) {
396389
cv.colorBytes = &colorBytes

modules/log/log.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"runtime"
1111
"strings"
1212
"sync"
13-
14-
"code.gitea.io/gitea/modules/process"
1513
)
1614

1715
type loggerMap struct {
@@ -288,15 +286,6 @@ func (l *LoggerAsWriter) Log(msg string) {
288286
}
289287

290288
func init() {
291-
process.Trace = func(start bool, pid process.IDType, description string, parentPID process.IDType, typ string) {
292-
if start && parentPID != "" {
293-
Log(1, TRACE, "Start %s: %s (from %s) (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(parentPID, FgYellow), NewColoredValue(typ, Reset))
294-
} else if start {
295-
Log(1, TRACE, "Start %s: %s (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(typ, Reset))
296-
} else {
297-
Log(1, TRACE, "Done %s: %s", NewColoredValue(pid, FgHiYellow), NewColoredValue(description, Reset))
298-
}
299-
}
300289
_, filename, _, _ := runtime.Caller(0)
301290
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
302291
if prefix == filename {

modules/process/manager.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package process
77

88
import (
99
"context"
10-
"log"
1110
"runtime/pprof"
1211
"strconv"
1312
"sync"
@@ -45,18 +44,6 @@ type IDType string
4544
// - it is simply an alias for context.CancelFunc and is only for documentary purposes
4645
type FinishedFunc = context.CancelFunc
4746

48-
var Trace = defaultTrace // this global can be overridden by particular logging packages - thus avoiding import cycles
49-
50-
func defaultTrace(start bool, pid IDType, description string, parentPID IDType, typ string) {
51-
if start && parentPID != "" {
52-
log.Printf("start process %s: %s (from %s) (%s)", pid, description, parentPID, typ)
53-
} else if start {
54-
log.Printf("start process %s: %s (%s)", pid, description, typ)
55-
} else {
56-
log.Printf("end process %s: %s", pid, description)
57-
}
58-
}
59-
6047
// Manager manages all processes and counts PIDs.
6148
type Manager struct {
6249
mutex sync.Mutex
@@ -168,7 +155,6 @@ func (pm *Manager) Add(ctx context.Context, description string, cancel context.C
168155

169156
pm.processMap[pid] = process
170157
pm.mutex.Unlock()
171-
Trace(true, pid, description, parentPID, processType)
172158

173159
pprofCtx := pprof.WithLabels(ctx, pprof.Labels(DescriptionPProfLabel, description, PPIDPProfLabel, string(parentPID), PIDPProfLabel, string(pid), ProcessTypePProfLabel, processType))
174160
if currentlyRunning {
@@ -200,12 +186,18 @@ func (pm *Manager) nextPID() (start time.Time, pid IDType) {
200186
return start, pid
201187
}
202188

189+
// Remove a process from the ProcessManager.
190+
func (pm *Manager) Remove(pid IDType) {
191+
pm.mutex.Lock()
192+
delete(pm.processMap, pid)
193+
pm.mutex.Unlock()
194+
}
195+
203196
func (pm *Manager) remove(process *process) {
204197
pm.mutex.Lock()
205198
defer pm.mutex.Unlock()
206199
if p := pm.processMap[process.PID]; p == process {
207200
delete(pm.processMap, process.PID)
208-
Trace(false, process.PID, process.Description, process.ParentPID, process.Type)
209201
}
210202
}
211203

modules/process/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestManager_Remove(t *testing.T) {
8383

8484
assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids got %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID())
8585

86-
finished()
86+
pm.Remove(GetPID(p2Ctx))
8787

8888
_, exists := pm.processMap[GetPID(p2Ctx)]
8989
assert.False(t, exists, "PID %d is in the list but shouldn't", GetPID(p2Ctx))

routers/web/repo/pull.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -912,54 +912,59 @@ func MergePullRequest(ctx *context.Context) {
912912
pr := issue.PullRequest
913913
pr.Issue = issue
914914
pr.Issue.Repo = ctx.Repo.Repository
915-
manualMerge := repo_model.MergeStyle(form.Do) == repo_model.MergeStyleManuallyMerged
915+
manuallMerge := repo_model.MergeStyle(form.Do) == repo_model.MergeStyleManuallyMerged
916916
forceMerge := form.ForceMerge != nil && *form.ForceMerge
917917

918918
// start with merging by checking
919-
if err := pull_service.CheckPullMergable(ctx, ctx.Doer, &ctx.Repo.Permission, pr, manualMerge, forceMerge); err != nil {
920-
switch {
921-
case errors.Is(err, pull_service.ErrIsClosed):
919+
if err := pull_service.CheckPullMergable(ctx, ctx.Doer, &ctx.Repo.Permission, pr, manuallMerge, forceMerge); err != nil {
920+
if errors.Is(err, pull_service.ErrIsClosed) {
922921
if issue.IsPull {
923922
ctx.Flash.Error(ctx.Tr("repo.pulls.is_closed"))
923+
ctx.Redirect(issue.Link())
924924
} else {
925925
ctx.Flash.Error(ctx.Tr("repo.issues.closed_title"))
926+
ctx.Redirect(issue.Link())
926927
}
927-
case errors.Is(err, pull_service.ErrUserNotAllowedToMerge):
928+
} else if errors.Is(err, pull_service.ErrUserNotAllowedToMerge) {
928929
ctx.Flash.Error(ctx.Tr("repo.pulls.update_not_allowed"))
929-
case errors.Is(err, pull_service.ErrHasMerged):
930+
ctx.Redirect(issue.Link())
931+
} else if errors.Is(err, pull_service.ErrHasMerged) {
930932
ctx.Flash.Error(ctx.Tr("repo.pulls.has_merged"))
931-
case errors.Is(err, pull_service.ErrIsWorkInProgress):
933+
ctx.Redirect(issue.Link())
934+
} else if errors.Is(err, pull_service.ErrIsWorkInProgress) {
932935
ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_wip"))
933-
case errors.Is(err, pull_service.ErrNotMergableState):
936+
ctx.Redirect(issue.Link())
937+
} else if errors.Is(err, pull_service.ErrNotMergableState) {
934938
ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_not_ready"))
935-
case models.IsErrDisallowedToMerge(err):
939+
ctx.Redirect(issue.Link())
940+
} else if models.IsErrDisallowedToMerge(err) {
936941
ctx.Flash.Error(ctx.Tr("repo.pulls.no_merge_not_ready"))
937-
case asymkey_service.IsErrWontSign(err):
938-
ctx.Flash.Error(err.Error()) // has no translation ...
939-
case errors.Is(err, pull_service.ErrDependenciesLeft):
942+
ctx.Redirect(issue.Link())
943+
} else if asymkey_service.IsErrWontSign(err) {
944+
ctx.Flash.Error(err.Error()) // has not translation ...
945+
ctx.Redirect(issue.Link())
946+
} else if errors.Is(err, pull_service.ErrDependenciesLeft) {
940947
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
941-
default:
948+
ctx.Redirect(issue.Link())
949+
} else {
942950
ctx.ServerError("WebCheck", err)
943-
return
944951
}
945-
946-
ctx.Redirect(issue.Link())
947952
return
948953
}
949954

950955
// handle manually-merged mark
951-
if manualMerge {
956+
if manuallMerge {
952957
if err := pull_service.MergedManually(pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil {
953-
switch {
954-
955-
case models.IsErrInvalidMergeStyle(err):
958+
if models.IsErrInvalidMergeStyle(err) {
956959
ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
957-
case strings.Contains(err.Error(), "Wrong commit ID"):
960+
ctx.Redirect(issue.Link())
961+
} else if strings.Contains(err.Error(), "Wrong commit ID") {
958962
ctx.Flash.Error(ctx.Tr("repo.pulls.wrong_commit_id"))
959-
default:
963+
ctx.Redirect(issue.Link())
964+
} else {
960965
ctx.ServerError("MergedManually", err)
961-
return
962966
}
967+
return
963968
}
964969

965970
ctx.Redirect(issue.Link())

services/automerge/automerge.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.
165165

166166
func handlePull(pullID int64, sha string) {
167167
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(),
168-
fmt.Sprintf("Handle AutoMerge of PR[%d] with sha[%s]", pullID, sha))
168+
fmt.Sprintf("Handle AutoMerge of pull[%d] with sha[%s]", pullID, sha))
169169
defer finished()
170170

171171
pr, err := issues_model.GetPullRequestByID(ctx, pullID)
@@ -177,7 +177,7 @@ func handlePull(pullID int64, sha string) {
177177
// Check if there is a scheduled pr in the db
178178
exists, scheduledPRM, err := pull_model.GetScheduledMergeByPullID(ctx, pr.ID)
179179
if err != nil {
180-
log.Error("%-v GetScheduledMergeByPullID: %v", pr, err)
180+
log.Error("pull[%d] GetScheduledMergeByPullID: %v", pr.ID, err)
181181
return
182182
}
183183
if !exists {
@@ -189,54 +189,54 @@ func handlePull(pullID int64, sha string) {
189189
// did not succeed or was not finished yet.
190190

191191
if err = pr.LoadHeadRepoCtx(ctx); err != nil {
192-
log.Error("%-v LoadHeadRepo: %v", pr, err)
192+
log.Error("pull[%d] LoadHeadRepoCtx: %v", pr.ID, err)
193193
return
194194
}
195195

196196
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
197197
if err != nil {
198-
log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
198+
log.Error("OpenRepository: %v", err)
199199
return
200200
}
201201
defer headGitRepo.Close()
202202

203203
headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch)
204204

205205
if pr.HeadRepo == nil || !headBranchExist {
206-
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
206+
log.Warn("Head branch of auto merge pr does not exist [HeadRepoID: %d, Branch: %s, PR ID: %d]", pr.HeadRepoID, pr.HeadBranch, pr.ID)
207207
return
208208
}
209209

210210
// Check if all checks succeeded
211211
pass, err := pull_service.IsPullCommitStatusPass(ctx, pr)
212212
if err != nil {
213-
log.Error("%-v IsPullCommitStatusPass: %v", pr, err)
213+
log.Error("IsPullCommitStatusPass: %v", err)
214214
return
215215
}
216216
if !pass {
217-
log.Info("Scheduled auto merge %-v has unsuccessful status checks", pr)
217+
log.Info("Scheduled auto merge pr has unsuccessful status checks [PullID: %d]", pr.ID)
218218
return
219219
}
220220

221221
// Merge if all checks succeeded
222222
doer, err := user_model.GetUserByIDCtx(ctx, scheduledPRM.DoerID)
223223
if err != nil {
224-
log.Error("Unable to get scheduled User[%d]: %v", scheduledPRM.DoerID, err)
224+
log.Error("GetUserByIDCtx: %v", err)
225225
return
226226
}
227227

228228
perm, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
229229
if err != nil {
230-
log.Error("GetUserRepoPermission %-v: %v", pr.HeadRepo, err)
230+
log.Error("GetUserRepoPermission: %v", err)
231231
return
232232
}
233233

234234
if err := pull_service.CheckPullMergable(ctx, doer, &perm, pr, false, false); err != nil {
235235
if errors.Is(pull_service.ErrUserNotAllowedToMerge, err) {
236-
log.Info("%-v was scheduled to automerge by an unauthorized user", pr)
236+
log.Info("PR %d was scheduled to automerge by an unauthorized user", pr.ID)
237237
return
238238
}
239-
log.Error("%-v CheckPullMergable: %v", pr, err)
239+
log.Error("pull[%d] CheckPullMergable: %v", pr.ID, err)
240240
return
241241
}
242242

@@ -245,13 +245,13 @@ func handlePull(pullID int64, sha string) {
245245
baseGitRepo = headGitRepo
246246
} else {
247247
if err = pr.LoadBaseRepoCtx(ctx); err != nil {
248-
log.Error("%-v LoadBaseRepo: %v", pr, err)
248+
log.Error("LoadBaseRepoCtx: %v", err)
249249
return
250250
}
251251

252252
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
253253
if err != nil {
254-
log.Error("OpenRepository %-v: %v", pr.BaseRepo, err)
254+
log.Error("OpenRepository: %v", err)
255255
return
256256
}
257257
defer baseGitRepo.Close()

0 commit comments

Comments
 (0)