-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgithub.go
115 lines (97 loc) · 2.75 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
110
111
112
113
114
115
//
// Copyright (c) 2023 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v48/github"
"golang.org/x/oauth2"
"k8s.io/klog/v2"
)
type GithubClient struct {
gh *github.Client
token string
}
func NewGithubClient() (*GithubClient, error) {
key := "GITHUB_TOKEN"
val, ok := os.LookupEnv(key)
if !ok {
klog.Errorf("%s not set\n", key)
return nil, fmt.Errorf("%s not set", key)
}
if val == "" {
klog.Errorf("%s is empty\n", key)
}
gh_client := &GithubClient{}
gh := gh_client.InitClient(val)
gh_client.gh = gh
gh_client.token = val
return gh_client, nil
}
func (gc *GithubClient) InitClient(val string) *github.Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: val},
)
tc := oauth2.NewClient(ctx, ts)
gh := github.NewClient(tc)
return gh
}
func (gc *GithubClient) Client() *github.Client {
return gc.gh
}
func (gc *GithubClient) SearchCommit(hash string, org string) (*github.Commit, error) {
query := "hash:" + hash + " is:public"
if len(org) > 0 {
query = query + " org:" + org
}
commits, _, err := gc.Client().Search.Commits(context.Background(), query, &github.SearchOptions{})
if err != nil {
//fmt.Println("Search error: ", err)
return nil, err
}
if commits.GetTotal() == 0 {
return nil, fmt.Errorf("error getting commit: no data found for %s", hash)
}
if commits.GetTotal() != 1 {
return nil, fmt.Errorf("error getting commit: not unique data returned for %s", hash)
}
commit := commits.Commits[0].Commit
return commit, nil
}
func (gc *GithubClient) GetCommitFromOrgAndRepo(org string, repo string, hash string) (*github.Commit, error) {
searchOrg := org
new_org := gc.LookupOrg(repo)
if new_org != "" {
searchOrg = new_org
}
commits, _, err := gc.Client().Repositories.GetCommit(context.Background(), searchOrg, repo, hash, &github.ListOptions{})
if err != nil {
//fmt.Println("Can't get ", hash, " use search instead")
return nil, err
}
commit := commits.Commit
return commit, nil
}
func (gc *GithubClient) LookupOrg(repo string) string {
repos := map[string]string{
"pipeline-service-exporter": "openshift-pipelines",
}
val, ok := repos[repo]
if !ok {
return ""
}
return val
}