2020-02-12 01:38:30 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
)
|
|
|
|
|
2020-02-24 14:48:12 -06:00
|
|
|
var commandPatternGA *regexp.Regexp
|
|
|
|
var commandPatternADO *regexp.Regexp
|
2020-02-12 01:38:30 -06:00
|
|
|
|
|
|
|
func init() {
|
2020-02-24 14:48:12 -06:00
|
|
|
commandPatternGA = regexp.MustCompile("^::([^ ]+)( (.+))?::([^\r\n]*)[\r\n]+$")
|
2021-01-12 00:39:43 -06:00
|
|
|
commandPatternADO = regexp.MustCompile("^##\\[([^ ]+)( (.+))?]([^\r\n]*)[\r\n]+$")
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:01:40 -06:00
|
|
|
func tryParseRawActionCommand(line string) (command string, kvPairs map[string]string, arg string, ok bool) {
|
|
|
|
if m := commandPatternGA.FindStringSubmatch(line); m != nil {
|
|
|
|
command = m[1]
|
|
|
|
kvPairs = parseKeyValuePairs(m[3], ",")
|
|
|
|
arg = m[4]
|
|
|
|
ok = true
|
|
|
|
} else if m := commandPatternADO.FindStringSubmatch(line); m != nil {
|
|
|
|
command = m[1]
|
|
|
|
kvPairs = parseKeyValuePairs(m[3], ";")
|
|
|
|
arg = m[4]
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:38:30 -06:00
|
|
|
func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
resumeCommand := ""
|
2020-02-24 14:48:12 -06:00
|
|
|
return func(line string) bool {
|
2023-01-13 11:01:40 -06:00
|
|
|
command, kvPairs, arg, ok := tryParseRawActionCommand(line)
|
|
|
|
if !ok {
|
2020-02-24 14:48:12 -06:00
|
|
|
return true
|
|
|
|
}
|
2020-02-12 01:38:30 -06:00
|
|
|
|
2020-02-24 14:48:12 -06:00
|
|
|
if resumeCommand != "" && command != resumeCommand {
|
2021-12-09 09:10:56 -06:00
|
|
|
logger.Infof(" \U00002699 %s", line)
|
2020-02-24 14:48:12 -06:00
|
|
|
return false
|
|
|
|
}
|
2020-11-02 07:40:46 -06:00
|
|
|
arg = unescapeCommandData(arg)
|
|
|
|
kvPairs = unescapeKvPairs(kvPairs)
|
2020-02-24 14:48:12 -06:00
|
|
|
switch command {
|
|
|
|
case "set-env":
|
|
|
|
rc.setEnv(ctx, kvPairs, arg)
|
|
|
|
case "set-output":
|
|
|
|
rc.setOutput(ctx, kvPairs, arg)
|
|
|
|
case "add-path":
|
|
|
|
rc.addPath(ctx, arg)
|
|
|
|
case "debug":
|
|
|
|
logger.Infof(" \U0001F4AC %s", line)
|
|
|
|
case "warning":
|
|
|
|
logger.Infof(" \U0001F6A7 %s", line)
|
|
|
|
case "error":
|
|
|
|
logger.Infof(" \U00002757 %s", line)
|
|
|
|
case "add-mask":
|
2022-03-02 02:29:34 -06:00
|
|
|
rc.AddMask(arg)
|
2021-05-15 16:11:16 -05:00
|
|
|
logger.Infof(" \U00002699 %s", "***")
|
2020-02-24 14:48:12 -06:00
|
|
|
case "stop-commands":
|
|
|
|
resumeCommand = arg
|
|
|
|
logger.Infof(" \U00002699 %s", line)
|
|
|
|
case resumeCommand:
|
|
|
|
resumeCommand = ""
|
|
|
|
logger.Infof(" \U00002699 %s", line)
|
2022-05-24 08:36:06 -05:00
|
|
|
case "save-state":
|
|
|
|
logger.Infof(" \U0001f4be %s", line)
|
|
|
|
rc.saveState(ctx, kvPairs, arg)
|
2023-01-13 11:01:40 -06:00
|
|
|
case "add-matcher":
|
|
|
|
logger.Infof(" \U00002753 add-matcher %s", arg)
|
2020-02-24 14:48:12 -06:00
|
|
|
default:
|
|
|
|
logger.Infof(" \U00002753 %s", line)
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
2020-02-24 14:48:12 -06:00
|
|
|
|
2023-03-17 10:01:31 -05:00
|
|
|
// return true to let gitea's logger handle these special outputs also
|
|
|
|
return true
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
|
2023-02-04 07:35:13 -06:00
|
|
|
name := kvPairs["name"]
|
|
|
|
common.Logger(ctx).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
|
2020-02-12 01:38:30 -06:00
|
|
|
if rc.Env == nil {
|
|
|
|
rc.Env = make(map[string]string)
|
|
|
|
}
|
2023-02-04 07:35:13 -06:00
|
|
|
if rc.GlobalEnv == nil {
|
|
|
|
rc.GlobalEnv = map[string]string{}
|
|
|
|
}
|
2023-04-18 13:09:57 -05:00
|
|
|
newenv := map[string]string{
|
|
|
|
name: arg,
|
|
|
|
}
|
|
|
|
mergeIntoMap := mergeIntoMapCaseSensitive
|
|
|
|
if rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
|
|
mergeIntoMap = mergeIntoMapCaseInsensitive
|
|
|
|
}
|
|
|
|
mergeIntoMap(rc.Env, newenv)
|
|
|
|
mergeIntoMap(rc.GlobalEnv, newenv)
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2021-04-02 15:40:44 -05:00
|
|
|
stepID := rc.CurrentStep
|
|
|
|
outputName := kvPairs["name"]
|
|
|
|
if outputMapping, ok := rc.OutputMappings[MappableOutput{StepID: stepID, OutputName: outputName}]; ok {
|
|
|
|
stepID = outputMapping.StepID
|
|
|
|
outputName = outputMapping.OutputName
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := rc.StepResults[stepID]
|
|
|
|
if !ok {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Infof(" \U00002757 no outputs used step '%s'", stepID)
|
2021-04-02 15:40:44 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
|
2021-04-02 15:40:44 -05:00
|
|
|
result.Outputs[outputName] = arg
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
func (rc *RunContext) addPath(ctx context.Context, arg string) {
|
|
|
|
common.Logger(ctx).Infof(" \U00002699 ::add-path:: %s", arg)
|
2022-12-09 05:16:15 -06:00
|
|
|
extraPath := []string{arg}
|
|
|
|
for _, v := range rc.ExtraPath {
|
|
|
|
if v != arg {
|
|
|
|
extraPath = append(extraPath, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc.ExtraPath = extraPath
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
|
2020-02-24 14:48:12 -06:00
|
|
|
func parseKeyValuePairs(kvPairs string, separator string) map[string]string {
|
2020-02-12 01:38:30 -06:00
|
|
|
rtn := make(map[string]string)
|
2020-02-24 14:48:12 -06:00
|
|
|
kvPairList := strings.Split(kvPairs, separator)
|
2020-02-12 01:38:30 -06:00
|
|
|
for _, kvPair := range kvPairList {
|
|
|
|
kv := strings.Split(kvPair, "=")
|
|
|
|
if len(kv) == 2 {
|
|
|
|
rtn[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
2020-11-02 07:40:46 -06:00
|
|
|
func unescapeCommandData(arg string) string {
|
|
|
|
escapeMap := map[string]string{
|
|
|
|
"%25": "%",
|
|
|
|
"%0D": "\r",
|
|
|
|
"%0A": "\n",
|
|
|
|
}
|
|
|
|
for k, v := range escapeMap {
|
2021-01-12 00:39:43 -06:00
|
|
|
arg = strings.ReplaceAll(arg, k, v)
|
2020-11-02 07:40:46 -06:00
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
func unescapeCommandProperty(arg string) string {
|
|
|
|
escapeMap := map[string]string{
|
|
|
|
"%25": "%",
|
|
|
|
"%0D": "\r",
|
|
|
|
"%0A": "\n",
|
|
|
|
"%3A": ":",
|
|
|
|
"%2C": ",",
|
|
|
|
}
|
|
|
|
for k, v := range escapeMap {
|
2021-01-12 00:39:43 -06:00
|
|
|
arg = strings.ReplaceAll(arg, k, v)
|
2020-11-02 07:40:46 -06:00
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
|
|
|
|
for k, v := range kvPairs {
|
|
|
|
kvPairs[k] = unescapeCommandProperty(v)
|
|
|
|
}
|
|
|
|
return kvPairs
|
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2023-07-10 19:12:12 -05:00
|
|
|
func (rc *RunContext) saveState(_ context.Context, kvPairs map[string]string, arg string) {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepID := rc.CurrentStep
|
|
|
|
if stepID != "" {
|
|
|
|
if rc.IntraActionState == nil {
|
|
|
|
rc.IntraActionState = map[string]map[string]string{}
|
|
|
|
}
|
|
|
|
state, ok := rc.IntraActionState[stepID]
|
|
|
|
if !ok {
|
|
|
|
state = map[string]string{}
|
|
|
|
rc.IntraActionState[stepID] = state
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
2022-12-15 11:08:31 -06:00
|
|
|
state[kvPairs["name"]] = arg
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
}
|