Skip to content

Commit 8317afc

Browse files
authored
Merge branch 'main' into feat/rawcii
2 parents 19edc33 + 4d1c531 commit 8317afc

File tree

17 files changed

+466
-218
lines changed

17 files changed

+466
-218
lines changed

.github/workflows/integration.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,25 @@ jobs:
7474
run: |
7575
go mod download
7676
77-
- name: Run E2E #using retry because the GitHub token is being throttled.
77+
- name: Run GITHUB_TOKEN E2E #using retry because the GitHub token is being throttled.
78+
uses: nick-invision/retry@7f8f3d9f0f62fe5925341be21c2e8314fd4f7c7c
79+
env:
80+
GITHUB_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81+
with:
82+
max_attempts: 3
83+
retry_on: error
84+
timeout_minutes: 30
85+
command: make e2e-gh-token
86+
87+
- name: Run PAT E2E #using retry because the GitHub token is being throttled.
7888
uses: nick-invision/retry@7f8f3d9f0f62fe5925341be21c2e8314fd4f7c7c
7989
env:
8090
GITHUB_AUTH_TOKEN: ${{ secrets.GH_AUTH_TOKEN }}
8191
with:
8292
max_attempts: 3
8393
retry_on: error
8494
timeout_minutes: 30
85-
command: make e2e
95+
command: make e2e-pat
8696

8797
- name: codecov
8898
uses: codecov/codecov-action@e3c560433a6cc60aec8812599b7844a7b4fa0d71 # 2.1.0

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ cron-github-server-docker:
277277

278278
##@ Tests
279279
################################# make test ###################################
280-
test-targets = unit-test e2e ci-e2e
280+
test-targets = unit-test e2e-pat e2e-gh-token ci-e2e
281281
.PHONY: test $(test-targets)
282282
test: $(test-targets)
283283

@@ -293,8 +293,13 @@ ifndef GITHUB_AUTH_TOKEN
293293
$(error GITHUB_AUTH_TOKEN is undefined)
294294
endif
295295

296-
e2e: ## Runs e2e tests. Requires GITHUB_AUTH_TOKEN env var to be set to GitHub personal access token
297-
e2e: build-scorecard check-env | $(GINKGO)
296+
e2e-pat: ## Runs e2e tests. Requires GITHUB_AUTH_TOKEN env var to be set to GitHub personal access token
297+
e2e-pat: build-scorecard check-env | $(GINKGO)
298298
# Run e2e tests. GITHUB_AUTH_TOKEN with personal access token must be exported to run this
299-
$(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out ./...
299+
TOKEN_TYPE="PAT" $(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out ./...
300+
301+
e2e-gh-token: ## Runs e2e tests. Requires GITHUB_AUTH_TOKEN env var to be set to default GITHUB_TOKEN
302+
e2e-gh-token: build-scorecard check-env | $(GINKGO)
303+
# Run e2e tests. GITHUB_AUTH_TOKEN set to secrets.GITHUB_TOKEN must be used to run this.
304+
TOKEN_TYPE="GITHUB_TOKEN" $(GINKGO) --race -p -v -cover -coverprofile=e2e-coverage.out ./...
300305
###############################################################################

checker/raw_result.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type RawResults struct {
3030
WebhookResults WebhooksData
3131
MaintainedResults MaintainedData
3232
SignedReleasesResults SignedReleasesData
33+
LicenseResults LicenseData
3334
}
3435

3536
// MaintainedData contains the raw results
@@ -40,6 +41,12 @@ type MaintainedData struct {
4041
ArchivedStatus ArchivedStatus
4142
}
4243

44+
// LicenseData contains the raw results
45+
// for the License check.
46+
type LicenseData struct {
47+
Files []File
48+
}
49+
4350
// CodeReviewData contains the raw results
4451
// for the Code-Review check.
4552
type CodeReviewData struct {

checks/evaluation/license.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 Security 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 evaluation
16+
17+
import (
18+
"github.com/ossf/scorecard/v4/checker"
19+
sce "github.com/ossf/scorecard/v4/errors"
20+
)
21+
22+
// License applies the score policy for the License check.
23+
func License(name string, dl checker.DetailLogger,
24+
r *checker.LicenseData,
25+
) checker.CheckResult {
26+
if r == nil {
27+
e := sce.WithMessage(sce.ErrScorecardInternal, "empty raw data")
28+
return checker.CreateRuntimeErrorResult(name, e)
29+
}
30+
31+
// Apply the policy evaluation.
32+
if r.Files == nil || len(r.Files) == 0 {
33+
return checker.CreateMinScoreResult(name, "license file not detected")
34+
}
35+
36+
for _, f := range r.Files {
37+
dl.Info(&checker.LogMessage{
38+
Path: f.Path,
39+
Type: checker.FileTypeSource,
40+
Offset: 1,
41+
})
42+
}
43+
44+
return checker.CreateMaxScoreResult(name, "license file detected")
45+
}

checks/license.go

Lines changed: 13 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,12 @@
1515
package checks
1616

1717
import (
18-
"fmt"
19-
"regexp"
20-
"strings"
21-
2218
"github.com/ossf/scorecard/v4/checker"
23-
"github.com/ossf/scorecard/v4/checks/fileparser"
19+
"github.com/ossf/scorecard/v4/checks/evaluation"
20+
"github.com/ossf/scorecard/v4/checks/raw"
21+
sce "github.com/ossf/scorecard/v4/errors"
2422
)
2523

26-
type check func(str string, extCheck []string) bool
27-
28-
type checks struct {
29-
rstr string // regex string
30-
f check
31-
p []string
32-
}
33-
3424
// CheckLicense is the registered name for License.
3525
const CheckLicense = "License"
3626

@@ -40,123 +30,24 @@ func init() {
4030
checker.FileBased,
4131
checker.CommitBased,
4232
}
43-
if err := registerCheck(CheckLicense, LicenseCheck, supportedRequestTypes); err != nil {
33+
if err := registerCheck(CheckLicense, License, supportedRequestTypes); err != nil {
4434
// this should never happen
4535
panic(err)
4636
}
4737
}
4838

49-
const (
50-
copying = "copy(ing|right)"
51-
license = "(un)?licen[sc]e"
52-
preferredExt = "*\\.(md|markdown|html)$"
53-
anyExt = ".[^./]"
54-
ofl = "ofl"
55-
patents = "patents"
56-
)
57-
58-
// Regex converted from
59-
// https://github.com/licensee/licensee/blob/master/lib/licensee/project_files/license_file.rb
60-
var (
61-
extensions = []string{"xml", "go", "gemspec"}
62-
regexChecks = []checks{
63-
{rstr: copying, f: nil},
64-
{rstr: license, f: nil},
65-
{rstr: license + preferredExt, f: nil},
66-
{rstr: copying + preferredExt, f: nil},
67-
{rstr: copying + anyExt, f: nil},
68-
{rstr: ofl, f: nil},
69-
{rstr: ofl + preferredExt, f: nil},
70-
{rstr: patents, f: nil},
71-
{rstr: license, f: extensionMatch, p: []string{"spdx", "header"}},
72-
{rstr: license + "[-_][^.]*", f: extensionMatch, p: extensions},
73-
{rstr: copying + "[-_][^.]*", f: extensionMatch, p: extensions},
74-
{rstr: "\\w+[-_]" + license + "[^.]*", f: extensionMatch, p: extensions},
75-
{rstr: "\\w+[-_]" + copying + "[^.]*", f: extensionMatch, p: extensions},
76-
{rstr: ofl, f: extensionMatch, p: extensions},
77-
}
78-
)
79-
80-
// ExtensionMatch to check for matching extension.
81-
func extensionMatch(f string, exts []string) bool {
82-
s := strings.Split(f, ".")
83-
84-
if len(s) <= 1 {
85-
return false
86-
}
87-
88-
fext := s[len(s)-1]
89-
90-
found := false
91-
for _, ext := range exts {
92-
if ext == fext {
93-
found = true
94-
break
95-
}
96-
}
97-
98-
return found
99-
}
100-
101-
// TestLicenseCheck used for testing purposes.
102-
func testLicenseCheck(name string) bool {
103-
return checkLicense(name)
104-
}
105-
106-
// LicenseCheck runs LicenseCheck check.
107-
func LicenseCheck(c *checker.CheckRequest) checker.CheckResult {
108-
var s string
109-
110-
err := fileparser.OnAllFilesDo(c.RepoClient, isLicenseFile, &s)
39+
// License runs License check.
40+
func License(c *checker.CheckRequest) checker.CheckResult {
41+
rawData, err := raw.License(c)
11142
if err != nil {
112-
return checker.CreateRuntimeErrorResult(CheckLicense, err)
113-
}
114-
if s != "" {
115-
c.Dlogger.Info(&checker.LogMessage{
116-
Path: s,
117-
Type: checker.FileTypeSource,
118-
Offset: 1,
119-
})
120-
return checker.CreateMaxScoreResult(CheckLicense, "license file detected")
43+
e := sce.WithMessage(sce.ErrScorecardInternal, err.Error())
44+
return checker.CreateRuntimeErrorResult(CheckLicense, e)
12145
}
122-
return checker.CreateMinScoreResult(CheckLicense, "license file not detected")
123-
}
12446

125-
var isLicenseFile fileparser.DoWhileTrueOnFilename = func(name string, args ...interface{}) (bool, error) {
126-
if len(args) != 1 {
127-
return false, fmt.Errorf("isLicenseFile requires exactly one argument: %w", errInvalidArgLength)
47+
// Set the raw results.
48+
if c.RawResults != nil {
49+
c.RawResults.LicenseResults = rawData
12850
}
129-
s, ok := args[0].(*string)
130-
if !ok {
131-
return false, fmt.Errorf("isLicenseFile requires argument of type: *string: %w", errInvalidArgType)
132-
}
133-
if checkLicense(name) {
134-
if s != nil {
135-
*s = name
136-
}
137-
return false, nil
138-
}
139-
return true, nil
140-
}
141-
142-
// CheckLicense to check whether the name parameter fulfill license file criteria.
143-
func checkLicense(name string) bool {
144-
for _, check := range regexChecks {
145-
rg := regexp.MustCompile(check.rstr)
14651

147-
nameLower := strings.ToLower(name)
148-
t := rg.MatchString(nameLower)
149-
if t {
150-
extFound := true
151-
152-
// check extension calling f function.
153-
// f function will always be func extensionMatch(..)
154-
if check.f != nil {
155-
extFound = check.f(nameLower, check.p)
156-
}
157-
158-
return extFound
159-
}
160-
}
161-
return false
52+
return evaluation.License(CheckLicense, c.Dlogger, &rawData)
16253
}

checks/license_test.go

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -28,86 +28,6 @@ import (
2828
scut "github.com/ossf/scorecard/v4/utests"
2929
)
3030

31-
func TestLicenseFileCheck(t *testing.T) {
32-
t.Parallel()
33-
34-
tests := []struct {
35-
name string
36-
filename string
37-
}{
38-
{
39-
name: "LICENSE.md",
40-
filename: "LICENSE.md",
41-
},
42-
{
43-
name: "LICENSE",
44-
filename: "LICENSE",
45-
},
46-
{
47-
name: "COPYING",
48-
filename: "COPYING",
49-
},
50-
{
51-
name: "COPYING.md",
52-
filename: "COPYING.md",
53-
},
54-
{
55-
name: "LICENSE.textile",
56-
filename: "LICENSE.textile",
57-
},
58-
{
59-
name: "COPYING.textile",
60-
filename: "COPYING.textile",
61-
},
62-
{
63-
name: "LICENSE-MIT",
64-
filename: "LICENSE-MIT",
65-
},
66-
{
67-
name: "COPYING-MIT",
68-
filename: "COPYING-MIT",
69-
},
70-
{
71-
name: "MIT-LICENSE-MIT",
72-
filename: "MIT-LICENSE-MIT",
73-
},
74-
{
75-
name: "MIT-COPYING",
76-
filename: "MIT-COPYING",
77-
},
78-
{
79-
name: "OFL.md",
80-
filename: "OFL.md",
81-
},
82-
{
83-
name: "OFL.textile",
84-
filename: "OFL.textile",
85-
},
86-
{
87-
name: "OFL",
88-
filename: "OFL",
89-
},
90-
{
91-
name: "PATENTS",
92-
filename: "PATENTS",
93-
},
94-
{
95-
name: "PATENTS.txt",
96-
filename: "PATENTS.txt",
97-
},
98-
}
99-
for _, tt := range tests {
100-
tt := tt // Re-initializing variable so it is not changed while executing the closure below
101-
t.Run(tt.name, func(t *testing.T) {
102-
t.Parallel()
103-
s := testLicenseCheck(tt.filename)
104-
if !s {
105-
t.Fail()
106-
}
107-
})
108-
}
109-
}
110-
11131
func TestLicenseFileSubdirectory(t *testing.T) {
11232
t.Parallel()
11333

@@ -167,7 +87,7 @@ func TestLicenseFileSubdirectory(t *testing.T) {
16787
Dlogger: &dl,
16888
}
16989

170-
res := LicenseCheck(&req)
90+
res := License(&req)
17191

17292
if !scut.ValidateTestReturn(t, tt.name, &tt.expected, &res, &dl) {
17393
t.Fail()

0 commit comments

Comments
 (0)