-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_github.go
108 lines (95 loc) · 3.2 KB
/
adapter_github.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
package main
import (
"code.google.com/p/goauth2/oauth"
"fmt"
"github.com/google/go-github/github"
"github.com/wsxiaoys/terminal/color"
"strings"
)
type AdapterGithub struct {
Token string
}
func (d *AdapterGithub) Update() error {
var already, newbie int
var closedIssueTitles []string
// establish connection with github api by oauth.
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: config.RemoteConfig.ApiToken},
}
client := github.NewClient(t.Client())
repoInfo := strings.Split(config.RemoteConfig.Repo, "/")
remoteOpenIssues, remoteClosedIssues, err := GetRemoteIssues(client, repoInfo)
if err != nil {
return err
}
if len(remoteOpenIssues) != 0 { // if remoteOpenIssues is 0
for _, i := range IssueList {
// check if the issue is closed in remote.
if IsInRemote(remoteClosedIssues, i.Title) {
closedIssueTitles = append(closedIssueTitles, i.Title)
already++
} else {
frag := fmt.Sprintf("filename: %s\nline: %d\nlabel: %s\n\n```%s\n%s\n```\n", i.FilePath, i.Line, i.Label, i.Lang, i.Fragment)
labels := []string{"erk", i.Label}
_, _, err := client.Issues.Create(repoInfo[0], repoInfo[1], &github.IssueRequest{Title: &i.Title, Body: &frag, Labels: labels})
if err != nil {
return err
}
newbie++
}
}
} else { // if there is remoteOpenIssues
for _, i := range IssueList {
// check if the issue is closed in remote.
if IsInRemote(remoteClosedIssues, i.Title) {
closedIssueTitles = append(closedIssueTitles, i.Title)
} else {
roi := GetInRemoteIssueByTitle(remoteOpenIssues, i.Title)
if roi != nil {
already++
} else {
frag := fmt.Sprintf("filename: %s\nline: %d\nlabel: %s\n\n```%s\n%s\n```\n", i.FilePath, i.Line, i.Label, i.Lang, i.Fragment)
labels := []string{"erk", i.Label}
_, _, err := client.Issues.Create(repoInfo[0], repoInfo[1], &github.IssueRequest{Title: &i.Title, Body: &frag, Labels: labels})
if err != nil {
return err
}
newbie++
}
}
}
}
fmt.Printf("%d new issues are registerd in remote, %d issues is already registerd. \n", newbie, already)
// print closed issues.
if len(closedIssueTitles) != 0 {
fmt.Printf("\nand the issues: \n")
for _, t := range closedIssueTitles {
color.Printf("- @{!w}%s\n", t)
}
color.Printf("seems to be closed. Do you forget to delete these \"%s\" line?\n", config.Label)
}
return nil
}
func IsInRemote(issues []github.Issue, title string) bool {
for _, i := range issues {
if *i.Title == title {
return true
}
}
return false
}
func GetInRemoteIssueByTitle(issues []github.Issue, title string) *github.Issue {
for _, i := range issues {
if *i.Title == title {
return &i
}
}
return nil
}
func GetRemoteIssues(client *github.Client, repoInfo []string) (remoteOpenIssues []github.Issue, remoteClosedIssues []github.Issue, err error) {
// get all remote issues that are opened.
remoteOpenIssues, _, err = client.Issues.ListByRepo(repoInfo[0], repoInfo[1], &github.IssueListByRepoOptions{Labels: []string{"erk"}})
// get all remote issues that are closed.
remoteClosedIssues, _, err = client.Issues.ListByRepo(repoInfo[0], repoInfo[1], &github.IssueListByRepoOptions{Labels: []string{"erk"}, State: "closed"})
return
}