Skip to content

Commit ea86c2b

Browse files
authored
Use GhostUser if needed for TrackedTimes (#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]>
1 parent 84d2a82 commit ea86c2b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

Diff for: models/issues/tracked_time.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package issues
55

66
import (
77
"context"
8+
"errors"
89
"time"
910

1011
"code.gitea.io/gitea/models/db"
@@ -46,33 +47,41 @@ func (t *TrackedTime) LoadAttributes() (err error) {
4647
}
4748

4849
func (t *TrackedTime) loadAttributes(ctx context.Context) (err error) {
50+
// Load the issue
4951
if t.Issue == nil {
5052
t.Issue, err = GetIssueByID(ctx, t.IssueID)
51-
if err != nil {
52-
return
53+
if err != nil && !errors.Is(err, util.ErrNotExist) {
54+
return err
5355
}
56+
}
57+
// Now load the repo for the issue (which we may have just loaded)
58+
if t.Issue != nil {
5459
err = t.Issue.LoadRepo(ctx)
55-
if err != nil {
56-
return
60+
if err != nil && !errors.Is(err, util.ErrNotExist) {
61+
return err
5762
}
5863
}
64+
// Load the user
5965
if t.User == nil {
6066
t.User, err = user_model.GetUserByID(ctx, t.UserID)
6167
if err != nil {
62-
return
68+
if !errors.Is(err, util.ErrNotExist) {
69+
return err
70+
}
71+
t.User = user_model.NewGhostUser()
6372
}
6473
}
65-
return err
74+
return nil
6675
}
6776

6877
// LoadAttributes load Issue, User
69-
func (tl TrackedTimeList) LoadAttributes() (err error) {
78+
func (tl TrackedTimeList) LoadAttributes() error {
7079
for _, t := range tl {
71-
if err = t.LoadAttributes(); err != nil {
80+
if err := t.LoadAttributes(); err != nil {
7281
return err
7382
}
7483
}
75-
return err
84+
return nil
7685
}
7786

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

Diff for: modules/convert/issue.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ func ToAPIIssueList(ctx context.Context, il issues_model.IssueList) []*api.Issue
110110
// ToTrackedTime converts TrackedTime to API format
111111
func ToTrackedTime(ctx context.Context, 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(ctx, t.Issue)

0 commit comments

Comments
 (0)