github.com/google/go-github/v50@v50.2.0/github/apps_marketplace.go (about) 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/en/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/en/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/en/rest/apps#list-plans 93 func (s *MarketplaceService) ListPlans(ctx context.Context, opts *ListOptions) ([]*MarketplacePlan, *Response, error) { 94 uri := s.marketplaceURI("plans") 95 u, err := addOptions(uri, opts) 96 if err != nil { 97 return nil, nil, err 98 } 99 100 req, err := s.client.NewRequest("GET", u, nil) 101 if err != nil { 102 return nil, nil, err 103 } 104 105 var plans []*MarketplacePlan 106 resp, err := s.client.Do(ctx, req, &plans) 107 if err != nil { 108 return nil, resp, err 109 } 110 111 return plans, resp, nil 112 } 113 114 // ListPlanAccountsForPlan lists all GitHub accounts (user or organization) on a specific plan. 115 // 116 // GitHub API docs: https://docs.github.com/en/rest/apps#list-accounts-for-a-plan 117 func (s *MarketplaceService) ListPlanAccountsForPlan(ctx context.Context, planID int64, opts *ListOptions) ([]*MarketplacePlanAccount, *Response, error) { 118 uri := s.marketplaceURI(fmt.Sprintf("plans/%v/accounts", planID)) 119 u, err := addOptions(uri, opts) 120 if err != nil { 121 return nil, nil, err 122 } 123 124 req, err := s.client.NewRequest("GET", u, nil) 125 if err != nil { 126 return nil, nil, err 127 } 128 129 var accounts []*MarketplacePlanAccount 130 resp, err := s.client.Do(ctx, req, &accounts) 131 if err != nil { 132 return nil, resp, err 133 } 134 135 return accounts, resp, nil 136 } 137 138 // GetPlanAccountForAccount get GitHub account (user or organization) associated with an account. 139 // 140 // GitHub API docs: https://docs.github.com/en/rest/apps#get-a-subscription-plan-for-an-account 141 func (s *MarketplaceService) GetPlanAccountForAccount(ctx context.Context, accountID int64) (*MarketplacePlanAccount, *Response, error) { 142 uri := s.marketplaceURI(fmt.Sprintf("accounts/%v", accountID)) 143 144 req, err := s.client.NewRequest("GET", uri, nil) 145 if err != nil { 146 return nil, nil, err 147 } 148 149 var account *MarketplacePlanAccount 150 resp, err := s.client.Do(ctx, req, &account) 151 if err != nil { 152 return nil, resp, err 153 } 154 155 return account, resp, nil 156 } 157 158 // ListMarketplacePurchasesForUser lists all GitHub marketplace purchases made by a user. 159 // 160 // GitHub API docs: https://docs.github.com/en/rest/apps/marketplace#list-subscriptions-for-the-authenticated-user-stubbed 161 // GitHub API docs: https://docs.github.com/en/rest/apps/marketplace#list-subscriptions-for-the-authenticated-user 162 func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context, opts *ListOptions) ([]*MarketplacePurchase, *Response, error) { 163 uri := "user/marketplace_purchases" 164 if s.Stubbed { 165 uri = "user/marketplace_purchases/stubbed" 166 } 167 168 u, err := addOptions(uri, opts) 169 if err != nil { 170 return nil, nil, err 171 } 172 173 req, err := s.client.NewRequest("GET", u, nil) 174 if err != nil { 175 return nil, nil, err 176 } 177 178 var purchases []*MarketplacePurchase 179 resp, err := s.client.Do(ctx, req, &purchases) 180 if err != nil { 181 return nil, resp, err 182 } 183 return purchases, resp, nil 184 } 185 186 func (s *MarketplaceService) marketplaceURI(endpoint string) string { 187 url := "marketplace_listing" 188 if s.Stubbed { 189 url = "marketplace_listing/stubbed" 190 } 191 return url + "/" + endpoint 192 }