Skip to content

Commit

Permalink
gitlab: license check (#2834)
Browse files Browse the repository at this point in the history
Signed-off-by: Raghav Kaul <[email protected]>
  • Loading branch information
raghavkaul authored May 17, 2023
1 parent 3b4236f commit 3722a44
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 28 deletions.
2 changes: 1 addition & 1 deletion clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 36 additions & 27 deletions clients/gitlabrepo/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
57 changes: 57 additions & 0 deletions e2e/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
})
Expand Down

0 comments on commit 3722a44

Please sign in to comment.