From 70ace69fa3f2a3f299d66fb4145c93f18f702306 Mon Sep 17 00:00:00 2001 From: "Hemanth Krishna (DarthBenro008)" Date: Thu, 4 Feb 2021 13:23:26 +0530 Subject: [PATCH 1/2] feat: #1791 new ListRepositories struct and tests --- github/apps_installation.go | 22 ++++++++++++---------- github/apps_installation_test.go | 8 ++++---- github/github-accessors.go | 8 ++++++++ github/github-accessors_test.go | 10 ++++++++++ 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/github/apps_installation.go b/github/apps_installation.go index 3da39131693..643830efc04 100644 --- a/github/apps_installation.go +++ b/github/apps_installation.go @@ -10,10 +10,15 @@ import ( "fmt" ) +type ListRepositories struct { + TotalCount *int `json:"total_count,omitempty"` + Repositories []*Repository `json:"repositories"` +} + // ListRepos lists the repositories that are accessible to the authenticated installation. // // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps/#list-repositories-accessible-to-the-app-installation -func (s *AppsService) ListRepos(ctx context.Context, opts *ListOptions) ([]*Repository, *Response, error) { +func (s *AppsService) ListRepos(ctx context.Context, opts *ListOptions) (*ListRepositories, *Response, error) { u, err := addOptions("installation/repositories", opts) if err != nil { return nil, nil, err @@ -24,22 +29,21 @@ func (s *AppsService) ListRepos(ctx context.Context, opts *ListOptions) ([]*Repo return nil, nil, err } - var r struct { - Repositories []*Repository `json:"repositories"` - } + var r *ListRepositories + resp, err := s.client.Do(ctx, req, &r) if err != nil { return nil, resp, err } - return r.Repositories, resp, nil + return r, resp, nil } // ListUserRepos lists repositories that are accessible // to the authenticated user for an installation. // // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps/#list-repositories-accessible-to-the-user-access-token -func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opts *ListOptions) ([]*Repository, *Response, error) { +func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opts *ListOptions) (*ListRepositories, *Response, error) { u := fmt.Sprintf("user/installations/%v/repositories", id) u, err := addOptions(u, opts) if err != nil { @@ -51,15 +55,13 @@ func (s *AppsService) ListUserRepos(ctx context.Context, id int64, opts *ListOpt return nil, nil, err } - var r struct { - Repositories []*Repository `json:"repositories"` - } + var r *ListRepositories resp, err := s.client.Do(ctx, req, &r) if err != nil { return nil, resp, err } - return r.Repositories, resp, nil + return r, resp, nil } // AddRepository adds a single repository to an installation. diff --git a/github/apps_installation_test.go b/github/apps_installation_test.go index 5c2a8b6b803..a73498c6dad 100644 --- a/github/apps_installation_test.go +++ b/github/apps_installation_test.go @@ -23,7 +23,7 @@ func TestAppsService_ListRepos(t *testing.T) { "page": "1", "per_page": "2", }) - fmt.Fprint(w, `{"repositories": [{"id":1}]}`) + fmt.Fprint(w, `{"total_count": 1,"repositories": [{"id": 1}]}`) }) opt := &ListOptions{Page: 1, PerPage: 2} @@ -33,7 +33,7 @@ func TestAppsService_ListRepos(t *testing.T) { t.Errorf("Apps.ListRepos returned error: %v", err) } - want := []*Repository{{ID: Int64(1)}} + want := &ListRepositories{TotalCount: Int(1), Repositories: []*Repository{{ID: Int64(1)}}} if !reflect.DeepEqual(repositories, want) { t.Errorf("Apps.ListRepos returned %+v, want %+v", repositories, want) } @@ -58,7 +58,7 @@ func TestAppsService_ListUserRepos(t *testing.T) { "page": "1", "per_page": "2", }) - fmt.Fprint(w, `{"repositories": [{"id":1}]}`) + fmt.Fprint(w, `{"total_count":1,"repositories": [{"id":1}]}`) }) opt := &ListOptions{Page: 1, PerPage: 2} @@ -68,7 +68,7 @@ func TestAppsService_ListUserRepos(t *testing.T) { t.Errorf("Apps.ListUserRepos returned error: %v", err) } - want := []*Repository{{ID: Int64(1)}} + want := &ListRepositories{TotalCount: Int(1), Repositories: []*Repository{{ID: Int64(1)}}} if !reflect.DeepEqual(repositories, want) { t.Errorf("Apps.ListUserRepos returned %+v, want %+v", repositories, want) } diff --git a/github/github-accessors.go b/github/github-accessors.go index 392065c7839..b9160192371 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -5988,6 +5988,14 @@ func (l *ListCollaboratorOptions) GetAffiliation() string { return *l.Affiliation } +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (l *ListRepositories) GetTotalCount() int { + if l == nil || l.TotalCount == nil { + return 0 + } + return *l.TotalCount +} + // GetEffectiveDate returns the EffectiveDate field if it's non-nil, zero value otherwise. func (m *MarketplacePendingChange) GetEffectiveDate() Timestamp { if m == nil || m.EffectiveDate == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 301c6225518..e7a22dca725 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -7012,6 +7012,16 @@ func TestListCollaboratorOptions_GetAffiliation(tt *testing.T) { l.GetAffiliation() } +func TestListRepositories_GetTotalCount(tt *testing.T) { + var zeroValue int + l := &ListRepositories{TotalCount: &zeroValue} + l.GetTotalCount() + l = &ListRepositories{} + l.GetTotalCount() + l = nil + l.GetTotalCount() +} + func TestMarketplacePendingChange_GetEffectiveDate(tt *testing.T) { var zeroValue Timestamp m := &MarketplacePendingChange{EffectiveDate: &zeroValue} From 645cf42a13e1cf2adc3758a5bc2f58e7eec43ed3 Mon Sep 17 00:00:00 2001 From: Hemanth Krishna Date: Thu, 4 Feb 2021 19:37:38 +0530 Subject: [PATCH 2/2] add: godoc comment for ListRepositories struct --- github/apps_installation.go | 1 + 1 file changed, 1 insertion(+) diff --git a/github/apps_installation.go b/github/apps_installation.go index 643830efc04..b4ac5eefefd 100644 --- a/github/apps_installation.go +++ b/github/apps_installation.go @@ -10,6 +10,7 @@ import ( "fmt" ) +// ListRepositories represents the response from the list repos endpoints. type ListRepositories struct { TotalCount *int `json:"total_count,omitempty"` Repositories []*Repository `json:"repositories"`