Skip to content
Snippets Groups Projects
Commit d0d8ef0c authored by Joe Burnett's avatar Joe Burnett
Browse files

Merge branch 'jburnett-steps-dir' into 'main'

Accept dir and file after /-/

Closes #16

See merge request !169
parents 893adafa 443d6a3c
No related branches found
No related tags found
1 merge request!169Accept dir and file after /-/
Pipeline #1620249313 passed
......@@ -45,7 +45,10 @@ run-action-e2e:
DOCKER_HOST: tcp://docker:2375
run:
- name: say_hi
step: gitlab.com/components/action-runner@main
step:
git:
url: gitlab.com/components/action-runner
rev: main
inputs:
action: mikefarah/yq@master
action_runner_image: registry.gitlab.com/components/action-runner:v0
......
## v0.3.0 (2025-01-06)
- Accept dir and file after /-/. See !169.
### Breaking Changes:
- Steps must be in the `steps` folder in the repository unless the
expanded step syntax is used. See !gitlab/177038 for documentation
update.
## v0.2.1 (2024-12-13)
- Initial working dir CI_PROJECT_DIR in CI. See !165.
......
......@@ -306,7 +306,12 @@ func (s *Step) compileScriptKeywordToStep() error {
if len(s.Inputs) != 0 {
return fmt.Errorf("the `script` keyword cannot be used with `inputs`")
}
s.Step = "https://gitlab.com/components/script@main"
s.Step = &Reference{
Git: GitReference{
Url: "https://gitlab.com/components/script",
Rev: "main",
},
}
s.Inputs = map[string]any{
"script": s.Script,
}
......@@ -324,7 +329,12 @@ func (s *Step) compileActionKeywordToStep() error {
if s.Script != nil && *s.Script != "" {
return fmt.Errorf("the `action` keyword cannot be used with the `script` keyword")
}
s.Step = "https://gitlab.com/components/action-runner@main"
s.Step = &Reference{
Git: GitReference{
Url: "https://gitlab.com/components/action-runner",
Rev: "main",
},
}
s.Inputs = map[string]any{
"action": s.Action,
"inputs": s.Inputs,
......@@ -377,7 +387,7 @@ func (sr shortReference) compile() (*proto.Step_Reference, error) {
}
func (sr shortReference) compileLocal() (*proto.Step_Reference, error) {
path, filename := pathFilename((*string)(&sr))
path, filename := pathFilename(string(sr))
return &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_local,
Path: path,
......@@ -390,54 +400,55 @@ func (sr shortReference) compileRemote() (*proto.Step_Reference, error) {
if len(parts) < 2 {
return nil, fmt.Errorf("expecting url@rev. got %q", sr)
}
url := strings.Join(parts[0:len(parts)-1], "@")
rest := strings.Join(parts[0:len(parts)-1], "@")
rev := parts[len(parts)-1]
url, err := defaultHTTPS(url)
if err != nil {
return nil, fmt.Errorf("parsing reference %q: %w", string(sr), err)
}
url, rest, _ := strings.Cut(rest, "/-/")
url = defaultHTTPS(url)
path, filename := pathFilename(rest)
path = append([]string{"steps"}, path...)
return &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: url,
Version: rev,
Filename: "step.yml",
Path: path,
Filename: filename,
}, nil
}
func (r *Reference) compile() (*proto.Step_Reference, error) {
url, err := defaultHTTPS(r.Git.Url)
if err != nil {
return nil, fmt.Errorf("parsing url as url: %w", err)
}
path, filename := pathFilename(r.Git.Dir)
version := ""
version = r.Git.Rev
return &proto.Step_Reference{
url := defaultHTTPS(r.Git.Url)
s := &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: url,
Path: path,
Filename: filename,
Version: version,
}, nil
Filename: "step.yml",
Version: r.Git.Rev,
}
if r.Git.Dir != nil {
s.Path = strings.Split(*r.Git.Dir, "/")
}
if r.Git.File != nil {
s.Filename = *r.Git.File
}
return s, nil
}
func defaultHTTPS(stepUrl string) (string, error) {
func defaultHTTPS(stepUrl string) string {
if strings.HasPrefix(stepUrl, "http://") || strings.HasPrefix(stepUrl, "https://") {
return stepUrl, nil
return stepUrl
}
return "https://" + stepUrl, nil
return "https://" + stepUrl
}
func pathFilename(pathStr *string) (path []string, filename string) {
func pathFilename(pathStr string) (path []string, filename string) {
filename = "step.yml"
if pathStr == nil {
if pathStr == "" {
return nil, filename
}
if *pathStr == "" {
return nil, filename
path = strings.Split(pathStr, "/")
if strings.HasSuffix(path[len(path)-1], ".yml") {
filename = path[len(path)-1]
path = path[:len(path)-1]
}
path = strings.Split(*pathStr, "/")
return path, filename
}
......
......@@ -194,6 +194,7 @@ steps:
protocol: git
url: "https://gitlab.com/components/foo"
version: v1
path: [ "steps" ]
filename: step.yml
env:
JOB_ID: ${{job.id}}
......@@ -266,6 +267,7 @@ steps:
protocol: git
url: "https://gitlab.com/components/foo"
version: v1
path: [ "steps" ]
filename: step.yml
delegate: delegate_me
`,
......@@ -329,7 +331,7 @@ func TestReferenceCompiler(t *testing.T) {
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: nil,
Path: []string{"steps"},
Filename: "step.yml",
Version: "v1",
},
......@@ -338,17 +340,44 @@ func TestReferenceCompiler(t *testing.T) {
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: nil,
Path: []string{"steps"},
Filename: "step.yml",
Version: "v1",
},
}, {
step: "step: gitlab.com/components/script/-/sub-step@v1",
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: []string{"steps", "sub-step"},
Filename: "step.yml",
Version: "v1",
},
}, {
step: "step: gitlab.com/components/script/-/my-step.yml@v1",
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: []string{"steps"},
Filename: "my-step.yml",
Version: "v1",
},
}, {
step: "step: gitlab.com/components/script/-/sub-step/my-step.yml@v1",
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: []string{"steps", "sub-step"},
Filename: "my-step.yml",
Version: "v1",
},
}, {
step: `
step:
git:
url: gitlab.com/components/script
dir: bash
rev: v1
url: gitlab.com/components/script
dir: bash
rev: v1
`,
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
......@@ -361,7 +390,23 @@ step:
step: `
step:
git:
url: http://bad.idea.com/my-step
url: gitlab.com/components/script
dir: bash
rev: v1
file: my-step.yml
`,
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "https://gitlab.com/components/script",
Path: []string{"bash"},
Filename: "my-step.yml",
Version: "v1",
},
}, {
step: `
step:
git:
url: http://bad.idea.com/my-step
rev: v1
`,
want: &proto.Step_Reference{
......@@ -375,7 +420,7 @@ step:
step: `
step:
git:
url: gitlab.com/components/script
url: gitlab.com/components/script
rev: v2.1
`,
want: &proto.Step_Reference{
......@@ -389,7 +434,7 @@ step:
step: `
step:
git:
url: gitlab.com/components/script
url: gitlab.com/components/script
rev: 20e9c40c
`,
want: &proto.Step_Reference{
......@@ -403,7 +448,7 @@ step:
step: `
step:
git:
url: gitlab.com/components/script
url: gitlab.com/components/script
rev: 20e9c40c9213f2a044e4a81906956a779af3da4b
`,
want: &proto.Step_Reference{
......@@ -420,7 +465,7 @@ step: http://gitlab-ci-token:ABCDEF@gitlab.com/josephburnett/hello-private-repo.
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "http://gitlab-ci-token:ABCDEF@gitlab.com/josephburnett/hello-private-repo.git",
Path: nil,
Path: []string{"steps"},
Filename: "step.yml",
Version: "main",
},
......@@ -431,7 +476,7 @@ step: http://gitlab-ci-token:${{ job.CI_JOB_TOKEN }}@gitlab.com/josephburnett/he
want: &proto.Step_Reference{
Protocol: proto.StepReferenceProtocol_git,
Url: "http://gitlab-ci-token:${{ job.CI_JOB_TOKEN }}@gitlab.com/josephburnett/hello-private-repo.git",
Path: nil,
Path: []string{"steps"},
Filename: "step.yml",
Version: "main",
},
......
......@@ -45,6 +45,9 @@ type GitReference struct {
// Url corresponds to the JSON schema field "url".
Url string `json:"url" yaml:"url" mapstructure:"url"`
// File corresponds to the JSON schema field "file".
File *string `json:"file,omitempty" yaml:"file,omitempty" mapstructure:"file,omitempty"`
}
// UnmarshalJSON implements json.Unmarshaler.
......
......@@ -178,6 +178,9 @@
},
"rev": {
"type": "string"
},
"file": {
"type": "string"
}
}
}
......
......@@ -60,6 +60,7 @@ step:
url: gitlab.com/my-org/my-step
rev: v1
dir: sub-dir
file: my-step.yml
`,
}, {
name: "remote nested step with additional properties",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment