forked from kubernetes/perf-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-configs-fetcher.go
85 lines (76 loc) · 2.49 KB
/
github-configs-fetcher.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
/*
Copyright 2019 The Kubernetes Authors.
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 (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"gopkg.in/yaml.v2"
"k8s.io/klog"
)
type githubDirContent struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
DownloadURL string `yaml:"download_url"`
Type string `yaml:"type"`
URL string `yaml:"url"`
}
// GetConfigsFromGithub gets config paths from github directory. It uses github API,
// which is documented here: https://developer.github.com/v3/repos/contents/
//
// Example url: https://api.github.com/repos/kubernetes/test-infra/contents/config/jobs/kubernetes/sig-release/release-branch-jobs
//
// Different branch can be specified by appending "?ref=branch-name" parameter at the end
// of the url.
func GetConfigsFromGithub(url string) ([]string, error) {
var result []string
contents, err := getGithubDirContents(url)
if err != nil {
return nil, err
}
for _, c := range contents {
// Dirs and non-yaml files are ignored; this means that there is no
// recursive search, it should be good enough for now.
if c.Type == "file" && strings.HasSuffix(c.Name, ".yaml") {
result = append(result, c.DownloadURL)
}
}
return result, nil
}
func getGithubDirContents(url string) ([]githubDirContent, error) {
klog.Infof("Downloading github spec from %v", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if token := os.Getenv("GITHUB_TOKEN"); len(token) != 0 {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error calling github API %s: %v", url, err)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading github response %s: %v", url, err)
}
var decoded []githubDirContent
err = yaml.Unmarshal(b, &decoded)
if err != nil {
return nil, fmt.Errorf("error unmarshall github response %s: %v", string(b), err)
}
return decoded, nil
}