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
Enhancement/add remove repository #749
Changes from 37 commits
aa10fb4
7772ba0
5d99cdf
2eab2b3
a35a566
3e14143
44e0f51
defe8b8
f31595a
5934515
e20f019
16bcc33
f90fa0d
71f1694
585efab
030ae55
11aefc3
173da1c
2c3db48
158fea8
0c7acc2
9b8d40c
b9c5873
e132615
926a90c
f1bf61b
5e8d98c
a612b5e
f35c3c9
d6c991e
4d5820a
b2f7773
60af6e6
8e876e4
42faa4d
326457b
b4b56c3
432dab7
c01f313
41d02d7
b5f3a3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,7 +5,10 @@ | |||
|
||||
package github | ||||
|
||||
import "context" | ||||
import ( | ||||
"context" | ||||
"fmt" | ||||
) | ||||
|
||||
// Installation represents a GitHub Apps installation. | ||||
type Installation struct { | ||||
|
@@ -47,3 +50,38 @@ func (s *AppsService) ListRepos(ctx context.Context, opt *ListOptions) ([]*Repos | |||
|
||||
return r.Repositories, resp, nil | ||||
} | ||||
|
||||
// AddRepo adds a single repository to an installation. | ||||
// | ||||
// GitHub API docs: https://developer.github.com/v3/apps/installations/#add-repository-to-installation | ||||
func (s *AppsService) AddRepo(ctx context.Context, instID, repoID int) (*Repository, *Response, error) { | ||||
u := fmt.Sprintf("/apps/installations/%v/repositories/%v", instID, repoID) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to not include the starting slash in the URLs, otherwise enterprise GH URLs will not work. This should be: u := fmt.Sprintf("apps/installations/%v/repositories/%v", instID, repoID) |
||||
req, err := s.client.NewRequest("PUT", u, nil) | ||||
if err != nil { | ||||
return nil, nil, err | ||||
} | ||||
|
||||
// TODO: remove custom Accept header when this API fully launches. | ||||
req.Header.Set("Accept", mediaTypeV3) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 269 in 0f6d3ce
So no need to do this, you can delete the two lines above. |
||||
|
||||
r := new(Repository) | ||||
resp, err := s.client.Do(ctx, req, r) | ||||
if err != nil { | ||||
return nil, nil, err | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if err != nil, you should return resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
} This is what all other methods do. |
||||
} | ||||
|
||||
return r, resp, nil | ||||
} | ||||
|
||||
// RemoveRepo removes a single repository from an installation. | ||||
// | ||||
// GitHub docs: https://developer.github.com/v3/apps/installations/#remove-repository-from-installation | ||||
func (s *AppsService) RemoveRepo(ctx context.Context, instID, repoID int) (*Response, error) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, this would be |
||||
u := fmt.Sprintf("/apps/installations/%v/repositories/%v", instID, repoID) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please remove the first slash in the URL. You'll notice all other endpoints don't include it either. |
||||
req, err := s.client.NewRequest("DELETE", u, nil) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
|
||||
return s.client.Do(ctx, req, nil) | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,39 @@ func TestAppsService_ListRepos(t *testing.T) { | |
t.Errorf("Apps.ListRepos returned %+v, want %+v", repositories, want) | ||
} | ||
} | ||
|
||
func TestAppsService_AddRepo(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/apps/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "PUT") | ||
testHeader(t, r, "Accept", mediaTypeV3) | ||
fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`) | ||
}) | ||
|
||
repo, _, err := client.Apps.AddRepo(context.Background(), 1, 1) | ||
if err != nil { | ||
t.Errorf("Apps.AddRepo returned error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice to rename the method name here too. |
||
} | ||
|
||
want := &Repository{ID: Int(1), Name: String("n"), Description: String("d"), Owner: &User{Login: String("l")}, License: &License{Key: String("mit")}} | ||
if !reflect.DeepEqual(repo, want) { | ||
t.Errorf("AddRepo returned %+v, want %+v", repo, want) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. |
||
} | ||
} | ||
|
||
func TestAppsService_RemoveRepo(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/apps/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
_, err := client.Apps.RemoveRepo(context.Background(), 1, 1) | ||
if err != nil { | ||
t.Errorf("Apps.RemoveRepo returned error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As well as here. |
||
} | ||
} |
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 think we should call this method
AddRepository
rather thanAddRepo
. We already use the full "Repository" word in many other method names, e.g.:ActivityService.GetRepositorySubscription
ActivityService.DeleteRepositorySubscription
IssuesService.ListRepositoryEvents