Skip to content

Commit

Permalink
✨ Add commit depth support for GitLab (#3672)
Browse files Browse the repository at this point in the history
* feat: Integrated paging to allow for querying based on the --commit-depth value provided

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>

* fix: rework git commits changes for readability

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>

* fix: add additional commit depth test

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>

---------

Signed-off-by: Allen Shearin <allen.p.shearin@gmail.com>
  • Loading branch information
ashearin committed Nov 15, 2023
1 parent a4ee314 commit 14f864b
Show file tree
Hide file tree
Showing 4 changed files with 636 additions and 16 deletions.
2 changes: 1 addition & 1 deletion clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
client.contributors.init(client.repourl)

// Init commitsHandler
client.commits.init(client.repourl)
client.commits.init(client.repourl, client.commitDepth)

// Init branchesHandler
client.branches.init(client.repourl)
Expand Down
55 changes: 40 additions & 15 deletions clients/gitlabrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,56 @@ 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) {
func (handler *commitsHandler) init(repourl *repoURL, commitDepth int) {
handler.repourl = repourl
handler.errSetup = nil
handler.once = new(sync.Once)
handler.commitDepth = commitDepth
}

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
opt := gitlab.ListOptions{
Page: 1,
PerPage: handler.commitDepth,
}

for {
c, resp, err := handler.glClient.Commits.ListCommits(handler.repourl.projectID,
&gitlab.ListCommitsOptions{
RefName: &handler.repourl.commitSHA,
ListOptions: opt,
})
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
}

commits = append(commits, c...)

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

// Exit the loop when we've seen all pages.
if resp.NextPage == 0 {
break
}

// Update the page number to get the next page.
opt.Page = resp.NextPage
}

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

import (
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/xanzy/go-gitlab"

"github.com/ossf/scorecard/v4/clients"
)

func TestParsingEmail(t *testing.T) {
Expand Down Expand Up @@ -56,3 +62,78 @@ 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: "request more commits than exist in the repo",
commitsPath: "./testdata/valid-commits",
commitDepth: 60,
want: 32,
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 14f864b

Please sign in to comment.