forked from remind101/conveyor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
109 lines (92 loc) · 2.63 KB
/
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
109
package conveyor
import (
"fmt"
"strings"
"github.com/google/go-github/github"
)
// NewHook returns a new github.Hook instance that represents the appropriate
// configuration for the Conveyor webhook.
func NewHook(url, secret string) *github.Hook {
return &github.Hook{
Events: []string{"push"},
Active: github.Bool(true),
Name: github.String("web"),
Config: map[string]interface{}{
"url": url,
"content_type": "json",
"secret": secret,
},
}
}
// GitHubAPI represents an interface for performing Git operations.
type GitHubAPI interface {
ResolveBranch(owner, repo, branch string) (sha string, err error)
InstallHook(owner, repo string, hook *github.Hook) error
}
func NewGitHub(c *github.Client) *GitHub {
return &GitHub{
Git: c.Git,
Repositories: c.Repositories,
}
}
// GitHub is an implementation of the Git interface
// backed by the GitHub API.
type GitHub struct {
Git *github.GitService
Repositories interface {
CreateHook(owner, repo string, hook *github.Hook) (*github.Hook, *github.Response, error)
ListHooks(owner, repo string, opt *github.ListOptions) ([]github.Hook, *github.Response, error)
EditHook(owner, repo string, id int, hook *github.Hook) (*github.Hook, *github.Response, error)
}
}
func (g *GitHub) ResolveBranch(owner, repo, branch string) (string, error) {
ref, _, err := g.Git.GetRef(owner, repo, fmt.Sprintf("refs/heads/%s", branch))
if err != nil {
return "", err
}
return *ref.Object.SHA, nil
}
// InstallHook installs the hook on the repo.
func (g *GitHub) InstallHook(owner, repo string, hook *github.Hook) (err error) {
var existingHook *github.Hook
existingHook, err = g.existingHook(owner, repo, hook)
if err != nil {
return
}
if existingHook != nil {
if _, _, err = g.Repositories.EditHook(owner, repo, *existingHook.ID, hook); err != nil {
return
}
} else {
if _, _, err = g.Repositories.CreateHook(owner, repo, hook); err != nil {
return
}
}
return
}
// existingHook returns an existing hook if it exists.
func (g *GitHub) existingHook(owner, repo string, hook *github.Hook) (*github.Hook, error) {
hooks, _, err := g.Repositories.ListHooks(owner, repo, nil)
if err != nil {
return nil, err
}
for _, existingHook := range hooks {
if equalHooks(&existingHook, hook) {
return &existingHook, nil
}
}
return nil, nil
}
func equalHooks(a, b *github.Hook) bool {
if *a.Name == *b.Name {
if *a.Name == "web" {
return a.Config["url"].(string) == b.Config["url"].(string)
}
}
return false
}
func splitRepo(fullRepo string) (owner, repo string) {
parts := strings.Split(fullRepo, "/")
owner, repo = parts[0], parts[1]
return
}