-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added Repository Collaborators API. #34
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2013 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ListCollaborators lists the Github users that have access to the repository. | ||
// | ||
// GitHub API docs: http://developer.github.com/v3/repos/collaborators/#list | ||
func (s *RepositoriesService) ListCollaborators(owner, repo string) ([]User, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/collaborators", owner, repo) | ||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
users := new([]User) | ||
resp, err := s.client.Do(req, users) | ||
return *users, resp, err | ||
} | ||
|
||
// IsCollaborator checks whether the specified Github user has collaborator | ||
// access to the given repo. | ||
// Note: This will return false if the user is not a collaborator OR the user | ||
// is not a GitHub user. | ||
// | ||
// GitHub API docs: http://developer.github.com/v3/repos/collaborators/#get | ||
func (s *RepositoriesService) IsCollaborator(owner, repo, user string) (bool, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user) | ||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
resp, err := s.client.Do(req, nil) | ||
isCollab, err := parseBoolResponse(err) | ||
return isCollab, resp, err | ||
} | ||
|
||
// 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) { | ||
u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user) | ||
req, err := s.client.NewRequest("PUT", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := s.client.Do(req, nil) | ||
return resp, err | ||
} | ||
|
||
// RemoveCollaborator removes the specified Github user as collaborator from the given repo. | ||
// Note: Does not return error if a valid user that is not a collaborator is removed. | ||
// | ||
// GitHub API docs: http://developer.github.com/v3/repos/collaborators/#remove-collaborator | ||
func (s *RepositoriesService) RemoveCollaborator(owner, repo, user string) (*Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user) | ||
req, err := s.client.NewRequest("DELETE", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := s.client.Do(req, nil) | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2013 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRepositoriesService_ListCollaborators(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) | ||
}) | ||
|
||
users, _, err := client.Repositories.ListCollaborators("o", "r") | ||
if err != nil { | ||
t.Errorf("Repositories.ListCollaborators returned error: %v", err) | ||
} | ||
|
||
want := []User{User{ID: 1}, User{ID: 2}} | ||
if !reflect.DeepEqual(users, want) { | ||
t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_IsCollaborator_True(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
isCollab, _, err := client.Repositories.IsCollaborator("o", "r", "u") | ||
if err != nil { | ||
t.Errorf("Repositories.IsCollaborator returned error: %v", err) | ||
} | ||
|
||
if !isCollab { | ||
t.Errorf("Repositories.IsCollaborator returned false, want true") | ||
} | ||
} | ||
|
||
func TestRepositoriesService_IsCollaborator_False(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
w.WriteHeader(http.StatusNotFound) | ||
}) | ||
|
||
isCollab, _, err := client.Repositories.IsCollaborator("o", "r", "u") | ||
if err != nil { | ||
t.Errorf("Repositories.IsCollaborator returned error: %v", err) | ||
} | ||
|
||
if isCollab { | ||
t.Errorf("Repositories.IsCollaborator returned true, want false") | ||
} | ||
} | ||
|
||
func TestRepositoriesService_AddCollaborator(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "PUT") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
_, err := client.Repositories.AddCollaborator("o", "r", "u") | ||
if err != nil { | ||
t.Errorf("Repositories.AddCollaborator returned error: %v", err) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_RemoveCollaborator(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
_, err := client.Repositories.RemoveCollaborator("o", "r", "u") | ||
if err != nil { | ||
t.Errorf("Repositories.RemoveCollaborator returned error: %v", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I'ved used the naming convention of
Check*()
for similar functions elsewhere that returnbool
s. And apparentlygists.go
includes aStarred()
method with no prefix. I don't have strong feelings about what prefix is used, but it should probably be consistent, unless there's particular reason not to.(
time.Time
does have theIsZero()
func, which leans toward your naming convention here. Do you know if the stdlib has conflicting examples?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we do stick with
Is*()
, don't worry about cleaning those others up in this PR... we can do it separately.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can tell the convention the stdlib takes is Is*(). See math, os.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's definitely go with that then. Would you mind filing a new issues to update the rest of the library, and reference this thread?