Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'deprecated-commands' (fix #234)
- Loading branch information
Showing
14 changed files
with
271 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package actionlint | ||
|
||
import "regexp" | ||
|
||
var deprecatedCommandsPattern = regexp.MustCompile(`(?:::(save-state|set-output|set-env)\s+name=[a-zA-Z][a-zA-Z_-]*::\S+|::(add-path)::\S+)`) | ||
|
||
// RuleDeprecatedCommands is a rule checker to detect deprecated workflow commands. Currently | ||
// 'set-state', 'set-output', `set-env' and 'add-path' are detected as deprecated. | ||
// | ||
// - https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/ | ||
// - https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ | ||
type RuleDeprecatedCommands struct { | ||
RuleBase | ||
} | ||
|
||
// NewRuleDeprecatedCommands creates a new RuleDeprecatedCommands instance. | ||
func NewRuleDeprecatedCommands() *RuleDeprecatedCommands { | ||
return &RuleDeprecatedCommands{ | ||
RuleBase: RuleBase{name: "deprecated-commands"}, | ||
} | ||
} | ||
|
||
// VisitStep is callback when visiting Step node. | ||
func (rule *RuleDeprecatedCommands) VisitStep(n *Step) error { | ||
if r, ok := n.Exec.(*ExecRun); ok && r.Run != nil { | ||
for _, m := range deprecatedCommandsPattern.FindAllStringSubmatch(r.Run.Value, -1) { | ||
c := m[1] | ||
if len(c) == 0 { | ||
c = m[2] | ||
} | ||
|
||
var a string | ||
switch c { | ||
case "set-output": | ||
a = `echo "{name}={value}" >> $GITHUB_OUTPUT` | ||
case "save-state": | ||
a = `echo "{name}={value}" >> $GITHUB_STATE` | ||
case "set-env": | ||
a = `echo "{name}={value}" >> $GITHUB_ENV` | ||
case "add-path": | ||
a = `echo "{path}" >> $GITHUB_PATH` | ||
default: | ||
panic("unreachable") | ||
} | ||
|
||
rule.errorf( | ||
r.Run.Pos, | ||
"workflow command %q was deprecated. use `%s` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions", | ||
c, | ||
a, | ||
) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package actionlint | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestRuleDeprecatedCommandsDetectTargetCommands(t *testing.T) { | ||
tests := []struct { | ||
what string | ||
run string | ||
want []string | ||
}{ | ||
{ | ||
what: "save-state", | ||
run: "::save-state name=foo::42", | ||
want: []string{"save-state"}, | ||
}, | ||
{ | ||
what: "set-output", | ||
run: "::set-output name=foo::42", | ||
want: []string{"set-output"}, | ||
}, | ||
{ | ||
what: "set-env", | ||
run: "::set-env name=foo::42", | ||
want: []string{"set-env"}, | ||
}, | ||
{ | ||
what: "add-path", | ||
run: "::add-path::/path/to/foo", | ||
want: []string{"add-path"}, | ||
}, | ||
{ | ||
what: "submatch", | ||
run: "hello::set-output name=foo::42 world", | ||
want: []string{"set-output"}, | ||
}, | ||
{ | ||
what: "multiple same commands", | ||
run: "::set-output name=foo::42 ::set-output name=bar::xxx", | ||
want: []string{"set-output", "set-output"}, | ||
}, | ||
{ | ||
what: "multiple different commands", | ||
run: "::set-output name=foo::42 ::add-path::/path/to/foo ::save-state name=foo::42", | ||
want: []string{"set-output", "add-path", "save-state"}, | ||
}, | ||
{ | ||
what: "multiple submatches", | ||
run: "hello::set-output name=foo::42 how ::add-path::/path/to/foo are ::save-state name=foo::42 you", | ||
want: []string{"set-output", "add-path", "save-state"}, | ||
}, | ||
{ | ||
what: "something between command and arguments", | ||
run: "::set-output hello name=foo::42", | ||
want: []string{}, | ||
}, | ||
{ | ||
what: "multiple spaces", | ||
run: "::set-output name=foo::42", | ||
want: []string{"set-output"}, | ||
}, | ||
{ | ||
what: "empty string", | ||
run: "", | ||
want: []string{}, | ||
}, | ||
{ | ||
what: "no command", | ||
run: "echo 'do not use set-output!'", | ||
want: []string{}, | ||
}, | ||
{ | ||
what: "hyphen and underscore in name", | ||
run: "::save-state name=foo_bar-woo::42", | ||
want: []string{"save-state"}, | ||
}, | ||
{ | ||
what: "invalid name", | ||
run: "::save-state name=-foo::42", | ||
want: []string{}, | ||
}, | ||
{ | ||
what: "different argument", | ||
run: "::save-state myname=foo::42", | ||
want: []string{}, | ||
}, | ||
} | ||
re := regexp.MustCompile(`\s+workflow command "([a-z-]+)" was deprecated\.`) | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.what, func(t *testing.T) { | ||
s := &Step{ | ||
Exec: &ExecRun{ | ||
Run: &String{ | ||
Value: tc.run, | ||
Pos: &Pos{}, | ||
}, | ||
}, | ||
} | ||
r := NewRuleDeprecatedCommands() | ||
if err := r.VisitStep(s); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
errs := r.Errs() | ||
have := []string{} | ||
for i, err := range errs { | ||
m := err.Error() | ||
ss := re.FindStringSubmatch(m) | ||
if len(ss) == 0 { | ||
t.Fatalf("%dth error was unexpected: %q", i, m) | ||
} | ||
have = append(have, ss[1]) | ||
} | ||
|
||
if !cmp.Equal(have, tc.want) { | ||
t.Fatal(cmp.Diff(have, tc.want)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test.yaml:8:14: workflow command "set-output" was deprecated. use `echo "{name}={value}" >> $GITHUB_OUTPUT` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions [deprecated-commands] | ||
test.yaml:9:14: workflow command "save-state" was deprecated. use `echo "{name}={value}" >> $GITHUB_STATE` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions [deprecated-commands] | ||
test.yaml:10:14: workflow command "set-env" was deprecated. use `echo "{name}={value}" >> $GITHUB_ENV` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions [deprecated-commands] | ||
test.yaml:11:14: workflow command "add-path" was deprecated. use `echo "{path}" >> $GITHUB_PATH` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions [deprecated-commands] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
on: push | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# ERROR | ||
- run: echo '::set-output name=foo::bar' | ||
- run: echo '::save-state name=foo::bar' | ||
- run: echo '::set-env name=foo::bar' | ||
- run: echo '::add-path::/path/to/foo' | ||
# OK | ||
- run: echo "::debug::Set the Octocat variable" | ||
- run: echo "::notice file=app.js,line=1,col=5,endColumn=7::Missing semicolon" | ||
- run: echo "::warning file=app.js,line=1,col=5,endColumn=7::Missing semicolon" | ||
- run: echo "::error file=app.js,line=1,col=5,endColumn=7::Missing semicolon" | ||
- run: echo "::group::My title" | ||
- run: echo "::endgroup::" | ||
- run: echo "::add-mask::Mona The Octocat" | ||
- run: echo "::stop-commands::hogehoge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test.yaml:8:14: workflow command "set-output" was deprecated. use `echo "{name}={value}" >> $GITHUB_OUTPUT` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions [deprecated-commands] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
on: push | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# ERROR: 'set-output' workflow command was deprecated | ||
- run: echo '::set-output name=foo::bar' | ||
# OK: Use this instead | ||
- run: echo "foo=bar" >> "$GITHUB_OUTPUT" | ||
# OK: 'debug' command is not deprecated | ||
- run: echo "::debug::Set the Octocat variable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters