Skip to content

Commit 175628e

Browse files
fix: batches now respects skip-errors for version check (#1135)
* fix: batches now respects skip-errors for version check Co-authored-by: Michael Bahr <[email protected]>
1 parent 20a3a02 commit 175628e

File tree

7 files changed

+74
-27
lines changed

7 files changed

+74
-27
lines changed

cmd/src/batch_common.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
cliLog "log"
89
"os"
910
"os/exec"
1011
"os/signal"
@@ -48,6 +49,7 @@ type batchExecutionFlags struct {
4849
api *api.Flags
4950
clearCache bool
5051
namespace string
52+
skipErrors bool
5153
}
5254

5355
func newBatchExecutionFlags(flagSet *flag.FlagSet) *batchExecutionFlags {
@@ -63,6 +65,10 @@ func newBatchExecutionFlags(flagSet *flag.FlagSet) *batchExecutionFlags {
6365
&bef.clearCache, "clear-cache", false,
6466
"If true, clears the execution cache and executes all steps anew.",
6567
)
68+
flagSet.BoolVar(
69+
&bef.skipErrors, "skip-errors", false,
70+
"If true, errors encountered won't stop the program, but only log them.",
71+
)
6672
flagSet.BoolVar(
6773
&bef.allowIgnored, "force-override-ignore", false,
6874
"Do not ignore repositories that have a .batchignore file.",
@@ -146,11 +152,6 @@ func newBatchExecuteFlags(flagSet *flag.FlagSet, cacheDir, tempDir string) *batc
146152
"If true, deletes downloaded repository archives after executing batch spec steps. Note that only the archives related to the actual repositories matched by the batch spec will be cleaned up, and clean up will not occur if src exits unexpectedly.",
147153
)
148154

149-
flagSet.BoolVar(
150-
&caf.skipErrors, "skip-errors", false,
151-
"If true, errors encountered while executing steps in a repository won't stop the execution of the batch spec but only cause that repository to be skipped.",
152-
)
153-
154155
flagSet.StringVar(
155156
&caf.workspace, "workspace", "auto",
156157
`Workspace mode to use ("auto", "bind", or "volume")`,
@@ -290,7 +291,7 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
290291
Client: opts.client,
291292
})
292293

293-
lr, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
294+
lr, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, opts.flags.skipErrors)
294295
if err != nil {
295296
return err
296297
}
@@ -302,8 +303,12 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
302303

303304
imageCache := docker.NewImageCache()
304305

305-
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
306-
return err
306+
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
307+
if !opts.flags.skipErrors {
308+
return err
309+
} else {
310+
cliLog.Printf("WARNING: %s", err)
311+
}
307312
}
308313

309314
if err := checkExecutable("git", "version"); err != nil {
@@ -655,7 +660,7 @@ func getBatchParallelism(ctx context.Context, flag int) (int, error) {
655660
return docker.NCPU(ctx)
656661
}
657662

658-
func validateSourcegraphVersionConstraint(ctx context.Context, ffs *batches.FeatureFlags) error {
663+
func validateSourcegraphVersionConstraint(ffs *batches.FeatureFlags) error {
659664
if ffs.Sourcegraph40 {
660665
return nil
661666
}

cmd/src/batch_new.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
cliLog "log"
78

89
"github.com/sourcegraph/src-cli/internal/api"
910
"github.com/sourcegraph/src-cli/internal/batches/service"
@@ -30,7 +31,12 @@ Examples:
3031
apiFlags := api.NewFlags(flagSet)
3132

3233
var (
33-
fileFlag = flagSet.String("f", "batch.yaml", "The name of the batch spec file to create.")
34+
fileFlag = flagSet.String("f", "batch.yaml", "The name of the batch spec file to create.")
35+
skipErrors bool
36+
)
37+
flagSet.BoolVar(
38+
&skipErrors, "skip-errors", false,
39+
"If true, errors encountered won't stop the program, but only log them.",
3440
)
3541

3642
handler := func(args []string) error {
@@ -48,13 +54,17 @@ Examples:
4854
Client: cfg.apiClient(apiFlags, flagSet.Output()),
4955
})
5056

51-
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
57+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, skipErrors)
5258
if err != nil {
5359
return err
5460
}
5561

56-
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
57-
return err
62+
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
63+
if !skipErrors {
64+
return err
65+
} else {
66+
cliLog.Printf("WARNING: %s", err)
67+
}
5868
}
5969

6070
if err := svc.GenerateExampleSpec(ctx, *fileFlag); err != nil {

cmd/src/batch_remote.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
cliLog "log"
78
"strings"
89
"time"
910

@@ -52,13 +53,17 @@ Examples:
5253
Client: cfg.apiClient(flags.api, flagSet.Output()),
5354
})
5455

55-
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
56+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, flags.skipErrors)
5657
if err != nil {
5758
return err
5859
}
5960

60-
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
61-
return err
61+
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
62+
if !flags.skipErrors {
63+
return err
64+
} else {
65+
cliLog.Printf("WARNING: %s", err)
66+
}
6267
}
6368

6469
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})

cmd/src/batch_repositories.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
cliLog "log"
78

89
"github.com/sourcegraph/sourcegraph/lib/errors"
910
"github.com/sourcegraph/sourcegraph/lib/output"
@@ -42,6 +43,7 @@ Examples:
4243
var (
4344
allowUnsupported bool
4445
allowIgnored bool
46+
skipErrors bool
4547
)
4648
flagSet.BoolVar(
4749
&allowUnsupported, "allow-unsupported", false,
@@ -51,6 +53,10 @@ Examples:
5153
&allowIgnored, "force-override-ignore", false,
5254
"Do not ignore repositories that have a .batchignore file.",
5355
)
56+
flagSet.BoolVar(
57+
&skipErrors, "skip-errors", false,
58+
"If true, errors encountered won't stop the program, but only log them.",
59+
)
5460

5561
handler := func(args []string) error {
5662
if err := flagSet.Parse(args); err != nil {
@@ -69,13 +75,17 @@ Examples:
6975
Client: client,
7076
})
7177

72-
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
78+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, skipErrors)
7379
if err != nil {
7480
return err
7581
}
7682

77-
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
78-
return err
83+
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
84+
if !skipErrors {
85+
return err
86+
} else {
87+
cliLog.Printf("WARNING: %s", err)
88+
}
7989
}
8090

8191
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})

cmd/src/batch_validate.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
cliLog "log"
78

89
"github.com/sourcegraph/sourcegraph/lib/output"
910

@@ -36,6 +37,7 @@ Examples:
3637
var (
3738
allowUnsupported bool
3839
allowIgnored bool
40+
skipErrors bool
3941
)
4042
flagSet.BoolVar(
4143
&allowUnsupported, "allow-unsupported", false,
@@ -45,6 +47,10 @@ Examples:
4547
&allowIgnored, "force-override-ignore", false,
4648
"Do not ignore repositories that have a .batchignore file.",
4749
)
50+
flagSet.BoolVar(
51+
&skipErrors, "skip-errors", false,
52+
"If true, errors encountered won't stop the program, but only log them.",
53+
)
4854

4955
handler := func(args []string) error {
5056
ctx := context.Background()
@@ -63,14 +69,18 @@ Examples:
6369
Client: cfg.apiClient(apiFlags, flagSet.Output()),
6470
})
6571

66-
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx)
72+
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, skipErrors)
6773
if err != nil {
6874
return err
6975
}
7076

71-
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
72-
ui.ExecutionError(err)
73-
return err
77+
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
78+
if !skipErrors {
79+
ui.ExecutionError(err)
80+
return err
81+
} else {
82+
cliLog.Printf("WARNING: %s", err)
83+
}
7484
}
7585

7686
file, err := getBatchSpecFile(flagSet, fileFlag)

internal/batches/features.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package batches
22

33
import (
4+
"fmt"
5+
"log"
6+
47
"github.com/sourcegraph/sourcegraph/lib/api"
58
"github.com/sourcegraph/sourcegraph/lib/errors"
69
)
@@ -12,7 +15,7 @@ type FeatureFlags struct {
1215
BinaryDiffs bool
1316
}
1417

15-
func (ff *FeatureFlags) SetFromVersion(version string) error {
18+
func (ff *FeatureFlags) SetFromVersion(version string, skipErrors bool) error {
1619
for _, feature := range []struct {
1720
flag *bool
1821
constraint string
@@ -32,7 +35,11 @@ func (ff *FeatureFlags) SetFromVersion(version string) error {
3235
} {
3336
value, err := api.CheckSourcegraphVersion(version, feature.constraint, feature.minDate)
3437
if err != nil {
35-
return errors.Wrap(err, "failed to check version returned by Sourcegraph")
38+
if skipErrors {
39+
log.Printf("failed to check version returned by Sourcegraph: %s. Assuming no feature flags.", version)
40+
} else {
41+
return errors.Wrap(err, fmt.Sprintf("failed to check version returned by Sourcegraph: %s", version))
42+
}
3643
}
3744
*feature.flag = value
3845
}

internal/batches/service/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (svc *Service) getSourcegraphVersionAndMaxChangesetsCount(ctx context.Conte
7676

7777
// DetermineLicenseAndFeatureFlags returns the enabled features and license restrictions
7878
// configured for the Sourcegraph instance.
79-
func (svc *Service) DetermineLicenseAndFeatureFlags(ctx context.Context) (*batches.LicenseRestrictions, *batches.FeatureFlags, error) {
79+
func (svc *Service) DetermineLicenseAndFeatureFlags(ctx context.Context, skipErrors bool) (*batches.LicenseRestrictions, *batches.FeatureFlags, error) {
8080
version, mc, err := svc.getSourcegraphVersionAndMaxChangesetsCount(ctx)
8181
if err != nil {
8282
return nil, nil, errors.Wrap(err, "failed to query Sourcegraph version and license info for instance")
@@ -87,7 +87,7 @@ func (svc *Service) DetermineLicenseAndFeatureFlags(ctx context.Context) (*batch
8787
}
8888

8989
ffs := &batches.FeatureFlags{}
90-
return lr, ffs, ffs.SetFromVersion(version)
90+
return lr, ffs, ffs.SetFromVersion(version, skipErrors)
9191

9292
}
9393

0 commit comments

Comments
 (0)