Skip to content

Commit 4a7dd75

Browse files
committed
Adds support for GET /repos/:owner/:repo/collaborators/:username/permission
1 parent c9c37fd commit 4a7dd75

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

github/repos_collaborators.go

+29
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ func (s *RepositoriesService) IsCollaborator(owner, repo, user string) (bool, *R
4949
return isCollab, resp, err
5050
}
5151

52+
// RepositoryPermissionLevel represents the permission level an organization
53+
// member has for a given repository.
54+
type RepositoryPermissionLevel struct {
55+
// Possible values: "admin", "write", "read", "none"
56+
Permission string `json:"permission"`
57+
58+
User *User `json:"user"`
59+
}
60+
61+
// GetPermissionLevel retrieves the specific permission level a collaborator has for a given repository.
62+
// GitHub API docs: https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
63+
func (s *RepositoriesService) GetPermissionLevel(owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) {
64+
u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user)
65+
req, err := s.client.NewRequest("GET", u, nil)
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
70+
// TODO: remove custom Accept header when this API fully launches.
71+
req.Header.Set("Accept", mediaTypeOrgMembershipPreview)
72+
73+
rpl := new(RepositoryPermissionLevel)
74+
resp, err := s.client.Do(req, rpl)
75+
if err != nil {
76+
return nil, resp, err
77+
}
78+
return rpl, resp, err
79+
}
80+
5281
// RepositoryAddCollaboratorOptions specifies the optional parameters to the
5382
// RepositoriesService.AddCollaborator method.
5483
type RepositoryAddCollaboratorOptions struct {

github/repos_collaborators_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) {
8383
testURLParseError(t, err)
8484
}
8585

86+
func TestRepositoryService_GetPermissionLevel(t *testing.T) {
87+
setup()
88+
defer teardown()
89+
90+
mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) {
91+
testMethod(t, r, "GET")
92+
testHeader(t, r, "Accept", mediaTypeOrgMembershipPreview)
93+
fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`)
94+
})
95+
96+
rpl, _, err := client.Repositories.GetPermissionLevel("o", "r", "u")
97+
if err != nil {
98+
t.Errorf("Repositories.GetPermissionLevel returned error: %v", err)
99+
}
100+
101+
want := &RepositoryPermissionLevel{
102+
Permission: "admin",
103+
User: &User{
104+
Login: String("u"),
105+
},
106+
}
107+
108+
if !reflect.DeepEqual(rpl, want) {
109+
t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want)
110+
}
111+
}
112+
86113
func TestRepositoriesService_AddCollaborator(t *testing.T) {
87114
setup()
88115
defer teardown()

0 commit comments

Comments
 (0)