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

Add permission filter for list collaborators #2653

Merged
merged 5 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions github/repos_collaborators.go
Expand Up @@ -23,6 +23,13 @@ type ListCollaboratorsOptions struct {
// Default value is "all".
Affiliation string `url:"affiliation,omitempty"`

// Permission specifies how collaborators should be filtered by the permissions they have on the repository.
// Possible values are:
// "pull", "triage", "push", "maintain", "admin"
//
// If not specified, all collaborators will be returned.
Permission string `url:"permission,omitempty"`

ListOptions
}

Expand Down
40 changes: 40 additions & 0 deletions github/repos_collaborators_test.go
Expand Up @@ -94,6 +94,46 @@ func TestRepositoriesService_ListCollaborators_withAffiliation(t *testing.T) {
})
}

func TestRepositoriesService_ListCollaborators_withPermission(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"permission": "pull", "page": "2"})
fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
})

opt := &ListCollaboratorsOptions{
ListOptions: ListOptions{Page: 2},
Permission: "pull",
}
ctx := context.Background()
users, _, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
if err != nil {
t.Errorf("Repositories.ListCollaborators returned error: %v", err)
}

want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
if !cmp.Equal(users, want) {
t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
}

const methodName = "ListCollaborators"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.ListCollaborators(ctx, "\n", "\n", opt)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.ListCollaborators(ctx, "o", "r", opt)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_ListCollaborators_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
Expand Down