This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
168 lines (135 loc) · 3.7 KB
/
provider.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package github
import (
"context"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"github.com/google/go-github/v31/github"
giturls "github.com/whilp/git-urls"
"golang.org/x/oauth2"
"github.com/weaveworks/go-git-providers/pkg/key"
)
const (
EnvVarGitHubToken = "GITHUB_TOKEN"
)
var (
sshFull = regexp.MustCompile(`ssh://git@github.com/([^/]+)/([^.]+).git`)
sshShort = regexp.MustCompile(`git@github.com:([^/]+)/([^.]+).git`)
patterns = []*regexp.Regexp{
sshFull,
sshShort,
}
)
// GitHubProvider accesses the Github API
type GitHubProvider struct {
owner, repo string
githubToken string
}
func NewGitHubProvider(repoURL string) (*GitHubProvider, error) {
githubToken := os.Getenv(EnvVarGitHubToken)
if githubToken == "" {
return nil,fmt.Errorf("%s is not set. Cannot authenticate to github.com", EnvVarGitHubToken)
}
repo, err := repoName(repoURL)
if err != nil {
return nil, err
}
owner, err := repoOwner(repoURL)
if err != nil {
return nil, err
}
return &GitHubProvider{
githubToken: githubToken,
owner: owner,
repo: repo,
}, nil
}
func (p *GitHubProvider) ListKeys(ctx context.Context) ([]key.SSHKey, error) {
gh := p.getGitHubAPIClient(ctx)
keys, resp, err := gh.Repositories.ListKeys(ctx, p.owner, p.repo, nil)
if err != nil {
return nil, fmt.Errorf("unable to list deploy keys from %s/%s", p.owner, p.repo)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to list deploy keys from %s/%s. Got response %s", p.owner, p.repo, resp.Status)
}
allKeys := make([]key.SSHKey, 0, len(keys))
for _, k := range keys {
allKeys = append(allKeys, key.SSHKey{
Title: *k.Title,
Key: *k.Key,
ReadOnly: *k.ReadOnly,
})
}
return allKeys, nil
}
func (p *GitHubProvider) AuthorizeSSHKey(ctx context.Context, key key.SSHKey) error {
gh := p.getGitHubAPIClient(ctx)
_, resp, err := gh.Repositories.CreateKey(ctx, p.owner, p.repo, &github.Key{
Key: &key.Key,
Title: &key.Title,
ReadOnly: &key.ReadOnly,
})
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unable to authorize SSH Key %q. Got StatusCode %s", key.Title, resp.Status)
}
return nil
}
func (p *GitHubProvider) DeleteSSHKey(ctx context.Context, title string) error {
gh := p.getGitHubAPIClient(ctx)
keys, _, err := gh.Repositories.ListKeys(ctx, p.owner, p.repo, &github.ListOptions{})
if err != nil {
return err
}
var keyID int64
for _, key := range keys {
if key.GetTitle() == title {
keyID = key.GetID()
break
}
}
if keyID == 0 {
return nil
}
if _, err := gh.Repositories.DeleteKey(ctx, p.owner, p.repo, keyID); err != nil {
return err
}
return nil
}
func (p *GitHubProvider) getGitHubAPIClient(ctx context.Context) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: p.githubToken},
)
tc := oauth2.NewClient(ctx, ts)
gh := github.NewClient(tc)
return gh
}
func repoName(repoURL string) (string, error) {
u, err := giturls.Parse(repoURL)
if err != nil {
return "", fmt.Errorf("unable to parse git URL '%s': %s", repoURL, err.Error())
}
parts := strings.Split(u.Path, "/")
if len(parts) == 0 {
return "", fmt.Errorf("could not find name of repository %s", repoURL)
}
lastPathSegment := parts[len(parts)-1]
return strings.TrimRight(lastPathSegment, ".git"), nil
}
func repoOwner(repoURL string) (string, error) {
u, err := giturls.Parse(repoURL)
if err != nil {
return "", fmt.Errorf("unable to parse git URL '%s': %s", repoURL, err.Error())
}
parts := strings.Split(u.Path, "/")
if len(parts) == 0 {
return "", fmt.Errorf("could not find name of repository %s", repoURL)
}
firstPathSegment := parts[0]
return firstPathSegment, nil
}