From 3722a44079cdedfd8415b1f45026f07d822966d5 Mon Sep 17 00:00:00 2001 From: raghavkaul <8695110+raghavkaul@users.noreply.github.com> Date: Wed, 17 May 2023 15:16:57 -0400 Subject: [PATCH] gitlab: license check (#2834) Signed-off-by: Raghav Kaul --- clients/gitlabrepo/client.go | 2 +- clients/gitlabrepo/licenses.go | 63 +++++++++++++++++++--------------- e2e/license_test.go | 57 ++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 28 deletions(-) diff --git a/clients/gitlabrepo/client.go b/clients/gitlabrepo/client.go index f70201f396f..2109bb593a3 100644 --- a/clients/gitlabrepo/client.go +++ b/clients/gitlabrepo/client.go @@ -130,7 +130,7 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD client.languages.init(client.repourl) // Init languagesHandler - client.licenses.init(client.repourl) + client.licenses.init(client.repourl, repo) // Init tarballHandler client.tarball.init(client.ctx, client.repourl, repo, commitSHA) diff --git a/clients/gitlabrepo/licenses.go b/clients/gitlabrepo/licenses.go index ec011c5048e..6ef7d827ecd 100644 --- a/clients/gitlabrepo/licenses.go +++ b/clients/gitlabrepo/licenses.go @@ -14,57 +14,66 @@ package gitlabrepo -// TODO: -// add "github.com/xanzy/go-gitlab" to this list. - import ( + "errors" "fmt" + "regexp" "sync" + "github.com/xanzy/go-gitlab" + "github.com/ossf/scorecard/v4/clients" ) type licensesHandler struct { - // TODO: glClient *gitlab.Client - once *sync.Once - errSetup error - repourl *repoURL - licenses []clients.License + glProject *gitlab.Project + once *sync.Once + errSetup error + repourl *repoURL + licenses []clients.License } -func (handler *licensesHandler) init(repourl *repoURL) { +func (handler *licensesHandler) init(repourl *repoURL, project *gitlab.Project) { handler.repourl = repourl + handler.glProject = project handler.errSetup = nil handler.once = new(sync.Once) } +var errLicenseURLParse = errors.New("couldn't parse gitlab repo license url") + func (handler *licensesHandler) setup() error { handler.once.Do(func() { - // TODO: find actual GitLab API, data type, and fields - // client := handler.glClient - // licenseMap, _, err := client.Projects.GetLicense(handler.repourl.projectID) licenseMap := []clients.License{} - // TODO: err := (*struct{})(nil) if len(licenseMap) == 0 { // TODO: handler.errSetup = fmt.Errorf("request for repo licenses failed with %w", err) handler.errSetup = fmt.Errorf("%w: ListLicenses not yet supported for gitlab", clients.ErrUnsupportedFeature) return } - // TODO: find actual GitLab API, data type, and fields - // TODO: for k, v := range *licenseMap { - // handler.licenses = append(handler.licenses, - // clients.License{ - // Key: "", - // Name: "", - // Path: "", - // Size: 0, - // SPDXId: "", - // Type: "", - // }, - // ) - // } - // + l := handler.glProject.License + + ptn, err := regexp.Compile(fmt.Sprintf("%s/~/blob/master/(.*)", handler.repourl.URI())) + if err != nil { + handler.errSetup = fmt.Errorf("couldn't parse License URL: %w", err) + return + } + + m := ptn.FindStringSubmatch(handler.glProject.LicenseURL) + if len(m) < 2 { + handler.errSetup = fmt.Errorf("%w: %s", errLicenseURLParse, handler.glProject.LicenseURL) + return + } + path := m[1] + + handler.licenses = append(handler.licenses, + clients.License{ + Key: l.Key, + Name: l.Name, + Path: path, + }, + ) + handler.errSetup = nil }) diff --git a/e2e/license_test.go b/e2e/license_test.go index 3081c866fbd..0c4c7501d7d 100644 --- a/e2e/license_test.go +++ b/e2e/license_test.go @@ -25,6 +25,7 @@ import ( "github.com/ossf/scorecard/v4/checks" "github.com/ossf/scorecard/v4/clients" "github.com/ossf/scorecard/v4/clients/githubrepo" + "github.com/ossf/scorecard/v4/clients/gitlabrepo" "github.com/ossf/scorecard/v4/clients/localdir" scut "github.com/ossf/scorecard/v4/utests" ) @@ -115,6 +116,62 @@ var _ = Describe("E2E TEST:"+checks.CheckLicense, func() { } result := checks.License(&req) + Expect(scut.ValidateTestReturn(nil, "license found", &expected, &result, + &dl)).Should(BeTrue()) + }) + It("Should return license check works - GitLab", func() { + skipIfTokenIsNot(gitlabPATTokenType, "GitLab only") + + dl := scut.TestDetailLogger{} + repo, err := gitlabrepo.MakeGitlabRepo("gitlab.com/N8BWert/scorecard-check-license-e2e") + Expect(err).Should(BeNil()) + repoClient, err := gitlabrepo.CreateGitlabClientWithToken(context.Background(), os.Getenv("GITLAB_AUTH_TOKEN"), repo) + Expect(err).Should(BeNil()) + err = repoClient.InitRepo(repo, clients.HeadSHA, 0) + Expect(err).Should(BeNil()) + req := checker.CheckRequest{ + Ctx: context.Background(), + RepoClient: repoClient, + Repo: repo, + Dlogger: &dl, + } + expected := scut.TestReturn{ + Error: nil, + Score: 9, + NumberOfWarn: 1, + NumberOfInfo: 1, + NumberOfDebug: 0, + } + result := checks.License(&req) + + Expect(scut.ValidateTestReturn(nil, "license found", &expected, &result, + &dl)).Should(BeTrue()) + }) + It("Should return license check works at commitSHA - GitLab", func() { + skipIfTokenIsNot(gitlabPATTokenType, "GitLab only") + + dl := scut.TestDetailLogger{} + repo, err := gitlabrepo.MakeGitlabRepo("gitlab.com/N8BWert/scorecard-check-license-e2e") + Expect(err).Should(BeNil()) + repoClient, err := gitlabrepo.CreateGitlabClientWithToken(context.Background(), os.Getenv("GITLAB_AUTH_TOKEN"), repo) + Expect(err).Should(BeNil()) + err = repoClient.InitRepo(repo, "c3a8778e73ea95f937c228a34ee57d5e006f7304", 0) + Expect(err).Should(BeNil()) + req := checker.CheckRequest{ + Ctx: context.Background(), + RepoClient: repoClient, + Repo: repo, + Dlogger: &dl, + } + expected := scut.TestReturn{ + Error: nil, + Score: 9, + NumberOfWarn: 1, + NumberOfInfo: 1, + NumberOfDebug: 0, + } + result := checks.License(&req) + Expect(scut.ValidateTestReturn(nil, "license found", &expected, &result, &dl)).Should(BeTrue()) })