15
15
package github
16
16
17
17
import (
18
+ "bytes"
18
19
"context"
19
20
"encoding/json"
20
21
"fmt"
21
22
"io"
23
+ "io/ioutil"
24
+ "log"
22
25
"net/http"
23
26
"net/url"
24
- "os"
25
27
26
28
"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper"
27
- "github.com/ossf/scorecard/v4/log"
28
- )
29
-
30
- const (
31
- baseRepoURL = "https://api.github.com/repos/"
29
+ sclog "github.com/ossf/scorecard/v4/log"
32
30
)
33
31
34
32
// RepoInfo is a struct for repository information.
@@ -43,9 +41,9 @@ type repo struct {
43
41
44
42
GITHUB_REPOSITORY_IS_FORK is true if the repository is a fork.
45
43
*/
46
- DefaultBranch string `json:"default_branch"`
47
- Fork bool `json:"fork"`
48
- Private bool `json:"private"`
44
+ DefaultBranch * string `json:"default_branch"`
45
+ Fork * bool `json:"fork"`
46
+ Private * bool `json:"private"`
49
47
}
50
48
51
49
// Client holds a context and roundtripper for querying repo info from GitHub.
@@ -54,20 +52,6 @@ type Client struct {
54
52
rt http.RoundTripper
55
53
}
56
54
57
- // NewClient returns a new Client for querying repo info from GitHub.
58
- func NewClient (ctx context.Context ) * Client {
59
- c := & Client {}
60
-
61
- defaultCtx := context .Background ()
62
- if ctx == nil {
63
- ctx = defaultCtx
64
- }
65
-
66
- c .SetContext (ctx )
67
- c .SetDefaultTransport ()
68
- return c
69
- }
70
-
71
55
// SetContext sets a context for a GitHub client.
72
56
func (c * Client ) SetContext (ctx context.Context ) {
73
57
c .ctx = ctx
@@ -80,82 +64,96 @@ func (c *Client) SetTransport(rt http.RoundTripper) {
80
64
81
65
// SetDefaultTransport sets the scorecard roundtripper for a GitHub client.
82
66
func (c * Client ) SetDefaultTransport () {
83
- logger := log .NewLogger (log .DefaultLevel )
67
+ logger := sclog .NewLogger (sclog .DefaultLevel )
84
68
rt := roundtripper .NewTransport (c .ctx , logger )
85
69
c .rt = rt
86
70
}
87
71
88
- // WriteRepoInfo queries GitHub for repo info and writes it to a file.
89
- func WriteRepoInfo (ctx context.Context , repoName , path string ) error {
90
- c := NewClient (ctx )
91
- repoInfo , err := c .RepoInfo (repoName )
92
- if err != nil {
93
- return fmt .Errorf ("getting repo info: %w" , err )
94
- }
95
-
96
- repoFile , err := os .Create (path )
97
- if err != nil {
98
- return fmt .Errorf ("creating repo info file: %w" , err )
99
- }
100
- defer repoFile .Close ()
101
-
102
- resp := repoInfo .respBytes
103
- _ , writeErr := repoFile .Write (resp )
104
- if writeErr != nil {
105
- return fmt .Errorf ("writing repo info: %w" , writeErr )
106
- }
107
-
108
- return nil
109
- }
110
-
111
- // RepoInfo is a function to get the repository information.
72
+ // ParseFromURL is a function to get the repository information.
112
73
// It is decided to not use the golang GitHub library because of the
113
74
// dependency on the github.com/google/go-github/github library
114
75
// which will in turn require other dependencies.
115
- func (c * Client ) RepoInfo (repoName string ) (RepoInfo , error ) {
116
- var r RepoInfo
117
-
76
+ func (c * Client ) ParseFromURL (baseRepoURL , repoName string ) (RepoInfo , error ) {
77
+ var ret RepoInfo
118
78
baseURL , err := url .Parse (baseRepoURL )
119
79
if err != nil {
120
- return r , fmt .Errorf ("parsing base repo URL: %w" , err )
80
+ return ret , fmt .Errorf ("parsing base repo URL: %w" , err )
121
81
}
122
82
123
- repoURL , err := baseURL .Parse (repoName )
83
+ repoURL , err := baseURL .Parse (fmt . Sprintf ( "repos/%s" , repoName ) )
124
84
if err != nil {
125
- return r , fmt .Errorf ("parsing repo endpoint: %w" , err )
85
+ return ret , fmt .Errorf ("parsing repo endpoint: %w" , err )
126
86
}
127
87
128
- method := "GET"
88
+ log . Printf ( "getting repo info from URL: %s" , repoURL . String ())
129
89
req , err := http .NewRequestWithContext (
130
90
c .ctx ,
131
- method ,
91
+ http . MethodGet ,
132
92
repoURL .String (),
133
- nil ,
134
- )
93
+ nil /*body*/ )
135
94
if err != nil {
136
- return r , fmt .Errorf ("error creating request: %w" , err )
95
+ return ret , fmt .Errorf ("error creating request: %w" , err )
137
96
}
138
97
139
98
resp , err := http .DefaultClient .Do (req )
140
99
if err != nil {
141
- return r , fmt .Errorf ("error creating request: %w" , err )
100
+ return ret , fmt .Errorf ("error creating request: %w" , err )
142
101
}
143
102
defer resp .Body .Close ()
144
103
if err != nil {
145
- return r , fmt .Errorf ("error reading response body: %w" , err )
104
+ return ret , fmt .Errorf ("error reading response body: %w" , err )
146
105
}
147
106
148
107
respBytes , err := io .ReadAll (resp .Body )
149
108
if err != nil {
150
- return r , fmt .Errorf ("error reading response body: %w" , err )
109
+ return ret , fmt .Errorf ("error reading response body: %w" , err )
151
110
}
152
111
153
- r .respBytes = respBytes
112
+ prettyPrintJSON (respBytes )
113
+ ret .respBytes = respBytes
114
+ if err := json .Unmarshal (respBytes , & ret .Repo ); err != nil {
115
+ return ret , fmt .Errorf ("error decoding response body: %w" , err )
116
+ }
117
+ return ret , nil
118
+ }
119
+
120
+ // ParseFromFile is a function to get the repository information
121
+ // from GitHub event file.
122
+ func (c * Client ) ParseFromFile (filepath string ) (RepoInfo , error ) {
123
+ var ret RepoInfo
154
124
155
- err = json .Unmarshal (respBytes , & r )
125
+ log .Printf ("getting repo info from file: %s" , filepath )
126
+ repoInfo , err := ioutil .ReadFile (filepath )
156
127
if err != nil {
157
- return r , fmt .Errorf ("error decoding response body : %w" , err )
128
+ return ret , fmt .Errorf ("reading GitHub event path : %w" , err )
158
129
}
159
130
160
- return r , nil
131
+ prettyPrintJSON (repoInfo )
132
+ if err := json .Unmarshal (repoInfo , & ret ); err != nil {
133
+ return ret , fmt .Errorf ("unmarshalling repo info: %w" , err )
134
+ }
135
+
136
+ return ret , nil
137
+ }
138
+
139
+ // NewClient returns a new Client for querying repo info from GitHub.
140
+ func NewClient (ctx context.Context ) * Client {
141
+ c := & Client {
142
+ ctx : ctx ,
143
+ }
144
+
145
+ if c .ctx == nil {
146
+ c .SetContext (context .Background ())
147
+ }
148
+ c .SetDefaultTransport ()
149
+ return c
150
+ }
151
+
152
+ func prettyPrintJSON (jsonBytes []byte ) {
153
+ var buf bytes.Buffer
154
+ if err := json .Indent (& buf , jsonBytes , "" , "" ); err != nil {
155
+ log .Printf ("%v" , err )
156
+ return
157
+ }
158
+ log .Println (buf .String ())
161
159
}
0 commit comments