This repository has been archived by the owner on Apr 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (237 loc) · 5.89 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/coreos/pkg/flagutil"
"github.com/google/go-github/github"
"github.com/sym3tri/go-pivotaltracker/v5/pivotal"
"golang.org/x/oauth2"
)
// make sure this exists in your repo
const migratedLabel = "migrated-pivotal"
var (
flags = struct {
owner string
repos flagutil.StringSliceFlag
ghToken string
ptToken string
ptProjId int
limit int
dryRun bool
}{}
ghSvc *github.IssuesService
ptSvc *pivotal.StoryService
)
func init() {
flag.StringVar(&flags.owner, "owner", "", "owner of the github repositories to be checked")
flag.Var(&flags.repos, "repos", "github repositories to be checked; if empty, all repositories of the given owner will be checked")
flag.StringVar(&flags.ghToken, "gh-token", "", "the GitHub API access token")
flag.StringVar(&flags.ptToken, "pt-token", "", "the Pivotal API access token")
flag.IntVar(&flags.ptProjId, "pt-proj-id", 0, "the Pivotal Project ID")
flag.IntVar(&flags.limit, "limit", 1000, "the max number of issues to attempt")
flag.BoolVar(&flags.dryRun, "dry-run", true, "print actions that would be taken")
}
func main() {
flag.Parse()
initClients()
repos := []string(flags.repos)
if len(repos) < 1 {
log.Fatal("no github repos specified")
}
for _, repo := range repos {
fmt.Printf("Analysing repo: %s/%s\n", flags.owner, repo)
iss, _, err := ghSvc.ListByRepo(flags.owner, repo, &github.IssueListByRepoOptions{
State: "open",
ListOptions: github.ListOptions{
PerPage: flags.limit,
},
})
if err != nil {
log.Fatalf("failed to list issues for repo: %s, error: %v", repo, err)
}
fmt.Printf("found %d issues to migrate", len(iss))
for _, is := range iss {
migrateIssue(repo, is)
}
}
fmt.Println("Finished.")
os.Exit(0)
}
func initClients() {
ptClient := pivotal.NewClient(flags.ptToken)
ptSvc = ptClient.Stories
ghClient := github.NewClient(
func(ghToken string) *http.Client {
if ghToken == "" {
return nil
}
return oauth2.NewClient(
oauth2.NoContext,
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: ghToken,
},
),
)
}(flags.ghToken),
)
ghSvc = ghClient.Issues
}
func alreadyMigrated(is *github.Issue) bool {
if len(is.Labels) == 0 {
return false
}
for _, l := range is.Labels {
if l.Name != nil && *l.Name == migratedLabel {
return true
}
}
return false
}
func migrateIssue(repo string, is *github.Issue) {
fmt.Printf("\n===== migrating issue number: %d =====\n", *is.Number)
fmt.Printf("original issue: %s\n", *is.HTMLURL)
if strings.Contains(*is.HTMLURL, "pull") {
fmt.Println("issue is a pull request, skipping.")
return
}
if alreadyMigrated(is) {
fmt.Println("issue already migrated, skipping.")
return
}
var newStory *pivotal.Story
var err error
storyReq := convertIssue(repo, is)
if flags.dryRun {
printIssue(is)
printStory(storyReq)
} else {
newStory, _, err = ptSvc.Create(flags.ptProjId, storyReq)
if err != nil {
log.Fatalf("error creating story: %v", err)
}
fmt.Printf("created pivotal story: %s\n", newStory.URL)
}
ghComments, _, err := ghSvc.ListComments(flags.owner, repo, *is.Number, &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: 1000,
},
})
if err != nil {
log.Fatalf("failed to list gh comments for repo: %s, issue %d: error: %v", repo, *is.Number, err)
}
if len(ghComments) > 0 {
fmt.Printf("migrating %d comments...", len(ghComments))
for _, cm := range ghComments {
commentReq := convertComment(cm)
if flags.dryRun {
printIssueComment(cm)
printStoryComment(commentReq)
} else {
_, _, err := ptSvc.AddComment(flags.ptProjId, newStory.Id, commentReq)
if err != nil {
log.Fatalf("error creating comment: %v", err)
}
}
}
}
// apply migrated label
_, _, err = ghSvc.AddLabelsToIssue(flags.owner, repo, *is.Number, []string{migratedLabel})
if err != nil {
log.Fatalf("failed to add '%s' label to gh issue for repo: %s, issue %d: error: %v", migratedLabel, repo, *is.Number, err)
}
}
func convertIssue(repo string, is *github.Issue) *pivotal.StoryRequest {
labels := []*pivotal.Label{
&pivotal.Label{Name: "github-migrated"},
&pivotal.Label{Name: fmt.Sprintf("migrated-repo/%s", repo)},
}
bodyFmt := "%s\n```"
bodyFmt += `
Migrated from Github
Created: %s
Labels: %q
`
bodyFmt += "```\n\n"
body := fmt.Sprintf(bodyFmt, *is.HTMLURL, *is.CreatedAt, is.Labels)
body += *is.Body
sr := &pivotal.StoryRequest{
Name: *is.Title,
Description: body,
Labels: &labels,
Type: pivotal.StoryTypeFeature,
State: pivotal.StoryStateUnscheduled,
}
return sr
}
func convertComment(cm *github.IssueComment) *pivotal.Comment {
bodyFmt := "%s\n```"
bodyFmt += `
Migrated from Github
Created: %s
Author: %s
`
bodyFmt += "```\n\n"
body := fmt.Sprintf(bodyFmt, *cm.HTMLURL, *cm.CreatedAt, *cm.User.Login)
body += *cm.Body
c := &pivotal.Comment{
Text: body,
}
return c
}
func printIssue(is *github.Issue) {
fmtStr := `
--- issue ---
Number: %d
Title: %s
URL: %s
Created: %s
Labels: %q
--- /issue ---
`
fmt.Printf(fmtStr, *is.Number, *is.Title, *is.HTMLURL, *is.CreatedAt, is.Labels)
}
func printStory(sr *pivotal.StoryRequest) {
labels := []string{}
for _, s := range *sr.Labels {
labels = append(labels, s.Name)
}
fmtStr := `
--- story ---
Name: %s
Description: %s
Type: %s
State: %s
Labels: %q
--- /story ---
`
fmt.Printf(fmtStr, sr.Name, trunc(sr.Description), sr.Type, sr.State, labels)
}
func printIssueComment(cm *github.IssueComment) {
fmtStr := `
--- issue comment ---
Author: %s
Created: %s
Body: %s
--- /issue comment ---
`
fmt.Printf(fmtStr, *cm.User.Login, *cm.CreatedAt, trunc(*cm.Body))
}
func printStoryComment(cm *pivotal.Comment) {
fmtStr := `
--- story comment ---
Text: %s
--- /story comment ---
`
fmt.Printf(fmtStr, trunc(cm.Text))
}
func trunc(s string) string {
if len(s) < 255 {
return s
}
return s[0:255]
}