From f39108f634e1bc1823ff5b730add2226b7ef025c Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Sun, 29 Jan 2017 22:11:43 -0500 Subject: [PATCH 1/2] Adds support for GET /repos/:owner/:repo/collaborators/:username/permission --- github/repos_collaborators.go | 29 +++++++++++++++++++++++++++++ github/repos_collaborators_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/github/repos_collaborators.go b/github/repos_collaborators.go index 68a9f466c84..5eb0ec5b71f 100644 --- a/github/repos_collaborators.go +++ b/github/repos_collaborators.go @@ -49,6 +49,35 @@ func (s *RepositoriesService) IsCollaborator(owner, repo, user string) (bool, *R return isCollab, resp, err } +// RepositoryPermissionLevel represents the permission level an organization +// member has for a given repository. +type RepositoryPermissionLevel struct { + // Possible values: "admin", "write", "read", "none" + Permission *string `json:"permission,omitempty"` + + User *User `json:"user,omitempty"` +} + +// GetPermissionLevel retrieves the specific permission level a collaborator has for a given repository. +// GitHub API docs: https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level +func (s *RepositoriesService) GetPermissionLevel(owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeOrgMembershipPreview) + + rpl := new(RepositoryPermissionLevel) + resp, err := s.client.Do(req, rpl) + if err != nil { + return nil, resp, err + } + return rpl, resp, err +} + // RepositoryAddCollaboratorOptions specifies the optional parameters to the // RepositoriesService.AddCollaborator method. type RepositoryAddCollaboratorOptions struct { diff --git a/github/repos_collaborators_test.go b/github/repos_collaborators_test.go index 75e3cf393de..436d6f2607e 100644 --- a/github/repos_collaborators_test.go +++ b/github/repos_collaborators_test.go @@ -83,6 +83,33 @@ func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) { testURLParseError(t, err) } +func TestRepositoryService_GetPermissionLevel(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeOrgMembershipPreview) + fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`) + }) + + rpl, _, err := client.Repositories.GetPermissionLevel("o", "r", "u") + if err != nil { + t.Errorf("Repositories.GetPermissionLevel returned error: %v", err) + } + + want := &RepositoryPermissionLevel{ + Permission: String("admin"), + User: &User{ + Login: String("u"), + }, + } + + if !reflect.DeepEqual(rpl, want) { + t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want) + } +} + func TestRepositoriesService_AddCollaborator(t *testing.T) { setup() defer teardown() From 9c6178cfcd02733df4f03ebede4a52939a48a1ed Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Tue, 31 Jan 2017 18:26:09 -0500 Subject: [PATCH 2/2] Explicitly returns nil --- github/repos_collaborators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/repos_collaborators.go b/github/repos_collaborators.go index 5eb0ec5b71f..7518d1b44f0 100644 --- a/github/repos_collaborators.go +++ b/github/repos_collaborators.go @@ -75,7 +75,7 @@ func (s *RepositoriesService) GetPermissionLevel(owner, repo, user string) (*Rep if err != nil { return nil, resp, err } - return rpl, resp, err + return rpl, resp, nil } // RepositoryAddCollaboratorOptions specifies the optional parameters to the