Skip to content

Commit 35fc9ad

Browse files
zeripathlunny
andauthored
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 <[email protected]> Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 6e4ba04 commit 35fc9ad

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

models/issues/tracked_time.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issues
66

77
import (
88
"context"
9+
"errors"
910
"time"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -47,33 +48,42 @@ func (t *TrackedTime) LoadAttributes() (err error) {
4748
}
4849

4950
func (t *TrackedTime) loadAttributes(ctx context.Context) (err error) {
51+
// Load the issue
5052
if t.Issue == nil {
5153
t.Issue, err = GetIssueByID(ctx, t.IssueID)
52-
if err != nil {
53-
return
54+
55+
if err != nil && !errors.Is(err, util.ErrNotExist) {
56+
return err
5457
}
58+
}
59+
// Now load the repo for the issue (which we may have just loaded)
60+
if t.Issue != nil {
5561
err = t.Issue.LoadRepo(ctx)
56-
if err != nil {
57-
return
62+
if err != nil && !errors.Is(err, util.ErrNotExist) {
63+
return err
5864
}
5965
}
66+
// Load the user
6067
if t.User == nil {
6168
t.User, err = user_model.GetUserByIDCtx(ctx, t.UserID)
6269
if err != nil {
63-
return
70+
if !errors.Is(err, util.ErrNotExist) {
71+
return err
72+
}
73+
t.User = user_model.NewGhostUser()
6474
}
6575
}
66-
return err
76+
return nil
6777
}
6878

6979
// LoadAttributes load Issue, User
70-
func (tl TrackedTimeList) LoadAttributes() (err error) {
80+
func (tl TrackedTimeList) LoadAttributes() error {
7181
for _, t := range tl {
72-
if err = t.LoadAttributes(); err != nil {
82+
if err := t.LoadAttributes(); err != nil {
7383
return err
7484
}
7585
}
76-
return err
86+
return nil
7787
}
7888

7989
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.

modules/convert/issue.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ func ToAPIIssueList(il issues_model.IssueList) []*api.Issue {
110110
// ToTrackedTime converts TrackedTime to API format
111111
func ToTrackedTime(t *issues_model.TrackedTime) (apiT *api.TrackedTime) {
112112
apiT = &api.TrackedTime{
113-
ID: t.ID,
114-
IssueID: t.IssueID,
115-
UserID: t.UserID,
116-
UserName: t.User.Name,
117-
Time: t.Time,
118-
Created: t.Created,
113+
ID: t.ID,
114+
IssueID: t.IssueID,
115+
UserID: t.UserID,
116+
Time: t.Time,
117+
Created: t.Created,
119118
}
120119
if t.Issue != nil {
121120
apiT.Issue = ToAPIIssue(t.Issue)

0 commit comments

Comments
 (0)