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