Skip to content

Commit

Permalink
added timestamp to push webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 25, 2019
1 parent d8fff5c commit 08514fd
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- 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).
- Implement timestamp value for GitHub push webhooks, from [@bradrydzewski](https://github.com/bradrydzewski).

### Fixed
- Fix issue with GitHub enterprise deep link including API prefix, from [@bradrydzewski](https://github.com/bradrydzewski).
Expand Down
4 changes: 2 additions & 2 deletions scm/driver/github/testdata/webhooks/push.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"Author": {
"Name": "Codertocat",
"Email": "21031067+Codertocat@users.noreply.github.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-15T13:01:51-07:00",
"Login": "Codertocat",
"Avatar": ""
},
"Committer": {
"Name": "GitHub",
"Email": "noreply@github.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-15T13:01:51-07:00",
"Login": "web-flow",
"Avatar": ""
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"Author": {
"Name": "Brad Rydzewski",
"Email": "brad.rydzewski@gmail.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-19T19:03:12-07:00",
"Login": "bradrydzewski",
"Avatar": ""
},
"Committer": {
"Name": "GitHub",
"Email": "noreply@github.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-19T19:03:12-07:00",
"Login": "web-flow",
"Avatar": ""
},
Expand Down
4 changes: 2 additions & 2 deletions scm/driver/github/testdata/webhooks/push_tag.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"Author": {
"Name": "Brad Rydzewski",
"Email": "brad.rydzewski@gmail.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-19T19:03:12-07:00",
"Login": "bradrydzewski",
"Avatar": ""
},
"Committer": {
"Name": "GitHub",
"Email": "noreply@github.com",
"Date": "0001-01-01T00:00:00Z",
"Date": "2018-06-19T19:03:12-07:00",
"Login": "web-flow",
"Avatar": ""
},
Expand Down
28 changes: 14 additions & 14 deletions scm/driver/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ type (
After string `json:"after"`
Compare string `json:"compare"`
Head struct {
ID string `json:"id"`
TreeID string `json:"tree_id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
URL string `json:"url"`
ID string `json:"id"`
TreeID string `json:"tree_id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp null.Time `json:"timestamp"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Expand All @@ -189,12 +189,12 @@ type (
Modified []string `json:"modified"`
} `json:"head_commit"`
Commits []struct {
ID string `json:"id"`
TreeID string `json:"tree_id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
URL string `json:"url"`
ID string `json:"id"`
TreeID string `json:"tree_id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp null.Time `json:"timestamp"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Expand Down Expand Up @@ -273,13 +273,13 @@ func convertPushHook(src *pushHook) *scm.PushHook {
Login: src.Head.Author.Username,
Email: src.Head.Author.Email,
Name: src.Head.Author.Name,
// TODO (bradrydzewski) set the timestamp
Date: src.Head.Timestamp.ValueOrZero(),
},
Committer: scm.Signature{
Login: src.Head.Committer.Username,
Email: src.Head.Committer.Email,
Name: src.Head.Committer.Name,
// TODO (bradrydzewski) set the timestamp
Date: src.Head.Timestamp.ValueOrZero(),
},
},
Repo: scm.Repository{
Expand Down
3 changes: 2 additions & 1 deletion scm/driver/internal/null/bool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2014, Greg Roseberry
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package null

Expand Down
3 changes: 2 additions & 1 deletion scm/driver/internal/null/int.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2014, Greg Roseberry
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package null

Expand Down
3 changes: 2 additions & 1 deletion scm/driver/internal/null/string.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2014, Greg Roseberry
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package null

Expand Down
65 changes: 65 additions & 0 deletions scm/driver/internal/null/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2014, Greg Roseberry
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package null

import (
"encoding/json"
"fmt"
"reflect"
"time"
)

// Time is a nullable time.Time.
// JSON marshals to the zero value for time.Time if null.
// Considered to be null to SQL if zero.
type Time struct {
Time time.Time
Valid bool
}

// MarshalJSON implements json.Marshaler.
// It will encode null if this time is null.
func (t Time) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return t.Time.MarshalJSON()
}

// UnmarshalJSON implements json.Unmarshaler.
// It supports string, object (e.g. pq.NullTime and friends)
// and null input.
func (t *Time) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
if err = json.Unmarshal(data, &v); err != nil {
return err
}
switch v.(type) {
case string:
err = t.Time.UnmarshalJSON(data)
case nil:
t.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name())
}
t.Valid = err == nil
return err
}

// ValueOrZero returns the inner value if valid, otherwise zero.
func (t Time) ValueOrZero() time.Time {
if !t.Valid {
return time.Time{}
}
return t.Time
}

// IsZero returns true for null or zero Times, for
// potential future omitempty support.
func (t Time) IsZero() bool {
return !t.Valid || t.Time.IsZero()
}

0 comments on commit 08514fd

Please sign in to comment.