Browse Source

bugfix: Fixed unusual issues that slipped through with shellAfterCompleted (#425)

* bugfix: Fixed unusual issues that slipped through with shellAfterCompleted #419 #420

* cicd: Fix broken style lint
James Read 1 năm trước cách đây
mục cha
commit
6c289506c2

+ 29 - 13
internal/executor/arguments.go

@@ -23,35 +23,51 @@ var (
 	}
 	}
 )
 )
 
 
+func parseCommandForReplacements(rawShellCommand string, values map[string]string) (string, map[string]string, error) {
+	r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
+	foundArgumentNames := r.FindAllStringSubmatch(rawShellCommand, -1)
+
+	usedArguments := make(map[string]string)
+
+	for _, match := range foundArgumentNames {
+		argName := match[1]
+		argValue, argProvided := values[argName]
+
+		if !argProvided {
+			return "", nil, errors.New("Required arg not provided: " + argName)
+		}
+
+		usedArguments[argName] = argValue
+
+		rawShellCommand = strings.ReplaceAll(rawShellCommand, match[0], argValue)
+	}
+
+	return rawShellCommand, usedArguments, nil
+}
+
 func parseActionArguments(rawShellCommand string, values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
 func parseActionArguments(rawShellCommand string, values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
 	log.WithFields(log.Fields{
 	log.WithFields(log.Fields{
 		"actionTitle": actionTitle,
 		"actionTitle": actionTitle,
 		"cmd":         rawShellCommand,
 		"cmd":         rawShellCommand,
 	}).Infof("Action parse args - Before")
 	}).Infof("Action parse args - Before")
 
 
-	r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
-	matches := r.FindAllStringSubmatch(rawShellCommand, -1)
+	rawShellCommand, usedArgs, err := parseCommandForReplacements(rawShellCommand, values)
 
 
-	for _, match := range matches {
-		argValue, argProvided := values[match[1]]
-
-		if !argProvided {
-			log.Infof("%v", values)
-			return "", errors.New("Required arg not provided: " + match[1])
-		}
+	if err != nil {
+		return "", err
+	}
 
 
-		err := typecheckActionArgument(match[1], argValue, action)
+	for argName, argValue := range usedArgs {
+		err := typecheckActionArgument(argName, argValue, action)
 
 
 		if err != nil {
 		if err != nil {
 			return "", err
 			return "", err
 		}
 		}
 
 
 		log.WithFields(log.Fields{
 		log.WithFields(log.Fields{
-			"name":  match[1],
+			"name":  argName,
 			"value": argValue,
 			"value": argValue,
 		}).Debugf("Arg assigned")
 		}).Debugf("Arg assigned")
-
-		rawShellCommand = strings.ReplaceAll(rawShellCommand, match[0], argValue)
 	}
 	}
 
 
 	rawShellCommand = sv.ReplaceEntityVars(entityPrefix, rawShellCommand)
 	rawShellCommand = sv.ReplaceEntityVars(entityPrefix, rawShellCommand)

+ 13 - 6
internal/executor/executor.go

@@ -528,7 +528,14 @@ func stepExecAfter(req *ExecutionRequest) bool {
 		"exitCode": fmt.Sprintf("%v", req.logEntry.ExitCode),
 		"exitCode": fmt.Sprintf("%v", req.logEntry.ExitCode),
 	}
 	}
 
 
-	finalParsedCommand, _ := parseActionArguments(req.Action.ShellAfterCompleted, args, req.Action, req.logEntry.ActionTitle, req.EntityPrefix)
+	finalParsedCommand, _, err := parseCommandForReplacements(req.Action.ShellAfterCompleted, args)
+
+	if err != nil {
+		msg := "Could not prepare shellAfterCompleted command: " + err.Error() + "\n"
+		req.logEntry.Output += msg
+		log.Warnf(msg)
+		return true
+	}
 
 
 	cmd := wrapCommandInShell(ctx, finalParsedCommand)
 	cmd := wrapCommandInShell(ctx, finalParsedCommand)
 	cmd.Stdout = &stdout
 	cmd.Stdout = &stdout
@@ -538,14 +545,14 @@ func stepExecAfter(req *ExecutionRequest) bool {
 
 
 	waiterr := cmd.Wait()
 	waiterr := cmd.Wait()
 
 
-	req.logEntry.Output += "\n" + stdout.String()
-	req.logEntry.Output += "OliveTin::shellAfterCompleted stdout\n" + stdout.String()
+	req.logEntry.Output += "\n"
+	req.logEntry.Output += "OliveTin::shellAfterCompleted stdout\n"
 	req.logEntry.Output += stdout.String()
 	req.logEntry.Output += stdout.String()
 
 
-	req.logEntry.Output += "OliveTin::shellAfterCompleted stderr\n" + stdout.String()
+	req.logEntry.Output += "OliveTin::shellAfterCompleted stderr\n"
 	req.logEntry.Output += stderr.String()
 	req.logEntry.Output += stderr.String()
 
 
-	req.logEntry.Output += "OliveTin::shellAfterCompleted errors and summary\n" + stdout.String()
+	req.logEntry.Output += "OliveTin::shellAfterCompleted errors and summary\n"
 	appendErrorToStderr(runerr, req.logEntry)
 	appendErrorToStderr(runerr, req.logEntry)
 	appendErrorToStderr(waiterr, req.logEntry)
 	appendErrorToStderr(waiterr, req.logEntry)
 
 
@@ -555,7 +562,7 @@ func stepExecAfter(req *ExecutionRequest) bool {
 
 
 	req.logEntry.Output += fmt.Sprintf("Your shellAfterCompleted exited with code %v\n", cmd.ProcessState.ExitCode())
 	req.logEntry.Output += fmt.Sprintf("Your shellAfterCompleted exited with code %v\n", cmd.ProcessState.ExitCode())
 
 
-	req.logEntry.Output += "OliveTin::shellAfterCompleted output complete\n" + stdout.String()
+	req.logEntry.Output += "OliveTin::shellAfterCompleted output complete\n"
 
 
 	return true
 	return true
 }
 }

+ 2 - 2
webui.dev/js/marshaller.js

@@ -34,12 +34,12 @@ function createAnnotation (key, val) {
 
 
   domAnnotation.appendChild(createElement('span', {
   domAnnotation.appendChild(createElement('span', {
     innerText: key,
     innerText: key,
-    classNames: ['annotationKey']
+    classNames: ['annotation-key']
   }))
   }))
 
 
   domAnnotation.appendChild(createElement('span', {
   domAnnotation.appendChild(createElement('span', {
     innerText: val,
     innerText: val,
-    classNames: ['annotationValue']
+    classNames: ['annotation-value']
   }))
   }))
 
 
   return domAnnotation
   return domAnnotation

+ 2 - 2
webui.dev/style.css

@@ -472,7 +472,7 @@ span.annotation {
   margin-right: .2em;
   margin-right: .2em;
 }
 }
 
 
-span.annotationKey {
+span.annotation-key {
   padding: 0.4em;
   padding: 0.4em;
   border-radius: 0.4em 0 0 .4em;
   border-radius: 0.4em 0 0 .4em;
   display: inline-block;
   display: inline-block;
@@ -480,7 +480,7 @@ span.annotationKey {
   color: #666;
   color: #666;
 }
 }
 
 
-span.annotationValue {
+span.annotation-value {
   padding: 0.4em;
   padding: 0.4em;
   border-radius: 0 .4em .4em 0;
   border-radius: 0 .4em .4em 0;
   display: inline-block;
   display: inline-block;