8000 feat(action/GitClone): depth and submodules opts (#3171) · ovh/cds@f8aadc9 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit f8aadc9

Browse files
yesnaultfsamin
authored andcommitted
feat(action/GitClone): depth and submodules opts (#3171)
1 parent 44a9a0e commit f8aadc9

File tree

12 files changed

+121
-29
lines changed

12 files changed

+121
-29
lines changed

engine/api/action/builtin.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ The public key have to be granted on your repository`,
109109
Value: "{{.cds.workspace}}",
110110
Type: sdk.StringParameter,
111111
})
112+
gitclone.Parameter(sdk.Parameter{
113+
Name: "depth",
114+
Description: "gitClone use a depth of 50 by default. You can remove --depth with the value 'false'",
115+
Value: "",
116+
Type: sdk.StringParameter,
117+
Advanced: true,
118+
})
119+
gitclone.Parameter(sdk.Parameter{
120+
Name: "submodules",
121+
Description: "gitClone clones submodules by default, you can set 'false' to avoid this",
122+
Value: "true",
123+
Type: sdk.BooleanParameter,
124+
Advanced: true,
125+
})
112126
gitclone.Requirement("git", sdk.BinaryRequirement, "git")
113127

114128
if err := checkBuiltinAction(db, gitclone); err != nil {
@@ -119,7 +133,12 @@ The public key have to be granted on your repository`,
119133
checkoutApplication := sdk.NewAction(sdk.CheckoutApplicationAction)
120134
checkoutApplication.Type = sdk.BuiltinAction
121135
checkoutApplication.Description = `CDS Builtin Action.
122-
Checkout a repository into a new directory.`
136+
Checkout a repository into a new directory.
137+
138+
This action use the configuration from application to git clone the repository.
139+
The clone will be done with a depth of 50 and with submodules.
140+
If you want to modify theses options, you have to use gitClone action.
141+
`
123142

124143
checkoutApplication.Parameter(sdk.Parameter{
125144
Name: "directory",

engine/sql/120_git_clone.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +migrate Up
2+
3+
INSERT into action_parameter (action_id, name, description, type, value, advanced) values((select id from action where name = 'GitClone'), 'depth', 'gitClone use a depth of 50 by default. You can remove --depth with the value ''false''', 'string', '', true);
4+
INSERT into action_parameter (action_id, name, description, type, value, advanced) values((select id from action where name = 'GitClone'), 'submodules', 'gitClone clones submodules by default, you can set ''false'' to avoid this', 'boolean', 'true', true);
5+
6+
-- +migrate Down
7+
8+
DELETE from action_parameter where name = 'depth' and action_id = (select id from action where name = 'GitClone');
9+
DELETE from action_parameter where name = 'submodules' and action_id = (select id from action where name = 'GitClone');

engine/worker/builtin_checkout_application.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func runCheckoutApplication(w *currentWorker) BuiltInAction {
3333
var opts = &git.CloneOpts{
3434
Recursive: true,
3535
NoStrictHostKeyChecking: true,
36+
Depth: 50,
3637
}
3738
if branch != nil {
3839
opts.Branch = branch.Value

engine/worker/builtin_gitclone.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"regexp"
8+
"strconv"
89
"strings"
910

1011
"github.com/blang/semver"
@@ -25,6 +26,8 @@ func runGitClone(w *currentWorker) BuiltInAction {
2526
defaultBranch := sdk.ParameterValue(*params, "git.default_branch")
2627
commit := sdk.ParameterFind(&a.Parameters, "commit")
2728
directory := sdk.ParameterFind(&a.Parameters, "directory")
29+
depth := sdk.ParameterFind(&a.Parameters, "depth")
30+
submodules := sdk.ParameterFind(&a.Parameters, "submodules")
2831

2932
if url == nil {
3033
res := sdk.Result{
@@ -128,12 +131,28 @@ func runGitClone(w *currentWorker) BuiltInAction {
128131
var opts = &git.CloneOpts{
129132
Recursive: true,
130133
NoStrictHostKeyChecking: true,
134+
Depth: 50,
131135
}
132136
if branch != nil {
133137
opts.Branch = branch.Value
134138
} else {
135139
opts.SingleBranch = true
136140
}
141+
if depth != nil {
142+
if depth.Value == "false" {
143+
opts.Depth = 0
144+
} else if depth.Value != "" {
145+
depthVal, errConv := strconv.Atoi(depth.Value)
146+
if errConv != nil {
147+
sendLog(fmt.Sprintf("invalid depth value. It must by empty, or false, or a numeric value. current value: %s", depth.Value))
148+
} else {
149+
opts.Depth = depthVal
150+
}
151+
}
152+
}
153+
if submodules != nil && submodules.Value == "false" {
154+
opts.Recursive = false
155+
}
137156

138157
// if there is no branch, check if there a defaultBranch
139158
if (opts.Branch == "" || opts.Branch == "{{.git.branch}}") && defaultBranch != "" {
@@ -168,7 +187,9 @@ func gitClone(w *currentWorker, params *[]sdk.Parameter, url string, dir string,
168187
git.LogFunc = log.Info
169188

170189
//Perform the git clone
171-
err := git.Clone(url, dir, auth, clone, output)
190+
userLogCommand, err := git.Clone(url, dir, auth, clone, output)
191+
192+
sendLog(userLogCommand)
172193

173194
//Send the logs
174195
if len(stdOut.Bytes()) > 0 {

sdk/exportentities/action.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func newSteps(a sdk.Action) []Step {
120120
if tag != nil {
121121
artifactUploadArgs["tag"] = tag.Value
122122
}
123-
124123
s["artifactUpload"] = artifactUploadArgs
125124
case sdk.GitCloneAction:
126125
gitCloneArgs := map[string]string{}
@@ -137,22 +136,29 @@ func newSteps(a sdk.Action) []Step {
137136
gitCloneArgs["directory"] = directory.Value
138137
}
139138
password := sdk.ParameterFind(&act.Parameters, "password")
140-
if password != nil {
139+
if password != nil && password.Value != "" {
141140
gitCloneArgs["password"] = password.Value
142141
}
143142
privateKey := sdk.ParameterFind(&act.Parameters, "privateKey")
144-
if privateKey != nil {
143+
if privateKey != nil && privateKey.Value != "" {
145144
gitCloneArgs["privateKey"] = privateKey.Value
146145
}
147146
url := sdk.ParameterFind(&act.Parameters, "url")
148147
if url != nil {
149148
gitCloneArgs["url"] = url.Value
150149
}
151150
user := sdk.ParameterFind(&act.Parameters, "user")
152-
if user != nil {
151+
if user != nil && user.Value != "" {
153152
gitCloneArgs["user"] = user.Value
154153
}
155-
154+
depth := sdk.ParameterFind(&act.Parameters, "depth")
155+
if depth != nil && depth.Value != "" && depth.Value != "50" {
156+
gitCloneArgs["depth"] = depth.Value
157+
}
158+
submodules := sdk.ParameterFind(&act.Parameters, "submodules")
159+
if submodules != nil && submodules.Value == "false" {
160+
gitCloneArgs["submodules"] = submodules.Value
161+
}
156162
s["gitClone"] = gitCloneArgs
157163
case sdk.JUnitAction:
158164
path := sdk.ParameterFind(&act.Parameters, "path")

sdk/exportentities/pipeline_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ steps:
551551
privateKey: '{{.cds.app.key}}'
552552
url: '{{.git.http_url}}'
553553
user: ""
554+
depth: '12'
554555
- artifactUpload:
555556
path: arti.tar.gz
556557
tag: '{{.cds.version}}'
@@ -567,7 +568,7 @@ steps:
567568
assert.Equal(t, sdk.GitCloneAction, p.Stages[0].Jobs[0].Action.Actions[0].Name)
568569
assert.Equal(t, sdk.ArtifactUpload, p.Stages[0].Jobs[0].Action.Actions[1].Name)
569570
assert.Equal(t, sdk.ArtifactUpload, p.Stages[0].Jobs[0].Action.Actions[2].Name)
570-
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[0].Parameters, 7)
571+
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[0].Parameters, 8)
571572
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[1].Parameters, 2)
572573
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[2].Parameters, 1)
573574
}

sdk/vcs/git/git_clone.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type CloneOpts struct {
1919
}
2020

2121
// Clone make a git clone
22-
func Clone(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) error {
22+
func Clone(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) (string, error) {
2323
if verbose {
2424
t1 := time.Now()
2525
if opts != nil && opts.CheckoutCommit != "" {
@@ -31,14 +31,15 @@ func Clone(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *Ou
3131
var commands []cmd
3232
repoURL, err := getRepoURL(repo, auth)
3333
if err != nil {
34-
return err
34+
return "", err
3535
}
3636

37-
commands = prepareGitCloneCommands(repoURL, path, opts)
38-
return runGitCommands(repo, commands, auth, output)
37+
var userLogCommand string
38+
userLogCommand, commands = prepareGitCloneCommands(repoURL, path, opts)
39+
return userLogCommand, runGitCommands(repo, commands, auth, output)
3940
}
4041

41-
func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
42+
func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) (string, cmds) {
4243
allCmd := []cmd{}
4344
gitcmd := cmd{
4445
cmd: "git",
@@ -63,15 +64,13 @@ func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
6364
} else if opts.SingleBranch {
6465
gitcmd.args = append(gitcmd.args, "--single-branch")
6566
}
66-
if !opts.SingleBranch && opts.Depth != 0 {
67-
gitcmd.args = append(gitcmd.args, "--no-single-branch")
68-
}
6967

7068
if opts.Recursive {
7169
gitcmd.args = append(gitcmd.args, "--recursive")
7270
}
7371
}
7472

73+
userLogCommand := "Executing: git " + strings.Join(gitcmd.args, " ") + " ... "
7574
gitcmd.args = append(gitcmd.args, repo)
7675

7776
if path != "" {
@@ -85,6 +84,7 @@ func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
8584
cmd: "git",
8685
args: []string{"reset", "--hard", opts.CheckoutCommit},
8786
}
87+
userLogCommand += "\n\rExecuting: git " + strings.Join(resetCmd.args, " ")
8888
//Locate the git reset cmd to the right directory
8989
if path == "" {
9090
t := strings.Split(repo, "/")
@@ -96,5 +96,5 @@ func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
9696
allCmd = append(allCmd, resetCmd)
9797
}
9898

99-
return cmds(allCmd)
99+
return userLogCommand, cmds(allCmd)
100100
}

sdk/vcs/git/git_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestClone(t *testing.T) {
4747
},
4848
}
4949
for _, tt := range tests {
50-
if err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
50+
if _, err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
5151
t.Errorf("%q. Clone() error = %v, wantErr %v", tt.name, err, tt.wantErr)
5252
}
5353
}
@@ -95,7 +95,7 @@ func Test_gitCloneOverHTTPS(t *testing.T) {
9595
Stderr: err,
9696
}
9797

98-
if err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
98+
if _, err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
9999
t.Errorf("%q. gitCloneOverHTTPS() error = %v, wantErr %v", tt.name, err, tt.wantErr)
100100
}
101101

@@ -148,7 +148,7 @@ func Test_gitCloneOverSSH(t *testing.T) {
148148
Stderr: err,
149149
}
150150

151-
if err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
151+
if _, err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
152152
t.Errorf("%q. gitCloneOverSSH() error = %v, wantErr %v", tt.name, err, tt.wantErr)
153153
}
154154

@@ -191,7 +191,7 @@ func Test_gitCommand(t *testing.T) {
191191
},
192192
},
193193
want: []string{
194-
"git clone --verbose --depth 1 --branch master --no-single-branch --recursive https://github.com/ovh/cds.git /tmp/Test_gitCommand-2",
194+
"git clone --verbose --depth 1 --branch master --recursive https://github.com/ovh/cds.git /tmp/Test_gitCommand-2",
195195
},
196196
},
197197
{
@@ -213,7 +213,7 @@ func Test_gitCommand(t *testing.T) {
213213
}
214214
for _, tt := range tests {
215215
os.RemoveAll(tt.args.path)
216-
if got := prepareGitCloneCommands(tt.args.repo, tt.args.path, tt.args.opts); !reflect.DeepEqual(got.Strings(), tt.want) {
216+
if _, got := prepareGitCloneCommands(tt.args.repo, tt.args.path, tt.args.opts); !reflect.DeepEqual(got.Strings(), tt.want) {
217217
t.Errorf("%q. gitCloneCommand() = %v, want %v", tt.name, got, tt.want)
218218
}
219219
}

tests/clictl_action.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ testcases:
88
- script: '[ -f ./tests/fixtures/action_disabled.yml ]'
99
assertions:
1010
- result.code ShouldEqual 0
11-
- name: test unknowned ation
11+
- name: test unknowned action
1212
steps:
13-
- script: {{.cds.build.cdsctl}} action delete --force foo
13+
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_unknowned
1414
assertions:
1515
- result.code ShouldEqual 0
1616
- result.systemout ShouldContainSubstring "action does not exist"
17-
- script: {{.cds.build.cdsctl}} action delete foo
17+
- script: {{.cds.build.cdsctl}} action delete CDS_TestIT_unknowned
1818
assertions:
1919
- result.code ShouldEqual 50
2020
- result.systemout ShouldContainSubstring "action does not exist"
2121
- name: prepare test
2222
steps:
2323
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_Enabled
2424
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_Disabled
25+
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_GitClone
2526
- name: action import
2627
steps:
2728
- script: {{.cds.build.cdsctl}} action import ./tests/fixtures/action_enabled.yml
@@ -32,12 +33,18 @@ testcases:
3233
assertions:
3334
- result.code ShouldEqual 0
3435
- result.systemout ShouldContainSubstring successfully
36+
- script: {{.cds.build.cdsctl}} action import ./tests/fixtures/action_git_clone.yml
37+
assertions:
38+
- result.code ShouldEqual 0
39+
- result.systemout ShouldContainSubstring successfully
3540
- name: action export
3641
steps:
3742
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Disabled > ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported
3843
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Enabled > ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported
44+
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_GitClone > ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported
3945
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled.yml
4046
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported ./tests/fixtures/action_enabled.yml
47+
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported ./tests/fixtures/action_git_clone.yml
4148
- name: action list
4249
steps:
4350
- script: {{.cds.build.cdsctl}} action list

tests/fixtures/action_git_clone.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: v1.0
2+
name: CDS_TestIT_GitClone
3+
parameters:
4+
name:
5+
type: string
6+
requirements:
7+
- binary: git
8+
steps:
9+
- gitClone:
10+
branch: '{{.git.branch}}'
11+
commit: '{{.git.hash}}'
12+
depth: "10"
13+
directory: '{{.cds.workspace}}'
14+
submodules: "false"
15+
url: '{{.git.url}}'
16+
- gitClone:
17+
branch: '{{.git.branch}}'
18+
commit: '{{.git.hash}}'
19+
directory: '{{.cds.workspace}}'
20+
url: '{{.git.url}}'
21+
- gitClone:
22+
branch: '{{.git.branch}}'
23+
commit: '{{.git.hash}}'
24+
depth: "10"
25+
directory: '{{.cds.workspace}}'
26+
url: '{{.git.url}}'
27+

0 commit comments

Comments
 (0)
0