Skip to content

Commit b5db603

Browse files
committed
feat: add 'merge_request.modified_files_list(patterns...)' that return the list of files modified, matching the optional pattern(s)
1 parent 43b1ce8 commit b5db603

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

.scm-engine.example.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ label:
9898
description: "Modified this a service directory"
9999
color: "$pink"
100100
script: >
101-
map(merge_request.diff_stats, { .path })
102-
| filter({ hasPrefix(#, "internal/service/") })
101+
merge_request.modified_files_list("internal/service/")
103102
| map({ filepath_dir(#) })
104103
| map({ trimPrefix(#, "internal/") })
105104
| uniq()
@@ -113,8 +112,7 @@ label:
113112
description: "Modified this my-command command"
114113
color: "$purple"
115114
script: >
116-
map(merge_request.diff_stats, { .path })
117-
| filter({ hasPrefix(#, "internal/app/my-command/subcommands/") })
115+
merge_request.modified_files_list("internal/app/my-command/subcommands/")
118116
| map({ filepath_dir(#) })
119117
| map({ trimPrefix(#, "internal/app/my-command/subcommands/") })
120118
| map({ string("command/" + #) })

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,17 @@ Returns wether any of the provided files patterns have been modified in the Merg
468468
The file patterns use the [`.gitignore` format](https://git-scm.com/docs/gitignore#_pattern_format).
469469

470470
```expr
471-
merge_request.modified_files("*.go", "docs/")
471+
merge_request.modified_files("*.go", "docs/") == true
472+
```
473+
474+
#### `merge_request.modified_files_list`
475+
476+
Returns an array of files matching the provided (optional) pattern thas has been modified in the Merge Request.
477+
478+
The file patterns use the [`.gitignore` format](https://git-scm.com/docs/gitignore#_pattern_format).
479+
480+
```expr
481+
merge_request.modified_files_list("*.go", "docs/") == ["example/file.go", "docs/index.md"]
472482
```
473483

474484
#### `merge_request.has_label`

pkg/scm/gitlab/context_merge_request.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ func (e ContextMergeRequest) HasLabel(in string) bool {
1717
return false
1818
}
1919

20+
func (e ContextMergeRequest) ModifiedFilesList(patterns ...string) []string {
21+
return e.findModifiedFiles(patterns...)
22+
}
23+
2024
// Partially lifted from https://github.com/hmarr/codeowners/blob/main/match.go
2125
func (e ContextMergeRequest) ModifiedFiles(patterns ...string) bool {
26+
return len(e.findModifiedFiles(patterns...)) > 0
27+
}
28+
29+
// Partially lifted from https://github.com/hmarr/codeowners/blob/main/match.go
30+
func (e ContextMergeRequest) findModifiedFiles(patterns ...string) []string {
2231
leftAnchoredLiteral := false
2332

33+
output := []string{}
34+
2435
for _, pattern := range patterns {
2536
if !strings.ContainsAny(pattern, "*?\\") && pattern[0] == '/' {
2637
leftAnchoredLiteral = true
@@ -31,6 +42,7 @@ func (e ContextMergeRequest) ModifiedFiles(patterns ...string) bool {
3142
panic(err)
3243
}
3344

45+
NEXT_FILE:
3446
for _, changedFile := range e.DiffStats {
3547
// Normalize Windows-style path separators to forward slashes
3648
testPath := filepath.ToSlash(changedFile.Path)
@@ -45,27 +57,35 @@ func (e ContextMergeRequest) ModifiedFiles(patterns ...string) bool {
4557

4658
// If the pattern ends with a slash we can do a simple prefix match
4759
if prefix[len(prefix)-1] == '/' && strings.HasPrefix(testPath, prefix) {
48-
return true
60+
output = append(output, testPath)
61+
62+
continue NEXT_FILE
4963
}
5064

5165
// If the strings are the same length, check for an exact match
5266
if len(testPath) == len(prefix) && testPath == prefix {
53-
return true
67+
output = append(output, testPath)
68+
69+
continue NEXT_FILE
5470
}
5571

5672
// Otherwise check if the test path is a subdirectory of the pattern
5773
if len(testPath) > len(prefix) && testPath[len(prefix)] == '/' && testPath[:len(prefix)] == prefix {
58-
return true
74+
output = append(output, testPath)
75+
76+
continue NEXT_FILE
5977
}
6078
}
6179

6280
if regex.MatchString(testPath) {
63-
return true
81+
output = append(output, testPath)
82+
83+
continue NEXT_FILE
6484
}
6585
}
6686
}
6787

68-
return false
88+
return output
6989
}
7090

7191
// buildPatternRegex compiles a new regexp object from a gitignore-style pattern string

0 commit comments

Comments
 (0)