Skip to content
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

✨ Gitlab: Maintained check #2860

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 37 additions & 43 deletions clients/gitlabrepo/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (handler *issuesHandler) setup() error {

// There doesn't seem to be a good way to get user access_levels in gitlab so the following way may seem incredibly
// barberic, however I couldn't find a better way in the docs.
projectAccessTokens, resp, err := handler.glClient.ProjectAccessTokens.ListProjectAccessTokens(
handler.repourl.project, &gitlab.ListProjectAccessTokensOptions{})
projMemberships, resp, err := handler.glClient.ProjectMembers.ListAllProjectMembers(
raghavkaul marked this conversation as resolved.
Show resolved Hide resolved
handler.repourl.project, &gitlab.ListProjectMembersOptions{})
if err != nil && resp.StatusCode != 401 {
handler.errSetup = fmt.Errorf("unable to find access tokens associated with the project id: %w", err)
return
Expand All @@ -58,26 +58,25 @@ func (handler *issuesHandler) setup() error {
return
}

if len(issues) > 0 {
for _, issue := range issues {
authorAssociation := clients.RepoAssociationMember
if resp.StatusCode != 401 {
authorAssociation = findAuthorAssociationFromUserID(projectAccessTokens, issue.Author.ID)
var authorAssociation clients.RepoAssociation
for _, issue := range issues {
for _, m := range projMemberships {
if issue.Author.ID == m.ID {
authorAssociation = accessLevelToRepoAssociation(m.AccessLevel)
}
issueIDString := fmt.Sprint(issue.ID)
handler.issues = append(handler.issues,
clients.Issue{
URI: &issueIDString,
CreatedAt: issue.CreatedAt,
Author: &clients.User{
ID: int64(issue.Author.ID),
},
AuthorAssociation: &authorAssociation,
Comments: nil,
})
}
} else {
handler.issues = nil

issueIDString := fmt.Sprint(issue.ID)
handler.issues = append(handler.issues,
clients.Issue{
URI: &issueIDString,
CreatedAt: issue.CreatedAt,
Author: &clients.User{
ID: int64(issue.Author.ID),
},
AuthorAssociation: &authorAssociation,
Comments: nil,
})
}
})
return handler.errSetup
Expand All @@ -91,28 +90,23 @@ func (handler *issuesHandler) listIssues() ([]clients.Issue, error) {
return handler.issues, nil
}

func findAuthorAssociationFromUserID(accessTokens []*gitlab.ProjectAccessToken, targetID int) clients.RepoAssociation {
for _, accessToken := range accessTokens {
if accessToken.UserID == targetID {
switch accessToken.AccessLevel {
case 0:
return clients.RepoAssociationNone
case 5:
return clients.RepoAssociationFirstTimeContributor
case 10:
return clients.RepoAssociationCollaborator
case 20:
return clients.RepoAssociationCollaborator
case 30:
return clients.RepoAssociationMember
case 40:
return clients.RepoAssociationMaintainer
case 50:
return clients.RepoAssociationOwner
default:
return clients.RepoAssociationNone
}
}
func accessLevelToRepoAssociation(l gitlab.AccessLevelValue) clients.RepoAssociation {
switch l {
case 0:
return clients.RepoAssociationNone
case 5:
return clients.RepoAssociationFirstTimeContributor
case 10:
return clients.RepoAssociationCollaborator
case 20:
return clients.RepoAssociationCollaborator
case 30:
return clients.RepoAssociationMember
case 40:
return clients.RepoAssociationMaintainer
case 50:
return clients.RepoAssociationOwner
default:
return clients.RepoAssociationNone
}
return clients.RepoAssociationNone
}
31 changes: 31 additions & 0 deletions e2e/maintained_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package e2e

import (
"context"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -24,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"
scut "github.com/ossf/scorecard/v4/utests"
)

Expand Down Expand Up @@ -54,5 +56,34 @@ var _ = Describe("E2E TEST:"+checks.CheckMaintained, func() {
Expect(scut.ValidateTestReturn(nil, "active repo", &expected, &result, &dl)).Should(BeTrue())
Expect(repoClient.Close()).Should(BeNil())
})
It("Should return valid maintained status - GitLab", func() {
skipIfTokenIsNot(gitlabPATTokenType, "GitLab only")

dl := scut.TestDetailLogger{}
repo, err := gitlabrepo.MakeGitlabRepo("gitlab.com/gitlab-org/gitlab")
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: checker.MaxResultScore,
NumberOfWarn: 0,
NumberOfInfo: 0,
NumberOfDebug: 0,
}
result := checks.Maintained(&req)
// New version.
Expect(scut.ValidateTestReturn(nil, "active repo", &expected, &result, &dl)).Should(BeTrue())
Expect(repoClient.Close()).Should(BeNil())
})
})
})