-
Notifications
You must be signed in to change notification settings - Fork 2.1k
add support for new repository invitations #373
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
Merged
+282
−6
Merged
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,91 @@ | ||
// Copyright 2016 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" | ||
|
||
// RepositoryInvitation represents an invitation to collaborate on a repo. | ||
type RepositoryInvitation struct { | ||
ID *int `json:"id,omitempty"` | ||
Repo *Repository `json:"repository,omitempty"` | ||
Invitee *User `json:"invitee,omitempty"` | ||
Inviter *User `json:"inviter,omitempty"` | ||
|
||
// Permissions represents the permissions that the associated user will have | ||
// on the repository. Possible values are: "read", "write", "admin". | ||
Permissions *string `json:"permissions,omitempty"` | ||
CreatedAt *Timestamp `json:"created_at,omitempty"` | ||
URL *string `json:"url,omitempty"` | ||
HTMLURL *string `json:"html_url,omitempty"` | ||
} | ||
|
||
// ListInvitations lists all currently-open repository invitations. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository | ||
func (s *RepositoriesService) ListInvitations(repoID int, opt *ListOptions) ([]*RepositoryInvitation, *Response, error) { | ||
u := fmt.Sprintf("repositories/%v/invitations", repoID) | ||
u, err := addOptions(u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
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", mediaTypeRepositoryInvitationsPreview) | ||
|
||
invites := []*RepositoryInvitation{} | ||
resp, err := s.client.Do(req, &invites) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return invites, resp, err | ||
} | ||
|
||
// DeleteInvitation deletes a repository invitation. | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/repos/invitations/#delete-a-repository-invitation | ||
func (s *RepositoriesService) DeleteInvitation(repoID, invitationID int) (*Response, error) { | ||
u := fmt.Sprintf("repositories/%v/invitations/%v", repoID, invitationID) | ||
req, err := s.client.NewRequest("DELETE", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview) | ||
|
||
return s.client.Do(req, nil) | ||
} | ||
|
||
// UpdateInvitation updates the permissions associated with a repository | ||
// invitation. | ||
// | ||
// permissions represents the permissions that the associated user will have | ||
// on the repository. Possible values are: "read", "write", "admin". | ||
// | ||
// GitHub API docs: https://developer.github.com/v3/repos/invitations/#update-a-repository-invitation | ||
func (s *RepositoriesService) UpdateInvitation(repoID, invitationID int, permissions string) (*RepositoryInvitation, *Response, error) { | ||
opts := &struct { | ||
Permissions string `json:"permissions"` | ||
}{Permissions: permissions} | ||
u := fmt.Sprintf("repositories/%v/invitations/%v", repoID, invitationID) | ||
req, err := s.client.NewRequest("PATCH", u, opts) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// TODO: remove custom Accept header when this API fully launches. | ||
req.Header.Set("Accept", mediaTypeRepositoryInvitationsPreview) | ||
|
||
invite := &RepositoryInvitation{} | ||
resp, err := s.client.Do(req, invite) | ||
return invite, resp, err | ||
} |
This file contains hidden or 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,73 @@ | ||
// Copyright 2016 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_ListInvitations(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repositories/1/invitations", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testHeader(t, r, "Accept", mediaTypeRepositoryInvitationsPreview) | ||
testFormValues(t, r, values{"page": "2"}) | ||
fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) | ||
}) | ||
|
||
opt := &ListOptions{Page: 2} | ||
got, _, err := client.Repositories.ListInvitations(1, opt) | ||
if err != nil { | ||
t.Errorf("Repositories.ListInvitations returned error: %v", err) | ||
} | ||
|
||
want := []*RepositoryInvitation{{ID: Int(1)}, {ID: Int(2)}} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("Repositories.ListInvitations = %+v, want %+v", got, want) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_DeleteInvitation(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repositories/1/invitations/2", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
testHeader(t, r, "Accept", mediaTypeRepositoryInvitationsPreview) | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
_, err := client.Repositories.DeleteInvitation(1, 2) | ||
if err != nil { | ||
t.Errorf("Repositories.DeleteInvitation returned error: %v", err) | ||
} | ||
} | ||
|
||
func TestRepositoriesService_UpdateInvitation(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repositories/1/invitations/2", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "PATCH") | ||
testHeader(t, r, "Accept", mediaTypeRepositoryInvitationsPreview) | ||
fmt.Fprintf(w, `{"id":1}`) | ||
}) | ||
|
||
got, _, err := client.Repositories.UpdateInvitation(1, 2, "write") | ||
if err != nil { | ||
t.Errorf("Repositories.UpdateInvitation returned error: %v", err) | ||
} | ||
|
||
want := &RepositoryInvitation{ID: Int(1)} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("Repositories.UpdateInvitation = %+v, want %+v", got, want) | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
It's interesting that this takes an existing, previously stable endpoint, and changes it slightly to use the (potentially less stable) preview API. (Usually preview APIs are for new endpoints.)
I think it's fine per our discussion in #376 because people who have really really specific needs will be vendoring this library anyway.