Skip to content

Commit 1aee1ca

Browse files
mrnuggetChris Pine
andauthored
Templating, lower_case variables and files (#361)
* Add support for templated campaign specs * Make search results accessible in campaign spec templates * Remove debug log * Remove shortcuts in templates for now * Report rendered steps * Remove unused parameter * Fix build * Add lower-case aliases and stdout/stderr to templates * Add test to check that empty context works * Nil-safe templates * Mount templated files into src-cli * WIP * Undo local file mounting * Allow template vars in step env * Clean up code a little bit * Add changelog entry * Fix test * Omit step.Files from CampaignSpec if empty for backwards-compatibility * Update CHANGELOG.md Co-authored-by: Chris Pine <[email protected]> * Clean up temp file code * Return slices of strings instead of whitespace-separated string * Incorporate Adam's feedback Co-authored-by: Chris Pine <[email protected]>
1 parent a7b1ce5 commit 1aee1ca

File tree

10 files changed

+504
-24
lines changed

10 files changed

+504
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.
1313

1414
### Added
1515

16+
- EXPERIMENTAL: Templated campaign specs and file mounting. The campaign specs evaluated by `src campaign [preview|apply]` can now include template variables in `steps.run`, `steps.env`, and the new `steps.files` property, which allows users to create files inside the container in which the step is executed. The feature is marked as EXPERIMENTAL because it might change in the near future until we deem it non-experimental. See [#361](https://github.com/sourcegraph/src-cli/pull/361) for details.
17+
1618
### Changed
1719

1820
### Fixed
File renamed without changes.

internal/campaigns/campaign_spec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Step struct {
6666
Run string `json:"run,omitempty" yaml:"run"`
6767
Container string `json:"container,omitempty" yaml:"container"`
6868
Env map[string]string `json:"env,omitempty" yaml:"env"`
69+
Files map[string]string `json:"files,omitempty" yaml:"files,omitempty"`
6970

7071
image string
7172
}

internal/campaigns/executor_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ func TestExecutor_Integration(t *testing.T) {
9898
executorTimeout: 100 * time.Millisecond,
9999
wantErrInclude: "execution in github.com/sourcegraph/src-cli failed: Timeout reached. Execution took longer than 100ms.",
100100
},
101+
{
102+
name: "templated",
103+
repos: []*graphql.Repository{srcCLIRepo},
104+
archives: []mockRepoArchive{
105+
{repo: srcCLIRepo, files: map[string]string{
106+
"README.md": "# Welcome to the README\n",
107+
"main.go": "package main\n\nfunc main() {\n\tfmt.Println( \"Hello World\")\n}\n",
108+
}},
109+
},
110+
steps: []Step{
111+
{Run: `go fmt main.go`, Container: "doesntmatter:13"},
112+
{Run: `touch modified-${{ join previous_step.modified_files " " }}.md`, Container: "alpine:13"},
113+
{Run: `touch added-${{ join previous_step.added_files " " }}`, Container: "alpine:13"},
114+
},
115+
wantFilesChanged: map[string][]string{
116+
srcCLIRepo.ID: []string{"main.go", "modified-main.go.md", "added-modified-main.go.md"},
117+
},
118+
},
101119
}
102120

103121
for _, tc := range tests {
@@ -170,11 +188,16 @@ func TestExecutor_Integration(t *testing.T) {
170188

171189
diffsByName := map[string]*diff.FileDiff{}
172190
for _, fd := range fileDiffs {
173-
diffsByName[fd.OrigName] = fd
191+
if fd.NewName == "/dev/null" {
192+
diffsByName[fd.OrigName] = fd
193+
} else {
194+
diffsByName[fd.NewName] = fd
195+
}
174196
}
197+
175198
for _, file := range wantFiles {
176199
if _, ok := diffsByName[file]; !ok {
177-
t.Errorf("%s was not changed", file)
200+
t.Errorf("%s was not changed (diffsByName=%#v)", file, diffsByName)
178201
}
179202
}
180203
}

internal/campaigns/graphql/repository.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package graphql
22

3-
import "strings"
3+
import (
4+
"sort"
5+
"strings"
6+
)
47

58
const RepositoryFieldsFragment = `
69
fragment repositoryFields on Repository {
@@ -30,6 +33,8 @@ type Repository struct {
3033
URL string
3134
ExternalRepository struct{ ServiceType string }
3235
DefaultBranch *Branch
36+
37+
FileMatches map[string]bool
3338
}
3439

3540
func (r *Repository) BaseRef() string {
@@ -43,3 +48,16 @@ func (r *Repository) Rev() string {
4348
func (r *Repository) Slug() string {
4449
return strings.ReplaceAll(r.Name, "/", "-")
4550
}
51+
52+
func (r *Repository) SearchResultPaths() (list fileMatchPathList) {
53+
var files []string
54+
for f := range r.FileMatches {
55+
files = append(files, f)
56+
}
57+
sort.Strings(files)
58+
return fileMatchPathList(files)
59+
}
60+
61+
type fileMatchPathList []string
62+
63+
func (f fileMatchPathList) String() string { return strings.Join(f, " ") }

0 commit comments

Comments
 (0)