Skip to content

Commit

Permalink
batches: upsert batch change when running `src batch remote (#787)
Browse files Browse the repository at this point in the history
* upsert batch change when running `src batch remote

* use batchChangeName for execution url

* update changelog

* add createBatchSpecFromRaw query

* Update CHANGELOG.md

Co-authored-by: Adam Harvey <adam@adamharvey.name>

* remove redundant UpsertBatchSpecInput method

Co-authored-by: Adam Harvey <adam@adamharvey.name>
  • Loading branch information
BolajiOlajide and LawnGnome authored Jun 17, 2022
1 parent 524c89a commit 96335e5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions cmd/src/batch_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 47 additions & 8 deletions internal/batches/service/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +74,7 @@ mutation UpsertBatchSpecInput(
}
`

func (svc *Service) UpsertBatchSpecInput(
func (svc *Service) CreateBatchSpecFromRaw(
ctx context.Context,
batchSpec string,
namespaceID string,
Expand All @@ -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,
Expand All @@ -63,7 +102,7 @@ func (svc *Service) UpsertBatchSpecInput(
return "", err
}

return resp.UpsertBatchSpecInput.ID, nil
return resp.CreateBatchSpecFromRaw.ID, nil
}

const executeBatchSpecQuery = `
Expand Down
8 changes: 8 additions & 0 deletions internal/batches/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 96335e5

Please sign in to comment.