Skip to content

Commit 251374e

Browse files
authored
More user-friendly error message if batch change name is invalid (#498)
* More user-friendly error message if batch change name is invalid This fixes https://github.com/sourcegraph/sourcegraph/issues/16955. * Add regression test for user-friendly error message
1 parent e8c1a0a commit 251374e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

internal/batches/batch_spec.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package batches
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/gobwas/glob"
78
"github.com/hashicorp/go-multierror"
@@ -107,6 +108,21 @@ type Group struct {
107108
func ParseBatchSpec(data []byte, features featureFlags) (*BatchSpec, error) {
108109
var spec BatchSpec
109110
if err := yaml.UnmarshalValidate(schema.BatchSpecJSON, data, &spec); err != nil {
111+
if multiErr, ok := err.(*multierror.Error); ok {
112+
var newMultiError *multierror.Error
113+
114+
for _, e := range multiErr.Errors {
115+
// In case of `name` we try to make the error message more user-friendly.
116+
if strings.Contains(e.Error(), "name: Does not match pattern") {
117+
newMultiError = multierror.Append(newMultiError, fmt.Errorf("The batch change name can only contain word characters, dots and dashes. No whitespace or newlines allowed."))
118+
} else {
119+
newMultiError = multierror.Append(newMultiError, e)
120+
}
121+
}
122+
123+
return nil, newMultiError.ErrorOrNil()
124+
}
125+
110126
return nil, err
111127
}
112128

internal/batches/batch_spec_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ steps:
4646
wantErr := `1 error occurred:
4747
* batch spec includes steps but no changesetTemplate
4848
49+
`
50+
haveErr := err.Error()
51+
if haveErr != wantErr {
52+
t.Fatalf("wrong error. want=%q, have=%q", wantErr, haveErr)
53+
}
54+
})
55+
56+
t.Run("invalid batch change name", func(t *testing.T) {
57+
const spec = `
58+
name: this name is invalid cause it contains whitespace
59+
description: Add Hello World to READMEs
60+
on:
61+
- repositoriesMatchingQuery: file:README.md
62+
steps:
63+
- run: echo Hello World | tee -a $(find -name README.md)
64+
container: alpine:3
65+
changesetTemplate:
66+
title: Hello World
67+
body: My first batch change!
68+
branch: hello-world
69+
commit:
70+
message: Append Hello World to all README.md files
71+
published: false
72+
`
73+
74+
_, err := ParseBatchSpec([]byte(spec), featureFlags{})
75+
if err == nil {
76+
t.Fatal("no error returned")
77+
}
78+
79+
// We expect this error to be user-friendly, which is why we test for
80+
// it specifically here.
81+
wantErr := `1 error occurred:
82+
* The batch change name can only contain word characters, dots and dashes. No whitespace or newlines allowed.
83+
4984
`
5085
haveErr := err.Error()
5186
if haveErr != wantErr {

0 commit comments

Comments
 (0)