Skip to content

Commit

Permalink
,
Browse files Browse the repository at this point in the history
  • Loading branch information
ashearin-lmco committed Nov 7, 2023
1 parent a8306f3 commit da41921
Show file tree
Hide file tree
Showing 3 changed files with 621 additions and 14 deletions.
48 changes: 34 additions & 14 deletions clients/gitlabrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
)

type commitsHandler struct {
glClient *gitlab.Client
once *sync.Once
errSetup error
repourl *repoURL
commitsRaw []*gitlab.Commit
glClient *gitlab.Client
once *sync.Once
errSetup error
repourl *repoURL
commitsRaw []*gitlab.Commit
commitDepth int
}

func (handler *commitsHandler) init(repourl *repoURL, commitDepth int) {
Expand All @@ -43,16 +44,35 @@ func (handler *commitsHandler) init(repourl *repoURL, commitDepth int) {

func (handler *commitsHandler) setup() error {
handler.once.Do(func() {
commits, _, err := handler.glClient.Commits.ListCommits(
handler.repourl.projectID,
&gitlab.ListCommitsOptions{
RefName: &handler.repourl.commitSHA,
},
)
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
var commits []*gitlab.Commit
i := 1

for {
c, _, err := handler.glClient.Commits.ListCommits(handler.repourl.projectID,
&gitlab.ListCommitsOptions{
ListOptions: gitlab.ListOptions{
Page: i,
PerPage: handler.commitDepth,
},
})
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
}

if len(c) == 0 {
break
}
i++

commits = append(commits, c...)

if len(commits) >= handler.commitDepth {
commits = commits[:handler.commitDepth]
break
}
}

handler.commitsRaw = commits
if handler.repourl.commitSHA != clients.HeadSHA {
//nolint:lll
Expand Down
73 changes: 73 additions & 0 deletions clients/gitlabrepo/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
package gitlabrepo

import (
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/ossf/scorecard/v4/clients"

Check failure on line 22 in clients/gitlabrepo/commits_test.go

View workflow job for this annotation

GitHub Actions / check-linter

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/ossf/scorecard) (gci)
"github.com/xanzy/go-gitlab"

Check failure on line 23 in clients/gitlabrepo/commits_test.go

View workflow job for this annotation

GitHub Actions / check-linter

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/ossf/scorecard) (gci)
)

func TestParsingEmail(t *testing.T) {
Expand Down Expand Up @@ -56,3 +61,71 @@ func TestParsingEmail(t *testing.T) {
})
}
}

func TestListRawCommits(t *testing.T) {
t.Parallel()
tests := []struct {
name string
commitsPath string
commitDepth int
want int
wantErr bool
}{
{
name: "commits to non-default depth",
commitsPath: "./testdata/valid-commits",
commitDepth: 17,
want: 17,
wantErr: false,
},
{
name: "commits to default depth",
commitsPath: "./testdata/valid-commits",
commitDepth: 30,
want: 30,
wantErr: false,
},
{
name: "failure fetching commits",
commitsPath: "./testdata/invalid-commits",
commitDepth: 30,
want: 0,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

httpClient := &http.Client{
Transport: suffixStubTripper{
responsePaths: map[string]string{
"commits": tt.commitsPath, // corresponds to projects/<id>/repository/commits
},
},
}
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
if err != nil {
t.Fatalf("gitlab.NewClient error: %v", err)
}
handler := &commitsHandler{
glClient: client,
}

repoURL := repoURL{
owner: "ossf-tests",
commitSHA: clients.HeadSHA,
}

handler.init(&repoURL, tt.commitDepth)
commits, err := handler.listRawCommits()
if (err != nil) != tt.wantErr {
t.Fatalf("listIssues error: %v, wantedErr: %t", err, tt.wantErr)
}
if !cmp.Equal(len(commits), tt.want) {
t.Errorf("listCommits() = %v, want %v", len(commits), cmp.Diff(len(commits), tt.want))
}
})
}
}
Loading

0 comments on commit da41921

Please sign in to comment.