Skip to content

Commit 08514fd

Browse files
committed
added timestamp to push webhooks
1 parent d8fff5c commit 08514fd

File tree

9 files changed

+92
-23
lines changed

9 files changed

+92
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## Unreleased
88
### Added
99
- Improve status display text in new bitbucket pull request screen, from [@bradrydzewski](https://github.com/bradrydzewski). See [#27](https://github.com/drone/go-scm/issues/27).
10+
- Implement timestamp value for GitHub push webhooks, from [@bradrydzewski](https://github.com/bradrydzewski).
1011

1112
### Fixed
1213
- Fix issue with GitHub enterprise deep link including API prefix, from [@bradrydzewski](https://github.com/bradrydzewski).

scm/driver/github/testdata/webhooks/push.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
"Author": {
2222
"Name": "Codertocat",
2323
"Email": "[email protected]",
24-
"Date": "0001-01-01T00:00:00Z",
24+
"Date": "2018-06-15T13:01:51-07:00",
2525
"Login": "Codertocat",
2626
"Avatar": ""
2727
},
2828
"Committer": {
2929
"Name": "GitHub",
3030
"Email": "[email protected]",
31-
"Date": "0001-01-01T00:00:00Z",
31+
"Date": "2018-06-15T13:01:51-07:00",
3232
"Login": "web-flow",
3333
"Avatar": ""
3434
},

scm/driver/github/testdata/webhooks/push_branch_create.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
"Author": {
2323
"Name": "Brad Rydzewski",
2424
"Email": "[email protected]",
25-
"Date": "0001-01-01T00:00:00Z",
25+
"Date": "2018-06-19T19:03:12-07:00",
2626
"Login": "bradrydzewski",
2727
"Avatar": ""
2828
},
2929
"Committer": {
3030
"Name": "GitHub",
3131
"Email": "[email protected]",
32-
"Date": "0001-01-01T00:00:00Z",
32+
"Date": "2018-06-19T19:03:12-07:00",
3333
"Login": "web-flow",
3434
"Avatar": ""
3535
},

scm/driver/github/testdata/webhooks/push_tag.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
"Author": {
2323
"Name": "Brad Rydzewski",
2424
"Email": "[email protected]",
25-
"Date": "0001-01-01T00:00:00Z",
25+
"Date": "2018-06-19T19:03:12-07:00",
2626
"Login": "bradrydzewski",
2727
"Avatar": ""
2828
},
2929
"Committer": {
3030
"Name": "GitHub",
3131
"Email": "[email protected]",
32-
"Date": "0001-01-01T00:00:00Z",
32+
"Date": "2018-06-19T19:03:12-07:00",
3333
"Login": "web-flow",
3434
"Avatar": ""
3535
},

scm/driver/github/webhook.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ type (
168168
After string `json:"after"`
169169
Compare string `json:"compare"`
170170
Head struct {
171-
ID string `json:"id"`
172-
TreeID string `json:"tree_id"`
173-
Distinct bool `json:"distinct"`
174-
Message string `json:"message"`
175-
Timestamp string `json:"timestamp"`
176-
URL string `json:"url"`
171+
ID string `json:"id"`
172+
TreeID string `json:"tree_id"`
173+
Distinct bool `json:"distinct"`
174+
Message string `json:"message"`
175+
Timestamp null.Time `json:"timestamp"`
176+
URL string `json:"url"`
177177
Author struct {
178178
Name string `json:"name"`
179179
Email string `json:"email"`
@@ -189,12 +189,12 @@ type (
189189
Modified []string `json:"modified"`
190190
} `json:"head_commit"`
191191
Commits []struct {
192-
ID string `json:"id"`
193-
TreeID string `json:"tree_id"`
194-
Distinct bool `json:"distinct"`
195-
Message string `json:"message"`
196-
Timestamp string `json:"timestamp"`
197-
URL string `json:"url"`
192+
ID string `json:"id"`
193+
TreeID string `json:"tree_id"`
194+
Distinct bool `json:"distinct"`
195+
Message string `json:"message"`
196+
Timestamp null.Time `json:"timestamp"`
197+
URL string `json:"url"`
198198
Author struct {
199199
Name string `json:"name"`
200200
Email string `json:"email"`
@@ -273,13 +273,13 @@ func convertPushHook(src *pushHook) *scm.PushHook {
273273
Login: src.Head.Author.Username,
274274
Email: src.Head.Author.Email,
275275
Name: src.Head.Author.Name,
276-
// TODO (bradrydzewski) set the timestamp
276+
Date: src.Head.Timestamp.ValueOrZero(),
277277
},
278278
Committer: scm.Signature{
279279
Login: src.Head.Committer.Username,
280280
Email: src.Head.Committer.Email,
281281
Name: src.Head.Committer.Name,
282-
// TODO (bradrydzewski) set the timestamp
282+
Date: src.Head.Timestamp.ValueOrZero(),
283283
},
284284
},
285285
Repo: scm.Repository{

scm/driver/internal/null/bool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2014, Greg Roseberry
2-
// All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
34

45
package null
56

scm/driver/internal/null/int.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2014, Greg Roseberry
2-
// All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
34

45
package null
56

scm/driver/internal/null/string.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2014, Greg Roseberry
2-
// All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
34

45
package null
56

scm/driver/internal/null/time.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2014, Greg Roseberry
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package null
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"reflect"
11+
"time"
12+
)
13+
14+
// Time is a nullable time.Time.
15+
// JSON marshals to the zero value for time.Time if null.
16+
// Considered to be null to SQL if zero.
17+
type Time struct {
18+
Time time.Time
19+
Valid bool
20+
}
21+
22+
// MarshalJSON implements json.Marshaler.
23+
// It will encode null if this time is null.
24+
func (t Time) MarshalJSON() ([]byte, error) {
25+
if !t.Valid {
26+
return []byte("null"), nil
27+
}
28+
return t.Time.MarshalJSON()
29+
}
30+
31+
// UnmarshalJSON implements json.Unmarshaler.
32+
// It supports string, object (e.g. pq.NullTime and friends)
33+
// and null input.
34+
func (t *Time) UnmarshalJSON(data []byte) error {
35+
var err error
36+
var v interface{}
37+
if err = json.Unmarshal(data, &v); err != nil {
38+
return err
39+
}
40+
switch v.(type) {
41+
case string:
42+
err = t.Time.UnmarshalJSON(data)
43+
case nil:
44+
t.Valid = false
45+
return nil
46+
default:
47+
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name())
48+
}
49+
t.Valid = err == nil
50+
return err
51+
}
52+
53+
// ValueOrZero returns the inner value if valid, otherwise zero.
54+
func (t Time) ValueOrZero() time.Time {
55+
if !t.Valid {
56+
return time.Time{}
57+
}
58+
return t.Time
59+
}
60+
61+
// IsZero returns true for null or zero Times, for
62+
// potential future omitempty support.
63+
func (t Time) IsZero() bool {
64+
return !t.Valid || t.Time.IsZero()
65+
}

0 commit comments

Comments
 (0)