-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgh_common.go
105 lines (82 loc) · 2.77 KB
/
gh_common.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
// Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements;
// and to You under the Apache License, Version 2.0. See LICENSE in project root for full license + copyright.
package keynuker
import (
"context"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/url"
)
// Wrap the github.RepositoryCommit and github.PushEventCommit into a single common interface
type WrappedCommit interface {
Sha() string
Url() string
}
type RepositoryCommit github.RepositoryCommit
type PushEventCommit github.PushEventCommit
func (r *RepositoryCommit) Sha() string {
return *r.SHA
}
func (p *PushEventCommit) Sha() string {
return *p.SHA
}
func (r *RepositoryCommit) Url() string {
return *r.URL
}
func (p *PushEventCommit) Url() string {
return *p.URL
}
func ConvertRepositoryCommits(repositoryCommits []*github.RepositoryCommit) []WrappedCommit {
result := make([]WrappedCommit, len(repositoryCommits))
for i, repositoryCommitPtr := range repositoryCommits {
repositoryCommit := *repositoryCommitPtr
resultCommit := RepositoryCommit(repositoryCommit)
result[i] = &resultCommit
}
return result
}
func ConvertPushEventCommits(pushEventCommits []github.PushEventCommit) []WrappedCommit {
result := make([]WrappedCommit, len(pushEventCommits))
for i, pushEventCommit := range pushEventCommits {
resultCommit := PushEventCommit(pushEventCommit)
result[i] = &resultCommit
}
return result
}
type GithubClientWrapper struct {
AccessToken string
ApiClient *github.Client
}
// The connection parameters requires to connect to the Github API on github.com or
// hosted on Github Enterprise.
type GithubConnectionParams struct {
// The URL of the github API to connect to. If blank, will connect to https://api.github.com.
// Github Enterprise users will need to set this to point to their Github Enterprise server
GithubApiUrl string
// The github access token, which needs "read:org" permissions in order to read the concealed "non-public"
// members of the orgs
GithubAccessToken string
}
// If you want to use the default github API (as opposed to github enterprise), pass
// in an empty string for the githubApiBaseUrl
func NewGithubClientWrapper(accessToken, githubApiBaseUrl string) *GithubClientWrapper {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// If an alternative github API base url was given, use that
if githubApiBaseUrl != "" {
baseUrl, err := url.Parse(githubApiBaseUrl)
if err != nil {
panic(fmt.Sprintf("Invalid Github API url given: %v", githubApiBaseUrl))
}
client.BaseURL = baseUrl
}
return &GithubClientWrapper{
AccessToken: accessToken,
ApiClient: client,
}
}