diff --git a/github/repos_collaborators.go b/github/repos_collaborators.go index 3ad6162..61dc4ef 100644 --- a/github/repos_collaborators.go +++ b/github/repos_collaborators.go @@ -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) } diff --git a/github/repos_collaborators_test.go b/github/repos_collaborators_test.go index de26ba9..ee6c498 100644 --- a/github/repos_collaborators_test.go +++ b/github/repos_collaborators_test.go @@ -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) } diff --git a/github/users.go b/github/users.go index bd68ac2..95cca6b 100644 --- a/github/users.go +++ b/github/users.go @@ -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 {