Skip to content

Commit 5ef3adb

Browse files
Balijepalli Vamshi KrishnaBalijepalli Vamshi Krishna
authored andcommitted
update wildcard function
1 parent 0eded23 commit 5ef3adb

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

remediation/workflow/pin/pinactions.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"path/filepath"
87
"regexp"
98
"strings"
109

@@ -229,8 +228,15 @@ func getSemanticVersion(client *github.Client, owner, repo, tagOrBranch, commitS
229228
// Function to check if an action matches any pattern in the list
230229
func ActionExists(actionName string, patterns []string) bool {
231230
for _, pattern := range patterns {
232-
// Use filepath.Match to match the pattern
233-
matched, err := filepath.Match(pattern, actionName)
231+
// Convert glob pattern to regex for path matching
232+
// Replace * with [^/]* to match within a path segment
233+
// Replace **/ with .* to match across path segments
234+
regexPattern := strings.ReplaceAll(pattern, "**", "§§")
235+
regexPattern = strings.ReplaceAll(regexPattern, "*", "[^/]*")
236+
regexPattern = strings.ReplaceAll(regexPattern, "§§", ".*")
237+
regexPattern = "^" + regexPattern + "($|/)"
238+
239+
matched, err := regexp.MatchString(regexPattern, actionName)
234240
if err != nil {
235241
// Handle invalid patterns
236242
fmt.Printf("Error matching pattern: %v\n", err)

remediation/workflow/pin/pinactions_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestPinActions(t *testing.T) {
293293
{fileName: "actionwithcomment.yml", wantUpdated: true, pinToImmutable: true},
294294
{fileName: "repeatedactionwithcomment.yml", wantUpdated: true, pinToImmutable: true},
295295
{fileName: "immutableaction-1.yml", wantUpdated: true, pinToImmutable: true},
296-
{fileName: "exemptaction.yml", wantUpdated: true, exemptedActions: []string{"actions/checkout", "rohith/*"}, pinToImmutable: true},
296+
{fileName: "exemptaction.yml", wantUpdated: true, exemptedActions: []string{"actions/checkout", "rohith/*", "praveen/*"}, pinToImmutable: true},
297297
{fileName: "donotpintoimmutable.yml", wantUpdated: true, pinToImmutable: false},
298298
{fileName: "invertedcommas.yml", wantUpdated: true, pinToImmutable: false},
299299
}
@@ -345,3 +345,23 @@ func Test_isAbsolute(t *testing.T) {
345345
})
346346
}
347347
}
348+
349+
func TestActionExists(t *testing.T) {
350+
result := ActionExists("actions/checkout", []string{"actions/checkout"})
351+
t.Log(result)
352+
if !result {
353+
t.Errorf("ActionExists returned false for actions/checkout")
354+
}
355+
356+
result = ActionExists("actions/checkout", []string{"actions/*"})
357+
t.Log(result)
358+
if !result {
359+
t.Errorf("ActionExists returned false for actions/checkout")
360+
}
361+
362+
result = ActionExists("actions/checkout/something", []string{"actions/*"})
363+
t.Log(result)
364+
if !result {
365+
t.Errorf("ActionExists returned true for actions/checkout/something")
366+
}
367+
}

testfiles/pinactions/input/exemptaction.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ jobs:
3838
- name: publish on version change
3939
id: publish_nuget
4040
uses: rohith/publish-nuget@v2
41+
with:
42+
PROJECT_FILE_PATH: Core/Core.csproj
43+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
44+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json
45+
46+
- name: publish on version change 2
47+
id: publish_nuget
48+
uses: praveen/publish-nuget/to-version@v2
4149
with:
4250
PROJECT_FILE_PATH: Core/Core.csproj
4351
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}

testfiles/pinactions/output/exemptaction.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ jobs:
3838
- name: publish on version change
3939
id: publish_nuget
4040
uses: rohith/publish-nuget@v2
41+
with:
42+
PROJECT_FILE_PATH: Core/Core.csproj
43+
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
44+
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json
45+
46+
- name: publish on version change 2
47+
id: publish_nuget
48+
uses: praveen/publish-nuget/to-version@v2
4149
with:
4250
PROJECT_FILE_PATH: Core/Core.csproj
4351
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)