Skip to content

Commit

Permalink
Add AppsService.{Add,Remove}Repository methods. (google#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
MorrisLaw authored and dmitshur committed Oct 13, 2017
1 parent bf5bc4e commit 99750d1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ i2bskn <[email protected]>
Isao Jonas <[email protected]>
isqua <[email protected]>
Jameel Haffejee <[email protected]>
Jeremy Morris <[email protected]>
Jan Kosecki <[email protected]>
Jihoon Chung <[email protected]>
Jimmi Dyson <[email protected]>
Expand Down
37 changes: 36 additions & 1 deletion github/apps_installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package github

import "context"
import (
"context"
"fmt"
)

// Installation represents a GitHub Apps installation.
type Installation struct {
Expand Down Expand Up @@ -47,3 +50,35 @@ func (s *AppsService) ListRepos(ctx context.Context, opt *ListOptions) ([]*Repos

return r.Repositories, resp, nil
}

// AddRepository adds a single repository to an installation.
//
// GitHub API docs: https://developer.github.com/v3/apps/installations/#add-repository-to-installation
func (s *AppsService) AddRepository(ctx context.Context, instID, repoID int) (*Repository, *Response, error) {
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
}

r := new(Repository)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}

return r, resp, nil
}

// RemoveRepository removes a single repository from an installation.
//
// GitHub docs: https://developer.github.com/v3/apps/installations/#remove-repository-from-installation
func (s *AppsService) RemoveRepository(ctx context.Context, instID, repoID int) (*Response, error) {
u := fmt.Sprintf("apps/installations/%v/repositories/%v", instID, repoID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
35 changes: 35 additions & 0 deletions github/apps_installation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@ func TestAppsService_ListRepos(t *testing.T) {
t.Errorf("Apps.ListRepos returned %+v, want %+v", repositories, want)
}
}

func TestAppsService_AddRepository(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/apps/installations/1/repositories/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"}}`)
})

repo, _, err := client.Apps.AddRepository(context.Background(), 1, 1)
if err != nil {
t.Errorf("Apps.AddRepository returned error: %v", err)
}

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("AddRepository returned %+v, want %+v", repo, want)
}
}

func TestAppsService_RemoveRepository(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.RemoveRepository(context.Background(), 1, 1)
if err != nil {
t.Errorf("Apps.RemoveRepository returned error: %v", err)
}
}

0 comments on commit 99750d1

Please sign in to comment.