-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathgithub.go
162 lines (139 loc) · 4.3 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
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
// Copyright OpenSSF 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 github provides repo information from GitHub.
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"github.com/ossf/scorecard/v5/clients/githubrepo/roundtripper"
sclog "github.com/ossf/scorecard/v5/log"
)
// RepoInfo is a struct for repository information.
type RepoInfo struct {
Repo repo `json:"repository"`
respBytes []byte
}
type repo struct {
/*
https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#github_repository_is_fork
GITHUB_REPOSITORY_IS_FORK is true if the repository is a fork.
*/
DefaultBranch *string `json:"default_branch"`
Fork *bool `json:"fork"`
Private *bool `json:"private"`
}
// Client holds a context and roundtripper for querying repo info from GitHub.
type Client struct {
ctx context.Context
rt http.RoundTripper
}
// SetContext sets a context for a GitHub client.
func (c *Client) SetContext(ctx context.Context) {
c.ctx = ctx
}
// SetTransport sets a http.RoundTripper for a GitHub client.
func (c *Client) SetTransport(rt http.RoundTripper) {
c.rt = rt
}
// Transport returns the http.RoundTripper for a GitHub client.
func (c *Client) Transport() http.RoundTripper {
return c.rt
}
// SetDefaultTransport sets the scorecard roundtripper for a GitHub client.
func (c *Client) SetDefaultTransport() {
logger := sclog.NewLogger(sclog.DefaultLevel)
rt := roundtripper.NewTransport(c.ctx, logger)
c.rt = rt
}
// ParseFromURL is a function to get the repository information.
// It is decided to not use the golang GitHub library because of the
// dependency on the github.com/google/go-github/github library
// which will in turn require other dependencies.
func (c *Client) ParseFromURL(baseRepoURL, repoName string) (RepoInfo, error) {
var ret RepoInfo
baseURL, err := url.Parse(baseRepoURL)
if err != nil {
return ret, fmt.Errorf("parsing base repo URL: %w", err)
}
repoURL := baseURL.JoinPath(fmt.Sprintf("repos/%s", repoName))
log.Printf("getting repo info from URL: %s", repoURL.String())
//nolint:noctx
req, err := http.NewRequestWithContext(
c.ctx,
http.MethodGet,
repoURL.String(),
nil /*body*/)
if err != nil {
return ret, fmt.Errorf("error creating request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return ret, fmt.Errorf("error creating request: %w", err)
}
defer resp.Body.Close()
if err != nil {
return ret, fmt.Errorf("error reading response body: %w", err)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return ret, fmt.Errorf("error reading response body: %w", err)
}
prettyPrintJSON(respBytes)
ret.respBytes = respBytes
if err := json.Unmarshal(respBytes, &ret.Repo); err != nil {
return ret, fmt.Errorf("error decoding response body: %w", err)
}
return ret, nil
}
// ParseFromFile is a function to get the repository information
// from GitHub event file.
func (c *Client) ParseFromFile(filepath string) (RepoInfo, error) {
var ret RepoInfo
log.Printf("getting repo info from file: %s", filepath)
repoInfo, err := os.ReadFile(filepath)
if err != nil {
return ret, fmt.Errorf("reading GitHub event path: %w", err)
}
prettyPrintJSON(repoInfo)
if err := json.Unmarshal(repoInfo, &ret); err != nil {
return ret, fmt.Errorf("unmarshalling repo info: %w", err)
}
return ret, nil
}
// NewClient returns a new Client for querying repo info from GitHub.
func NewClient(ctx context.Context) *Client {
c := &Client{
ctx: ctx,
}
if c.ctx == nil {
c.SetContext(context.Background())
}
c.SetDefaultTransport()
return c
}
func prettyPrintJSON(jsonBytes []byte) {
var buf bytes.Buffer
if err := json.Indent(&buf, jsonBytes, "", ""); err != nil {
log.Printf("%v", err)
return
}
log.Println(buf.String())
}