Repository: ovh/cds
Pull request #4903 (2020-01-20 13:11:56)

fix(worker): overwrite value each time
1. Description
1. Related issues
1. About tests
1. Mentions

@ovh/cds



Patch:
diff --git a/engine/worker/cmd_upload.go b/engine/worker/cmd_upload.go
index ce935f831..f7f7c94f7 100644
--- a/engine/worker/cmd_upload.go
+++ b/engine/worker/cmd_upload.go
@@ -88,7 +88,7 @@ func uploadCmd() func(cmd *cobra.Command, args []string) {
 			}
 
 			if resp.StatusCode >= 300 {
-				sdk.Exit("cannot artefact upload HTTP %d\n", resp.StatusCode)
+				sdk.Exit("cannot artifact upload HTTP %d\n", resp.StatusCode)
 			}
 		}
 	}
diff --git a/engine/worker/internal/action/builtin_artifact_upload.go b/engine/worker/internal/action/builtin_artifact_upload.go
index 342a67f45..0a5624da3 100644
--- a/engine/worker/internal/action/builtin_artifact_upload.go
+++ b/engine/worker/internal/action/builtin_artifact_upload.go
@@ -4,7 +4,6 @@ import (
 	"context"
 	"errors"
 	"fmt"
-	"path"
 	"path/filepath"
 	"strings"
 	"sync"
@@ -42,7 +41,7 @@ func RunArtifactUpload(ctx context.Context, wk workerruntime.Runtime, a sdk.Acti
 		abs = workdir.Name()
 	}
 
-	if !path.IsAbs(artifactPath) {
+	if !sdk.PathIsAbs(artifactPath) {
 		artifactPath = filepath.Join(abs, artifactPath)
 	}
 
diff --git a/engine/worker/internal/action/builtin_coverage.go b/engine/worker/internal/action/builtin_coverage.go
index 9580f36e7..9e97c5947 100644
--- a/engine/worker/internal/action/builtin_coverage.go
+++ b/engine/worker/internal/action/builtin_coverage.go
@@ -3,7 +3,6 @@ package action
 import (
 	"context"
 	"fmt"
-	"path"
 	"path/filepath"
 	"strconv"
 
@@ -64,7 +63,7 @@ func RunParseCoverageResultAction(ctx context.Context, wk workerruntime.Runtime,
 		abs = workdir.Name()
 	}
 
-	if !path.IsAbs(p) {
+	if !sdk.PathIsAbs(p) {
 		fpath = filepath.Join(abs, p)
 	} else {
 		fpath = p
diff --git a/engine/worker/internal/action/builtin_junit.go b/engine/worker/internal/action/builtin_junit.go
index 8a461401d..a16277537 100644
--- a/engine/worker/internal/action/builtin_junit.go
+++ b/engine/worker/internal/action/builtin_junit.go
@@ -5,7 +5,6 @@ import (
 	"encoding/xml"
 	"errors"
 	"fmt"
-	"path"
 	"path/filepath"
 
 	"github.com/spf13/afero"
@@ -43,7 +42,7 @@ func RunParseJunitTestResultAction(ctx context.Context, wk workerruntime.Runtime
 		abs = workdir.Name()
 	}
 
-	if !path.IsAbs(p) {
+	if !sdk.PathIsAbs(p) {
 		p = filepath.Join(abs, p)
 	}
 
diff --git a/engine/worker/internal/handler_upload.go b/engine/worker/internal/handler_upload.go
index e4c47435f..8db4e24e9 100644
--- a/engine/worker/internal/handler_upload.go
+++ b/engine/worker/internal/handler_upload.go
@@ -6,7 +6,6 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
-	"path"
 	"path/filepath"
 
 	"github.com/ovh/cds/engine/worker/internal/action"
@@ -31,7 +30,7 @@ func uploadHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
 		}
 
 		artifactPath := art.Name
-		if !path.IsAbs(artifactPath) {
+		if !sdk.PathIsAbs(artifactPath) {
 			artifactPath = filepath.Join(art.WorkingDirectory, art.Name)
 		}
 
diff --git a/engine/worker/internal/run.go b/engine/worker/internal/run.go
index dceea93b9..c58976a6f 100644
--- a/engine/worker/internal/run.go
+++ b/engine/worker/internal/run.go
@@ -126,8 +126,11 @@ func (w *CurrentWorker) runJob(ctx context.Context, a *sdk.Action, jobID int64,
 			// Check if all newVariables are in currentJob.params
 			// variable can be add in w.currentJob.newVariables by worker command export
 			for _, newVariableFromHandler := range w.currentJob.newVariables {
-				if sdk.ParameterFind(w.currentJob.params, newVariableFromHandler.Name) == nil {
+				p := sdk.ParameterFind(w.currentJob.params, newVariableFromHandler.Name)
+				if p == nil {
 					w.currentJob.params = append(w.currentJob.params, newVariableFromHandler.ToParameter(""))
+				} else {
+					p.Value = newVariableFromHandler.Value
 				}
 			}
 
@@ -270,8 +273,11 @@ func (w *CurrentWorker) runSteps(ctx context.Context, steps []sdk.Action, a sdk.
 		// Check if all newVariables are in currentJob.params
 		// variable can be add in w.currentJob.newVariables by worker command export
 		for _, newVariableFromHandler := range w.currentJob.newVariables {
-			if sdk.ParameterFind(w.currentJob.params, newVariableFromHandler.Name) == nil {
+			p := sdk.ParameterFind(w.currentJob.params, newVariableFromHandler.Name)
+			if p == nil {
 				w.currentJob.params = append(w.currentJob.params, newVariableFromHandler.ToParameter(""))
+			} else {
+				p.Value = newVariableFromHandler.Value
 			}
 		}
 
diff --git a/sdk/cache.go b/sdk/cache.go
index 9afaac51e..d1be77ca3 100644
--- a/sdk/cache.go
+++ b/sdk/cache.go
@@ -7,7 +7,6 @@ import (
 	"io"
 	"net/url"
 	"os"
-	"path"
 	"path/filepath"
 	"strings"
 
@@ -58,7 +57,7 @@ func CreateTarFromPaths(fs afero.Fs, cwd string, paths []string, opts *TarOption
 		// ensure the src actually exists before trying to tar it
 
 		completePath := p
-		if !path.IsAbs(p) {
+		if !PathIsAbs(p) {
 			completePath = filepath.Join(cwd, p)
 		}
 
@@ -85,14 +84,13 @@ func CreateTarFromPaths(fs afero.Fs, cwd string, paths []string, opts *TarOption
 				header.Name = strings.TrimPrefix(strings.TrimPrefix(header.Name, opts.TrimDirName), string(filepath.Separator))
 			}
 			if fi.Mode()&os.ModeSymlink != 0 {
+
 				symlink, errEval := filepath.EvalSymlinks(file)
 				if errEval != nil {
 					return errEval
 				}
-				abs, errAbs := filepath.Abs(header.Name)
-				if errAbs != nil {
-					return errAbs
-				}
+
+				abs := filepath.Dir(filepath.Join(cwd, header.Name))
 
 				symlinkRel, errRel := filepath.Rel(abs, symlink)
 				if errRel != nil {
diff --git a/sdk/common.go b/sdk/common.go
index dfe3fc5da..02f9e31ef 100644
--- a/sdk/common.go
+++ b/sdk/common.go
@@ -11,8 +11,10 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"path"
 	"reflect"
 	"regexp"
+	"runtime"
 	"strings"
 	"unicode"
 
@@ -269,3 +271,12 @@ func RemoveNotPrintableChar(in string) string {
 	}
 	return strings.Map(m, in)
 }
+
+var windowsPathRegex = regexp.MustCompile(`^[a-zA-Z]:\\[\\\S|*\S]?.*$`)
+
+func PathIsAbs(s string) bool {
+	if GOOS == "windows" || runtime.GOOS == "windows" {
+		return windowsPathRegex.MatchString(s)
+	}
+	return path.IsAbs(s)
+}


Test patch:
diff --git a/sdk/common_test.go b/sdk/common_test.go
index 595b67a4a..acd62275e 100644
--- a/sdk/common_test.go
+++ b/sdk/common_test.go
@@ -32,3 +32,12 @@ func TestRemoveNotPrintableChar(t *testing.T) {
 		})
 	}
 }
+
+func TestPathIsAbs(t *testing.T) {
+	GOOS = "windows"
+	assert.True(t, PathIsAbs(`C:\Program Files (x86)\Foo`))
+	assert.False(t, PathIsAbs(`Program Files (x86)\Foo`))
+	GOOS = "linux"
+	assert.True(t, PathIsAbs(`/tmp`))
+	assert.False(t, PathIsAbs(`tmp`))
+}