|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Tier represents a GitHub Marketplace pricing tier. |
| 14 | +type Tier string |
| 15 | + |
| 16 | +const ( |
| 17 | + // TierFree is the default tier for users without a paid subscription. |
| 18 | + TierFree Tier = "free" |
| 19 | + // TierPro is the Pro tier for individual users. |
| 20 | + TierPro Tier = "pro" |
| 21 | + // TierFlock is the Flock tier for teams/organizations. |
| 22 | + TierFlock Tier = "flock" |
| 23 | +) |
| 24 | + |
| 25 | +// MarketplacePlan represents a GitHub Marketplace subscription plan. |
| 26 | +type MarketplacePlan struct { |
| 27 | + Name string `json:"name"` |
| 28 | +} |
| 29 | + |
| 30 | +// MarketplaceAccount represents a GitHub Marketplace account subscription. |
| 31 | +type MarketplaceAccount struct { |
| 32 | + Plan MarketplacePlan `json:"plan"` |
| 33 | +} |
| 34 | + |
| 35 | +// UserTier fetches the user's GitHub Marketplace subscription tier. |
| 36 | +// Returns TierFree if no subscription exists or on API errors (graceful degradation). |
| 37 | +func (c *Client) UserTier(ctx context.Context, username string) (Tier, error) { |
| 38 | + if username == "" { |
| 39 | + return TierFree, errors.New("username cannot be empty") |
| 40 | + } |
| 41 | + |
| 42 | + // Query GitHub Marketplace API for user's subscription |
| 43 | + url := fmt.Sprintf("https://api.github.com/marketplace_listing/accounts/%s", username) |
| 44 | + |
| 45 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| 46 | + if err != nil { |
| 47 | + return TierFree, fmt.Errorf("failed to create marketplace API request: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token)) |
| 51 | + req.Header.Set("Accept", "application/vnd.github+json") |
| 52 | + req.Header.Set("X-Github-Api-Version", "2022-11-28") |
| 53 | + req.Header.Set("User-Agent", "webhook-sprinkler/1.0") |
| 54 | + |
| 55 | + resp, err := c.httpClient.Do(req) |
| 56 | + if err != nil { |
| 57 | + c.logger.Warn("marketplace API request failed", "error", err, "username", username) |
| 58 | + return TierFree, fmt.Errorf("marketplace API request failed: %w", err) |
| 59 | + } |
| 60 | + defer func() { |
| 61 | + if closeErr := resp.Body.Close(); closeErr != nil { |
| 62 | + c.logger.Warn("failed to close marketplace response body", "error", closeErr) |
| 63 | + } |
| 64 | + }() |
| 65 | + |
| 66 | + // 404 means no subscription - this is normal, not an error |
| 67 | + if resp.StatusCode == http.StatusNotFound { |
| 68 | + c.logger.Info("no marketplace subscription found", "username", username) |
| 69 | + return TierFree, nil |
| 70 | + } |
| 71 | + |
| 72 | + // Handle other error status codes |
| 73 | + if resp.StatusCode != http.StatusOK { |
| 74 | + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<10)) // Read up to 1KB for error message |
| 75 | + if err != nil { |
| 76 | + c.logger.Warn("failed to read error response", "error", err) |
| 77 | + } |
| 78 | + c.logger.Warn("marketplace API returned error", |
| 79 | + "status", resp.StatusCode, |
| 80 | + "username", username, |
| 81 | + "body", string(body)) |
| 82 | + return TierFree, fmt.Errorf("marketplace API error: status %d", resp.StatusCode) |
| 83 | + } |
| 84 | + |
| 85 | + // Parse successful response |
| 86 | + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) // 1MB limit |
| 87 | + if err != nil { |
| 88 | + c.logger.Warn("failed to read marketplace response", "error", err) |
| 89 | + return TierFree, fmt.Errorf("failed to read marketplace response: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + var account MarketplaceAccount |
| 93 | + if err := json.Unmarshal(body, &account); err != nil { |
| 94 | + c.logger.Warn("failed to parse marketplace response", "error", err, "body", string(body)) |
| 95 | + return TierFree, fmt.Errorf("failed to parse marketplace response: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + tier := mapPlanToTier(account.Plan.Name) |
| 99 | + c.logger.Info("marketplace tier detected", |
| 100 | + "username", username, |
| 101 | + "plan", account.Plan.Name, |
| 102 | + "tier", tier) |
| 103 | + |
| 104 | + return tier, nil |
| 105 | +} |
| 106 | + |
| 107 | +// mapPlanToTier maps GitHub Marketplace plan names to internal tier constants. |
| 108 | +// Update this function when your marketplace listing is approved with the actual plan names. |
| 109 | +func mapPlanToTier(planName string) Tier { |
| 110 | + switch strings.ToLower(planName) { |
| 111 | + case "pro": |
| 112 | + return TierPro |
| 113 | + case "flock", "team", "enterprise": |
| 114 | + return TierFlock |
| 115 | + default: |
| 116 | + // Unknown plan names default to free tier |
| 117 | + return TierFree |
| 118 | + } |
| 119 | +} |
0 commit comments