forked from ossf/scorecard-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
161 lines (134 loc) · 3.93 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
// 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
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper"
"github.com/ossf/scorecard/v4/log"
)
const (
baseRepoURL = "https://api.github.com/repos/"
)
// 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
}
// NewClient returns a new Client for querying repo info from GitHub.
func NewClient(ctx context.Context) *Client {
c := &Client{}
defaultCtx := context.Background()
if ctx == nil {
ctx = defaultCtx
}
c.SetContext(ctx)
c.SetDefaultTransport()
return c
}
// 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
}
// SetDefaultTransport sets the scorecard roundtripper for a GitHub client.
func (c *Client) SetDefaultTransport() {
logger := log.NewLogger(log.DefaultLevel)
rt := roundtripper.NewTransport(c.ctx, logger)
c.rt = rt
}
// WriteRepoInfo queries GitHub for repo info and writes it to a file.
func WriteRepoInfo(ctx context.Context, repoName, path string) error {
c := NewClient(ctx)
repoInfo, err := c.RepoInfo(repoName)
if err != nil {
return fmt.Errorf("getting repo info: %w", err)
}
repoFile, err := os.Create(path)
if err != nil {
return fmt.Errorf("creating repo info file: %w", err)
}
defer repoFile.Close()
resp := repoInfo.respBytes
_, writeErr := repoFile.Write(resp)
if writeErr != nil {
return fmt.Errorf("writing repo info: %w", writeErr)
}
return nil
}
// RepoInfo 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) RepoInfo(repoName string) (RepoInfo, error) {
var r RepoInfo
baseURL, err := url.Parse(baseRepoURL)
if err != nil {
return r, fmt.Errorf("parsing base repo URL: %w", err)
}
repoURL, err := baseURL.Parse(repoName)
if err != nil {
return r, fmt.Errorf("parsing repo endpoint: %w", err)
}
method := "GET"
req, err := http.NewRequestWithContext(
c.ctx,
method,
repoURL.String(),
nil,
)
if err != nil {
return r, fmt.Errorf("error creating request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return r, fmt.Errorf("error creating request: %w", err)
}
defer resp.Body.Close()
if err != nil {
return r, fmt.Errorf("error reading response body: %w", err)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return r, fmt.Errorf("error reading response body: %w", err)
}
r.respBytes = respBytes
err = json.Unmarshal(respBytes, &r)
if err != nil {
return r, fmt.Errorf("error decoding response body: %w", err)
}
return r, nil
}