Skip to content

Commit 322c6e0

Browse files
🌱 Final bits of porting the shell to go
- Final bits of porting the shell script to `go` - Tests included for the commandline args to Scorecard.
1 parent 5b4ee38 commit 322c6e0

File tree

4 files changed

+333
-0
lines changed

4 files changed

+333
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/ossf/scorecard-action
22

33
go 1.17
4+
5+
require github.com/google/go-cmp v0.5.7 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
2+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
3+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"net/http"
2323
"os"
24+
"os/exec"
2425
"strconv"
2526
"strings"
2627
)
@@ -39,6 +40,7 @@ var (
3940
errEmptyDefaultBranch = errors.New("default branch is empty")
4041
errEmptyGitHubAuthToken = errors.New("repo_token variable is empty")
4142
errOnlyDefaultBranchSupported = errors.New("only default branch is supported")
43+
errEmptyScorecardBin = errors.New("scorecard_bin variable is empty")
4244
)
4345

4446
type repositoryInformation struct {
@@ -55,6 +57,7 @@ const (
5557
githubEventName = "GITHUB_EVENT_NAME"
5658
githubRepository = "GITHUB_REPOSITORY"
5759
githubRef = "GITHUB_REF"
60+
githubWorkspace = "GITHUB_WORKSPACE"
5861
//nolint:gosec
5962
githubAuthToken = "GITHUB_AUTH_TOKEN"
6063
inputresultsfile = "INPUT_RESULTS_FILE"
@@ -103,6 +106,25 @@ func main() {
103106
if err := validate(os.Stderr); err != nil {
104107
panic(err)
105108
}
109+
110+
// gets the cmd run settings
111+
cmd, err := runScorecardSettings(os.Getenv(githubEventName),
112+
os.Getenv(scorecardPolicyFile), os.Getenv(scorecardResultsFormat),
113+
os.Getenv(scorecardBin), os.Getenv(scorecardResultsFile), os.Getenv(githubRepository))
114+
if err != nil {
115+
panic(err)
116+
}
117+
cmd.Dir = os.Getenv(githubWorkspace)
118+
if err := cmd.Run(); err != nil {
119+
panic(err)
120+
}
121+
122+
results, err := ioutil.ReadFile(os.Getenv(scorecardResultsFile))
123+
if err != nil {
124+
panic(err)
125+
}
126+
127+
fmt.Println(string(results))
106128
}
107129

108130
// initalizeENVVariables is a function to initialize the environment variables required for the action.
@@ -334,3 +356,72 @@ func validate(writer io.Writer) error {
334356
}
335357
return nil
336358
}
359+
360+
func runScorecardSettings(githubEventName, scorecardPolicyFile, scorecardResultsFormat, scorecardBin,
361+
scorecardResultsFile, githubRepository string) (*exec.Cmd, error) {
362+
if scorecardBin == "" {
363+
return nil, errEmptyScorecardBin
364+
}
365+
var result exec.Cmd
366+
result.Path = scorecardBin
367+
// if pull_request
368+
if strings.Contains(githubEventName, "pull_request") {
369+
// empty policy file
370+
if scorecardPolicyFile == "" {
371+
result.Args = []string{
372+
"--local",
373+
".",
374+
"--format",
375+
scorecardResultsFormat,
376+
"--show-details",
377+
">",
378+
scorecardResultsFile,
379+
}
380+
return &result, nil
381+
}
382+
result.Args = []string{
383+
"--local",
384+
".",
385+
"--format",
386+
scorecardResultsFormat,
387+
"--policy",
388+
scorecardPolicyFile,
389+
"--show-details",
390+
">",
391+
scorecardResultsFile,
392+
}
393+
return &result, nil
394+
}
395+
396+
enabledChecks := ""
397+
if githubEventName == "branch_protection_rule" {
398+
enabledChecks = "--checks Branch-Protection"
399+
}
400+
401+
if scorecardPolicyFile == "" {
402+
result.Args = []string{
403+
"--repo",
404+
githubRepository,
405+
"--format",
406+
enabledChecks,
407+
scorecardResultsFormat,
408+
"--show-details",
409+
">",
410+
scorecardResultsFile,
411+
}
412+
return &result, nil
413+
}
414+
result.Args = []string{
415+
"--repo",
416+
githubRepository,
417+
"--format",
418+
enabledChecks,
419+
scorecardResultsFormat,
420+
"--policy",
421+
scorecardPolicyFile,
422+
"--show-details",
423+
">",
424+
scorecardResultsFile,
425+
}
426+
return &result, nil
427+
}

main_test.go

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ import (
1818
"fmt"
1919
"io/ioutil"
2020
"os"
21+
"os/exec"
2122
"strconv"
2223
"testing"
24+
25+
"github.com/google/go-cmp/cmp"
2326
)
2427

2528
//not setting t.Parallel() here because we are mutating the env variables
@@ -482,3 +485,237 @@ func Test_validate(t *testing.T) {
482485
})
483486
}
484487
}
488+
489+
func Test_runScorecardSettings(t *testing.T) {
490+
t.Parallel()
491+
type args struct {
492+
githubEventName string
493+
scorecardPolicyFile string
494+
scorecardResultsFormat string
495+
scorecardBin string
496+
scorecardResultsFile string
497+
githubRepository string
498+
}
499+
//nolint
500+
tests := []struct {
501+
wantErr bool
502+
name string
503+
args args
504+
want *exec.Cmd
505+
}{
506+
{
507+
name: "Success - scorecardFork set",
508+
args: args{
509+
githubEventName: "pull_request",
510+
scorecardPolicyFile: "./testdata/scorecard.yaml",
511+
scorecardResultsFormat: "json",
512+
scorecardBin: "scorecard",
513+
scorecardResultsFile: "./testdata/scorecard.json",
514+
githubRepository: "foo/bar",
515+
},
516+
want: &exec.Cmd{
517+
Path: "scorecard",
518+
Args: []string{
519+
"scorecard",
520+
"--policy",
521+
"./testdata/scorecard.yaml",
522+
"--results-format",
523+
"json",
524+
"--results-file",
525+
"./testdata/scorecard.json",
526+
"--repo",
527+
"foo/bar",
528+
},
529+
},
530+
},
531+
{
532+
name: "Success - scorecardFork set",
533+
args: args{
534+
githubEventName: "pull_request",
535+
scorecardPolicyFile: "./testdata/scorecard.yaml",
536+
scorecardResultsFormat: "json",
537+
scorecardBin: "scorecard",
538+
scorecardResultsFile: "./testdata/scorecard.json",
539+
githubRepository: "foo/bar",
540+
},
541+
want: &exec.Cmd{
542+
Path: "scorecard",
543+
Args: []string{
544+
"scorecard",
545+
"--policy",
546+
"./testdata/scorecard.yaml",
547+
"--results-format",
548+
"json",
549+
"--results-file",
550+
"./testdata/scorecard.json",
551+
"--repo",
552+
"foo/bar",
553+
},
554+
},
555+
},
556+
{
557+
name: "Success - scorecardFork set",
558+
args: args{
559+
githubEventName: "pull_request",
560+
scorecardPolicyFile: "./testdata/scorecard.yaml",
561+
scorecardResultsFormat: "json",
562+
scorecardBin: "scorecard",
563+
scorecardResultsFile: "./testdata/scorecard.json",
564+
githubRepository: "foo/bar",
565+
},
566+
want: &exec.Cmd{
567+
Path: "scorecard",
568+
Args: []string{
569+
"scorecard",
570+
"--policy",
571+
"./testdata/scorecard.yaml",
572+
"--results-format",
573+
"json",
574+
"--results-file",
575+
"./testdata/scorecard.json",
576+
"--repo",
577+
"foo/bar",
578+
},
579+
},
580+
},
581+
{
582+
name: "Success - scorecardFork set",
583+
args: args{
584+
githubEventName: "pull_request",
585+
scorecardResultsFormat: "json",
586+
scorecardBin: "scorecard",
587+
scorecardResultsFile: "./testdata/scorecard.json",
588+
githubRepository: "foo/bar",
589+
},
590+
want: &exec.Cmd{
591+
Path: "scorecard",
592+
Args: []string{
593+
"scorecard",
594+
"--results-format",
595+
"json",
596+
"--results-file",
597+
"./testdata/scorecard.json",
598+
"--repo",
599+
"foo/bar",
600+
},
601+
},
602+
},
603+
{
604+
name: "Success - scorecardFork set",
605+
args: args{
606+
githubEventName: "pull_request",
607+
scorecardResultsFormat: "json",
608+
scorecardBin: "scorecard",
609+
scorecardResultsFile: "./testdata/scorecard.json",
610+
githubRepository: "foo/bar",
611+
},
612+
want: &exec.Cmd{
613+
Path: "scorecard",
614+
Args: []string{
615+
"scorecard",
616+
"--results-format",
617+
"json",
618+
"--results-file",
619+
"./testdata/scorecard.json",
620+
"--repo",
621+
"foo/bar",
622+
},
623+
},
624+
},
625+
{
626+
name: "Success - scorecardFork set",
627+
args: args{
628+
scorecardResultsFormat: "json",
629+
scorecardBin: "scorecard",
630+
scorecardResultsFile: "./testdata/scorecard.json",
631+
githubRepository: "foo/bar",
632+
},
633+
want: &exec.Cmd{
634+
Path: "scorecard",
635+
Args: []string{
636+
"scorecard",
637+
"--results-format",
638+
"json",
639+
"--results-file",
640+
"./testdata/scorecard.json",
641+
"--repo",
642+
"foo/bar",
643+
},
644+
},
645+
},
646+
{
647+
name: "Success - Branch protection rule",
648+
args: args{
649+
githubEventName: "branch_protection_rule",
650+
scorecardResultsFormat: "json",
651+
scorecardBin: "scorecard",
652+
scorecardResultsFile: "./testdata/scorecard.json",
653+
githubRepository: "foo/bar",
654+
},
655+
want: &exec.Cmd{
656+
Path: "scorecard",
657+
Args: []string{
658+
"scorecard",
659+
"--results-format",
660+
"json",
661+
"--results-file",
662+
"./testdata/scorecard.json",
663+
"--repo",
664+
"foo/bar",
665+
},
666+
},
667+
},
668+
{
669+
name: "Success - Branch protection rule",
670+
args: args{
671+
scorecardPolicyFile: "./testdata/scorecard.yaml",
672+
githubEventName: "branch_protection_rule",
673+
scorecardResultsFormat: "json",
674+
scorecardBin: "scorecard",
675+
scorecardResultsFile: "./testdata/scorecard.json",
676+
githubRepository: "foo/bar",
677+
},
678+
want: &exec.Cmd{
679+
Path: "scorecard",
680+
Args: []string{
681+
"scorecard",
682+
"--policy",
683+
"./testdata/scorecard.yaml",
684+
"--results-format",
685+
"json",
686+
"--results-file",
687+
"./testdata/scorecard.json",
688+
"--repo",
689+
"foo/bar",
690+
},
691+
},
692+
},
693+
{
694+
name: "Want error - Branch protection rule",
695+
args: args{
696+
githubEventName: "",
697+
scorecardResultsFormat: "",
698+
scorecardBin: "",
699+
scorecardResultsFile: "",
700+
githubRepository: "",
701+
},
702+
wantErr: true,
703+
},
704+
}
705+
706+
for _, tt := range tests {
707+
tt := tt
708+
t.Run(tt.name, func(t *testing.T) {
709+
t.Parallel()
710+
got, err := runScorecardSettings(tt.args.githubEventName, tt.args.scorecardPolicyFile,
711+
tt.args.scorecardResultsFormat, tt.args.scorecardBin, tt.args.scorecardResultsFile, tt.args.githubRepository)
712+
if (err != nil) != tt.wantErr {
713+
t.Errorf("runScorecardSettings() error = %v, wantErr %v", err, tt.wantErr)
714+
return
715+
}
716+
if !tt.wantErr && cmp.Equal(got.Args, tt.want.Args) {
717+
t.Errorf("runScorecardSettings() = %v, want %v", got, tt.want)
718+
}
719+
})
720+
}
721+
}

0 commit comments

Comments
 (0)