-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 Fix failing GitLab e2e tests (#3393)
* Fix branch protection test type mismatch. Signed-off-by: Spencer Schrock <sschrock@google.com> * add unit test for gitlab listissues. Signed-off-by: Spencer Schrock <sschrock@google.com> * Delete gitlab issues e2e test. It's been replaced with a unit test. Signed-off-by: Spencer Schrock <sschrock@google.com> --------- Signed-off-by: Spencer Schrock <sschrock@google.com>
- Loading branch information
1 parent
accaffd
commit 1a32607
Showing
5 changed files
with
231 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2023 OpenSSF Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gitlabrepo | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/xanzy/go-gitlab" | ||
|
||
"github.com/ossf/scorecard/v4/clients" | ||
) | ||
|
||
// suffix may not be the best term, but maps the final part of a path to a response file. | ||
// this is helpful when multiple API calls need to be made. | ||
// e.g. a call to /foo/bar/some/endpoint would have "endpoint" as a suffix. | ||
type suffixStubTripper struct { | ||
// key is suffix, value is response file. | ||
responsePaths map[string]string | ||
} | ||
|
||
func (s suffixStubTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
pathParts := strings.Split(r.URL.Path, "/") | ||
suffix := pathParts[len(pathParts)-1] | ||
f, err := os.Open(s.responsePaths[suffix]) | ||
if err != nil { | ||
//nolint:wrapcheck | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
Status: "200 OK", | ||
StatusCode: http.StatusOK, | ||
Body: f, | ||
}, nil | ||
} | ||
|
||
func strptr(s string) *string { | ||
return &s | ||
} | ||
|
||
func associationptr(r clients.RepoAssociation) *clients.RepoAssociation { | ||
return &r | ||
} | ||
|
||
func timeptr(t time.Time) *time.Time { | ||
return &t | ||
} | ||
|
||
func Test_listIssues(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
issuePath string | ||
memberPath string | ||
want []clients.Issue | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "issue with maintainer as author", | ||
issuePath: "./testdata/valid-issues", | ||
memberPath: "./testdata/valid-repo-members", | ||
want: []clients.Issue{ | ||
{ | ||
URI: strptr("131356518"), | ||
CreatedAt: timeptr(time.Date(2023, time.July, 26, 14, 22, 52, 0, time.UTC)), | ||
Author: &clients.User{ | ||
ID: 1355794, | ||
}, | ||
AuthorAssociation: associationptr(clients.RepoAssociationMaintainer), | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "failure fetching issues", | ||
issuePath: "./testdata/invalid-issues", | ||
want: nil, | ||
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{ | ||
"issues": tt.issuePath, // corresponds to projects/<id>/issues | ||
"all": tt.memberPath, // corresponds to projects/<id>/members/all | ||
}, | ||
}, | ||
} | ||
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient)) | ||
if err != nil { | ||
t.Fatalf("gitlab.NewClient error: %v", err) | ||
} | ||
handler := &issuesHandler{ | ||
glClient: client, | ||
} | ||
|
||
repoURL := repoURL{ | ||
owner: "ossf-tests", | ||
commitSHA: clients.HeadSHA, | ||
} | ||
handler.init(&repoURL) | ||
got, err := handler.listIssues() | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("listIssues error: %v, wantedErr: %t", err, tt.wantErr) | ||
} | ||
if !cmp.Equal(got, tt.want) { | ||
t.Errorf("listIssues() = %v, want %v", got, cmp.Diff(got, tt.want)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
[ | ||
{ | ||
"id": 131356518, | ||
"iid": 1, | ||
"project_id": 48033865, | ||
"title": "test issue", | ||
"description": "", | ||
"state": "opened", | ||
"created_at": "2023-07-26T14:22:52.000Z", | ||
"updated_at": "2023-07-26T14:22:52.000Z", | ||
"closed_at": null, | ||
"closed_by": null, | ||
"labels": [], | ||
"milestone": null, | ||
"assignees": [], | ||
"author": { | ||
"id": 1355794, | ||
"username": "maintainer1", | ||
"name": "Bob", | ||
"state": "active", | ||
"avatar_url": "https://secure.gravatar.com/avatar/5eefb3063e3a44f456e4c1ca5740105b?s=80&d=identicon", | ||
"web_url": "https://gitlab.com/maintainer1" | ||
}, | ||
"type": "ISSUE", | ||
"assignee": null, | ||
"user_notes_count": 0, | ||
"merge_requests_count": 0, | ||
"upvotes": 0, | ||
"downvotes": 0, | ||
"due_date": null, | ||
"confidential": false, | ||
"discussion_locked": null, | ||
"issue_type": "issue", | ||
"web_url": "https://gitlab.com/ossf-test/e2e-issues/-/issues/1", | ||
"time_stats": { | ||
"time_estimate": 0, | ||
"total_time_spent": 0, | ||
"human_time_estimate": null, | ||
"human_total_time_spent": null | ||
}, | ||
"task_completion_status": { | ||
"count": 0, | ||
"completed_count": 0 | ||
}, | ||
"blocking_issues_count": 0, | ||
"has_tasks": true, | ||
"task_status": "", | ||
"_links": { | ||
"self": "https://gitlab.com/api/v4/projects/48033865/issues/1", | ||
"notes": "https://gitlab.com/api/v4/projects/48033865/issues/1/notes", | ||
"award_emoji": "https://gitlab.com/api/v4/projects/48033865/issues/1/award_emoji", | ||
"project": "https://gitlab.com/api/v4/projects/48033865", | ||
"closed_as_duplicate_of": null | ||
}, | ||
"references": { | ||
"short": "#1", | ||
"relative": "#1", | ||
"full": "ossf-test/e2e-issues#1" | ||
}, | ||
"severity": "UNKNOWN", | ||
"moved_to_id": null, | ||
"service_desk_reply_to": null | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"access_level": 50, | ||
"created_at": "2023-02-21T18:24:39.599Z", | ||
"expires_at": null, | ||
"id": 3332021, | ||
"username": "owner1", | ||
"name": "Alice", | ||
"state": "active", | ||
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/3332021/avatar.png", | ||
"web_url": "https://gitlab.com/owner1", | ||
"membership_state": "active" | ||
}, | ||
{ | ||
"access_level": 40, | ||
"created_at": "2023-02-28T20:36:17.805Z", | ||
"created_by": { | ||
"id": 3332021, | ||
"username": "owner1", | ||
"name": "Alice", | ||
"state": "active", | ||
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/3332021/avatar.png", | ||
"web_url": "https://gitlab.com/owner1" | ||
}, | ||
"expires_at": null, | ||
"id": 1355794, | ||
"username": "maintainer1", | ||
"name": "Bob", | ||
"state": "active", | ||
"avatar_url": "https://secure.gravatar.com/avatar/5eefb3063e3a44f456e4c1ca5740105b?s=80&d=identicon", | ||
"web_url": "https://gitlab.com/maintainer1", | ||
"membership_state": "active" | ||
} | ||
] |