diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-05-19 01:43:07 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-05-19 01:49:27 -0600 |
commit | 798f82a342d3885db23733c00b9978e8d77fdcb3 (patch) | |
tree | 5565c30958aabdd955ec8a4725888c31e4cb960c | |
parent | 3eb2af2532a6a38be3582c629f2df866830eb0dc (diff) |
cmd/generate: Add a withinOneSecond helper, use it
-rw-r--r-- | cmd/generate/src_contribs.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/cmd/generate/src_contribs.go b/cmd/generate/src_contribs.go index 0b5acb5..eb611a3 100644 --- a/cmd/generate/src_contribs.go +++ b/cmd/generate/src_contribs.go @@ -258,6 +258,14 @@ func (c Contribution) fetchSubmittedAt() (time.Time, error) { return time.Time{}, fmt.Errorf("idk how to get created timestamp for %q", c.URLs[0]) } +func withinOneSecond(a, b time.Time) bool { + d := a.Sub(b) + if d < 0 { + d = -d + } + return d <= time.Second +} + func (c Contribution) fetchLastUpdated() (time.Time, User, error) { for _, u := range c.URLs { if m := reGoLangGerritCL.FindStringSubmatch(u); m != nil { @@ -283,7 +291,7 @@ func (c Contribution) fetchLastUpdated() (time.Time, User, error) { retUpdatedAt := obj.Updated.Val var retUser User for _, message := range obj.Messages { - if message.Date.Val == retUpdatedAt { + if withinOneSecond(message.Date.Val, retUpdatedAt) { if message.Author.DisplayName != "" { retUser.Name = message.Author.DisplayName } else { @@ -323,11 +331,11 @@ func (c Contribution) fetchLastUpdated() (time.Time, User, error) { retUpdatedAt := obj.UpdatedAt var retUser User - if retUser == (User{}) && obj.CreatedAt == retUpdatedAt { + if retUser == (User{}) && withinOneSecond(obj.CreatedAt, retUpdatedAt) { retUser.Name = obj.CreatedBy.Login retUser.URL = obj.CreatedBy.HTMLURL } - if retUser == (User{}) && obj.MergedAt == retUpdatedAt { + if retUser == (User{}) && withinOneSecond(obj.MergedAt, retUpdatedAt) { retUser.Name = obj.MergedBy.Login retUser.URL = obj.MergedBy.HTMLURL } @@ -344,7 +352,7 @@ func (c Contribution) fetchLastUpdated() (time.Time, User, error) { return time.Time{}, User{}, err } for _, comment := range comments { - if comment.UpdatedAt == retUpdatedAt || comment.UpdatedAt.Add(1*time.Second) == retUpdatedAt { + if withinOneSecond(comment.UpdatedAt, retUpdatedAt) { retUser.Name = comment.User.Login retUser.URL = comment.User.HTMLURL break @@ -364,7 +372,7 @@ func (c Contribution) fetchLastUpdated() (time.Time, User, error) { return time.Time{}, User{}, err } for _, comment := range reviewComments { - if comment.UpdatedAt == retUpdatedAt { + if withinOneSecond(comment.UpdatedAt, retUpdatedAt) { retUser.Name = comment.User.Login retUser.URL = comment.User.HTMLURL break @@ -383,7 +391,7 @@ func (c Contribution) fetchLastUpdated() (time.Time, User, error) { return time.Time{}, User{}, err } for _, event := range events { - if event.CreatedAt == retUpdatedAt { + if withinOneSecond(event.CreatedAt, retUpdatedAt) { retUser.Name = event.Actor.Login retUser.URL = event.Actor.HTMLURL break |