Skip to content

Commit

Permalink
feat: integrate billing add ons into the CLI (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert authored Feb 27, 2025
1 parent e96f64e commit 0f2899e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
8 changes: 4 additions & 4 deletions cmd/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func activateWebhooks(ctx context.Context) error {
return fmt.Errorf("failed to get sdk from context: %w", err)
}

if _, err = sdk.Workspaces.SetFeatureFlags(ctx, shared.WorkspaceFeatureFlagRequest{
FeatureFlags: []shared.WorkspaceFeatureFlag{
shared.WorkspaceFeatureFlagWebhooks,
if _, err = sdk.Organizations.CreateBillingAddOns(ctx, shared.OrganizationBillingAddOnRequest{
AddOns: []shared.BillingAddOn{
shared.BillingAddOnWebhooks,
},
}); err != nil {
return fmt.Errorf("failed to set feature flags: %w", err)
return fmt.Errorf("failed to set billing add on: %w", err)
}

logger.Println("Successfully upgraded - webhooks are now enabled")
Expand Down
8 changes: 2 additions & 6 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/pkg/errors"
"github.com/speakeasy-api/sdk-gen-config/workspace"
"github.com/speakeasy-api/speakeasy-core/auth"
spkErrors "github.com/speakeasy-api/speakeasy-core/errors"
"github.com/speakeasy-api/speakeasy/internal/run"
"github.com/speakeasy-api/speakeasy/internal/testcmd"
Expand Down Expand Up @@ -566,13 +565,10 @@ func configureTesting(ctx context.Context, flags ConfigureTestsFlags) error {
return err
}

accountType := auth.GetAccountTypeFromContext(ctx)
if !testcmd.CheckTestingAccountType(*accountType) {
return fmt.Errorf("testing is not supported on the %s account tier. Contact %s for more information", *accountType, styles.RenderSalesEmail())
if err := testcmd.CheckTestingEnabled(ctx); err != nil {
return err
}

// TODO: Add feature add-on prompt here during add-ons project

actionWorkingDir := getActionWorkingDirectoryFromFlag(rootDir, flags.WorkflowDirectory)

workflowFile, workflowFilePath, _ := workflow.Load(filepath.Join(rootDir, actionWorkingDir))
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ require (
github.com/sethvargo/go-githubactions v1.1.0
github.com/speakeasy-api/huh v1.1.2
github.com/speakeasy-api/openapi v0.1.6
github.com/speakeasy-api/openapi-generation/v2 v2.531.3
github.com/speakeasy-api/openapi-generation/v2 v2.533.0
github.com/speakeasy-api/openapi-overlay v0.10.1
github.com/speakeasy-api/sdk-gen-config v1.30.6
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.24.0
github.com/speakeasy-api/speakeasy-core v0.19.4
github.com/speakeasy-api/speakeasy-core v0.19.6
github.com/speakeasy-api/speakeasy-proxy v0.0.2
github.com/speakeasy-api/versioning-reports v0.6.0
github.com/spf13/cobra v1.8.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,16 @@ github.com/speakeasy-api/libopenapi v0.0.0-20241006201546-c9e5f704a939 h1:LGtzWU
github.com/speakeasy-api/libopenapi v0.0.0-20241006201546-c9e5f704a939/go.mod h1:9ap4lXBHgxGyFwxtOfa+B1C3IQ0rvnqteqjJvJ11oiQ=
github.com/speakeasy-api/openapi v0.1.6 h1:bpBiJ9P4hSVv2Vddk+ny6zeytPsDq0ttxl6DLAwC0zo=
github.com/speakeasy-api/openapi v0.1.6/go.mod h1:t1HA3wPC8jpGRr0UHW+SIvIB8dT5RXXi39XLrIG/URU=
github.com/speakeasy-api/openapi-generation/v2 v2.531.3 h1:qTO39Ea2mjMsMuamD1meaMNaB0ECKbdtB6pNNgA5Hp8=
github.com/speakeasy-api/openapi-generation/v2 v2.531.3/go.mod h1:JxRowcC688fh/hhH6Cf9AHEKpYMW5Uv+Iaby8z8703c=
github.com/speakeasy-api/openapi-generation/v2 v2.533.0 h1:mRsfaYYhHbyzcL/7VtCNRlra+ofJXhMq2dUlkRLeuGk=
github.com/speakeasy-api/openapi-generation/v2 v2.533.0/go.mod h1:rUj7T0SYilFBCd6ekhNrlhhcSLiiGfu6VYvR4E+rwXk=
github.com/speakeasy-api/openapi-overlay v0.10.1 h1:XFx/GvJvtAGf4dcQ6bxzsLNf76x/QWE2X0SSZrWojBQ=
github.com/speakeasy-api/openapi-overlay v0.10.1/go.mod h1:n0iOU7AqKpNFfEt6tq7qYITC4f0yzVVdFw0S7hukemg=
github.com/speakeasy-api/sdk-gen-config v1.30.6 h1:TqwG2EPlQBHMyJi0dNeWKvs1ebJoCN3Uy4/BS50l3EI=
github.com/speakeasy-api/sdk-gen-config v1.30.6/go.mod h1:e9PjnCRHGa4K4EFKVU+kKmihOZjJ2V4utcU+274+bnQ=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.24.0 h1:Lhzk66xMdSXe/H9EDgzJP5NVynyCKzcYs1vZTmzciD4=
github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.24.0/go.mod h1:k9JD6Rj0+Iizc5COoLZHyRIOGGITpKZ2qBuFFO8SqNI=
github.com/speakeasy-api/speakeasy-core v0.19.4 h1:gyfsdB9owp0fQfvpetwm/HXPyrEiF4esFWh8+YEcslo=
github.com/speakeasy-api/speakeasy-core v0.19.4/go.mod h1:M3ZBHTexZINVbpcz+nIEoaFw4Ti0Gc1rnZbdQjiFlaA=
github.com/speakeasy-api/speakeasy-core v0.19.6 h1:w0VtRMzQCrFY6XEXdIJlywex8xROvoq5qlg6Acjvohg=
github.com/speakeasy-api/speakeasy-core v0.19.6/go.mod h1:M3ZBHTexZINVbpcz+nIEoaFw4Ti0Gc1rnZbdQjiFlaA=
github.com/speakeasy-api/speakeasy-go-sdk v1.8.1 h1:atzohw12oQ5ipaLb1q7ntTu4vvAgKDJsrvaUoOu6sw0=
github.com/speakeasy-api/speakeasy-go-sdk v1.8.1/go.mod h1:XbzaM0sMjj8bGooz/uEtNkOh1FQiJK7RFuNG3LPBSAU=
github.com/speakeasy-api/speakeasy-proxy v0.0.2 h1:u4rQ8lXvuYRCSxiLQGb5JxkZRwNIDlyh+pMFYD6OGjA=
Expand Down
4 changes: 3 additions & 1 deletion internal/run/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ func targetEnablesTesting(ctx context.Context, target workflow.Target) bool {
return false
}

return testcmd.CheckTestingAccountType(*accountType) && *target.Testing.Enabled
err := testcmd.CheckTestingEnabled(ctx)

return err == nil && *target.Testing.Enabled
}
18 changes: 1 addition & 17 deletions internal/testcmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/speakeasy-api/openapi-generation/v2/pkg/generate"
"github.com/speakeasy-api/sdk-gen-config/workflow"
"github.com/speakeasy-api/speakeasy-core/auth"
"github.com/speakeasy-api/speakeasy/internal/charm/styles"
"github.com/speakeasy-api/speakeasy/internal/env"
"github.com/speakeasy-api/speakeasy/internal/links"
Expand Down Expand Up @@ -64,7 +63,7 @@ func NewRunner(ctx context.Context, opts ...RunnerOpt) *Runner {

// Run loads the workflow, loads the generator, and runs target testing.
func (r *Runner) Run(ctx context.Context) error {
if err := r.checkAccountType(ctx); err != nil {
if err := CheckTestingEnabled(ctx); err != nil {
return err
}

Expand Down Expand Up @@ -136,21 +135,6 @@ func (r *Runner) RunWithVisualization(ctx context.Context) error {
return errors.Join(err, runErr)
}

// Checks the account type to ensure testing is enabled.
func (r *Runner) checkAccountType(ctx context.Context) error {
accountType := auth.GetAccountTypeFromContext(ctx)

if accountType == nil {
return fmt.Errorf("Account type not found. Ensure you are logged in via the `speakeasy auth login` command or SPEAKEASY_API_KEY environment variable.")
}

if !CheckTestingAccountType(*accountType) {
return fmt.Errorf("Testing is not supported on the %s account tier. Contact %s for more information.", *accountType, styles.RenderSupportEmail())
}

return nil
}

// Discovers the workflow configuration to set the runner project directory and
// workflow.
func (r *Runner) loadWorkflow() error {
Expand Down
20 changes: 20 additions & 0 deletions internal/testcmd/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/speakeasy-api/speakeasy-client-sdk-go/v3/pkg/models/shared"
"github.com/speakeasy-api/speakeasy-core/auth"
"github.com/speakeasy-api/speakeasy-core/events"
"github.com/speakeasy-api/speakeasy/internal/charm/styles"
)

func ExecuteTargetTesting(ctx context.Context, generator *generate.Generator, workflowTarget workflow.Target, targetName, outDir string) (string, error) {
Expand All @@ -40,6 +41,25 @@ func ExecuteTargetTesting(ctx context.Context, generator *generate.Generator, wo
return testReportURL, err
}

func CheckTestingEnabled(ctx context.Context) error {
accountType := auth.GetAccountTypeFromContext(ctx)
orgSlug := auth.GetOrgSlugFromContext(ctx)
workspaceSlug := auth.GetWorkspaceSlugFromContext(ctx)
if accountType == nil {
return fmt.Errorf("Account type not found. Ensure you are logged in via the `speakeasy auth login` command or SPEAKEASY_API_KEY environment variable.")
}

if !slices.Contains([]shared.AccountType{shared.AccountTypeEnterprise, shared.AccountTypeBusiness}, *accountType) {
return fmt.Errorf("testing is not supported on the %s account tier. Contact %s for more information", *accountType, styles.RenderSalesEmail())
}

if ok, _ := auth.HasBillingAddOn(ctx, shared.BillingAddOnSDKTesting); !ok {
return fmt.Errorf("The SDK testing add-on must be enabled to use testing. Please visit %s", fmt.Sprintf("https://app.speakeasy.com/org/%s/%s/settings/billing", orgSlug, workspaceSlug))
}

return nil
}

func CheckTestingAccountType(accountType shared.AccountType) bool {
return slices.Contains([]shared.AccountType{shared.AccountTypeEnterprise, shared.AccountTypeBusiness}, accountType)
}
Expand Down

0 comments on commit 0f2899e

Please sign in to comment.