1// Copyright 2017 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 6package github 7 8import ( 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/ 17type MarketplaceService struct { 18 client *Client 19 // Stubbed controls whether endpoints that return stubbed data are used 20 // instead of production endpoints. Stubbed data is fake data that's useful 21 // for testing your GitHub Apps. Stubbed data is hard-coded and will not 22 // change based on actual subscriptions. 23 // 24 // GitHub API docs: https://developer.github.com/v3/apps/marketplace/ 25 Stubbed bool 26} 27 28// MarketplacePlan represents a GitHub Apps Marketplace Listing Plan. 29type MarketplacePlan struct { 30 URL *string `json:"url,omitempty"` 31 AccountsURL *string `json:"accounts_url,omitempty"` 32 ID *int64 `json:"id,omitempty"` 33 Name *string `json:"name,omitempty"` 34 Description *string `json:"description,omitempty"` 35 MonthlyPriceInCents *int `json:"monthly_price_in_cents,omitempty"` 36 YearlyPriceInCents *int `json:"yearly_price_in_cents,omitempty"` 37 // The pricing model for this listing. Can be one of "flat-rate", "per-unit", or "free". 38 PriceModel *string `json:"price_model,omitempty"` 39 UnitName *string `json:"unit_name,omitempty"` 40 Bullets *[]string `json:"bullets,omitempty"` 41 // State can be one of the values "draft" or "published". 42 State *string `json:"state,omitempty"` 43 HasFreeTrial *bool `json:"has_free_trial,omitempty"` 44} 45 46// MarketplacePurchase represents a GitHub Apps Marketplace Purchase. 47type MarketplacePurchase struct { 48 // BillingCycle can be one of the values "yearly", "monthly" or nil. 49 BillingCycle *string `json:"billing_cycle,omitempty"` 50 NextBillingDate *Timestamp `json:"next_billing_date,omitempty"` 51 UnitCount *int `json:"unit_count,omitempty"` 52 Plan *MarketplacePlan `json:"plan,omitempty"` 53 Account *MarketplacePlanAccount `json:"account,omitempty"` 54 OnFreeTrial *bool `json:"on_free_trial,omitempty"` 55 FreeTrialEndsOn *Timestamp `json:"free_trial_ends_on,omitempty"` 56} 57 58// MarketplacePendingChange represents a pending change to a GitHub Apps Marketplace Plan. 59type MarketplacePendingChange struct { 60 EffectiveDate *Timestamp `json:"effective_date,omitempty"` 61 UnitCount *int `json:"unit_count,omitempty"` 62 ID *int64 `json:"id,omitempty"` 63 Plan *MarketplacePlan `json:"plan,omitempty"` 64} 65 66// MarketplacePlanAccount represents a GitHub Account (user or organization) on a specific plan. 67type MarketplacePlanAccount struct { 68 URL *string `json:"url,omitempty"` 69 Type *string `json:"type,omitempty"` 70 ID *int64 `json:"id,omitempty"` 71 NodeID *string `json:"node_id,omitempty"` 72 Login *string `json:"login,omitempty"` 73 Email *string `json:"email,omitempty"` 74 OrganizationBillingEmail *string `json:"organization_billing_email,omitempty"` 75 MarketplacePurchase *MarketplacePurchase `json:"marketplace_purchase,omitempty"` 76 MarketplacePendingChange *MarketplacePendingChange `json:"marketplace_pending_change,omitempty"` 77} 78 79// ListPlans lists all plans for your Marketplace listing. 80// 81// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-plans-for-your-marketplace-listing 82func (s *MarketplaceService) ListPlans(ctx context.Context, opt *ListOptions) ([]*MarketplacePlan, *Response, error) { 83 uri := s.marketplaceURI("plans") 84 u, err := addOptions(uri, opt) 85 if err != nil { 86 return nil, nil, err 87 } 88 89 req, err := s.client.NewRequest("GET", u, nil) 90 if err != nil { 91 return nil, nil, err 92 } 93 94 var plans []*MarketplacePlan 95 resp, err := s.client.Do(ctx, req, &plans) 96 if err != nil { 97 return nil, resp, err 98 } 99 100 return plans, resp, nil 101} 102 103// ListPlanAccountsForPlan lists all GitHub accounts (user or organization) on a specific plan. 104// 105// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-github-accounts-user-or-organization-on-a-specific-plan 106func (s *MarketplaceService) ListPlanAccountsForPlan(ctx context.Context, planID int64, opt *ListOptions) ([]*MarketplacePlanAccount, *Response, error) { 107 uri := s.marketplaceURI(fmt.Sprintf("plans/%v/accounts", planID)) 108 u, err := addOptions(uri, opt) 109 if err != nil { 110 return nil, nil, err 111 } 112 113 req, err := s.client.NewRequest("GET", u, nil) 114 if err != nil { 115 return nil, nil, err 116 } 117 118 var accounts []*MarketplacePlanAccount 119 resp, err := s.client.Do(ctx, req, &accounts) 120 if err != nil { 121 return nil, resp, err 122 } 123 124 return accounts, resp, nil 125} 126 127// ListPlanAccountsForAccount lists all GitHub accounts (user or organization) associated with an account. 128// 129// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#check-if-a-github-account-is-associated-with-any-marketplace-listing 130func (s *MarketplaceService) ListPlanAccountsForAccount(ctx context.Context, accountID int64, opt *ListOptions) ([]*MarketplacePlanAccount, *Response, error) { 131 uri := s.marketplaceURI(fmt.Sprintf("accounts/%v", accountID)) 132 u, err := addOptions(uri, opt) 133 if err != nil { 134 return nil, nil, err 135 } 136 137 req, err := s.client.NewRequest("GET", u, nil) 138 if err != nil { 139 return nil, nil, err 140 } 141 142 var accounts []*MarketplacePlanAccount 143 resp, err := s.client.Do(ctx, req, &accounts) 144 if err != nil { 145 return nil, resp, err 146 } 147 148 return accounts, resp, nil 149} 150 151// ListMarketplacePurchasesForUser lists all GitHub marketplace purchases made by a user. 152// 153// GitHub API docs: https://developer.github.com/v3/apps/marketplace/#get-a-users-marketplace-purchases 154func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context, opt *ListOptions) ([]*MarketplacePurchase, *Response, error) { 155 uri := "user/marketplace_purchases" 156 if s.Stubbed { 157 uri = "user/marketplace_purchases/stubbed" 158 } 159 160 u, err := addOptions(uri, opt) 161 if err != nil { 162 return nil, nil, err 163 } 164 165 req, err := s.client.NewRequest("GET", u, nil) 166 if err != nil { 167 return nil, nil, err 168 } 169 170 var purchases []*MarketplacePurchase 171 resp, err := s.client.Do(ctx, req, &purchases) 172 if err != nil { 173 return nil, resp, err 174 } 175 return purchases, resp, nil 176} 177 178func (s *MarketplaceService) marketplaceURI(endpoint string) string { 179 url := "marketplace_listing" 180 if s.Stubbed { 181 url = "marketplace_listing/stubbed" 182 } 183 return url + "/" + endpoint 184} 185