diff --git a/CHANGELOG.md b/CHANGELOG.md index fac7567786..a9d1946946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ All notable changes to `src-cli` are documented in this file. ### Fixed +- The preview link shown when running `src batch remote` to create a new batch change no longer 404s. [sourcegraph/src-cli](https://github.com/sourcegraph/src-cli/pull/787) + ### Removed ## 3.40.11 diff --git a/cmd/src/batch_remote.go b/cmd/src/batch_remote.go index 9384f32bfd..9d542ae9b5 100644 --- a/cmd/src/batch_remote.go +++ b/cmd/src/batch_remote.go @@ -78,8 +78,15 @@ Examples: } ui.ResolvingNamespaceSuccess(namespace.ID) + ui.SendingBatchChange() + batchChangeName, err := svc.UpsertBatchChange(ctx, spec.Name, namespace.ID) + if err != nil { + return err + } + ui.SendingBatchChangeSuccess() + ui.SendingBatchSpec() - batchSpecID, err := svc.UpsertBatchSpecInput( + batchSpecID, err := svc.CreateBatchSpecFromRaw( ctx, raw, namespace.ID, @@ -125,7 +132,7 @@ Examples: "%s/%s/batch-changes/%s/executions/%s", strings.TrimSuffix(cfg.Endpoint, "/"), strings.TrimPrefix(namespace.URL, "/"), - spec.Name, + batchChangeName, batchSpecID, ) ui.RemoteSuccess(executionURL) diff --git a/internal/batches/service/remote.go b/internal/batches/service/remote.go index 2bead9d6bc..dc63a47daf 100644 --- a/internal/batches/service/remote.go +++ b/internal/batches/service/remote.go @@ -15,15 +15,54 @@ func (svc *Service) areServerSideBatchChangesSupported() error { return nil } -const upsertBatchSpecInputQuery = ` -mutation UpsertBatchSpecInput( +const upsertEmptyBatchChangeQuery = ` +mutation UpsertEmptyBatchChange( + $name: String! + $namespace: ID! +) { + upsertEmptyBatchChange( + name: $name, + namespace: $namespace + ) { + name + } +} +` + +func (svc *Service) UpsertBatchChange( + ctx context.Context, + name string, + namespaceID string, +) (string, error) { + if err := svc.areServerSideBatchChangesSupported(); err != nil { + return "", err + } + + var resp struct { + UpsertEmptyBatchChange struct { + Name string `json:"name"` + } `json:"upsertEmptyBatchChange"` + } + + if ok, err := svc.client.NewRequest(upsertEmptyBatchChangeQuery, map[string]interface{}{ + "name": name, + "namespace": namespaceID, + }).Do(ctx, &resp); err != nil || !ok { + return "", err + } + + return resp.UpsertEmptyBatchChange.Name, nil +} + +const createBatchSpecFromRawQuery = ` +mutation CreateBatchSpecFromRaw( $batchSpec: String!, $namespace: ID!, $allowIgnored: Boolean!, $allowUnsupported: Boolean!, $noCache: Boolean!, ) { - upsertBatchSpecInput( + createBatchSpecFromRaw( batchSpec: $batchSpec, namespace: $namespace, allowIgnored: $allowIgnored, @@ -35,7 +74,7 @@ mutation UpsertBatchSpecInput( } ` -func (svc *Service) UpsertBatchSpecInput( +func (svc *Service) CreateBatchSpecFromRaw( ctx context.Context, batchSpec string, namespaceID string, @@ -48,12 +87,12 @@ func (svc *Service) UpsertBatchSpecInput( } var resp struct { - UpsertBatchSpecInput struct { + CreateBatchSpecFromRaw struct { ID string `json:"id"` - } `json:"upsertBatchSpecInput"` + } `json:"createBatchSpecFromRaw"` } - if ok, err := svc.client.NewRequest(upsertBatchSpecInputQuery, map[string]interface{}{ + if ok, err := svc.client.NewRequest(createBatchSpecFromRawQuery, map[string]interface{}{ "batchSpec": batchSpec, "namespace": namespaceID, "allowIgnored": allowIgnored, @@ -63,7 +102,7 @@ func (svc *Service) UpsertBatchSpecInput( return "", err } - return resp.UpsertBatchSpecInput.ID, nil + return resp.CreateBatchSpecFromRaw.ID, nil } const executeBatchSpecQuery = ` diff --git a/internal/batches/ui/tui.go b/internal/batches/ui/tui.go index 2b001d893b..f146ab77fc 100644 --- a/internal/batches/ui/tui.go +++ b/internal/batches/ui/tui.go @@ -223,6 +223,14 @@ func (ui *TUI) ApplyingBatchSpecSuccess(batchChangeURL string) { block.Writef("%s", batchChangeURL) } +func (ui *TUI) SendingBatchChange() { + ui.pending = batchCreatePending(ui.Out, "Sending batch change") +} + +func (ui *TUI) SendingBatchChangeSuccess() { + batchCompletePending(ui.pending, "Sending batch change") +} + func (ui *TUI) SendingBatchSpec() { ui.pending = batchCreatePending(ui.Out, "Sending batch spec") }