Browse Source

add org permissions to repo collaborator methods

- Add new options struct to AddCollaborator (BREAKING CHANGE)
- Add Permissions field to User struct, which gets populated when
  calling ListCollaborators
Will Norris 11 years ago
parent
commit
4476aa45b5
3 changed files with 40 additions and 4 deletions
  1. +22
    -2
      github/repos_collaborators.go
  2. +14
    -2
      github/repos_collaborators_test.go
  3. +4
    -0
      github/users.go

+ 22
- 2
github/repos_collaborators.go View File

@ -22,6 +22,8 @@ func (s *RepositoriesService) ListCollaborators(owner, repo string, opt *ListOpt
return nil, nil, err
}
req.Header.Set("Accept", mediaTypeOrgPermissionPreview)
users := new([]User)
resp, err := s.client.Do(req, users)
if err != nil {
@ -49,15 +51,33 @@ func (s *RepositoriesService) IsCollaborator(owner, repo, user string) (bool, *R
return isCollab, resp, err
}
// RepositoryAddCollaboratorOptions specifies the optional parameters to the
// RepositoriesService.AddCollaborator method.
type RepositoryAddCollaboratorOptions struct {
// Permission specifies the permission to grant the user on this repository.
// Possible values are:
// pull - team members can pull, but not push to or administer this repository
// push - team members can pull and push, but not administer this repository
// admin - team members can pull, push and administer this repository
//
// Default value is "pull". This option is only valid for organization-owned repositories.
Permission string `json:"permission,omitempty"`
}
// AddCollaborator adds the specified Github user as collaborator to the given repo.
//
// GitHub API docs: http://developer.github.com/v3/repos/collaborators/#add-collaborator
func (s *RepositoriesService) AddCollaborator(owner, repo, user string) (*Response, error) {
func (s *RepositoriesService) AddCollaborator(owner, repo, user string, opt *RepositoryAddCollaboratorOptions) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
req, err := s.client.NewRequest("PUT", u, nil)
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, err
}
if opt != nil {
req.Header.Set("Accept", mediaTypeOrgPermissionPreview)
}
return s.client.Do(req, nil)
}


+ 14
- 2
github/repos_collaborators_test.go View File

@ -6,6 +6,7 @@
package github
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
@ -18,6 +19,7 @@ func TestRepositoriesService_ListCollaborators(t *testing.T) {
mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeOrgPermissionPreview)
testFormValues(t, r, values{"page": "2"})
fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
})
@ -86,19 +88,29 @@ func TestRepositoriesService_AddCollaborator(t *testing.T) {
setup()
defer teardown()
opt := &RepositoryAddCollaboratorOptions{Permission: "admin"}
mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
v := new(RepositoryAddCollaboratorOptions)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PUT")
testHeader(t, r, "Accept", mediaTypeOrgPermissionPreview)
if !reflect.DeepEqual(v, opt) {
t.Errorf("Request body = %+v, want %+v", v, opt)
}
w.WriteHeader(http.StatusNoContent)
})
_, err := client.Repositories.AddCollaborator("o", "r", "u")
_, err := client.Repositories.AddCollaborator("o", "r", "u", opt)
if err != nil {
t.Errorf("Repositories.AddCollaborator returned error: %v", err)
}
}
func TestRepositoriesService_AddCollaborator_invalidUser(t *testing.T) {
_, err := client.Repositories.AddCollaborator("%", "%", "%")
_, err := client.Repositories.AddCollaborator("%", "%", "%", nil)
testURLParseError(t, err)
}


+ 4
- 0
github/users.go View File

@ -59,6 +59,10 @@ type User struct {
// TextMatches is only populated from search results that request text matches
// See: search.go and https://developer.github.com/v3/search/#text-match-metadata
TextMatches []TextMatch `json:"text_matches,omitempty"`
// Permissions identifies the permissions that a user has on a given
// repository. This is only populated when calling Repositories.ListCollaborators.
Permissions *map[string]bool `json:"permissions,omitempty"`
}
func (u User) String() string {


Loading…
Cancel
Save