forked from ossf/scorecard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial implementation of ossf#1369 (comment) to provide more license…
… details Signed-off-by: Scott Hissam <[email protected]>
- Loading branch information
Showing
9 changed files
with
203 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2021 Security Scorecard 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 githubrepo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
"sync" | ||
|
||
"github.com/google/go-github/v38/github" | ||
|
||
"github.com/ossf/scorecard/v4/clients" | ||
) | ||
|
||
type licensesHandler struct { | ||
ghclient *github.Client | ||
once *sync.Once | ||
ctx context.Context | ||
errSetup error | ||
repourl *repoURL | ||
licenses []clients.License | ||
} | ||
|
||
func (handler *licensesHandler) init(ctx context.Context, repourl *repoURL) { | ||
handler.ctx = ctx | ||
handler.repourl = repourl | ||
handler.errSetup = nil | ||
handler.once = new(sync.Once) | ||
fmt.Println ("licenses handler initialized\n") | ||
} | ||
|
||
// TODO: Can add support to parse the raw response JSON and mark licenses that are not in | ||
// our defined License consts in clients/licenses.go as "not supported licenses". | ||
func (handler *licensesHandler) setup() error { | ||
handler.once.Do(func() { | ||
client := handler.ghclient | ||
// defined at docs.github.com/en/rest/licenses#get-the-license-for-a-repository | ||
reqURL := path.Join("repos", handler.repourl.owner, handler.repourl.repo, "license") | ||
req, err := client.NewRequest("GET", reqURL, nil) | ||
if err != nil { | ||
handler.errSetup = fmt.Errorf("request for repo license failed with %w", err) | ||
return | ||
} | ||
bodyJSON := github.RepositoryLicense{} | ||
// The client.repoClient.Do API writes the response body to var bodyJSON, | ||
// so we can ignore the first returned variable (the entire http response object) | ||
// since we only need the response body here. | ||
_, err = client.Do(handler.ctx, req, &bodyJSON) | ||
if err != nil { | ||
handler.errSetup = fmt.Errorf("response for repo license failed with %w", err) | ||
return | ||
} | ||
|
||
// TODO: github.RepositoryLicense{} only supports one license per repo | ||
// should that change to an array of licenses, the change would | ||
// be here to iterate over any such range. | ||
handler.licenses = append(handler.licenses, clients.License{ | ||
Key: bodyJSON.GetLicense().GetKey(), | ||
Name: bodyJSON.GetLicense().GetName(), | ||
SPDXId: bodyJSON.GetLicense().GetSPDXID(), | ||
Path: bodyJSON.GetName(), | ||
Type: bodyJSON.GetType(), | ||
Size: bodyJSON.GetSize(), | ||
}, | ||
) | ||
handler.errSetup = nil | ||
}) | ||
|
||
return handler.errSetup | ||
} | ||
|
||
func (handler *licensesHandler) listLicenses() ([]clients.License, error) { | ||
if err := handler.setup(); err != nil { | ||
return nil, fmt.Errorf("error during licensesHandler.setup: %w", err) | ||
} | ||
return handler.licenses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 Security Scorecard 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 clients | ||
|
||
// License represents a customized struct for licenses used by clients. | ||
// from pkg.go.dev/github.com/google/go-github/github#RepositoryLicense | ||
type License struct { | ||
Key string // RepositoryLicense.GetLicense().GetKey() | ||
Name string // RepositoryLicense.GetLicense().GetName() | ||
Path string // RepositoryLicense.GetName() | ||
Size int // RepositoryLicense.GetSize() | ||
SPDXId string // RepositoryLicense.GetLicense().GetSPDXID() | ||
Type string // RepositoryLicense.GetType() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters