Skip to content

Commit 0fe8858

Browse files
authored
Merge branch 'main' into add-openfga-cron
2 parents f614f62 + 1819192 commit 0fe8858

4 files changed

Lines changed: 254 additions & 5 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2023 OpenSSF Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gitlabrepo
16+
17+
import (
18+
"context"
19+
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("E2E TEST: gitlabrepo.ListFiles", func() {
25+
Context("Test List Files- GitLab", func() {
26+
It("returns list of files", func() {
27+
repo, err := MakeGitlabRepo("https://gitlab.com/ossf-test/scorecard-pipeline-example")
28+
Expect(err).Should(BeNil())
29+
30+
client, err := CreateGitlabClient(context.Background(), repo.Host())
31+
Expect(err).Should(BeNil())
32+
33+
err = client.InitRepo(repo, "HEAD", 0)
34+
Expect(err).Should(BeNil())
35+
36+
files, err := client.ListFiles(func(s string) (bool, error) {
37+
return true, nil
38+
})
39+
Expect(err).Should(BeNil())
40+
Expect(len(files)).ShouldNot(BeZero())
41+
42+
data, err := client.GetFileContent("README.md")
43+
Expect(err).Should(BeNil())
44+
Expect(len(data)).ShouldNot(BeZero())
45+
})
46+
})
47+
})

cmd/internal/scdiff/app/format/format.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,46 @@
1515
package format
1616

1717
import (
18-
"os"
18+
"io"
19+
"sort"
20+
"time"
1921

2022
"github.com/ossf/scorecard/v4/docs/checks"
2123
"github.com/ossf/scorecard/v4/log"
2224
"github.com/ossf/scorecard/v4/pkg"
2325
)
2426

27+
const logLevel = log.DefaultLevel
28+
29+
func normalize(r *pkg.ScorecardResult) {
30+
if r == nil {
31+
return
32+
}
33+
34+
// these fields will change run-to-run, and aren't indicative of behavior changes.
35+
r.Repo.CommitSHA = ""
36+
r.Scorecard = pkg.ScorecardInfo{}
37+
r.Date = time.Time{}
38+
39+
sort.Slice(r.Checks, func(i, j int) bool {
40+
return r.Checks[i].Name < r.Checks[j].Name
41+
})
42+
43+
for i := range r.Checks {
44+
check := &r.Checks[i]
45+
sort.Slice(check.Details, func(i, j int) bool {
46+
return pkg.DetailToString(&check.Details[i], logLevel) < pkg.DetailToString(&check.Details[j], logLevel)
47+
})
48+
}
49+
}
50+
2551
//nolint:wrapcheck
26-
func JSON(r *pkg.ScorecardResult) error {
52+
func JSON(r *pkg.ScorecardResult, w io.Writer) error {
2753
const details = true
2854
docs, err := checks.Read()
2955
if err != nil {
3056
return err
3157
}
32-
// TODO standardize the input, and output it to a file
33-
return r.AsJSON2(details, log.DefaultLevel, docs, os.Stdout)
58+
normalize(r)
59+
return r.AsJSON2(details, logLevel, docs, w)
3460
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright 2023 OpenSSF Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package format
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
"time"
21+
22+
"github.com/google/go-cmp/cmp"
23+
24+
"github.com/ossf/scorecard/v4/checker"
25+
"github.com/ossf/scorecard/v4/pkg"
26+
)
27+
28+
func TestJSON(t *testing.T) {
29+
t.Parallel()
30+
tests := []struct {
31+
name string
32+
a, b pkg.ScorecardResult
33+
}{
34+
{
35+
name: "repo commit SHA standardized",
36+
a: pkg.ScorecardResult{
37+
Repo: pkg.RepoInfo{
38+
Name: "github.com/foo/bar",
39+
CommitSHA: "commit a",
40+
},
41+
},
42+
b: pkg.ScorecardResult{
43+
Repo: pkg.RepoInfo{
44+
Name: "github.com/foo/bar",
45+
CommitSHA: "commit b",
46+
},
47+
},
48+
},
49+
{
50+
name: "dates standardized",
51+
a: pkg.ScorecardResult{
52+
Date: time.Now(),
53+
},
54+
b: pkg.ScorecardResult{
55+
Date: time.Now().AddDate(0, 0, -1),
56+
},
57+
},
58+
{
59+
name: "scorecard info standardized",
60+
a: pkg.ScorecardResult{
61+
Scorecard: pkg.ScorecardInfo{
62+
Version: "version a",
63+
CommitSHA: "scorecard commit x",
64+
},
65+
},
66+
b: pkg.ScorecardResult{
67+
Scorecard: pkg.ScorecardInfo{
68+
Version: "version b",
69+
CommitSHA: "scorecard commit y",
70+
},
71+
},
72+
},
73+
{
74+
name: "check order standardized",
75+
a: pkg.ScorecardResult{
76+
Checks: []checker.CheckResult{
77+
{
78+
Name: "Token-Permissions",
79+
Score: 10,
80+
},
81+
{
82+
Name: "License",
83+
Score: 10,
84+
},
85+
},
86+
},
87+
b: pkg.ScorecardResult{
88+
Checks: []checker.CheckResult{
89+
{
90+
Name: "License",
91+
Score: 10,
92+
},
93+
{
94+
Name: "Token-Permissions",
95+
Score: 10,
96+
},
97+
},
98+
},
99+
},
100+
{
101+
name: "detail order standardized",
102+
a: pkg.ScorecardResult{
103+
Checks: []checker.CheckResult{
104+
{
105+
Name: "Token-Permissions",
106+
Score: 10,
107+
Details: []checker.CheckDetail{
108+
{
109+
Msg: checker.LogMessage{
110+
Text: "foo",
111+
},
112+
Type: checker.DetailInfo,
113+
},
114+
{
115+
Msg: checker.LogMessage{
116+
Text: "bar",
117+
},
118+
Type: checker.DetailWarn,
119+
},
120+
},
121+
},
122+
},
123+
},
124+
b: pkg.ScorecardResult{
125+
Checks: []checker.CheckResult{
126+
{
127+
Name: "Token-Permissions",
128+
Score: 10,
129+
Details: []checker.CheckDetail{
130+
{
131+
Msg: checker.LogMessage{
132+
Text: "bar",
133+
},
134+
Type: checker.DetailWarn,
135+
},
136+
{
137+
Msg: checker.LogMessage{
138+
Text: "foo",
139+
},
140+
Type: checker.DetailInfo,
141+
},
142+
},
143+
},
144+
},
145+
},
146+
},
147+
}
148+
for _, tt := range tests {
149+
tt := tt
150+
t.Run(tt.name, func(t *testing.T) {
151+
t.Parallel()
152+
var bufA, bufB bytes.Buffer
153+
err := JSON(&tt.a, &bufA)
154+
if err != nil {
155+
t.Errorf("unexpected error: %v", err)
156+
}
157+
err = JSON(&tt.b, &bufB)
158+
if err != nil {
159+
t.Errorf("unexpected error: %v", err)
160+
}
161+
if bufA.String() != bufB.String() {
162+
t.Errorf("outputs not identical: %s", cmp.Diff(bufA.String(), bufB.String()))
163+
}
164+
})
165+
}
166+
}
167+
168+
func Test_normalize_nil_safe(t *testing.T) {
169+
var x, y *pkg.ScorecardResult
170+
normalize(x)
171+
normalize(y)
172+
if !cmp.Equal(x, y) {
173+
t.Errorf("normalized results differ: %v", cmp.Diff(x, y))
174+
}
175+
}

cmd/internal/scdiff/app/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ var (
5050
if err != nil {
5151
return fmt.Errorf("running scorecard on %s: %w", scanner.Text(), err)
5252
}
53-
err = format.JSON(&results)
53+
// TODO output it to a file, preferably in pretty printed json
54+
err = format.JSON(&results, os.Stdout)
5455
if err != nil {
5556
return fmt.Errorf("formatting results: %w", err)
5657
}

0 commit comments

Comments
 (0)