Skip to content

Commit

Permalink
feat(action/GitClone): depth and submodules opts (#3171)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Aug 9, 2018
1 parent 44a9a0e commit f8aadc9
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 29 deletions.
21 changes: 20 additions & 1 deletion engine/api/action/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ The public key have to be granted on your repository`,
Value: "{{.cds.workspace}}",
Type: sdk.StringParameter,
})
gitclone.Parameter(sdk.Parameter{
Name: "depth",
Description: "gitClone use a depth of 50 by default. You can remove --depth with the value 'false'",
Value: "",
Type: sdk.StringParameter,
Advanced: true,
})
gitclone.Parameter(sdk.Parameter{
Name: "submodules",
Description: "gitClone clones submodules by default, you can set 'false' to avoid this",
Value: "true",
Type: sdk.BooleanParameter,
Advanced: true,
})
gitclone.Requirement("git", sdk.BinaryRequirement, "git")

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

checkoutApplication.Parameter(sdk.Parameter{
Name: "directory",
Expand Down
9 changes: 9 additions & 0 deletions engine/sql/120_git_clone.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up

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);
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);

-- +migrate Down

DELETE from action_parameter where name = 'depth' and action_id = (select id from action where name = 'GitClone');
DELETE from action_parameter where name = 'submodules' and action_id = (select id from action where name = 'GitClone');
1 change: 1 addition & 0 deletions engine/worker/builtin_checkout_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func runCheckoutApplication(w *currentWorker) BuiltInAction {
var opts = &git.CloneOpts{
Recursive: true,
NoStrictHostKeyChecking: true,
Depth: 50,
}
if branch != nil {
opts.Branch = branch.Value
Expand Down
23 changes: 22 additions & 1 deletion engine/worker/builtin_gitclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"regexp"
"strconv"
"strings"

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

if url == nil {
res := sdk.Result{
Expand Down Expand Up @@ -128,12 +131,28 @@ func runGitClone(w *currentWorker) BuiltInAction {
var opts = &git.CloneOpts{
Recursive: true,
NoStrictHostKeyChecking: true,
Depth: 50,
}
if branch != nil {
opts.Branch = branch.Value
} else {
opts.SingleBranch = true
}
if depth != nil {
if depth.Value == "false" {
opts.Depth = 0
} else if depth.Value != "" {
depthVal, errConv := strconv.Atoi(depth.Value)
if errConv != nil {
sendLog(fmt.Sprintf("invalid depth value. It must by empty, or false, or a numeric value. current value: %s", depth.Value))
} else {
opts.Depth = depthVal
}
}
}
if submodules != nil && submodules.Value == "false" {
opts.Recursive = false
}

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

//Perform the git clone
err := git.Clone(url, dir, auth, clone, output)
userLogCommand, err := git.Clone(url, dir, auth, clone, output)

sendLog(userLogCommand)

//Send the logs
if len(stdOut.Bytes()) > 0 {
Expand Down
16 changes: 11 additions & 5 deletions sdk/exportentities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func newSteps(a sdk.Action) []Step {
if tag != nil {
artifactUploadArgs["tag"] = tag.Value
}

s["artifactUpload"] = artifactUploadArgs
case sdk.GitCloneAction:
gitCloneArgs := map[string]string{}
Expand All @@ -137,22 +136,29 @@ func newSteps(a sdk.Action) []Step {
gitCloneArgs["directory"] = directory.Value
}
password := sdk.ParameterFind(&act.Parameters, "password")
if password != nil {
if password != nil && password.Value != "" {
gitCloneArgs["password"] = password.Value
}
privateKey := sdk.ParameterFind(&act.Parameters, "privateKey")
if privateKey != nil {
if privateKey != nil && privateKey.Value != "" {
gitCloneArgs["privateKey"] = privateKey.Value
}
url := sdk.ParameterFind(&act.Parameters, "url")
if url != nil {
gitCloneArgs["url"] = url.Value
}
user := sdk.ParameterFind(&act.Parameters, "user")
if user != nil {
if user != nil && user.Value != "" {
gitCloneArgs["user"] = user.Value
}

depth := sdk.ParameterFind(&act.Parameters, "depth")
if depth != nil && depth.Value != "" && depth.Value != "50" {
gitCloneArgs["depth"] = depth.Value
}
submodules := sdk.ParameterFind(&act.Parameters, "submodules")
if submodules != nil && submodules.Value == "false" {
gitCloneArgs["submodules"] = submodules.Value
}
s["gitClone"] = gitCloneArgs
case sdk.JUnitAction:
path := sdk.ParameterFind(&act.Parameters, "path")
Expand Down
3 changes: 2 additions & 1 deletion sdk/exportentities/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ steps:
privateKey: '{{.cds.app.key}}'
url: '{{.git.http_url}}'
user: ""
depth: '12'
- artifactUpload:
path: arti.tar.gz
tag: '{{.cds.version}}'
Expand All @@ -567,7 +568,7 @@ steps:
assert.Equal(t, sdk.GitCloneAction, p.Stages[0].Jobs[0].Action.Actions[0].Name)
assert.Equal(t, sdk.ArtifactUpload, p.Stages[0].Jobs[0].Action.Actions[1].Name)
assert.Equal(t, sdk.ArtifactUpload, p.Stages[0].Jobs[0].Action.Actions[2].Name)
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[0].Parameters, 7)
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[0].Parameters, 8)
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[1].Parameters, 2)
assert.Len(t, p.Stages[0].Jobs[0].Action.Actions[2].Parameters, 1)
}
Expand Down
18 changes: 9 additions & 9 deletions sdk/vcs/git/git_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type CloneOpts struct {
}

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

commands = prepareGitCloneCommands(repoURL, path, opts)
return runGitCommands(repo, commands, auth, output)
var userLogCommand string
userLogCommand, commands = prepareGitCloneCommands(repoURL, path, opts)
return userLogCommand, runGitCommands(repo, commands, auth, output)
}

func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) (string, cmds) {
allCmd := []cmd{}
gitcmd := cmd{
cmd: "git",
Expand All @@ -63,15 +64,13 @@ func prepareGitCloneCommands(repo string, path string, opts *CloneOpts) cmds {
} else if opts.SingleBranch {
gitcmd.args = append(gitcmd.args, "--single-branch")
}
if !opts.SingleBranch && opts.Depth != 0 {
gitcmd.args = append(gitcmd.args, "--no-single-branch")
}

if opts.Recursive {
gitcmd.args = append(gitcmd.args, "--recursive")
}
}

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

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

return cmds(allCmd)
return userLogCommand, cmds(allCmd)
}
10 changes: 5 additions & 5 deletions sdk/vcs/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestClone(t *testing.T) {
},
}
for _, tt := range tests {
if err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
if _, err := Clone(tt.args.repo, tt.args.path, tt.args.auth, tt.args.opts, tt.args.output); (err != nil) != tt.wantErr {
t.Errorf("%q. Clone() error = %v, wantErr %v", tt.name, err, tt.wantErr)
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func Test_gitCloneOverHTTPS(t *testing.T) {
Stderr: err,
}

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

Expand Down Expand Up @@ -148,7 +148,7 @@ func Test_gitCloneOverSSH(t *testing.T) {
Stderr: err,
}

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

Expand Down Expand Up @@ -191,7 +191,7 @@ func Test_gitCommand(t *testing.T) {
},
},
want: []string{
"git clone --verbose --depth 1 --branch master --no-single-branch --recursive https://github.com/ovh/cds.git /tmp/Test_gitCommand-2",
"git clone --verbose --depth 1 --branch master --recursive https://github.com/ovh/cds.git /tmp/Test_gitCommand-2",
},
},
{
Expand All @@ -213,7 +213,7 @@ func Test_gitCommand(t *testing.T) {
}
for _, tt := range tests {
os.RemoveAll(tt.args.path)
if got := prepareGitCloneCommands(tt.args.repo, tt.args.path, tt.args.opts); !reflect.DeepEqual(got.Strings(), tt.want) {
if _, got := prepareGitCloneCommands(tt.args.repo, tt.args.path, tt.args.opts); !reflect.DeepEqual(got.Strings(), tt.want) {
t.Errorf("%q. gitCloneCommand() = %v, want %v", tt.name, got, tt.want)
}
}
Expand Down
13 changes: 10 additions & 3 deletions tests/clictl_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ testcases:
- script: '[ -f ./tests/fixtures/action_disabled.yml ]'
assertions:
- result.code ShouldEqual 0
- name: test unknowned ation
- name: test unknowned action
steps:
- script: {{.cds.build.cdsctl}} action delete --force foo
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_unknowned
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "action does not exist"
- script: {{.cds.build.cdsctl}} action delete foo
- script: {{.cds.build.cdsctl}} action delete CDS_TestIT_unknowned
assertions:
- result.code ShouldEqual 50
- result.systemout ShouldContainSubstring "action does not exist"
- name: prepare test
steps:
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_Enabled
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_Disabled
- script: {{.cds.build.cdsctl}} action delete --force CDS_TestIT_GitClone
- name: action import
steps:
- script: {{.cds.build.cdsctl}} action import ./tests/fixtures/action_enabled.yml
Expand All @@ -32,12 +33,18 @@ testcases:
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring successfully
- script: {{.cds.build.cdsctl}} action import ./tests/fixtures/action_git_clone.yml
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring successfully
- name: action export
steps:
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Disabled > ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Enabled > ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_GitClone > ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported ./tests/fixtures/action_enabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported ./tests/fixtures/action_git_clone.yml
- name: action list
steps:
- script: {{.cds.build.cdsctl}} action list
27 changes: 27 additions & 0 deletions tests/fixtures/action_git_clone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: v1.0
name: CDS_TestIT_GitClone
parameters:
name:
type: string
requirements:
- binary: git
steps:
- gitClone:
branch: '{{.git.branch}}'
commit: '{{.git.hash}}'
depth: "10"
directory: '{{.cds.workspace}}'
submodules: "false"
url: '{{.git.url}}'
- gitClone:
branch: '{{.git.branch}}'
commit: '{{.git.hash}}'
directory: '{{.cds.workspace}}'
url: '{{.git.url}}'
- gitClone:
branch: '{{.git.branch}}'
commit: '{{.git.hash}}'
depth: "10"
directory: '{{.cds.workspace}}'
url: '{{.git.url}}'

3 changes: 2 additions & 1 deletion ui/src/app/shared/parameter/form/parameter.form.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<label>{{'parameter_description' | translate}}</label>
<textarea [(ngModel)]="newParameter.description" name="description" rows="{{ _sharedService.getTextAreaheight(newParameter.description)}}"></textarea>
</div>
<div class="two wide field">
<div class="two wide field" *ngIf="!project">
<label>{{'parameter_advanced' | translate}}</label>
<div class="ui checkbox">
<input type="checkbox" name="advanced" [(ngModel)]="newParameter.advanced">
<label></label>
Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/shared/parameter/list/parameter.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<th class="two wide" *ngIf="mode !=='launcher' && mode !== 'job'">{{ 'parameter_type' | translate }}</th>
<th class="wide" [class.four]="mode !== 'launcher' && mode !== 'job'" [class.eight]="mode === 'launcher' || mode === 'job'">{{ 'parameter_value' | translate }}</th>
<th class="four wide" *ngIf="mode !== 'launcher' && mode !== 'job'">{{ 'parameter_description' | translate }}</th>
<th class="two wide" *ngIf="mode === 'edit'">{{ 'parameter_advanced' | translate }}</th>
<th class="two wide" *ngIf="mode !== 'launcher' && mode !== 'job' && !project">{{ 'parameter_advanced' | translate }}</th>
<th class="two wide"></th>
</tr>
</thead>
Expand Down Expand Up @@ -64,9 +64,9 @@
</textarea>
<span *ngIf="mode === 'launcher' && (!p.description && p.description === '')">{{'common_no_description' | translate}}</span>
</td>
<td class="center" *ngIf="mode === 'edit'">
<td class="center" *ngIf="mode !== 'launcher' && mode !== 'job' && !project">
<div class="ui checkbox">
<input type="checkbox" name="advanced" [(ngModel)]="p.advanced">
<input type="checkbox" name="advanced" [(ngModel)]="p.advanced" [readonly]="mode === 'launcher'">
<label></label>
</div>
</td>
Expand Down

0 comments on commit f8aadc9

Please sign in to comment.