1515package github
1616
1717import (
18+ "bytes"
1819 "context"
1920 "encoding/json"
2021 "fmt"
2122 "io"
23+ "io/ioutil"
24+ "log"
2225 "net/http"
2326 "net/url"
24- "os"
2527
2628 "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"
3230)
3331
3432// RepoInfo is a struct for repository information.
@@ -43,9 +41,9 @@ type repo struct {
4341
4442 GITHUB_REPOSITORY_IS_FORK is true if the repository is a fork.
4543 */
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"`
4947}
5048
5149// Client holds a context and roundtripper for querying repo info from GitHub.
@@ -54,20 +52,6 @@ type Client struct {
5452 rt http.RoundTripper
5553}
5654
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-
7155// SetContext sets a context for a GitHub client.
7256func (c * Client ) SetContext (ctx context.Context ) {
7357 c .ctx = ctx
@@ -80,82 +64,96 @@ func (c *Client) SetTransport(rt http.RoundTripper) {
8064
8165// SetDefaultTransport sets the scorecard roundtripper for a GitHub client.
8266func (c * Client ) SetDefaultTransport () {
83- logger := log .NewLogger (log .DefaultLevel )
67+ logger := sclog .NewLogger (sclog .DefaultLevel )
8468 rt := roundtripper .NewTransport (c .ctx , logger )
8569 c .rt = rt
8670}
8771
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.
11273// It is decided to not use the golang GitHub library because of the
11374// dependency on the github.com/google/go-github/github library
11475// 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
11878 baseURL , err := url .Parse (baseRepoURL )
11979 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 )
12181 }
12282
123- repoURL , err := baseURL .Parse (repoName )
83+ repoURL , err := baseURL .Parse (fmt . Sprintf ( "repos/%s" , repoName ) )
12484 if err != nil {
125- return r , fmt .Errorf ("parsing repo endpoint: %w" , err )
85+ return ret , fmt .Errorf ("parsing repo endpoint: %w" , err )
12686 }
12787
128- method := "GET"
88+ log . Printf ( "getting repo info from URL: %s" , repoURL . String ())
12989 req , err := http .NewRequestWithContext (
13090 c .ctx ,
131- method ,
91+ http . MethodGet ,
13292 repoURL .String (),
133- nil ,
134- )
93+ nil /*body*/ )
13594 if err != nil {
136- return r , fmt .Errorf ("error creating request: %w" , err )
95+ return ret , fmt .Errorf ("error creating request: %w" , err )
13796 }
13897
13998 resp , err := http .DefaultClient .Do (req )
14099 if err != nil {
141- return r , fmt .Errorf ("error creating request: %w" , err )
100+ return ret , fmt .Errorf ("error creating request: %w" , err )
142101 }
143102 defer resp .Body .Close ()
144103 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 )
146105 }
147106
148107 respBytes , err := io .ReadAll (resp .Body )
149108 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 )
151110 }
152111
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
154124
155- err = json .Unmarshal (respBytes , & r )
125+ log .Printf ("getting repo info from file: %s" , filepath )
126+ repoInfo , err := ioutil .ReadFile (filepath )
156127 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 )
158129 }
159130
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 ())
161159}
0 commit comments