Skip to content

Commit

Permalink
feat(action/GitClone): if no auth & url, use app vcs strategy (#3174)
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored and fsamin committed Aug 10, 2018
1 parent f8aadc9 commit 76afc19
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions engine/worker/builtin_gitclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ func runGitClone(w *currentWorker) BuiltInAction {
depth := sdk.ParameterFind(&a.Parameters, "depth")
submodules := sdk.ParameterFind(&a.Parameters, "submodules")

if url == nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: "Git repository URL is not set. Nothing to perform.",
}
sendLog(res.Reason)
return res
}

deprecatedKey := true

if privateKey != nil && (strings.HasPrefix(privateKey.Value, "app-") || strings.HasPrefix(privateKey.Value, "proj-") || strings.HasPrefix(privateKey.Value, "env-")) {
Expand Down Expand Up @@ -96,18 +87,6 @@ func runGitClone(w *currentWorker) BuiltInAction {
}
}

//If url is not http(s), a key must be found
if !strings.HasPrefix(url.Value, "http") {
if errK == sdk.ErrKeyNotFound || key == nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("SSH Key not found. Unable to perform git clone"),
}
sendLog(res.Reason)
return res
}
}

//Prepare all options - credentials
var auth *git.AuthOpts
if user != nil || password != nil {
Expand All @@ -127,6 +106,48 @@ func runGitClone(w *currentWorker) BuiltInAction {
auth.PrivateKey = *key
}

var gitURL string
if url != nil {
gitURL = url.Value
}

// if not auth setted in GitClone action, we try to use VCS Strategy
// if failed with VCS Strategy: warn only user (user can use GitClone without auth, with a git url valid)
if gitURL == "" && (auth == nil || (auth.Username == "" && auth.Password == "" && len(auth.PrivateKey.Content) == 0)) {
sendLog("no url and auth parameters, trying to use VCS Strategy from application")
var errExtract error
gitURL, auth, errExtract = extractVCSInformations(*params, secrets)
if errExtract != nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("Could not use VCS Auth Strategy from application: %v", errExtract),
}
sendLog(res.Reason)
return res
}
}

if gitURL == "" {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("Git repository URL is not set. Nothing to perform."),
}
sendLog(res.Reason)
return res
}

//If url is not http(s), a key must be found
if !strings.HasPrefix(gitURL, "http") {
if errK == sdk.ErrKeyNotFound || key == nil {
res := sdk.Result{
Status: sdk.StatusFail.String(),
Reason: fmt.Sprintf("SSH Key not found. Unable to perform git clone"),
}
sendLog(res.Reason)
return res
}
}

//Prepare all options - clone options
var opts = &git.CloneOpts{
Recursive: true,
Expand Down Expand Up @@ -170,7 +191,7 @@ func runGitClone(w *currentWorker) BuiltInAction {
if directory != nil {
dir = directory.Value
}
return gitClone(w, params, url.Value, dir, auth, opts, sendLog)
return gitClone(w, params, gitURL, dir, auth, opts, sendLog)
}
}

Expand Down

0 comments on commit 76afc19

Please sign in to comment.