-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
59 lines (51 loc) · 1.2 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
package vcs
import (
"context"
"fmt"
"net/http"
"strings"
gogithub "github.com/google/go-github/v61/github"
)
const (
GitHubSSHKey = "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl"
)
type githubClient struct {
client *gogithub.Client
}
func NewGithubClient(httpClient *http.Client, token string) *githubClient {
client := gogithub.NewClient(httpClient).WithAuthToken(token)
return &githubClient{
client: client,
}
}
var _ providerClient = (*githubClient)(nil)
func (g *githubClient) CreateDeployKey(
ctx context.Context,
id string,
opts ...deployKeyOption,
) (*deployKey, error) {
deployKey, err := genDeployKey(opts...)
if err != nil {
return nil, err
}
idSplit := strings.Split(id, "/")
if len(idSplit) != 2 {
return nil, fmt.Errorf(
"%w: %s doesn't correspond to the owner/repo format",
ErrRepositoryID,
id,
)
}
keyReqBody := gogithub.Key{
Title: &deployKey.title,
Key: &deployKey.publicKeyOpenSSH,
}
_, _, err = g.client.Repositories.CreateKey(ctx, idSplit[0], idSplit[1], &keyReqBody)
if err != nil {
return nil, err
}
return deployKey, nil
}
func (g *githubClient) GetHostPublicSSHKey() string {
return GitHubSSHKey
}