1package github
2
3import (
4 "context"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "io/ioutil"
9 "math/rand"
10 "net/http"
11 "time"
12
13 "github.com/MichaelMure/git-bug/bridge/core"
14 "github.com/MichaelMure/git-bug/bug"
15 "github.com/MichaelMure/git-bug/cache"
16 "github.com/shurcooL/githubv4"
17)
18
19const (
20 keyGithubIdExport = "github-id"
21 keyGithubUrlExport = "github-url"
22)
23
24// githubImporter implement the Importer interface
25type githubExporter struct {
26 gc *githubv4.Client
27 conf core.Configuration
28 cachedLabels map[string]githubv4.ID
29}
30
31// Init .
32func (ge *githubExporter) Init(conf core.Configuration) error {
33 ge.gc = buildClient(conf["token"])
34 ge.conf = conf
35 ge.cachedLabels = make(map[string]githubv4.ID)
36 return nil
37}
38
39// ExportAll export all event made by the current user to Github
40func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) error {
41 identity, err := repo.GetUserIdentity()
42 if err != nil {
43 return err
44 }
45
46 allBugsIds := repo.AllBugsIds()
47
48 // collect bugs
49 bugs := make([]*cache.BugCache, 0)
50 for _, id := range allBugsIds {
51 b, err := repo.ResolveBug(id)
52 if err != nil {
53 return err
54 }
55
56 snapshot := b.Snapshot()
57
58 // ignore issues edited before since date
59 if snapshot.LastEditTime().Before(since) {
60 continue
61 }
62
63 // if identity participated in a bug
64 for _, p := range snapshot.Participants {
65 if p.Id() == identity.Id() {
66 bugs = append(bugs, b)
67 }
68 }
69 }
70
71 // get repository node id
72 repositoryID, err := getRepositoryNodeID(
73 ge.conf[keyOwner],
74 ge.conf[keyProject],
75 ge.conf[keyToken],
76 )
77 if err != nil {
78 return err
79 }
80
81 for _, b := range bugs {
82 snapshot := b.Snapshot()
83 bugGithubID := ""
84
85 for _, op := range snapshot.Operations {
86 // treat only operations after since date
87 if op.Time().Before(since) {
88 continue
89 }
90
91 // ignore SetMetadata operations
92 if _, ok := op.(*bug.SetMetadataOperation); ok {
93 continue
94 }
95
96 // ignore imported issues and operations from github
97 if _, ok := op.GetMetadata(keyGithubId); ok {
98 continue
99 }
100
101 // get operation hash
102 hash, err := op.Hash()
103 if err != nil {
104 return fmt.Errorf("reading operation hash: %v", err)
105 }
106
107 // ignore already exported issues and operations
108 if _, err := b.ResolveOperationWithMetadata("github-exported-op", hash.String()); err != nil {
109 continue
110 }
111
112 switch op.(type) {
113 case *bug.CreateOperation:
114 opr := op.(*bug.CreateOperation)
115 //TODO export files
116 bugGithubID, err = ge.createGithubIssue(repositoryID, opr.Title, opr.Message)
117 if err != nil {
118 return fmt.Errorf("exporting bug %v: %v", b.HumanId(), err)
119 }
120
121 case *bug.AddCommentOperation:
122 opr := op.(*bug.AddCommentOperation)
123 bugGithubID, err = ge.addCommentGithubIssue(bugGithubID, opr.Message)
124 if err != nil {
125 return fmt.Errorf("adding comment %v: %v", "", err)
126 }
127
128 case *bug.EditCommentOperation:
129 opr := op.(*bug.EditCommentOperation)
130 if err := ge.editCommentGithubIssue(bugGithubID, opr.Message); err != nil {
131 return fmt.Errorf("editing comment %v: %v", "", err)
132 }
133
134 case *bug.SetStatusOperation:
135 opr := op.(*bug.SetStatusOperation)
136 if err := ge.updateGithubIssueStatus(bugGithubID, opr.Status); err != nil {
137 return fmt.Errorf("updating status %v: %v", bugGithubID, err)
138 }
139
140 case *bug.SetTitleOperation:
141 opr := op.(*bug.SetTitleOperation)
142 if err := ge.updateGithubIssueTitle(bugGithubID, opr.Title); err != nil {
143 return fmt.Errorf("editing comment %v: %v", bugGithubID, err)
144 }
145
146 case *bug.LabelChangeOperation:
147 opr := op.(*bug.LabelChangeOperation)
148 if err := ge.updateGithubIssueLabels(bugGithubID, opr.Added, opr.Removed); err != nil {
149 return fmt.Errorf("updating labels %v: %v", bugGithubID, err)
150 }
151
152 default:
153 // ignore other type of operations
154 }
155
156 }
157
158 if err := b.CommitAsNeeded(); err != nil {
159 return fmt.Errorf("bug commit: %v", err)
160 }
161
162 fmt.Printf("debug: %v", bugGithubID)
163 }
164
165 return nil
166}
167
168// getRepositoryNodeID request github api v3 to get repository node id
169func getRepositoryNodeID(owner, project, token string) (string, error) {
170 url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
171
172 client := &http.Client{
173 Timeout: defaultTimeout,
174 }
175
176 req, err := http.NewRequest("GET", url, nil)
177 if err != nil {
178 return "", err
179 }
180
181 // need the token for private repositories
182 req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
183
184 resp, err := client.Do(req)
185 if err != nil {
186 return "", err
187 }
188
189 if resp.StatusCode != http.StatusOK {
190 return "", fmt.Errorf("error retrieving repository node id %v", resp.StatusCode)
191 }
192
193 aux := struct {
194 NodeID string `json:"node_id"`
195 }{}
196
197 data, _ := ioutil.ReadAll(resp.Body)
198 defer resp.Body.Close()
199
200 err = json.Unmarshal(data, &aux)
201 if err != nil {
202 return "", err
203 }
204
205 return aux.NodeID, nil
206}
207
208func (ge *githubExporter) markOperationAsExported(b *cache.BugCache, opHash string) error {
209 return nil
210}
211
212// get label from github
213func (ge *githubExporter) getGithubLabelID(label string) (string, error) {
214 url := fmt.Sprintf("%s/repos/%s/%s/labels/%s", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject], label)
215
216 client := &http.Client{
217 Timeout: defaultTimeout,
218 }
219
220 req, err := http.NewRequest("GET", url, nil)
221 if err != nil {
222 return "", err
223 }
224
225 // need the token for private repositories
226 req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
227
228 resp, err := client.Do(req)
229 if err != nil {
230 return "", err
231 }
232
233 if resp.StatusCode != http.StatusFound {
234 return "", fmt.Errorf("error getting label: status code: %v", resp.StatusCode)
235 }
236
237 aux := struct {
238 ID string `json:"id"`
239 NodeID string `json:"node_id"`
240 Color string `json:"color"`
241 }{}
242
243 data, _ := ioutil.ReadAll(resp.Body)
244 defer resp.Body.Close()
245
246 err = json.Unmarshal(data, &aux)
247 if err != nil {
248 return "", err
249 }
250
251 return aux.NodeID, nil
252}
253
254// create github label using api v3
255func (ge *githubExporter) createGithubLabel(label, labelColor string) (string, error) {
256 url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
257
258 client := &http.Client{
259 Timeout: defaultTimeout,
260 }
261
262 req, err := http.NewRequest("POST", url, nil)
263 if err != nil {
264 return "", err
265 }
266
267 // need the token for private repositories
268 req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
269
270 resp, err := client.Do(req)
271 if err != nil {
272 return "", err
273 }
274
275 if resp.StatusCode != http.StatusCreated {
276 return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
277 }
278
279 aux := struct {
280 ID string `json:"id"`
281 NodeID string `json:"node_id"`
282 Color string `json:"color"`
283 }{}
284
285 data, _ := ioutil.ReadAll(resp.Body)
286 defer resp.Body.Close()
287
288 err = json.Unmarshal(data, &aux)
289 if err != nil {
290 return "", err
291 }
292
293 return aux.NodeID, nil
294}
295
296// randomHexColor return a random hex color code
297func randomHexColor() string {
298 bytes := make([]byte, 6)
299 if _, err := rand.Read(bytes); err != nil {
300 return "fffff"
301 }
302
303 return hex.EncodeToString(bytes)
304}
305
306func (ge *githubExporter) getOrCreateGithubLabelID(repositoryID, label string) (string, error) {
307 // try to get label id
308 labelID, err := ge.getGithubLabelID(label)
309 if err == nil {
310 return labelID, nil
311 }
312
313 // random color
314 color := randomHexColor()
315
316 // create label and return id
317 labelID, err = ge.createGithubLabel(label, color)
318 if err != nil {
319 return "", err
320 }
321
322 return labelID, nil
323}
324
325func (ge *githubExporter) getLabelsIDs(repositoryID string, labels []bug.Label) ([]githubv4.ID, error) {
326 ids := make([]githubv4.ID, 0, len(labels))
327 var err error
328
329 // check labels ids
330 for _, l := range labels {
331 label := string(l)
332
333 id, ok := ge.cachedLabels[label]
334 if !ok {
335 // try to query label id
336 id, err = ge.getOrCreateGithubLabelID(repositoryID, label)
337 if err != nil {
338 return nil, fmt.Errorf("get or create github label: %v", err)
339 }
340
341 // cache label id
342 ge.cachedLabels[label] = id
343 }
344
345 ids = append(ids, githubv4.ID(id))
346 }
347
348 return ids, nil
349}
350
351// create a github issue and return it ID
352func (ge *githubExporter) createGithubIssue(repositoryID, title, body string) (string, error) {
353 m := &createIssueMutation{}
354 input := &githubv4.CreateIssueInput{
355 RepositoryID: repositoryID,
356 Title: githubv4.String(title),
357 Body: (*githubv4.String)(&body),
358 }
359
360 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
361 return "", err
362 }
363
364 return m.CreateIssue.Issue.ID, nil
365}
366
367// add a comment to an issue and return it ID
368func (ge *githubExporter) addCommentGithubIssue(subjectID string, body string) (string, error) {
369 m := &addCommentToIssueMutation{}
370 input := &githubv4.AddCommentInput{
371 SubjectID: subjectID,
372 Body: githubv4.String(body),
373 }
374
375 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
376 return "", err
377 }
378
379 return m.AddComment.CommentEdge.Node.ID, nil
380}
381
382func (ge *githubExporter) editCommentGithubIssue(commentID, body string) error {
383 m := &updateIssueCommentMutation{}
384 input := &githubv4.UpdateIssueCommentInput{
385 ID: commentID,
386 Body: githubv4.String(body),
387 }
388
389 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
390 return err
391 }
392
393 return nil
394}
395
396func (ge *githubExporter) updateGithubIssueStatus(id string, status bug.Status) error {
397 m := &updateIssueMutation{}
398
399 // set state
400 state := githubv4.IssueStateClosed
401 if status == bug.OpenStatus {
402 state = githubv4.IssueStateOpen
403 }
404
405 input := &githubv4.UpdateIssueInput{
406 ID: id,
407 State: &state,
408 }
409
410 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
411 return err
412 }
413
414 return nil
415}
416
417func (ge *githubExporter) updateGithubIssueBody(id string, body string) error {
418 m := &updateIssueMutation{}
419 input := &githubv4.UpdateIssueInput{
420 ID: id,
421 Body: (*githubv4.String)(&body),
422 }
423
424 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
425 return err
426 }
427
428 return nil
429}
430
431func (ge *githubExporter) updateGithubIssueTitle(id, title string) error {
432 m := &updateIssueMutation{}
433 input := &githubv4.UpdateIssueInput{
434 ID: id,
435 Title: (*githubv4.String)(&title),
436 }
437
438 if err := ge.gc.Mutate(context.TODO(), m, input, nil); err != nil {
439 return err
440 }
441
442 return nil
443}
444
445// update github issue labels
446func (ge *githubExporter) updateGithubIssueLabels(labelableID string, added, removed []bug.Label) error {
447 addedIDs, err := ge.getLabelsIDs(labelableID, added)
448 if err != nil {
449 return fmt.Errorf("getting added labels ids: %v", err)
450 }
451
452 m := &updateIssueMutation{}
453 inputAdd := &githubv4.AddLabelsToLabelableInput{
454 LabelableID: labelableID,
455 LabelIDs: addedIDs,
456 }
457
458 // add labels
459 if err := ge.gc.Mutate(context.TODO(), m, inputAdd, nil); err != nil {
460 return err
461 }
462
463 removedIDs, err := ge.getLabelsIDs(labelableID, added)
464 if err != nil {
465 return fmt.Errorf("getting added labels ids: %v", err)
466 }
467
468 inputRemove := &githubv4.RemoveLabelsFromLabelableInput{
469 LabelableID: labelableID,
470 LabelIDs: removedIDs,
471 }
472
473 // remove label labels
474 if err := ge.gc.Mutate(context.TODO(), m, inputRemove, nil); err != nil {
475 return err
476 }
477
478 return nil
479}