Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-28 04:36:11 +01:00

Use GhostUser if needed for TrackedTimes (#22021) (#22029)

Backport #22021

When getting tracked times out of the db and loading their attributes
handle not exist errors in a nicer way. (Also prevent an NPE.)

Fix #22006

Signed-off-by: Andrew Thornton <art27@cantab.net>

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
zeripath 2022-12-05 12:20:37 +00:00 committed by GitHub
parent 6e4ba04843
commit 35fc9ad984
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 27 deletions

View file

@ -6,6 +6,7 @@ package issues
import ( import (
"context" "context"
"errors"
"time" "time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
@ -47,33 +48,42 @@ func (t *TrackedTime) LoadAttributes() (err error) {
} }
func (t *TrackedTime) loadAttributes(ctx context.Context) (err error) { func (t *TrackedTime) loadAttributes(ctx context.Context) (err error) {
// Load the issue
if t.Issue == nil { if t.Issue == nil {
t.Issue, err = GetIssueByID(ctx, t.IssueID) t.Issue, err = GetIssueByID(ctx, t.IssueID)
if err != nil {
return
}
err = t.Issue.LoadRepo(ctx)
if err != nil {
return
}
}
if t.User == nil {
t.User, err = user_model.GetUserByIDCtx(ctx, t.UserID)
if err != nil {
return
}
}
return err
}
// LoadAttributes load Issue, User if err != nil && !errors.Is(err, util.ErrNotExist) {
func (tl TrackedTimeList) LoadAttributes() (err error) {
for _, t := range tl {
if err = t.LoadAttributes(); err != nil {
return err return err
} }
} }
return err // Now load the repo for the issue (which we may have just loaded)
if t.Issue != nil {
err = t.Issue.LoadRepo(ctx)
if err != nil && !errors.Is(err, util.ErrNotExist) {
return err
}
}
// Load the user
if t.User == nil {
t.User, err = user_model.GetUserByIDCtx(ctx, t.UserID)
if err != nil {
if !errors.Is(err, util.ErrNotExist) {
return err
}
t.User = user_model.NewGhostUser()
}
}
return nil
}
// LoadAttributes load Issue, User
func (tl TrackedTimeList) LoadAttributes() error {
for _, t := range tl {
if err := t.LoadAttributes(); err != nil {
return err
}
}
return nil
} }
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored. // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.

View file

@ -110,12 +110,11 @@ func ToAPIIssueList(il issues_model.IssueList) []*api.Issue {
// ToTrackedTime converts TrackedTime to API format // ToTrackedTime converts TrackedTime to API format
func ToTrackedTime(t *issues_model.TrackedTime) (apiT *api.TrackedTime) { func ToTrackedTime(t *issues_model.TrackedTime) (apiT *api.TrackedTime) {
apiT = &api.TrackedTime{ apiT = &api.TrackedTime{
ID: t.ID, ID: t.ID,
IssueID: t.IssueID, IssueID: t.IssueID,
UserID: t.UserID, UserID: t.UserID,
UserName: t.User.Name, Time: t.Time,
Time: t.Time, Created: t.Created,
Created: t.Created,
} }
if t.Issue != nil { if t.Issue != nil {
apiT.Issue = ToAPIIssue(t.Issue) apiT.Issue = ToAPIIssue(t.Issue)