Skip to content

Commit 9901531

Browse files
committed
google#720 Initial commit for listing Plans and Plan Accounts
* Includes basic testing for Listing Plans and the Accounts linked to a Plan (including stubs)
1 parent 511f540 commit 9901531

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

github/apps_marketplace.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// MarketplaceService handles communication with the marketplace related
14+
// methods of the GitHub API.
15+
//
16+
// GitHub API docs: https://developer.github.com/v3/apps/marketplace/
17+
type MarketplaceService service
18+
19+
// MarketplacePlan represents a GitHub Apps Marketplace Listing Plan.
20+
type MarketplacePlan struct {
21+
URL *string `json:"url"`
22+
AccountsURL *string `json:"accounts_url"`
23+
ID *int `json:"id"`
24+
Name *string `json:"name"`
25+
Description *string `json:"description"`
26+
MonthlyPriceInCents *int `json:"monthly_price_in_cents"`
27+
YearlyPriceInCents *int `json:"yearly_price_in_cents"`
28+
PriceModel *string `json:"price_model"`
29+
UnitName *string `json:"unit_name"`
30+
Bullets *[]string `json:"bullets"`
31+
}
32+
33+
// MarketplacePurchase represents a GitHub Apps Marketplace Purchase.
34+
type MarketplacePurchase struct {
35+
BillingCycle *string `json:"billing_cycle"`
36+
NextBillingDate *string `json:"next_billing_date"`
37+
UnitCount *int `json:"unit_count"`
38+
Plan *MarketplacePlan `json:"plan"`
39+
}
40+
41+
// MarketplacePlanAccount represents a GitHub Account (user or organization) on a specific plan
42+
type MarketplacePlanAccount struct {
43+
URL *string `json:"url"`
44+
AccountType *string `json:"type"`
45+
ID *int `json:"id"`
46+
Login *string `json:"login"`
47+
Email *string `json:"email"`
48+
OrganizationBillingEmail *string `json:"organization_billing_email"`
49+
MarketplacePurchase *MarketplacePurchase `json:"marketplace_purchase"`
50+
}
51+
52+
// ListPlans lists all plans for your Marketplace listing
53+
//
54+
// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-plans-for-your-marketplace-listing
55+
func (s *MarketplaceService) ListPlans(ctx context.Context, stubbed bool, opt *ListOptions) ([]*MarketplacePlan, *Response, error) {
56+
u, err := addOptions(fmt.Sprintf("%v/plans", marketplaceURI(stubbed)), opt)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
61+
req, err := s.client.NewRequest("GET", u, nil)
62+
if err != nil {
63+
return nil, nil, err
64+
}
65+
66+
// TODO: remove custom Accept header when this API fully launches.
67+
req.Header.Set("Accept", mediaTypeMarketplacePreview)
68+
69+
var i []*MarketplacePlan
70+
resp, err := s.client.Do(ctx, req, &i)
71+
if err != nil {
72+
return nil, resp, err
73+
}
74+
75+
return i, resp, nil
76+
}
77+
78+
// ListPlanAccounts lists all GitHub accounts (user or organization) on a specific plan
79+
//
80+
// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-github-accounts-user-or-organization-on-a-specific-plan
81+
func (s *MarketplaceService) ListPlanAccounts(ctx context.Context, planID int, stubbed bool, opt *ListOptions) ([]*MarketplacePlanAccount, *Response, error) {
82+
u, err := addOptions(fmt.Sprintf("%v/plans/%v/accounts", marketplaceURI(stubbed), planID), opt)
83+
if err != nil {
84+
return nil, nil, err
85+
}
86+
87+
req, err := s.client.NewRequest("GET", u, nil)
88+
if err != nil {
89+
return nil, nil, err
90+
}
91+
92+
// TODO: remove custom Accept header when this API fully launches.
93+
req.Header.Set("Accept", mediaTypeMarketplacePreview)
94+
95+
var i []*MarketplacePlanAccount
96+
resp, err := s.client.Do(ctx, req, &i)
97+
if err != nil {
98+
return nil, resp, err
99+
}
100+
101+
return i, resp, nil
102+
}
103+
104+
func marketplaceURI(stubbed bool) string {
105+
if stubbed {
106+
return "apps/marketplace_listing/stubbed"
107+
}
108+
return "apps/marketplace_listing"
109+
}

github/apps_marketplace_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"net/http"
12+
"reflect"
13+
"testing"
14+
)
15+
16+
func TestMarketplaceService_ListPlans(t *testing.T) {
17+
setup()
18+
defer teardown()
19+
20+
mux.HandleFunc("/apps/marketplace_listing/plans", func(w http.ResponseWriter, r *http.Request) {
21+
testMethod(t, r, "GET")
22+
testHeader(t, r, "Accept", mediaTypeMarketplacePreview)
23+
testFormValues(t, r, values{
24+
"page": "1",
25+
"per_page": "2",
26+
})
27+
fmt.Fprint(w, `[{"id":1}]`)
28+
})
29+
30+
opt := &ListOptions{Page: 1, PerPage: 2}
31+
plans, _, err := client.Marketplace.ListPlans(context.Background(), false, opt)
32+
if err != nil {
33+
t.Errorf("Marketplace.ListPlans returned error: %v", err)
34+
}
35+
36+
want := []*MarketplacePlan{{ID: Int(1)}}
37+
if !reflect.DeepEqual(plans, want) {
38+
t.Errorf("Marketplace.ListPlans returned %+v, want %+v", plans, want)
39+
}
40+
}
41+
42+
func TestMarketplaceService_ListPlansStubbed(t *testing.T) {
43+
setup()
44+
defer teardown()
45+
46+
mux.HandleFunc("/apps/marketplace_listing/stubbed/plans", func(w http.ResponseWriter, r *http.Request) {
47+
testMethod(t, r, "GET")
48+
testHeader(t, r, "Accept", mediaTypeMarketplacePreview)
49+
fmt.Fprint(w, `[{"id":1}]`)
50+
})
51+
52+
opt := &ListOptions{Page: 1, PerPage: 2}
53+
plans, _, err := client.Marketplace.ListPlans(context.Background(), true, opt)
54+
if err != nil {
55+
t.Errorf("Marketplace.ListPlans returned error: %v", err)
56+
}
57+
58+
want := []*MarketplacePlan{{ID: Int(1)}}
59+
if !reflect.DeepEqual(plans, want) {
60+
t.Errorf("Marketplace.ListPlans returned %+v, want %+v", plans, want)
61+
}
62+
}
63+
64+
func TestMarketplaceService_ListPlanAccounts(t *testing.T) {
65+
setup()
66+
defer teardown()
67+
68+
mux.HandleFunc("/apps/marketplace_listing/plans/1/accounts", func(w http.ResponseWriter, r *http.Request) {
69+
testMethod(t, r, "GET")
70+
testHeader(t, r, "Accept", mediaTypeMarketplacePreview)
71+
fmt.Fprint(w, `[{"id":1}]`)
72+
})
73+
74+
opt := &ListOptions{Page: 1, PerPage: 2}
75+
accounts, _, err := client.Marketplace.ListPlanAccounts(context.Background(), 1, false, opt)
76+
if err != nil {
77+
t.Errorf("Marketplace.ListPlanAccounts returned error: %v", err)
78+
}
79+
80+
want := []*MarketplacePlanAccount{{ID: Int(1)}}
81+
if !reflect.DeepEqual(accounts, want) {
82+
t.Errorf("Marketplace.ListPlanAccounts returned %+v, want %+v", accounts, want)
83+
}
84+
}

github/github.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ const (
102102

103103
// https://developer.github.com/changes/2017-07-26-team-review-request-thor-preview/
104104
mediaTypeTeamReviewPreview = "application/vnd.github.thor-preview+json"
105+
106+
// https://developer.github.com/v3/apps/marketplace/
107+
mediaTypeMarketplacePreview = "application/vnd.github.valkyrie-preview+json"
105108
)
106109

107110
// A Client manages communication with the GitHub API.
@@ -143,6 +146,7 @@ type Client struct {
143146
Licenses *LicensesService
144147
Migrations *MigrationService
145148
Reactions *ReactionsService
149+
Marketplace *MarketplaceService
146150
}
147151

148152
type service struct {
@@ -224,6 +228,7 @@ func NewClient(httpClient *http.Client) *Client {
224228
c.Gitignores = (*GitignoresService)(&c.common)
225229
c.Issues = (*IssuesService)(&c.common)
226230
c.Licenses = (*LicensesService)(&c.common)
231+
c.Marketplace = (*MarketplaceService)(&c.common)
227232
c.Migrations = (*MigrationService)(&c.common)
228233
c.Organizations = (*OrganizationsService)(&c.common)
229234
c.Projects = (*ProjectsService)(&c.common)

0 commit comments

Comments
 (0)