Skip to content

Commit 715ed43

Browse files
authored
batches: query max unlicensed changesets count (#929)
replace hardcoded permitted unlicensed changesets count by querying maxUnlicensedChangesets
1 parent c967c5e commit 715ed43

10 files changed

Lines changed: 42 additions & 29 deletions

File tree

cmd/src/batch_common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
290290
Client: opts.client,
291291
})
292292

293-
ffs, err := svc.DetermineFeatureFlags(ctx)
293+
lr, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
294294
if err != nil {
295295
return err
296296
}
@@ -521,7 +521,7 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
521521
execUI.CreatingBatchSpec()
522522
id, url, err := svc.CreateBatchSpec(ctx, namespace.ID, rawSpec, ids)
523523
if err != nil {
524-
return execUI.CreatingBatchSpecError(err)
524+
return execUI.CreatingBatchSpecError(lr.MaxUnlicensedChangesets, err)
525525
}
526526
previewURL := cfg.Endpoint + url
527527
execUI.CreatingBatchSpecSuccess(previewURL)

cmd/src/batch_new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Examples:
4848
Client: cfg.apiClient(apiFlags, flagSet.Output()),
4949
})
5050

51-
ffs, err := svc.DetermineFeatureFlags(ctx)
51+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
5252
if err != nil {
5353
return err
5454
}

cmd/src/batch_remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Examples:
5252
Client: cfg.apiClient(flags.api, flagSet.Output()),
5353
})
5454

55-
ffs, err := svc.DetermineFeatureFlags(ctx)
55+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
5656
if err != nil {
5757
return err
5858
}

cmd/src/batch_repositories.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Examples:
6969
Client: client,
7070
})
7171

72-
ffs, err := svc.DetermineFeatureFlags(ctx)
72+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
7373
if err != nil {
7474
return err
7575
}

cmd/src/batch_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Examples:
6363
Client: cfg.apiClient(apiFlags, flagSet.Output()),
6464
})
6565

66-
ffs, err := svc.DetermineFeatureFlags(ctx)
66+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
6767
if err != nil {
6868
return err
6969
}

internal/batches/license.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package batches
2+
3+
type LicenseRestrictions struct {
4+
MaxUnlicensedChangesets int
5+
}

internal/batches/service/service.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func New(opts *Opts) *Service {
4343
// The reason we ask for batchChanges here is to surface errors about trying to use batch
4444
// changes in an unsupported environment sooner, since the version check is typically the
4545
// first thing we do.
46-
const sourcegraphVersionQuery = `query SourcegraphVersion {
46+
const getInstanceInfo = `query InstanceInfo {
4747
site {
4848
productVersion
4949
}
50+
maxUnlicensedChangesets
5051
batchChanges(first: 1) {
5152
nodes {
5253
id
@@ -55,32 +56,39 @@ const sourcegraphVersionQuery = `query SourcegraphVersion {
5556
}
5657
`
5758

58-
// getSourcegraphVersion queries the Sourcegraph GraphQL API to get the
59-
// current version of the Sourcegraph instance.
60-
func (svc *Service) getSourcegraphVersion(ctx context.Context) (string, error) {
59+
// getSourcegraphVersionAndMaxChangesetsCount queries the Sourcegraph GraphQL API to get the
60+
// current version and max unlicensed changesets count for the Sourcegraph instance.
61+
func (svc *Service) getSourcegraphVersionAndMaxChangesetsCount(ctx context.Context) (string, int, error) {
6162
var result struct {
62-
Site struct {
63+
MaxUnlicensedChangesets int
64+
Site struct {
6365
ProductVersion string
6466
}
6567
}
6668

67-
ok, err := svc.client.NewQuery(sourcegraphVersionQuery).Do(ctx, &result)
69+
ok, err := svc.client.NewQuery(getInstanceInfo).Do(ctx, &result)
6870
if err != nil || !ok {
69-
return "", err
71+
return "", 0, err
7072
}
7173

72-
return result.Site.ProductVersion, err
74+
return result.Site.ProductVersion, result.MaxUnlicensedChangesets, err
7375
}
7476

75-
// DetermineFeatureFlags fetches the version of the configured Sourcegraph and
76-
// returns the enabled features.
77-
func (svc *Service) DetermineFeatureFlags(ctx context.Context) (*batches.FeatureFlags, error) {
78-
version, err := svc.getSourcegraphVersion(ctx)
77+
// DetermineLicenseAndFeatureFlags returns the enabled features and license restrictions
78+
// configured for the Sourcegraph instance.
79+
func (svc *Service) DetermineLicenseAndFeatureFlags(ctx context.Context) (*batches.LicenseRestrictions, *batches.FeatureFlags, error) {
80+
version, mc, err := svc.getSourcegraphVersionAndMaxChangesetsCount(ctx)
7981
if err != nil {
80-
return nil, errors.Wrap(err, "failed to query Sourcegraph version to check for available features")
82+
return nil, nil, errors.Wrap(err, "failed to query Sourcegraph version and license info for instance")
83+
}
84+
85+
lr := &batches.LicenseRestrictions{
86+
MaxUnlicensedChangesets: mc,
8187
}
88+
8289
ffs := &batches.FeatureFlags{}
83-
return ffs, ffs.SetFromVersion(version)
90+
return lr, ffs, ffs.SetFromVersion(version)
91+
8492
}
8593

8694
const applyBatchChangeMutation = `

internal/batches/ui/exec_ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type ExecUI interface {
4040

4141
CreatingBatchSpec()
4242
CreatingBatchSpecSuccess(previewURL string)
43-
CreatingBatchSpecError(err error) error
43+
CreatingBatchSpecError(maxUnlicensedCS int, err error) error
4444

4545
PreviewBatchSpec(previewURL string)
4646

internal/batches/ui/json_lines.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (ui *JSONLines) CreatingBatchSpecSuccess(batchSpecURL string) {
149149
})
150150
}
151151

152-
func (ui *JSONLines) CreatingBatchSpecError(err error) error {
152+
func (ui *JSONLines) CreatingBatchSpecError(_ int, err error) error {
153153
logOperationFailure(batcheslib.LogEventOperationCreatingBatchSpec, &batcheslib.CreatingBatchSpecMetadata{})
154154
return err
155155
}

internal/batches/ui/tui.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ func (ui *TUI) CreatingBatchSpecSuccess(previewURL string) {
217217
batchCompletePending(ui.pending, "Creating batch spec on Sourcegraph")
218218
}
219219

220-
func (ui *TUI) CreatingBatchSpecError(err error) error {
221-
return prettyPrintBatchUnlicensedError(ui.Out, err)
220+
func (ui *TUI) CreatingBatchSpecError(maxUnlicensedCS int, err error) error {
221+
return prettyPrintBatchUnlicensedError(ui.Out, maxUnlicensedCS, err)
222222
}
223223

224224
func (ui *TUI) DockerWatchDogWarning(err error) {
@@ -307,7 +307,7 @@ func (ui *TUI) RemoteSuccess(url string) {
307307
// is, then a better message is output. Regardless, the return value of this
308308
// function should be used to replace the original error passed in to ensure
309309
// that the displayed output is sensible.
310-
func prettyPrintBatchUnlicensedError(out *output.Output, err error) error {
310+
func prettyPrintBatchUnlicensedError(out *output.Output, maxUnlicensedCS int, err error) error {
311311
// Pull apart the error to see if it's a licensing error: if so, we should
312312
// display a friendlier and more actionable message than the usual GraphQL
313313
// error output.
@@ -321,19 +321,19 @@ func prettyPrintBatchUnlicensedError(out *output.Output, err error) error {
321321
// verbose mode, but let the original error bubble up rather
322322
// than this one.
323323
out.Verbosef("Unexpected error parsing the GraphQL error: %v", cerr)
324-
} else if code == "ErrBatchChangesUnlicensed" {
324+
} else if code == "ErrBatchChangesUnlicensed" || code == "ErrBatchChangesOverLimit" {
325325
// OK, let's print a better message, then return an
326326
// exitCodeError to suppress the normal automatic error block.
327327
// Note that we have hand wrapped the output at 80 (printable)
328328
// characters: having automatic wrapping some day would be nice,
329329
// but this should be sufficient for now.
330330
block := out.Block(output.Line("🪙", output.StyleWarning, "Batch Changes is a paid feature of Sourcegraph. All users can create sample"))
331-
block.WriteLine(output.Linef("", output.StyleWarning, "batch changes with up to 10 changesets without a license. Contact Sourcegraph"))
331+
block.WriteLine(output.Linef("", output.StyleWarning, "batch changes with up to %v changesets without a license. Contact Sourcegraph", maxUnlicensedCS))
332332
block.WriteLine(output.Linef("", output.StyleWarning, "sales at %shttps://about.sourcegraph.com/contact/sales/%s to obtain a trial", output.StyleSearchLink, output.StyleWarning))
333333
block.WriteLine(output.Linef("", output.StyleWarning, "license."))
334334
block.Write("")
335-
block.WriteLine(output.Linef("", output.StyleWarning, "To proceed with this batch change, you will need to create 5 or fewer"))
336-
block.WriteLine(output.Linef("", output.StyleWarning, "changesets. To do so, you could try adding %scount:5%s to your", output.StyleSearchAlertProposedQuery, output.StyleWarning))
335+
block.WriteLine(output.Linef("", output.StyleWarning, "To proceed with this batch change, you will need to create %v or fewer", maxUnlicensedCS))
336+
block.WriteLine(output.Linef("", output.StyleWarning, "changesets. To do so, you could try adding %scount:%v%s to your", output.StyleSearchAlertProposedQuery, maxUnlicensedCS, output.StyleWarning))
337337
block.WriteLine(output.Linef("", output.StyleWarning, "%srepositoriesMatchingQuery%s search, or reduce the number of changesets in", output.StyleReset, output.StyleWarning))
338338
block.WriteLine(output.Linef("", output.StyleWarning, "%simportChangesets%s.", output.StyleReset, output.StyleWarning))
339339
block.Close()

0 commit comments

Comments
 (0)