/
main.go
136 lines (120 loc) · 2.9 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
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
"github.com/github/hub/git"
"github.com/github/hub/github"
)
const (
UmengLabel = "umeng"
)
func getToken() (token string, err error) {
return getConfig("github.token")
}
func getConfig(key string) (value string, err error) {
value, err = git.Config(key)
if value != "" && err == nil {
return
}
value, err = git.GlobalConfig(key)
return
}
func getOnwerRepo() (owner string, name string, err error) {
repo, err := github.LocalRepo()
if err != nil {
return
}
project, err := repo.CurrentProject()
if err != nil {
return
}
return project.Owner, project.Name, err
}
func handleError(err error) {
if err != nil {
fmt.Print(err)
os.Exit(1)
}
}
func main() {
app := cli.NewApp()
app.Name = "umeng2github"
app.Usage = "Import umeng error data which exported from umeng website as csv file to Github issues"
app.Commands = []cli.Command{
{
Name: "import",
Flags: []cli.Flag{
cli.StringFlag{
Name: "token, k",
Usage: "github personal token to use, can also set in git config with github.token",
EnvVar: "GITHUB_TOKEN",
},
cli.StringFlag{
Name: "owner, o",
Usage: "owner of the repo to which the issues are created",
EnvVar: "OWNER",
},
cli.StringFlag{
Name: "repo, r",
Usage: "repo name to which the issues are created",
EnvVar: "REPO",
},
cli.BoolFlag{
Name: "short",
Usage: "make output short, i.e. report success only",
EnvVar: "SHORT",
},
},
Usage: "import umeng error data csv to github",
Description: "The only argument is the filepath",
Action: func(c *cli.Context) {
token := c.String("token")
owner := c.String("owner")
repo := c.String("repo")
if token == "" {
tokenFromConfig, err := getToken()
handleError(err)
token = tokenFromConfig
}
if token == "" {
fmt.Println("Cannot find github.token in either global git config or local git config")
os.Exit(1)
}
if owner == "" || repo == "" {
ownerFromConfig, repoFromConfig, err := getOnwerRepo()
handleError(err)
if owner == "" {
owner = ownerFromConfig
}
if repo == "" {
repo = repoFromConfig
}
}
if len(c.Args()) < 1 {
fmt.Println("Missing file path argument")
os.Exit(1)
}
client := NewClient(token, owner, repo)
shortResultFlag := c.Bool("short")
for _, arg := range c.Args() {
report, err := ReadReport(arg)
handleError(err)
for _, record := range report {
body, err := FormatRecord(record)
handleError(err)
issue, err := client.CreateIssue(record.Title, body)
handleError(err)
if !shortResultFlag {
fmt.Printf("issue #%d created at %s\n", *issue.Number, *issue.HTMLURL)
}
}
}
if shortResultFlag {
fmt.Println("success")
}
},
},
}
app.Run(os.Args)
}