Skip to content

Commit 8c0f438

Browse files
authored
Validate spec contains changesetTemplate if steps are set (#407)
This fixes #406 and #404 by making sure that a `changesetTemplate` is defined when `steps` are set. (Since we allow using only `importChangesets` in a campaign spec, we need to make this conditional on whether steps are set or not)
1 parent 10672e8 commit 8c0f438

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ All notable changes to `src-cli` are documented in this file.
2323

2424
- Two race conditions in the terminal UI of `src campaign [apply|preview]` have been fixed. [#399](https://github.com/sourcegraph/src-cli/pull/399)
2525
- A regression caused repositories on unsupported code host to not be skipped by `src campaign [apply|preview]`, regardless of whether `-allow-unsupported` was set or not. [#403](https://github.com/sourcegraph/src-cli/pull/403)
26+
- Previously `src campaign [apply|preview]` would crash when executing a campaign spec that contained `steps` but no `changesetTemplate`. [#406](https://github.com/sourcegraph/src-cli/issues/406)
2627

2728
### Removed
2829

internal/campaigns/campaign_spec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func ParseCampaignSpec(data []byte, features featureFlags) (*CampaignSpec, error
101101
}
102102
}
103103

104+
if len(spec.Steps) != 0 && spec.ChangesetTemplate == nil {
105+
errs = multierror.Append(errs, errors.New("campaign spec includes steps but no changesetTemplate"))
106+
}
107+
104108
if spec.TransformChanges != nil && !features.allowtransformChanges {
105109
errs = multierror.Append(errs, errors.New("campaign spec includes transformChanges, which is not supported in this Sourcegraph version"))
106110
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package campaigns
2+
3+
import "testing"
4+
5+
func TestParseCampaignSpec(t *testing.T) {
6+
t.Run("valid", func(t *testing.T) {
7+
const spec = `
8+
name: hello-world
9+
description: Add Hello World to READMEs
10+
on:
11+
- repositoriesMatchingQuery: file:README.md
12+
steps:
13+
- run: echo Hello World | tee -a $(find -name README.md)
14+
container: alpine:3
15+
changesetTemplate:
16+
title: Hello World
17+
body: My first campaign!
18+
branch: hello-world
19+
commit:
20+
message: Append Hello World to all README.md files
21+
published: false
22+
`
23+
24+
_, err := ParseCampaignSpec([]byte(spec), featureFlags{})
25+
if err != nil {
26+
t.Fatalf("parsing valid spec returned error: %s", err)
27+
}
28+
})
29+
30+
t.Run("missing changesetTemplate", func(t *testing.T) {
31+
const spec = `
32+
name: hello-world
33+
description: Add Hello World to READMEs
34+
on:
35+
- repositoriesMatchingQuery: file:README.md
36+
steps:
37+
- run: echo Hello World | tee -a $(find -name README.md)
38+
container: alpine:3
39+
`
40+
41+
_, err := ParseCampaignSpec([]byte(spec), featureFlags{})
42+
if err == nil {
43+
t.Fatal("no error returned")
44+
}
45+
46+
wantErr := `1 error occurred:
47+
* campaign spec includes steps but no changesetTemplate
48+
49+
`
50+
haveErr := err.Error()
51+
if haveErr != wantErr {
52+
t.Fatalf("wrong error. want=%q, have=%q", wantErr, haveErr)
53+
}
54+
})
55+
}

0 commit comments

Comments
 (0)