Skip to content

Commit

Permalink
fix: stop using replace (#473)
Browse files Browse the repository at this point in the history
* fix: stop using replace

* test: fix testdata

* test: add test cases

* test: fix test data

* test: fix test data
  • Loading branch information
suzuki-shunsuke authored Jun 26, 2024
1 parent e150b83 commit 646ec29
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
32 changes: 17 additions & 15 deletions pkg/controller/run/parse_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import (
)

var (
usesPattern = regexp.MustCompile(`^ +(?:- )?['"]?uses['"]? *: +(['"]?)(.*?)@([^ '"]+)['"]?(?:( +# +(?:tag=)?)(v?\d+[^ ]*))?`)
usesPattern = regexp.MustCompile(`^( +(?:- )?['"]?uses['"]? *: +)(['"]?)(.*?)@([^ '"]+)['"]?(?:( +# +(?:tag=)?)(v?\d+[^ ]*)(.*))?`)
fullCommitSHAPattern = regexp.MustCompile(`\b[0-9a-f]{40}\b`)
semverPattern = regexp.MustCompile(`^v?\d+\.\d+\.\d+[^ ]*$`)
shortTagPattern = regexp.MustCompile(`^v\d+$`)
)

type Action struct {
Uses string
Name string
Version string
Tag string
VersionTagSeparator string
RepoOwner string
RepoName string
Quote string
Suffix string
}

type VersionType int
Expand Down Expand Up @@ -61,11 +63,13 @@ func parseAction(line string) *Action {
return nil
}
return &Action{
Quote: matches[1], // empty, ', "
Name: matches[2], // local action is excluded by the regular expression because local action doesn't have version @
Version: matches[3], // full commit hash, main, v3, v3.0.0
VersionTagSeparator: matches[4], // empty, " # ", " # tag="
Tag: matches[5], // empty, v1, v3.0.0
Uses: matches[1], // " - uses: "
Quote: matches[2], // empty, ', "
Name: matches[3], // local action is excluded by the regular expression because local action doesn't have version @
Version: matches[4], // full commit hash, main, v3, v3.0.0
VersionTagSeparator: matches[5], // empty, " # ", " # tag="
Tag: matches[6], // empty, v1, v3.0.0
Suffix: matches[7],
}
}

Expand Down Expand Up @@ -120,7 +124,7 @@ func (c *Controller) parseLine(ctx context.Context, logE *logrus.Entry, line str
}
}
// @yyy # longVersion
return patchLine(line, action, sha, longVersion), nil
return patchLine(action, sha, longVersion), nil
case Semver:
// verify commit hash
if !cfg.IsVerify {
Expand Down Expand Up @@ -150,20 +154,18 @@ func (c *Controller) parseLine(ctx context.Context, logE *logrus.Entry, line str
logE.Debug("failed to get a long tag")
return line, nil
}
return patchLine(line, action, action.Version, longVersion), nil
return patchLine(action, action.Version, longVersion), nil
default:
return line, nil
}
}

func patchLine(line string, action *Action, version, tag string) string {
if action.Tag == "" {
if version == tag {
return line
}
return strings.Replace(line, "@"+action.Version+action.Quote, fmt.Sprintf("@%s%s # %s", version, action.Quote, tag), 1)
func patchLine(action *Action, version, tag string) string {
sep := action.VersionTagSeparator
if sep == "" {
sep = " # "
}
return strings.Replace(line, fmt.Sprintf("@%s%s%s%s", action.Version, action.Quote, action.VersionTagSeparator, action.Tag), fmt.Sprintf("@%s%s%s%s", action.Version, action.Quote, action.VersionTagSeparator, tag), 1)
return action.Uses + action.Quote + action.Name + "@" + version + action.Quote + sep + tag + action.Suffix
}

func (c *Controller) getLongVersionFromSHA(ctx context.Context, action *Action, sha string) (string, error) {
Expand Down
18 changes: 13 additions & 5 deletions pkg/controller/run/parse_line_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v3",
line: " - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3",
exp: &Action{
Uses: " - uses: ",
Name: "actions/checkout",
Version: "8e5e7e5ab8b370d6c329ec480221332ada57f0ab",
VersionTagSeparator: " # ",
Expand All @@ -36,6 +37,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v2",
line: " uses: actions/checkout@v2",
exp: &Action{
Uses: " uses: ",
Name: "actions/checkout",
Version: "v2",
},
Expand All @@ -44,6 +46,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v3 (single quote)",
line: ` - "uses": 'actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab' # v3`,
exp: &Action{
Uses: ` - "uses": `,
Name: "actions/checkout",
Version: "8e5e7e5ab8b370d6c329ec480221332ada57f0ab",
VersionTagSeparator: " # ",
Expand All @@ -55,6 +58,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v3 (double quote)",
line: ` - 'uses': "actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab" # v3`,
exp: &Action{
Uses: ` - 'uses': `,
Name: "actions/checkout",
Version: "8e5e7e5ab8b370d6c329ec480221332ada57f0ab",
VersionTagSeparator: " # ",
Expand All @@ -66,6 +70,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v2 (single quote)",
line: ` "uses": 'actions/checkout@v2'`,
exp: &Action{
Uses: ` "uses": `,
Name: "actions/checkout",
Version: "v2",
Tag: "",
Expand All @@ -76,6 +81,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "checkout v2 (double quote)",
line: ` 'uses': "actions/checkout@v2"`,
exp: &Action{
Uses: ` 'uses': `,
Name: "actions/checkout",
Version: "v2",
Tag: "",
Expand All @@ -86,6 +92,7 @@ func Test_parseAction(t *testing.T) { //nolint:funlen
name: "tag=",
line: ` - uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # tag=v3`,
exp: &Action{
Uses: ` - uses: `,
Name: "actions/checkout",
Version: "83b7061638ee4956cf7545a6f7efe594e5ad0247",
VersionTagSeparator: " # tag=",
Expand Down Expand Up @@ -208,17 +215,17 @@ func Test_patchLine(t *testing.T) {
t.Parallel()
data := []struct {
name string
line string
tag string
version string
action *Action
exp string
}{
{
name: "checkout v3",
line: " - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3",
exp: " - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2",
exp: " - uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v3.5.2",
action: &Action{
Uses: " - uses: ",
Name: "actions/checkout",
Version: "8e5e7e5ab8b370d6c329ec480221332ada57f0ab",
VersionTagSeparator: " # ",
Tag: "v3",
Expand All @@ -228,9 +235,10 @@ func Test_patchLine(t *testing.T) {
},
{
name: "checkout v2",
line: " uses: actions/checkout@v2",
exp: " uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.17.0",
action: &Action{
Uses: " uses: ",
Name: "actions/checkout",
Version: "v2",
},
version: "ee0669bd1cc54295c223e0bb666b733df41de1c5",
Expand All @@ -240,7 +248,7 @@ func Test_patchLine(t *testing.T) {
for _, d := range data {
t.Run(d.name, func(t *testing.T) {
t.Parallel()
line := patchLine(d.line, d.action, d.version, d.tag)
line := patchLine(d.action, d.version, d.tag)
if line != d.exp {
t.Fatalf(`wanted %s, got %s`, d.exp, line)
}
Expand Down
16 changes: 9 additions & 7 deletions testdata/foo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ jobs:
runs-on: ubuntu-latest
permissions: {}
steps:
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # tag=v3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # tag=v3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # tag=v3
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247
- uses: suzuki-shunsuke/repo-404@83b7061638ee4956cf7545a6f7efe594e5ad0247
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: actions/setup-java@v3
- 'uses': "actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247" # v3.5.1
- "uses": 'actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247' # v3.5.1
- 'uses': "actions/checkout@v3"
- "uses": 'actions/checkout@v3'
actionlint:
uses: suzuki-shunsuke/actionlint-workflow/.github/workflows/actionlint.yaml@b6a5f966d4504893b2aeb60cf2b0de8946e48504 # v0.5.0
uses: suzuki-shunsuke/actionlint-workflow/.github/workflows/[email protected]
with:
aqua_version: v2.3.4
permissions:
Expand Down
10 changes: 6 additions & 4 deletions testdata/foo.yaml.after
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ jobs:
runs-on: ubuntu-latest
permissions: {}
steps:
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # v3.5.1
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247 # tag=v3.5.1
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # tag=v3.5.3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # tag=v3.5.3
- uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247
- uses: suzuki-shunsuke/repo-404@83b7061638ee4956cf7545a6f7efe594e5ad0247
- uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0
- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
- uses: actions/setup-java@v3
- 'uses': "actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247" # v3.5.1
- "uses": 'actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247' # v3.5.1
- 'uses': "actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744" # v3.6.0
- "uses": 'actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744' # v3.6.0
actionlint:
uses: suzuki-shunsuke/actionlint-workflow/.github/workflows/actionlint.yaml@b6a5f966d4504893b2aeb60cf2b0de8946e48504 # v0.5.0
with:
Expand Down

0 comments on commit 646ec29

Please sign in to comment.