-
Notifications
You must be signed in to change notification settings - Fork 232
/
milestone.go
130 lines (116 loc) · 3.79 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gitea
import (
"context"
"fmt"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/null"
)
type milestoneService struct {
client *wrapper
}
func (s *milestoneService) Find(ctx context.Context, repo string, id int) (*scm.Milestone, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("api/v1/repos/%s/%s/milestones/%d", namespace, name, 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) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("api/v1/repos/%s/%s/milestones%s", namespace, name, 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) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("api/v1/repos/%s/%s/milestones", namespace, name)
in := &milestoneInput{
Title: input.Title,
Description: input.Description,
State: stateOpen,
Deadline: null.TimeFrom(input.DueDate),
}
if input.State == "closed" {
in.State = stateClosed
}
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) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("api/v1/repos/%s/%s/milestones/%d", namespace, name, 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) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("api/v1/repos/%s/%s/milestones/%d", namespace, name, id)
in := milestoneInput{}
if input.Title != "" {
in.Title = input.Title
}
switch input.State {
case "open":
in.State = stateOpen
case "close", "closed":
in.State = stateClosed
}
if input.Description != "" {
in.Description = input.Description
}
if !input.DueDate.IsZero() {
in.Deadline = null.TimeFrom(input.DueDate)
}
out := new(milestone)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertMilestone(out), res, err
}
// stateType issue state type
type stateType string
const (
// stateOpen pr/issue is open
stateOpen stateType = "open"
// stateClosed pr/issue is closed
stateClosed stateType = "closed"
// stateAll is all
stateAll stateType = "all"
)
type milestone struct {
ID int64 `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
State stateType `json:"state"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
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 null.Time `json:"due_on"`
}
func convertMilestoneList(src []*milestone) []*scm.Milestone {
var dst []*scm.Milestone
for _, v := range src {
dst = append(dst, convertMilestone(v))
}
return dst
}
func convertMilestone(src *milestone) *scm.Milestone {
if src == nil || src.Deadline.IsZero() {
return nil
}
return &scm.Milestone{
Number: int(src.ID),
ID: int(src.ID),
Title: src.Title,
Description: src.Description,
State: string(src.State),
DueDate: src.Deadline.ValueOrZero(),
}
}