Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/src/batch_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand All @@ -30,7 +30,7 @@ Examples:
flagSet := flag.NewFlagSet("apply", flag.ExitOnError)
flags := newBatchApplyFlags(flagSet, batchDefaultCacheDir(), batchDefaultTempDirPrefix())

doApply := func(ctx context.Context, out *output.Output, svc *batches.Service, flags *batchApplyFlags) error {
doApply := func(ctx context.Context, out *output.Output, svc *service.Service, flags *batchApplyFlags) error {
id, _, err := batchExecute(ctx, out, svc, flags)
if err != nil {
return err
Expand Down Expand Up @@ -67,7 +67,7 @@ Examples:
ctx, cancel := contextCancelOnInterrupt(context.Background())
defer cancel()

svc := batches.NewService(&batches.ServiceOpts{
svc := service.New(&service.Opts{
AllowUnsupported: flags.allowUnsupported,
AllowIgnored: flags.allowIgnored,
Client: cfg.apiClient(flags.api, flagSet.Output()),
Expand Down
15 changes: 8 additions & 7 deletions cmd/src/batch_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"github.com/sourcegraph/go-diff/diff"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand Down Expand Up @@ -189,7 +191,7 @@ func batchOpenFileFlag(flag *string) (io.ReadCloser, error) {
// batchExecute performs all the steps required to upload the campaign spec
// to Sourcegraph, including execution as needed. The return values are the
// spec ID, spec URL, and error.
func batchExecute(ctx context.Context, out *output.Output, svc *batches.Service, flags *batchApplyFlags) (graphql.BatchSpecID, string, error) {
func batchExecute(ctx context.Context, out *output.Output, svc *service.Service, flags *batchApplyFlags) (graphql.BatchSpecID, string, error) {
if err := checkExecutable("git", "version"); err != nil {
return "", "", err
}
Expand Down Expand Up @@ -275,13 +277,12 @@ func batchExecute(ctx context.Context, out *output.Output, svc *batches.Service,
task.Archive = fetcher.Checkout(task.Repository, task.ArchivePathToFetch())
}

opts := batches.ExecutorOpts{
Cache: svc.NewExecutionCache(flags.cacheDir),
opts := executor.Opts{
TempDir: flags.tempDir,
Creator: workspaceCreator,
ClearCache: flags.clearCache,
KeepLogs: flags.keepLogs,
Timeout: flags.timeout,
TempDir: flags.tempDir,
Parallelism: flags.parallelism,
}

Expand Down Expand Up @@ -354,7 +355,7 @@ func batchExecute(ctx context.Context, out *output.Output, svc *batches.Service,
// batchParseSpec parses and validates the given batch spec. If the spec has
// validation errors, the errors are output in a human readable form and an
// exitCodeError is returned.
func batchParseSpec(out *output.Output, svc *batches.Service, input io.ReadCloser) (*batches.BatchSpec, string, error) {
func batchParseSpec(out *output.Output, svc *service.Service, input io.ReadCloser) (*batches.BatchSpec, string, error) {
spec, raw, err := svc.ParseBatchSpec(input)
if err != nil {
if merr, ok := err.(*multierror.Error); ok {
Expand Down Expand Up @@ -400,7 +401,7 @@ func printExecutionError(out *output.Output, err error) {
}

for _, e := range errs {
if taskErr, ok := e.(batches.TaskExecutionErr); ok {
if taskErr, ok := e.(executor.TaskExecutionErr); ok {
block.Write(formatTaskExecutionErr(taskErr))
} else {
if err == context.Canceled {
Expand Down Expand Up @@ -455,7 +456,7 @@ func flattenErrs(err error) (result []error) {
return result
}

func formatTaskExecutionErr(err batches.TaskExecutionErr) string {
func formatTaskExecutionErr(err executor.TaskExecutionErr) string {
if ee, ok := errors.Cause(err).(*exec.ExitError); ok && ee.String() == "signal: killed" {
return fmt.Sprintf(
"%s%s%s: killed by interrupt signal",
Expand Down
4 changes: 2 additions & 2 deletions cmd/src/batch_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ Examples:
ctx, cancel := contextCancelOnInterrupt(context.Background())
defer cancel()

svc := batches.NewService(&batches.ServiceOpts{
svc := service.New(&service.Opts{
AllowUnsupported: flags.allowUnsupported,
AllowIgnored: flags.allowIgnored,
Client: cfg.apiClient(flags.api, flagSet.Output()),
Expand Down
18 changes: 9 additions & 9 deletions cmd/src/batch_progress_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/sourcegraph/go-diff/diff"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/output"
"golang.org/x/sync/semaphore"
)
Expand All @@ -22,7 +22,7 @@ func newBatchProgressPrinter(out *output.Output, verbose bool, numParallelism in
numParallelism: numParallelism,

completedTasks: map[string]bool{},
runningTasks: map[string]*batches.TaskStatus{},
runningTasks: map[string]*executor.TaskStatus{},

repoStatusBar: map[string]int{},
statusBarRepo: map[int]string{},
Expand All @@ -46,13 +46,13 @@ type batchProgressPrinter struct {
numParallelism int

completedTasks map[string]bool
runningTasks map[string]*batches.TaskStatus
runningTasks map[string]*executor.TaskStatus

repoStatusBar map[string]int
statusBarRepo map[int]string
}

func (p *batchProgressPrinter) initProgressBar(statuses []*batches.TaskStatus) int {
func (p *batchProgressPrinter) initProgressBar(statuses []*executor.TaskStatus) int {
numStatusBars := p.numParallelism
if len(statuses) < numStatusBars {
numStatusBars = len(statuses)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (p *batchProgressPrinter) updateProgressBar(completed, errored, total int)
p.progress.SetLabelAndRecalc(0, label)
}

func (p *batchProgressPrinter) PrintStatuses(statuses []*batches.TaskStatus) {
func (p *batchProgressPrinter) PrintStatuses(statuses []*executor.TaskStatus) {
if len(statuses) == 0 {
return
}
Expand All @@ -109,8 +109,8 @@ func (p *batchProgressPrinter) PrintStatuses(statuses []*batches.TaskStatus) {
p.numStatusBars = p.initProgressBar(statuses)
}

newlyCompleted := []*batches.TaskStatus{}
currentlyRunning := []*batches.TaskStatus{}
newlyCompleted := []*executor.TaskStatus{}
currentlyRunning := []*executor.TaskStatus{}
errored := 0

for _, ts := range statuses {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (p *batchProgressPrinter) PrintStatuses(statuses []*batches.TaskStatus) {

p.updateProgressBar(len(p.completedTasks), errored, len(statuses))

newlyStarted := map[string]*batches.TaskStatus{}
newlyStarted := map[string]*executor.TaskStatus{}
statusBarIndex := 0
for _, ts := range currentlyRunning {
if _, ok := p.runningTasks[ts.DisplayName()]; ok {
Expand Down Expand Up @@ -252,7 +252,7 @@ type statusTexter interface {
StatusText() string
}

func taskStatusBarText(ts *batches.TaskStatus) (string, error) {
func taskStatusBarText(ts *executor.TaskStatus) (string, error) {
var statusText string

if ts.IsCompleted() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/src/batch_progress_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/executor"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func TestBatchProgressPrinterIntegration(t *testing.T) {
})

now := time.Now()
statuses := []*batches.TaskStatus{
statuses := []*executor.TaskStatus{
{
RepoName: "github.com/sourcegraph/sourcegraph",
StartedAt: now,
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestBatchProgressPrinterIntegration(t *testing.T) {
}

// Now mark the last task as completed
statuses[len(statuses)-1] = &batches.TaskStatus{
statuses[len(statuses)-1] = &executor.TaskStatus{
RepoName: "github.com/sourcegraph/automation-testing",
StartedAt: now.Add(time.Duration(-5) * time.Second),
FinishedAt: now.Add(time.Duration(5) * time.Second),
Expand Down
4 changes: 2 additions & 2 deletions cmd/src/batch_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/pkg/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand Down Expand Up @@ -48,7 +48,7 @@ Examples:
ctx := context.Background()
client := cfg.apiClient(apiFlags, flagSet.Output())

svc := batches.NewService(&batches.ServiceOpts{Client: client})
svc := service.New(&service.Opts{Client: client})

if err := svc.DetermineFeatureFlags(ctx); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/src/batch_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/output"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ Examples:
}
defer specFile.Close()

svc := batches.NewService(&batches.ServiceOpts{})
svc := service.New(&service.Opts{})

out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
if _, _, err := batchParseSpec(out, svc, specFile); err != nil {
Expand Down
38 changes: 34 additions & 4 deletions internal/batches/batch_spec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package batches

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -72,6 +73,14 @@ type WorkspaceConfiguration struct {
glob glob.Glob
}

func (wc *WorkspaceConfiguration) SetGlob(g glob.Glob) {
wc.glob = g
}

func (wc *WorkspaceConfiguration) Matches(repoName string) bool {
return wc.glob.Match(repoName)
}

type OnQueryOrRepository struct {
RepositoriesMatchingQuery string `json:"repositoriesMatchingQuery,omitempty" yaml:"repositoriesMatchingQuery"`
Repository string `json:"repository,omitempty" yaml:"repository"`
Expand All @@ -88,6 +97,27 @@ type Step struct {
image docker.Image
}

func (s *Step) SetImage(image docker.Image) {
s.image = image
}

// TODO(mrnugget): All of these wrappers are not good
func (s Step) ImageDigest(ctx context.Context) (string, error) {
return s.image.Digest(ctx)
}

func (s Step) DockerImage() docker.Image {
return s.image
}

func (s Step) EnsureImage(ctx context.Context) error {
return s.image.Ensure(ctx)
}

func (s Step) ImageUIDGID(ctx context.Context) (docker.UIDGID, error) {
return s.image.UIDGID(ctx)
}

type Outputs map[string]Output

type Output struct {
Expand All @@ -105,7 +135,7 @@ type Group struct {
Repository string `json:"repository,omitempty" yaml:"repository"`
}

func ParseBatchSpec(data []byte, features featureFlags) (*BatchSpec, error) {
func ParseBatchSpec(data []byte, features FeatureFlags) (*BatchSpec, error) {
var spec BatchSpec
if err := yaml.UnmarshalValidate(schema.BatchSpecJSON, data, &spec); err != nil {
if multiErr, ok := err.(*multierror.Error); ok {
Expand All @@ -128,7 +158,7 @@ func ParseBatchSpec(data []byte, features featureFlags) (*BatchSpec, error) {

var errs *multierror.Error

if !features.allowArrayEnvironments {
if !features.AllowArrayEnvironments {
for i, step := range spec.Steps {
if !step.Env.IsStatic() {
errs = multierror.Append(errs, errors.Errorf("step %d includes one or more dynamic environment variables, which are unsupported in this Sourcegraph version", i+1))
Expand All @@ -140,11 +170,11 @@ func ParseBatchSpec(data []byte, features featureFlags) (*BatchSpec, error) {
errs = multierror.Append(errs, errors.New("batch spec includes steps but no changesetTemplate"))
}

if spec.TransformChanges != nil && !features.allowtransformChanges {
if spec.TransformChanges != nil && !features.AllowTransformChanges {
errs = multierror.Append(errs, errors.New("batch spec includes transformChanges, which is not supported in this Sourcegraph version"))
}

if len(spec.Workspaces) != 0 && !features.allowtransformChanges {
if len(spec.Workspaces) != 0 && !features.AllowTransformChanges {
errs = multierror.Append(errs, errors.New("batch spec includes workspaces, which is not supported in this Sourcegraph version"))
}

Expand Down
6 changes: 3 additions & 3 deletions internal/batches/batch_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ changesetTemplate:
published: false
`

_, err := ParseBatchSpec([]byte(spec), featureFlags{})
_, err := ParseBatchSpec([]byte(spec), FeatureFlags{})
if err != nil {
t.Fatalf("parsing valid spec returned error: %s", err)
}
Expand All @@ -38,7 +38,7 @@ steps:
container: alpine:3
`

_, err := ParseBatchSpec([]byte(spec), featureFlags{})
_, err := ParseBatchSpec([]byte(spec), FeatureFlags{})
if err == nil {
t.Fatal("no error returned")
}
Expand Down Expand Up @@ -71,7 +71,7 @@ changesetTemplate:
published: false
`

_, err := ParseBatchSpec([]byte(spec), featureFlags{})
_, err := ParseBatchSpec([]byte(spec), FeatureFlags{})
if err == nil {
t.Fatal("no error returned")
}
Expand Down
Loading