-
Notifications
You must be signed in to change notification settings - Fork 609
✨ Enable gitlab Packaging Reporting #2941
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b1b7138
feat: Added yaml file that contains the full, flattened gitlab ci/cd …
jimrobison b9892a0
refactor: Updated to meet linting failures
jimrobison 576dc02
refactor: Updated filename for flattened gitlab
jimrobison d3d7c49
refactor: Updated to include the generated, flattened ci yaml in the …
jimrobison ff3af00
refactor: Updated the apiFunction to be part of the handler
jimrobison 3a1c376
refactor: Moved packaging collection to be a repoClient specific sub-…
jimrobison 2824a8c
feat: Added path for gitlab projects, including a basic search for li…
jimrobison 642d117
test: Added tests for gitlab packaging finders
jimrobison 5d7e292
test: Added more tests for parsing through the client and grouping pa…
jimrobison 91d8f6c
refactor: Utilizing existing mock objects to prevent race condition e…
jimrobison 0881a68
refactor: Addressed linting errors
jimrobison 69b0016
test: Updated expectation for log message
jimrobison e588004
refactor: Reverted back to the original error
jimrobison 7bc6993
Merge branch 'main' into gitlab-packaging
raghavkaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright 2021 OpenSSF Scorecard 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 fileparser | ||
|
|
||
| // IsGitlabWorkflowFile determines if a file is a workflow | ||
| // as a callback to use for repo client's ListFiles() API. | ||
| func IsGitlabWorkflowFile(pathfn string) (bool, error) { | ||
| return pathfn == "gitlabscorecard_flattened_ci.yaml", nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2020 OpenSSF Scorecard 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 gitlab | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/ossf/scorecard/v4/checker" | ||
| "github.com/ossf/scorecard/v4/checks/fileparser" | ||
| "github.com/ossf/scorecard/v4/finding" | ||
| ) | ||
|
|
||
| // Packaging checks for packages. | ||
| func Packaging(c *checker.CheckRequest) (checker.PackagingData, error) { | ||
| var data checker.PackagingData | ||
| matchedFiles, err := c.RepoClient.ListFiles(fileparser.IsGitlabWorkflowFile) | ||
| if err != nil { | ||
| return data, fmt.Errorf("RepoClient.ListFiles: %w", err) | ||
| } | ||
|
|
||
| for _, fp := range matchedFiles { | ||
| fc, err := c.RepoClient.GetFileContent(fp) | ||
| if err != nil { | ||
| return data, fmt.Errorf("RepoClient.GetFileContent: %w", err) | ||
| } | ||
|
|
||
| file, found := isGitlabPackagingWorkflow(fc, fp) | ||
|
|
||
| if found { | ||
| data.Packages = append(data.Packages, checker.Package{ | ||
| Name: new(string), | ||
| Job: &checker.WorkflowJob{}, | ||
| File: &file, | ||
| Msg: nil, | ||
| Runs: []checker.Run{{URL: c.Repo.URI()}}, | ||
| }) | ||
| return data, nil | ||
| } | ||
| } | ||
|
|
||
| return data, nil | ||
| } | ||
|
|
||
| func StringPointer(s string) *string { | ||
| return &s | ||
| } | ||
|
|
||
| func isGitlabPackagingWorkflow(fc []byte, fp string) (checker.File, bool) { | ||
| lineNumber := checker.OffsetDefault | ||
|
|
||
| packagingStrings := []string{ | ||
| "docker push", | ||
| "nuget push", | ||
| "poetry publish", | ||
| "twine upload", | ||
| } | ||
|
|
||
| ParseLines: | ||
| for idx, val := range strings.Split(string(fc[:]), "\n") { | ||
| for _, element := range packagingStrings { | ||
| if strings.Contains(val, element) { | ||
| lineNumber = uint(idx + 1) | ||
| break ParseLines | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return checker.File{ | ||
| Path: fp, | ||
| Offset: lineNumber, | ||
| Type: finding.FileTypeSource, | ||
| }, lineNumber != checker.OffsetDefault | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Copyright 2020 OpenSSF Scorecard 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 gitlab | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/golang/mock/gomock" | ||
|
|
||
| "github.com/ossf/scorecard/v4/checker" | ||
| mockrepo "github.com/ossf/scorecard/v4/clients/mockclients" | ||
| ) | ||
|
|
||
| func TestGitlabPackagingYamlCheck(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| //nolint | ||
| tests := []struct { | ||
| name string | ||
| lineNumber uint | ||
| filename string | ||
| exists bool | ||
| }{ | ||
| { | ||
| name: "No Publishing Detected", | ||
| filename: "./testdata/no-publishing.yaml", | ||
| lineNumber: 1, | ||
| exists: false, | ||
| }, | ||
| { | ||
| name: "Docker", | ||
| filename: "./testdata/docker.yaml", | ||
| lineNumber: 31, | ||
| exists: true, | ||
| }, | ||
| { | ||
| name: "Nuget", | ||
| filename: "./testdata/nuget.yaml", | ||
| lineNumber: 21, | ||
| exists: true, | ||
| }, | ||
| { | ||
| name: "Poetry", | ||
| filename: "./testdata/poetry.yaml", | ||
| lineNumber: 30, | ||
| exists: true, | ||
| }, | ||
| { | ||
| name: "Twine", | ||
| filename: "./testdata/twine.yaml", | ||
| lineNumber: 26, | ||
| exists: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| var content []byte | ||
| var err error | ||
|
|
||
| content, err = os.ReadFile(tt.filename) | ||
| if err != nil { | ||
| t.Errorf("cannot read file: %v", err) | ||
| } | ||
|
|
||
| file, found := isGitlabPackagingWorkflow(content, tt.filename) | ||
|
|
||
| if tt.exists && !found { | ||
| t.Errorf("Packaging %q should exist", tt.name) | ||
| } else if !tt.exists && found { | ||
| t.Errorf("No packaging information should have been found in %q", tt.name) | ||
| } | ||
|
|
||
| if file.Offset != tt.lineNumber { | ||
| t.Errorf("Expected line number: %d != %d", tt.lineNumber, file.Offset) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGitlabPackagingPackager(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| //nolint | ||
| tests := []struct { | ||
| name string | ||
| lineNumber uint | ||
| filename string | ||
| exists bool | ||
| }{ | ||
| { | ||
| name: "No Publishing Detected", | ||
| filename: "./testdata/no-publishing.yaml", | ||
| lineNumber: 1, | ||
| exists: false, | ||
| }, | ||
| { | ||
| name: "Docker", | ||
| filename: "./testdata/docker.yaml", | ||
| lineNumber: 31, | ||
| exists: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| moqRepoClient := mockrepo.NewMockRepoClient(ctrl) | ||
| moqRepo := mockrepo.NewMockRepo(ctrl) | ||
|
|
||
| moqRepoClient.EXPECT().ListFiles(gomock.Any()). | ||
| Return([]string{tt.filename}, nil).AnyTimes() | ||
|
|
||
| moqRepoClient.EXPECT().GetFileContent(tt.filename). | ||
| DoAndReturn(func(b string) ([]byte, error) { | ||
| //nolint: errcheck | ||
| content, _ := os.ReadFile(b) | ||
| return content, nil | ||
| }).AnyTimes() | ||
|
|
||
| if tt.exists { | ||
| moqRepo.EXPECT().URI().Return("myurl.com/owner/project") | ||
| } | ||
|
|
||
| req := checker.CheckRequest{ | ||
| RepoClient: moqRepoClient, | ||
| Repo: moqRepo, | ||
| } | ||
|
|
||
| //nolint: errcheck | ||
| packagingData, _ := Packaging(&req) | ||
|
|
||
| if !tt.exists { | ||
| if len(packagingData.Packages) != 0 { | ||
| t.Errorf("Repo should not contain any packages") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if len(packagingData.Packages) == 0 { | ||
| t.Fatalf("Repo should contain related packages") | ||
| } | ||
|
|
||
| pkg := packagingData.Packages[0].File | ||
|
|
||
| if pkg.Offset != tt.lineNumber { | ||
| t.Errorf("Expected line number: %d != %d", tt.lineNumber, pkg.Offset) | ||
| } | ||
| if pkg.Path != tt.filename { | ||
| t.Errorf("Expected filename: %v != %v", tt.filename, pkg.Path) | ||
| } | ||
|
|
||
| runs := packagingData.Packages[0].Runs | ||
|
|
||
| if len(runs) != 1 { | ||
| t.Errorf("Expected only a single run count, but received %d", len(runs)) | ||
| } | ||
|
|
||
| if runs[0].URL != "myurl.com/owner/project" { | ||
| t.Errorf("URL did not match expected value %q", runs[0].URL) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.