/
push.go
128 lines (116 loc) · 2.91 KB
/
push.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
"github.com/google/go-github/github"
"github.com/lgtmco/lgtm-go/lgtm"
"golang.org/x/oauth2"
)
var PushCmd = cli.Command{
Name: "push",
Usage: "push the maintainer file to GitHub",
Action: func(c *cli.Context) {
handle(c, pushCmd)
},
}
func pushCmd(c *cli.Context, _ lgtm.Client) error {
var (
src io.ReadCloser
repo string = c.Args().Get(0)
path string = c.Args().Get(1)
err error
)
if len(path) != 0 {
src, err = os.Open(path)
if err != nil {
return err
}
defer src.Close()
} else {
src = os.Stdin
}
var dest bytes.Buffer
io.Copy(&dest, src)
if err != nil {
return err
}
v := new(lgtm.Maintainer)
_, err = toml.Decode(dest.String(), v)
if err != nil {
return err
}
//server := c.GlobalString("github-server")
token := c.GlobalString("github-token")
client := setupClient(token)
sha, err := createUpdateFile(
client,
strings.Split(repo, "/")[0],
strings.Split(repo, "/")[1],
dest.Bytes(),
)
if err == nil {
fmt.Printf("pushed MAINTAINERS file to %s@%s\n", repo, sha[:7])
}
return err
}
func setupClient(accessToken string) *github.Client {
token := oauth2.Token{AccessToken: accessToken}
source := oauth2.StaticTokenSource(&token)
client := oauth2.NewClient(oauth2.NoContext, source)
return github.NewClient(client)
}
func createUpdateFile(client *github.Client, owner, name string, data []byte) (string, error) {
sha, err := getSha(client, owner, name)
if err != nil {
return createFile(client, owner, name, data)
} else {
return updateFile(client, owner, name, sha, data)
}
}
// helper function to get the MAINTAINER files SHA
func getSha(client *github.Client, owner, name string) (string, error) {
opt := new(github.RepositoryContentGetOptions)
res, _, _, err := client.Repositories.GetContents(owner, name, "MAINTAINERS", opt)
if err != nil {
return "", err
}
return *res.SHA, nil
}
// helper function to update the MAINTAINER file
func updateFile(client *github.Client, owner, name, sha string, data []byte) (string, error) {
res, _, err := client.Repositories.UpdateFile(
owner,
name,
"MAINTAINERS",
&github.RepositoryContentFileOptions{
Content: data,
Message: github.String("Updated MAINTAINERS file [CI SKIP]"),
SHA: github.String(sha),
},
)
if err != nil {
return "", fmt.Errorf("Error updating MAINTAINERS file at %s. %s", sha, err)
}
return *res.SHA, nil
}
// helper function to update the MAINTAINER file
func createFile(client *github.Client, owner, name string, data []byte) (string, error) {
res, _, err := client.Repositories.CreateFile(
owner,
name,
"MAINTAINERS",
&github.RepositoryContentFileOptions{
Content: data,
Message: github.String("Created MAINTAINERS file [CI SKIP]"),
},
)
if err != nil {
return "", fmt.Errorf("Error creating MAINTAINERS file. %s", err)
}
return *res.SHA, nil
}