Skip to content

Commit

Permalink
main: Wire up to entrypoint package
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed Feb 27, 2022
1 parent df3762a commit 37271a9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
34 changes: 19 additions & 15 deletions entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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 main
package entrypoint

import (
"encoding/json"
Expand All @@ -26,6 +26,8 @@ import (
"strings"
)

type Options struct{}

var (
errInputResultFileNotSet = errors.New("INPUT_RESULTS_FILE is not set")
errInputResultFileEmpty = errors.New("INPUT_RESULTS_FILE is empty")
Expand Down Expand Up @@ -74,62 +76,64 @@ const (
sarif = "sarif"
)

// main is the entrypoint for the action.
func main() {
// Run is the entrypoint for the action.
func Run(o *Options) error {
// TODO - This is a port of the entrypoint.sh script.
// This is still a work in progress.
if err := initalizeENVVariables(); err != nil {
panic(err)
if err := initializeEnvVariables(); err != nil {
return err
}
if err := checkIfRequiredENVSet(); err != nil {
panic(err)
return err
}

repository := os.Getenv(githubRepository)
token := os.Getenv(githubAuthToken)

repo, err := getRepositoryInformation(repository, token)
if err != nil {
panic(err)
return err
}

if err := updateRepositoryInformation(repo.Private, repo.DefaultBranch); err != nil {
panic(err)
return err
}

if err := updateEnvVariables(); err != nil {
panic(err)
return err
}

printEnvVariables(os.Stdout)

if err := validate(os.Stderr); err != nil {
panic(err)
return err
}

// gets the cmd run settings
cmd, err := runScorecardSettings(os.Getenv(githubEventName),
scorecardPolicyFile, scorecardResultsFormat,
scorecardBin, scorecardResultsFile, os.Getenv(githubRepository))
if err != nil {
panic(err)
return err
}
cmd.Dir = os.Getenv(githubWorkspace)
if err := cmd.Run(); err != nil {
panic(err)
return err
}

results, err := ioutil.ReadFile(scorecardResultsFile)
if err != nil {
panic(err)
return err
}

fmt.Println(string(results))

return nil
}

// initalizeENVVariables is a function to initialize the environment variables required for the action.
// initializeEnvVariables is a function to initialize the environment variables required for the action.
//nolint
func initalizeENVVariables() error {
func initializeEnvVariables() error {
/*
https://docs.github.com/en/actions/learn-github-actions/environment-variables
GITHUB_EVENT_PATH contains the json file for the event.
Expand Down
8 changes: 4 additions & 4 deletions entrypoint/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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 main
package entrypoint

import (
"bytes"
Expand Down Expand Up @@ -94,7 +94,7 @@ func Test_scorecardIsFork(t *testing.T) {

//not setting t.Parallel() here because we are mutating the env variables
//nolint
func Test_initalizeENVVariables(t *testing.T) {
func Test_initializeEnvVariables(t *testing.T) {
//nolint
tests := []struct {
name string
Expand Down Expand Up @@ -196,8 +196,8 @@ func Test_initalizeENVVariables(t *testing.T) {
} else {
os.Unsetenv(githubEventPath)
}
if err := initalizeENVVariables(); (err != nil) != tt.wantErr {
t.Errorf("initalizeENVVariables() error = %v, wantErr %v %v", err, tt.wantErr, t.Name())
if err := initializeEnvVariables(); (err != nil) != tt.wantErr {
t.Errorf("initializeEnvVariables() error = %v, wantErr %v %v", err, tt.wantErr, t.Name())
}

envvars := make(map[string]string)
Expand Down
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 main

import "github.com/ossf/scorecard-action/entrypoint"

var opts = &entrypoint.Options{}

func main() {
err := entrypoint.Run(opts)
if err != nil {
// TODO: Don't panic!
panic(err)
}
}

0 comments on commit 37271a9

Please sign in to comment.