Skip to content

Commit 71491e8

Browse files
committed
Bring in pr formatting from go-gitea#22568
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 8293fb3 commit 71491e8

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

models/issues/pull.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,39 @@ func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error {
202202
return err
203203
}
204204

205+
// ColorFormat writes a colored string to identify this struct
206+
func (pr *PullRequest) ColorFormat(s fmt.State) {
207+
if pr == nil {
208+
log.ColorFprintf(s, "PR[%d][%s:%s...%s:%s]",
209+
log.NewColoredIDValue(0),
210+
log.NewColoredValue("<nil>/<nil>"),
211+
log.NewColoredValue("<nil>"),
212+
log.NewColoredValue("<nil>/<nil>"),
213+
log.NewColoredValue("<nil>"),
214+
)
215+
return
216+
}
217+
218+
log.ColorFprintf(s, "PR[%d][", log.NewColoredIDValue(pr.ID))
219+
if pr.BaseRepo != nil {
220+
log.ColorFprintf(s, "%s:%s...", log.NewColoredValue(pr.BaseRepo.FullName()), log.NewColoredValue(pr.BaseBranch))
221+
} else {
222+
log.ColorFprintf(s, "Repo[%d]:%s...", log.NewColoredIDValue(pr.BaseRepoID), log.NewColoredValue(pr.BaseBranch))
223+
}
224+
if pr.HeadRepoID == pr.BaseRepoID {
225+
log.ColorFprintf(s, "%s]", log.NewColoredValue(pr.HeadBranch))
226+
} else if pr.HeadRepo != nil {
227+
log.ColorFprintf(s, "%s:%s]", log.NewColoredValue(pr.HeadRepo.FullName()), log.NewColoredValue(pr.HeadBranch))
228+
} else {
229+
log.ColorFprintf(s, "Repo[%d]:%s]", log.NewColoredIDValue(pr.HeadRepoID), log.NewColoredValue(pr.HeadBranch))
230+
}
231+
}
232+
233+
// String represents the pr as a simple string
234+
func (pr *PullRequest) String() string {
235+
return log.ColorFormatAsString(pr)
236+
}
237+
205238
// MustHeadUserName returns the HeadRepo's username if failed return blank
206239
func (pr *PullRequest) MustHeadUserName(ctx context.Context) string {
207240
if err := pr.LoadHeadRepo(ctx); err != nil {
@@ -234,7 +267,8 @@ func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
234267
return nil
235268
}
236269

237-
// LoadHeadRepo loads the head repository
270+
// LoadHeadRepo loads the head repository, pr.HeadRepo will remain nil if it does not exist
271+
// and thus ErrRepoNotExist will never be returned
238272
func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
239273
if !pr.isHeadRepoLoaded && pr.HeadRepo == nil && pr.HeadRepoID > 0 {
240274
if pr.HeadRepoID == pr.BaseRepoID {
@@ -249,14 +283,14 @@ func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
249283

250284
pr.HeadRepo, err = repo_model.GetRepositoryByID(ctx, pr.HeadRepoID)
251285
if err != nil && !repo_model.IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work
252-
return fmt.Errorf("GetRepositoryByID(head): %w", err)
286+
return fmt.Errorf("pr[%d].LoadHeadRepo[%d]: %w", pr.ID, pr.HeadRepoID, err)
253287
}
254288
pr.isHeadRepoLoaded = true
255289
}
256290
return nil
257291
}
258292

259-
// LoadBaseRepo loads the target repository
293+
// LoadBaseRepo loads the target repository. ErrRepoNotExist may be returned.
260294
func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
261295
if pr.BaseRepo != nil {
262296
return nil
@@ -274,7 +308,7 @@ func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
274308

275309
pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID)
276310
if err != nil {
277-
return fmt.Errorf("repo_model.GetRepositoryByID(base): %w", err)
311+
return fmt.Errorf("pr[%d].LoadBaseRepo[%d]: %w", pr.ID, pr.BaseRepoID, err)
278312
}
279313
return nil
280314
}

modules/log/colors.go

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

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

0 commit comments

Comments
 (0)