Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add more info to logs #155

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ export ENABLED_CHECKS=
#
# Boolean inputs are strings https://github.com/actions/runner/issues/1483.
# ===============================================================================
curl -s -H "Authorization: Bearer $GITHUB_AUTH_TOKEN" https://api.github.com/repos/$GITHUB_REPOSITORY > repo_info.json
status_code=$(curl -s -H "Authorization: Bearer $GITHUB_AUTH_TOKEN" https://api.github.com/repos/"$GITHUB_REPOSITORY" -o repo_info.json -w '%{http_code}')
if [[ $status_code -ge 400 ]]; then
error_msg=$(jq -r .message repo_info.json 2>/dev/null || echo 'unknown error')
echo "Failed to get repository information from GitHub, response $status_code: $error_msg"
exit 1;
fi

export SCORECARD_PRIVATE_REPOSITORY="$(cat repo_info.json | jq -r '.private')"
export SCORECARD_DEFAULT_BRANCH="refs/heads/$(cat repo_info.json | jq -r '.default_branch')"
export SCORECARD_IS_FORK="$(cat repo_info.json | jq -r '.fork')"
rm repo_info.json

# If the repository is private, never publish the results.
if [[ "$SCORECARD_PRIVATE_REPOSITORY" == "true" ]]; then
Expand All @@ -73,6 +78,9 @@ echo "Publication enabled: $SCORECARD_PUBLISH_RESULTS"
echo "Format: $SCORECARD_RESULTS_FORMAT"
echo "Policy file: $SCORECARD_POLICY_FILE"
echo "Default branch: $SCORECARD_DEFAULT_BRANCH"
echo "repo_info:"
cat repo_info.json
rm repo_info.json

if [[ -z "$GITHUB_AUTH_TOKEN" ]]; then
echo "The 'repo_token' variable is empty."
Expand Down
13 changes: 3 additions & 10 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"net/http"
"net/url"
"os"

"github.com/ossf/scorecard/v4/clients/githubrepo/roundtripper"
"github.com/ossf/scorecard/v4/log"
Expand Down Expand Up @@ -85,22 +84,16 @@ func (c *Client) SetDefaultTransport() {
c.rt = rt
}

// WriteRepoInfo queries GitHub for repo info and writes it to a file.
func WriteRepoInfo(ctx context.Context, repoName, path string) error {
// WriteRepoInfo queries GitHub for repo info and writes it to an io.Writer.
func WriteRepoInfo(ctx context.Context, repoName string, writer io.Writer) 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)
_, writeErr := writer.Write(resp)
if writeErr != nil {
return fmt.Errorf("writing repo info: %w", writeErr)
}
Expand Down
13 changes: 8 additions & 5 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package options

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -64,6 +65,7 @@ type Options struct {
IsForkStr string `env:"SCORECARD_IS_FORK"`
// TODO(options): This may be better as a bool
PrivateRepoStr string `env:"SCORECARD_PRIVATE_REPOSITORY"`
RepoInfoStr string

// Input parameters
InputResultsFile string `env:"INPUT_RESULTS_FILE"`
Expand Down Expand Up @@ -196,6 +198,7 @@ func (o *Options) Print() {
fmt.Printf("Format: %s\n", o.ScorecardOpts.Format)
fmt.Printf("Policy file: %s\n", o.ScorecardOpts.PolicyFile)
fmt.Printf("Default branch: %s\n", o.DefaultBranch)
fmt.Printf("repo_info:\n%s\n", o.RepoInfoStr)
}

// SetPublishResults sets whether results should be published based on a
Expand Down Expand Up @@ -226,13 +229,13 @@ func (o *Options) SetRepoInfo() error {
return errGithubEventPathEmpty
}

repoInfo, err := ioutil.ReadFile(eventPath)
if err != nil {
return fmt.Errorf("reading GitHub event path: %w", err)
ios := bytes.NewBufferString(o.RepoInfoStr)
if err := github.WriteRepoInfo(context.Background(), o.ScorecardOpts.Repo, ios); err != nil {
return fmt.Errorf("github.WriteRepoInfo: %w", err)
}

var r github.RepoInfo
if err := json.Unmarshal(repoInfo, &r); err != nil {
if err := json.Unmarshal([]byte(o.RepoInfoStr), &r); err != nil {
return fmt.Errorf("unmarshalling repo info: %w", err)
}

Expand Down