Skip to content

Commit

Permalink
update milestone/release to use null.time instead of time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
eoinmcafee00 committed Sep 29, 2021
1 parent 725ce80 commit 9184b69
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
19 changes: 9 additions & 10 deletions scm/driver/gitea/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package gitea
import (
"context"
"fmt"
"time"

"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/null"
)

type milestoneService struct {
Expand Down Expand Up @@ -35,7 +34,7 @@ func (s *milestoneService) Create(ctx context.Context, repo string, input *scm.M
Title: input.Title,
Description: input.Description,
State: StateOpen,
Deadline: input.DueDate,
Deadline: null.NewTime(input.DueDate, true),
}
if input.State == "closed" {
in.State = StateClosed
Expand Down Expand Up @@ -68,7 +67,7 @@ func (s *milestoneService) Update(ctx context.Context, repo string, id int, inpu
in.Description = input.Description
}
if !input.DueDate.IsZero() {
in.Deadline = input.DueDate
in.Deadline = null.NewTime(input.DueDate, true)
}
out := new(milestone)
res, err := s.client.do(ctx, "PATCH", path, in, out)
Expand All @@ -94,17 +93,17 @@ type milestone struct {
State StateType `json:"state"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
Closed time.Time `json:"closed_at"`
Deadline time.Time `json:"due_on"`
Created null.Time `json:"created_at"`
Updated null.Time `json:"updated_at"`
Closed null.Time `json:"closed_at"`
Deadline null.Time `json:"due_on"`
}

type milestoneInput struct {
Title string `json:"title"`
Description string `json:"description"`
State StateType `json:"state"`
Deadline time.Time `json:"due_on"`
Deadline null.Time `json:"due_on"`
}

func convertMilestoneList(src []*milestone) []*scm.Milestone {
Expand All @@ -125,6 +124,6 @@ func convertMilestone(src *milestone) *scm.Milestone {
Title: src.Title,
Description: src.Description,
State: string(src.State),
DueDate: src.Deadline,
DueDate: src.Deadline.ValueOrZero(),
}
}
13 changes: 6 additions & 7 deletions scm/driver/gitea/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package gitea
import (
"context"
"fmt"
"time"

"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/null"
)

type releaseService struct {
Expand Down Expand Up @@ -110,8 +109,8 @@ type release struct {
ZipURL string `json:"zipball_url"`
IsDraft bool `json:"draft"`
IsPrerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
CreatedAt null.Time `json:"created_at"`
PublishedAt null.Time `json:"published_at"`
Publisher *user `json:"author"`
Attachments []*Attachment `json:"assets"`
}
Expand All @@ -121,7 +120,7 @@ type Attachment struct {
Name string `json:"name"`
Size int64 `json:"size"`
DownloadCount int64 `json:"download_count"`
Created time.Time `json:"created_at"`
Created null.Time `json:"created_at"`
UUID string `json:"uuid"`
DownloadURL string `json:"browser_download_url"`
}
Expand All @@ -136,8 +135,8 @@ func convertRelease(src *release) *scm.Release {
Commitish: src.Target,
Draft: src.IsDraft,
Prerelease: src.IsPrerelease,
Created: src.CreatedAt,
Published: src.PublishedAt,
Created: src.CreatedAt.ValueOrZero(),
Published: src.PublishedAt.ValueOrZero(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions scm/driver/github/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *milestoneService) Create(ctx context.Context, repo string, input *scm.M
Title: input.Title,
State: input.State,
Description: input.Description,
DueOn: null.Time{Time: input.DueDate, Valid: true},
DueOn: null.NewTime(input.DueDate, true),
}
out := new(milestone)
res, err := s.client.do(ctx, "POST", path, in, out)
Expand All @@ -83,7 +83,7 @@ func (s *milestoneService) Update(ctx context.Context, repo string, id int, inpu
in.Description = input.Description
}
if !input.DueDate.IsZero() {
in.DueOn = null.Time{Time: input.DueDate, Valid: true}
in.DueOn = null.NewTime(input.DueDate, true)
}
out := new(milestone)
res, err := s.client.do(ctx, "PATCH", path, in, out)
Expand Down
5 changes: 3 additions & 2 deletions scm/driver/gitlab/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/drone/go-scm/scm/driver/internal/null"
"net/url"
"time"

Expand Down Expand Up @@ -71,8 +72,8 @@ type milestone struct {
State string `json:"state"`
DueDate isoTime `json:"due_date"`
StartDate isoTime `json:"start_date"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt null.Time `json:"created_at"`
UpdatedAt null.Time `json:"updated_at"`
Expired bool `json:"expired"`
}

Expand Down
22 changes: 22 additions & 0 deletions scm/driver/internal/null/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,25 @@ func (t Time) ValueOrZero() time.Time {
func (t Time) IsZero() bool {
return !t.Valid || t.Time.IsZero()
}

// Ptr returns a pointer to this Time's value,
// or a nil pointer if this Time is zero.
func (t Time) Ptr() *time.Time {
if !t.Valid {
return nil
}
return &t.Time
}

// NewTime creates a new Time.
func NewTime(t time.Time, valid bool) Time {
return Time{
Time: t,
Valid: valid,
}
}

// TimeFrom creates a new Time that will always be valid.
func TimeFrom(t time.Time) Time {
return NewTime(t, true)
}

0 comments on commit 9184b69

Please sign in to comment.