Skip to content

Commit dbb3736

Browse files
yp05327wxiaoguangGiteaBot
authored
Fix incorrect webhook time and use relative-time to display it (go-gitea#24477)
Fixes go-gitea#24414 After click replay this webhook, it will display `now` ![image](https://user-images.githubusercontent.com/18380374/235559399-05a23927-13f5-442d-8f10-2c7cd24022a0.png) --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 parent 4a722c9 commit dbb3736

File tree

5 files changed

+57
-27
lines changed

5 files changed

+57
-27
lines changed

models/webhook/hooktask.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/setting"
1414
api "code.gitea.io/gitea/modules/structs"
15+
"code.gitea.io/gitea/modules/timeutil"
1516
webhook_module "code.gitea.io/gitea/modules/webhook"
1617

1718
gouuid "github.com/google/uuid"
@@ -40,15 +41,14 @@ type HookResponse struct {
4041

4142
// HookTask represents a hook task.
4243
type HookTask struct {
43-
ID int64 `xorm:"pk autoincr"`
44-
HookID int64 `xorm:"index"`
45-
UUID string `xorm:"unique"`
46-
api.Payloader `xorm:"-"`
47-
PayloadContent string `xorm:"LONGTEXT"`
48-
EventType webhook_module.HookEventType
49-
IsDelivered bool
50-
Delivered int64
51-
DeliveredString string `xorm:"-"`
44+
ID int64 `xorm:"pk autoincr"`
45+
HookID int64 `xorm:"index"`
46+
UUID string `xorm:"unique"`
47+
api.Payloader `xorm:"-"`
48+
PayloadContent string `xorm:"LONGTEXT"`
49+
EventType webhook_module.HookEventType
50+
IsDelivered bool
51+
Delivered timeutil.TimeStampNano
5252

5353
// History info.
5454
IsSucceed bool
@@ -75,8 +75,6 @@ func (t *HookTask) BeforeUpdate() {
7575

7676
// AfterLoad updates the webhook object upon setting a column
7777
func (t *HookTask) AfterLoad() {
78-
t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
79-
8078
if len(t.RequestContent) == 0 {
8179
return
8280
}
@@ -115,12 +113,17 @@ func HookTasks(hookID int64, page int) ([]*HookTask, error) {
115113
// CreateHookTask creates a new hook task,
116114
// it handles conversion from Payload to PayloadContent.
117115
func CreateHookTask(ctx context.Context, t *HookTask) (*HookTask, error) {
118-
data, err := t.Payloader.JSONPayload()
119-
if err != nil {
120-
return nil, err
121-
}
122116
t.UUID = gouuid.New().String()
123-
t.PayloadContent = string(data)
117+
if t.Payloader != nil {
118+
data, err := t.Payloader.JSONPayload()
119+
if err != nil {
120+
return nil, err
121+
}
122+
t.PayloadContent = string(data)
123+
}
124+
if t.Delivered == 0 {
125+
t.Delivered = timeutil.TimeStampNanoNow()
126+
}
124127
return t, db.Insert(ctx, t)
125128
}
126129

@@ -161,13 +164,11 @@ func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask,
161164
}
162165
}
163166

164-
newTask := &HookTask{
165-
UUID: gouuid.New().String(),
167+
return CreateHookTask(ctx, &HookTask{
166168
HookID: task.HookID,
167169
PayloadContent: task.PayloadContent,
168170
EventType: task.EventType,
169-
}
170-
return newTask, db.Insert(ctx, newTask)
171+
})
171172
}
172173

173174
// FindUndeliveredHookTaskIDs will find the next 100 undelivered hook tasks with ID greater than the provided lowerID

models/webhook/webhook_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/unittest"
1313
"code.gitea.io/gitea/modules/json"
1414
api "code.gitea.io/gitea/modules/structs"
15+
"code.gitea.io/gitea/modules/timeutil"
1516
"code.gitea.io/gitea/modules/util"
1617
webhook_module "code.gitea.io/gitea/modules/webhook"
1718

@@ -222,7 +223,6 @@ func TestUpdateHookTask(t *testing.T) {
222223

223224
hook := unittest.AssertExistsAndLoadBean(t, &HookTask{ID: 1})
224225
hook.PayloadContent = "new payload content"
225-
hook.DeliveredString = "new delivered string"
226226
hook.IsDelivered = true
227227
unittest.AssertNotExistsBean(t, hook)
228228
assert.NoError(t, UpdateHookTask(hook))
@@ -235,7 +235,7 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
235235
HookID: 3,
236236
Payloader: &api.PushPayload{},
237237
IsDelivered: true,
238-
Delivered: time.Now().UnixNano(),
238+
Delivered: timeutil.TimeStampNanoNow(),
239239
}
240240
unittest.AssertNotExistsBean(t, hookTask)
241241
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -268,7 +268,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
268268
HookID: 4,
269269
Payloader: &api.PushPayload{},
270270
IsDelivered: true,
271-
Delivered: time.Now().UnixNano(),
271+
Delivered: timeutil.TimeStampNanoNow(),
272272
}
273273
unittest.AssertNotExistsBean(t, hookTask)
274274
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -285,7 +285,7 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
285285
HookID: 3,
286286
Payloader: &api.PushPayload{},
287287
IsDelivered: true,
288-
Delivered: time.Now().AddDate(0, 0, -8).UnixNano(),
288+
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
289289
}
290290
unittest.AssertNotExistsBean(t, hookTask)
291291
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -318,7 +318,7 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test
318318
HookID: 4,
319319
Payloader: &api.PushPayload{},
320320
IsDelivered: true,
321-
Delivered: time.Now().AddDate(0, 0, -6).UnixNano(),
321+
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
322322
}
323323
unittest.AssertNotExistsBean(t, hookTask)
324324
_, err := CreateHookTask(db.DefaultContext, hookTask)

modules/timeutil/timestampnano.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package timeutil
5+
6+
import (
7+
"time"
8+
9+
"code.gitea.io/gitea/modules/setting"
10+
)
11+
12+
// TimeStampNano is for nano time in database, do not use it unless there is a real requirement.
13+
type TimeStampNano int64
14+
15+
// TimeStampNanoNow returns now nano int64
16+
func TimeStampNanoNow() TimeStampNano {
17+
return TimeStampNano(time.Now().UnixNano())
18+
}
19+
20+
// AsTime convert timestamp as time.Time in Local locale
21+
func (tsn TimeStampNano) AsTime() (tm time.Time) {
22+
return tsn.AsTimeInLocation(setting.DefaultUILocation)
23+
}
24+
25+
// AsTimeInLocation convert timestamp as time.Time in Local locale
26+
func (tsn TimeStampNano) AsTimeInLocation(loc *time.Location) time.Time {
27+
return time.Unix(0, int64(tsn)).In(loc)
28+
}

services/webhook/deliver.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/proxy"
2626
"code.gitea.io/gitea/modules/queue"
2727
"code.gitea.io/gitea/modules/setting"
28+
"code.gitea.io/gitea/modules/timeutil"
2829
webhook_module "code.gitea.io/gitea/modules/webhook"
2930

3031
"github.com/gobwas/glob"
@@ -175,7 +176,7 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error {
175176

176177
// All code from this point will update the hook task
177178
defer func() {
178-
t.Delivered = time.Now().UnixNano()
179+
t.Delivered = timeutil.TimeStampNanoNow()
179180
if t.IsSucceed {
180181
log.Trace("Hook delivered: %s", t.UUID)
181182
} else if !w.IsActive {

templates/repo/settings/webhook/history.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<a class="ui primary sha label toggle button show-panel" data-panel="#info-{{.ID}}">{{.UUID}}</a>
2222
<div class="ui right">
2323
<span class="text grey time">
24-
{{.DeliveredString}}
24+
{{TimeSince .Delivered.AsTime $.locale}}
2525
</span>
2626
</div>
2727
</div>

0 commit comments

Comments
 (0)