Skip to content

Commit

Permalink
🐛 handle gitlab repos with no commits (#3731)
Browse files Browse the repository at this point in the history
* fix: handle gitlab repos with no commits

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

* fix: gitlab listcommits tests, remove else in commit array length check

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

* rename test file, remove unneeded 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 Jan 4, 2024
1 parent 658a77b commit 141ac4d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clients/gitlabrepo/checkruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_CheckRuns(t *testing.T) {
},
{
name: "valid checkruns with zero results",
responsePath: "./testdata/valid-checkruns-1",
responsePath: "./testdata/empty-response",
ref: "eb94b618fb5865b26e80fdd8ae531b7a63ad851a",
wantErr: false,
},
Expand Down
4 changes: 4 additions & 0 deletions clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func (client *Client) ListCommits() ([]clients.Commit, error) {
return []clients.Commit{}, err
}

if len(commitsRaw) < 1 {
return []clients.Commit{}, nil
}

before := commitsRaw[0].CommittedDate
// Get merge request details from GraphQL
// GitLab REST API doesn't provide a way to link Merge Requests and Commits that
Expand Down
73 changes: 73 additions & 0 deletions clients/gitlabrepo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package gitlabrepo

import (
"context"
"errors"
"net/http"
"testing"

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

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

func TestCheckRepoInaccessible(t *testing.T) {
Expand Down Expand Up @@ -69,3 +74,71 @@ func TestCheckRepoInaccessible(t *testing.T) {
})
}
}

func TestListCommits(t *testing.T) {
t.Parallel()

tests := []struct {
name string
responsePath string
commits []clients.Commit
wantErr bool
}{
{
name: "Error in ListRawCommits",
responsePath: "./testdata/invalid-commits",
commits: []clients.Commit{},
wantErr: true,
},
{
name: "No commits in repo",
responsePath: "./testdata/empty-response",
commits: []clients.Commit{},
wantErr: false,
},
}
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.responsePath, // corresponds to projects/<id>/repository/commits
},
},
}
glclient, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
if err != nil {
t.Fatalf("gitlab.NewClient error: %v", err)
}
commitshandler := &commitsHandler{
glClient: glclient,
}

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

commitshandler.init(&repoURL, 30)

gqlhandler := graphqlHandler{
client: httpClient,
}
gqlhandler.init(context.Background(), &repoURL)

client := &Client{glClient: glclient, commits: commitshandler, graphql: &gqlhandler}

got, Err := client.ListCommits()

if (Err != nil) != tt.wantErr {
t.Fatalf("ListCommits, wanted Error: %v, got Error: %v", tt.wantErr, Err)
}
if !cmp.Equal(got, tt.commits) {
t.Errorf("ListCommits() got %v, want %v", got, tt.commits)
}
})
}
}
File renamed without changes.

0 comments on commit 141ac4d

Please sign in to comment.