forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
milestone.go
111 lines (99 loc) · 3.28 KB
/
milestone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package github
import (
"context"
"fmt"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type milestoneService struct {
client *wrapper
}
type milestone struct {
ID int `json:"id"`
Number int `json:"number"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
DueOn time.Time `json:"due_on"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
LabelsURL string `json:"labels_url"`
Creator user `json:"creator"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
NodeID string `json:"node_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt time.Time `json:"closed_at"`
}
type milestoneInput struct {
Title string `json:"title"`
State string `json:"state"`
Description string `json:"description"`
DueOn time.Time `json:"due_on"`
}
func (s *milestoneService) Find(ctx context.Context, repo string, id int) (*scm.Milestone, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/milestones/%d", repo, id)
out := new(milestone)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertMilestone(out), res, err
}
func (s *milestoneService) List(ctx context.Context, repo string, opts scm.MilestoneListOptions) ([]*scm.Milestone, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/milestones?%s", repo, encodeMilestoneListOptions(opts))
out := []*milestone{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertMilestoneList(out), res, err
}
func (s *milestoneService) Create(ctx context.Context, repo string, input *scm.MilestoneInput) (*scm.Milestone, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/milestones", repo)
in := &milestoneInput{
Title: input.Title,
State: input.State,
Description: input.Description,
DueOn: *input.DueDate,
}
out := new(milestone)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertMilestone(out), res, err
}
func (s *milestoneService) Delete(ctx context.Context, repo string, id int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/milestones/%d", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *milestoneService) Update(ctx context.Context, repo string, id int, input *scm.MilestoneInput) (*scm.Milestone, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/milestones/%d", repo, id)
in := &milestoneInput{}
if input.Title != "" {
in.Title = input.Title
}
if input.State != "" {
in.State = input.State
}
if input.Description != "" {
in.Description = input.Description
}
if input.DueDate != nil {
in.DueOn = *input.DueDate
}
out := new(milestone)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertMilestone(out), res, err
}
func convertMilestoneList(from []*milestone) []*scm.Milestone {
var to []*scm.Milestone
for _, m := range from {
to = append(to, convertMilestone(m))
}
return to
}
func convertMilestone(from *milestone) *scm.Milestone {
return &scm.Milestone{
Number: from.Number,
ID: from.ID,
Title: from.Title,
Description: from.Description,
Link: from.HTMLURL,
State: from.State,
DueDate: &from.DueOn,
}
}