-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (142 loc) · 4.13 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
package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
tagger "github.com/garypwhite/tag-your-git/pkg/tagger"
tagmatcher "github.com/garypwhite/tag-your-git/pkg/tagmatcher"
github "github.com/google/go-github/github"
"github.com/urfave/cli/v2"
"golang.org/x/oauth2"
)
func makeClient(url, apikey string) (*github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: apikey},
)
tc := oauth2.NewClient(ctx, ts)
if len(url) != 0 {
return github.NewEnterpriseClient(url, url, tc) // use default client (nil)
}
return github.NewClient(tc), nil
}
func makeTagger(enterpriseURL, apikey, tagsJSON string) (*tagger.Tagger, error) {
githubClient, err := makeClient(enterpriseURL, apikey)
if err != nil {
return nil, err
}
mapping, err := tagmatcher.Unmarshal([]byte(tagsJSON))
if err != nil {
return nil, err
}
tagger := tagger.Tagger{
Client: githubClient,
TagMapping: mapping,
}
return &tagger, nil
}
func getPullRequestDetails(prURL string) (owner, repo string, pullRequestNumber int, err error) {
// assuming a url with trailing ..../owner/repo/pull/number....
splitStrings := strings.Split(strings.Trim(prURL, "/"), "/")
lenStrings := len(splitStrings)
if lenStrings < 4 { // sanity check -- even partial URL will have at least 4
err = fmt.Errorf("Invalid PR input, should be like https://github.com/garypwhite/tag-your-git/pull/10, got %s", prURL)
return
}
owner = splitStrings[lenStrings-4]
repo = splitStrings[lenStrings-3]
pullRequestNumber, err = strconv.Atoi(splitStrings[lenStrings-1])
return
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "enterprise-URL",
Aliases: []string{"enterprise-url"},
EnvVars: []string{"TYG_ENTERPRISE_URL"},
},
&cli.StringFlag{
Name: "tags",
Aliases: []string{"t"},
Usage: "JSON struct of tags to post. Uses the format [ { [patterns], [tags] } ]",
EnvVars: []string{"TYG_TAGS"},
FilePath: "/etc/tag-your-git/tags.json", // Recommended to use a docker image
},
&cli.StringFlag{
Name: "git-api-key",
Aliases: []string{"gapi-key"},
Usage: "API key to post to specified Git API",
EnvVars: []string{"TYG_GIT_API_KEY"},
FilePath: "/etc/tag-your-git/git-api-key", // Recommended to use a docker image
},
},
Commands: []*cli.Command{
{
Name: "label",
Usage: "label one Pull Request (-pr, -pull-request) with a specified tags JSON array (-tags, -t)",
Action: func(c *cli.Context) error {
tc, err := makeTagger(c.String("enterprise-URL"), c.String("git-api-key"), c.String("tags"))
if err != nil {
return err
}
owner, repo, pullRequestNumber, err := tagger.GetPullRequestDetails(c.String("pull-request"))
pr, _, err := tc.Client.PullRequests.Get(
context.Background(),
owner,
repo,
pullRequestNumber,
)
if err != nil {
return err
}
return tc.PostTagsToPullRequest(pr)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pull-request",
Aliases: []string{"pr"},
Usage: "Pull Request URL to tag",
EnvVars: []string{"TYG_PULL_REQUEST"},
Required: true,
},
},
},
{
Name: "listen",
Usage: "Listen for incoming webhooks to process + post tags according to (--tags, /etc/tag-your-git/tags.json, TYG_TAGS) specified",
Action: func(c *cli.Context) error {
// use CLI context to make client
tagger, err := makeTagger(c.String("enterprise-URL"), c.String("git-api-key"), c.String("tags"))
if err != nil {
return err
}
secretKey := []byte(c.String("webhook-secret-key"))
return tagger.Listen(secretKey)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "webhook-secret-key",
Usage: "secret provided from GitHub webhook",
EnvVars: []string{"TYG_WEBHOOK_SECRET_KEY"},
},
},
},
},
Name: "tag-your-git (tyg)",
Version: "v0.1.0",
Authors: []*cli.Author{
&cli.Author{
Name: "Gary White Jr.",
Email: "garypwhite@github",
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}