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://docs.github.com/en/free-pro-team@latest/rest/reference/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://docs.github.com/en/free-pro-team@latest/rest/reference/apps#testing-with-stubbed-endpoints
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	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.
48type MarketplacePurchase struct {
49	// BillingCycle can be one of the values "yearly", "monthly" or nil.
50	BillingCycle    *string          `json:"billing_cycle,omitempty"`
51	NextBillingDate *Timestamp       `json:"next_billing_date,omitempty"`
52	UnitCount       *int             `json:"unit_count,omitempty"`
53	Plan            *MarketplacePlan `json:"plan,omitempty"`
54	OnFreeTrial     *bool            `json:"on_free_trial,omitempty"`
55	FreeTrialEndsOn *Timestamp       `json:"free_trial_ends_on,omitempty"`
56	UpdatedAt       *Timestamp       `json:"updated_at,omitempty"`
57}
58
59// MarketplacePendingChange represents a pending change to a GitHub Apps Marketplace Plan.
60type MarketplacePendingChange struct {
61	EffectiveDate *Timestamp       `json:"effective_date,omitempty"`
62	UnitCount     *int             `json:"unit_count,omitempty"`
63	ID            *int64           `json:"id,omitempty"`
64	Plan          *MarketplacePlan `json:"plan,omitempty"`
65}
66
67// MarketplacePlanAccount represents a GitHub Account (user or organization) on a specific plan.
68type MarketplacePlanAccount struct {
69	URL                      *string                   `json:"url,omitempty"`
70	Type                     *string                   `json:"type,omitempty"`
71	ID                       *int64                    `json:"id,omitempty"`
72	Login                    *string                   `json:"login,omitempty"`
73	OrganizationBillingEmail *string                   `json:"organization_billing_email,omitempty"`
74	MarketplacePurchase      *MarketplacePurchase      `json:"marketplace_purchase,omitempty"`
75	MarketplacePendingChange *MarketplacePendingChange `json:"marketplace_pending_change,omitempty"`
76}
77
78// ListPlans lists all plans for your Marketplace listing.
79//
80// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#list-plans
81func (s *MarketplaceService) ListPlans(ctx context.Context, opts *ListOptions) ([]*MarketplacePlan, *Response, error) {
82	uri := s.marketplaceURI("plans")
83	u, err := addOptions(uri, opts)
84	if err != nil {
85		return nil, nil, err
86	}
87
88	req, err := s.client.NewRequest("GET", u, nil)
89	if err != nil {
90		return nil, nil, err
91	}
92
93	var plans []*MarketplacePlan
94	resp, err := s.client.Do(ctx, req, &plans)
95	if err != nil {
96		return nil, resp, err
97	}
98
99	return plans, resp, nil
100}
101
102// ListPlanAccountsForPlan lists all GitHub accounts (user or organization) on a specific plan.
103//
104// GitHub API docs: https://docs.github.com/en/rest/reference/apps#list-accounts-for-a-plan
105func (s *MarketplaceService) ListPlanAccountsForPlan(ctx context.Context, planID int64, opts *ListOptions) ([]*MarketplacePlanAccount, *Response, error) {
106	uri := s.marketplaceURI(fmt.Sprintf("plans/%v/accounts", planID))
107	u, err := addOptions(uri, opts)
108	if err != nil {
109		return nil, nil, err
110	}
111
112	req, err := s.client.NewRequest("GET", u, nil)
113	if err != nil {
114		return nil, nil, err
115	}
116
117	var accounts []*MarketplacePlanAccount
118	resp, err := s.client.Do(ctx, req, &accounts)
119	if err != nil {
120		return nil, resp, err
121	}
122
123	return accounts, resp, nil
124}
125
126// GetPlanAccountForAccount get GitHub account (user or organization) associated with an account.
127//
128// GitHub API docs: https://docs.github.com/en/rest/reference/apps#get-a-subscription-plan-for-an-account
129func (s *MarketplaceService) GetPlanAccountForAccount(ctx context.Context, accountID int64) (*MarketplacePlanAccount, *Response, error) {
130	uri := s.marketplaceURI(fmt.Sprintf("accounts/%v", accountID))
131
132	req, err := s.client.NewRequest("GET", uri, nil)
133	if err != nil {
134		return nil, nil, err
135	}
136
137	var account *MarketplacePlanAccount
138	resp, err := s.client.Do(ctx, req, &account)
139	if err != nil {
140		return nil, resp, err
141	}
142
143	return account, resp, nil
144}
145
146// ListMarketplacePurchasesForUser lists all GitHub marketplace purchases made by a user.
147//
148// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps/#list-subscriptions-for-the-authenticated-user-stubbed
149// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps/#list-subscriptions-for-the-authenticated-user
150func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context, opts *ListOptions) ([]*MarketplacePurchase, *Response, error) {
151	uri := "user/marketplace_purchases"
152	if s.Stubbed {
153		uri = "user/marketplace_purchases/stubbed"
154	}
155
156	u, err := addOptions(uri, opts)
157	if err != nil {
158		return nil, nil, err
159	}
160
161	req, err := s.client.NewRequest("GET", u, nil)
162	if err != nil {
163		return nil, nil, err
164	}
165
166	var purchases []*MarketplacePurchase
167	resp, err := s.client.Do(ctx, req, &purchases)
168	if err != nil {
169		return nil, resp, err
170	}
171	return purchases, resp, nil
172}
173
174func (s *MarketplaceService) marketplaceURI(endpoint string) string {
175	url := "marketplace_listing"
176	if s.Stubbed {
177		url = "marketplace_listing/stubbed"
178	}
179	return url + "/" + endpoint
180}
181