/
milestones.go
151 lines (141 loc) · 3.93 KB
/
milestones.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package sync
import (
"fmt"
"github.com/hairyhenderson/github-sync-labels-milestones/config"
"github.com/google/go-github/github"
)
func (g *GitHubClient) updateMilestones(repo *config.Repository, milestones []*config.Milestone) error {
openMSes, err := g.getAllMilestones(repo, "open")
if err != nil {
return err
}
closedMSes, err := g.getAllMilestones(repo, "closed")
if err != nil {
return err
}
existingMSes := append(openMSes, closedMSes...)
if err != nil {
return err
}
// 3. for each _given_ MS:
for _, ms := range milestones {
// 3.1. is this MS in (1+2)? (include Title + PreviousTitles in search)
existingMS, err := searchMS(ms, existingMSes)
if err != nil {
return err
}
if existingMS != nil {
ms.Number = existingMS.Number
}
if existingMS.Equals(ms) {
// fmt.Printf("%s up-to-date, nothing to do!\n", ms.Title)
} else if existingMS == nil && ms.State == "absent" {
// fmt.Printf("%s absent, nothing to do!\n", ms.Title)
} else if existingMS == nil && ms.State != "absent" {
// ms doesn't exist yet, create it
fmt.Printf("%s not found, creating!\n", ms.Title)
err := g.createMilestone(repo, ms)
if err != nil {
return err
}
} else if existingMS != nil && ms.State == "absent" {
fmt.Printf("%s found, deleting!\n", ms.Title)
err := g.deleteMilestone(repo, ms)
if err != nil {
return err
}
} else {
// update milestone in GitHub
fmt.Printf("%s found, updating!\n\told: %+v\n\tnew: %+v\n", ms.Title, existingMS, ms)
err := g.updateMilestone(repo, ms)
if err != nil {
return err
}
}
}
// 3.1.
return nil
}
// getAllMilestones - Retrieve a list of all milestones from GH
func (g *GitHubClient) getAllMilestones(repo *config.Repository, state string) ([]*config.Milestone, error) {
// get from GitHub
opt := &github.MilestoneListOptions{
State: state,
ListOptions: github.ListOptions{PerPage: 100},
}
var allMilestones []*github.Milestone
for {
milestones, resp, err := g.client.Issues.ListMilestones(repo.User, repo.Repo, opt)
if err != nil {
return nil, err
}
allMilestones = append(allMilestones, milestones...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
ms := config.NewMilestonesFromGH(allMilestones)
return ms, nil
}
func searchMS(ms *config.Milestone, milestones []*config.Milestone) (*config.Milestone, error) {
found := []*config.Milestone{}
titles := append(ms.PreviousTitles, ms.Title)
for _, m := range milestones {
for _, t := range titles {
if m.Title == t {
found = append(found, m)
}
}
}
// if multiples are found, panic!
if len(found) > 1 {
return nil, fmt.Errorf("error: more than 1 matching remote milestone for %s", ms.Title)
}
if len(found) == 1 {
return found[0], nil
}
return nil, nil
}
func (g *GitHubClient) createMilestone(repo *config.Repository, ms *config.Milestone) error {
m := &github.Milestone{
Title: &ms.Title,
State: &ms.State,
Description: &ms.Description,
DueOn: &ms.DueOn,
}
if !g.dryRun {
_, resp, err := g.client.Issues.CreateMilestone(repo.User, repo.Repo, m)
if err != nil {
return err
}
fmt.Printf("create milestone: %d (%+v)\n", resp.StatusCode, resp)
}
return nil
}
func (g *GitHubClient) deleteMilestone(repo *config.Repository, ms *config.Milestone) error {
if !g.dryRun {
resp, err := g.client.Issues.DeleteMilestone(repo.User, repo.Repo, ms.Number)
if err != nil {
return err
}
fmt.Printf("delete milestone: %d (%+v)\n", resp.StatusCode, resp)
}
return nil
}
func (g *GitHubClient) updateMilestone(repo *config.Repository, ms *config.Milestone) error {
m := &github.Milestone{
Title: &ms.Title,
State: &ms.State,
Description: &ms.Description,
DueOn: &ms.DueOn,
}
if !g.dryRun {
_, resp, err := g.client.Issues.EditMilestone(repo.User, repo.Repo, ms.Number, m)
if err != nil {
return err
}
fmt.Printf("edited milestone: %d (%+v)\n", resp.StatusCode, resp)
}
return nil
}