Skip to content

Commit afe83e5

Browse files
jazannemmrj
andauthored
feat: don't skip .github dir (#441)
allow scanning for flag keys used in github workflows with https://github.com/launchdarkly/gha-flags Co-authored-by: Molly <[email protected]>
1 parent d908e6e commit afe83e5

File tree

28 files changed

+111
-25
lines changed

28 files changed

+111
-25
lines changed

docs/ALIASES.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,29 @@ For a file that has already been committed:
123123
```sh
124124
git update-index --chmod=+x .launchdarkly/flagAlias.sh
125125
```
126+
127+
## Finding flags used in GitHub workflow files
128+
129+
If you are using [launchdarkly/gha-flags](https://github.com/launchdarkly/gha-flags), your flag keys will not be wrapped in delimiters, so it is important to either configure the proper aliases or disable delimiters.
130+
131+
Example alias configuration:
132+
133+
```yaml
134+
// given flag key "enable-new-feature"
135+
aliases:
136+
- type: kebabcase
137+
```
138+
139+
```yaml
140+
// given flag key "enable_new_feature"
141+
aliases:
142+
- type: snakecase
143+
```
144+
145+
Example delimiter disable:
146+
147+
```yaml
148+
delimiters:
149+
disableDefaults: true # will find keys without surrounding delimiters like ' or "
150+
```
151+

docs/CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ delimiters:
136136
137137
## Ignoring files and directories
138138
139-
All dotfiles and patterns in `.gitignore` and `.ignore` will be excluded by default.
139+
All dotfiles and patterns in `.gitignore` and `.ignore` will be excluded by default, except the `.github` directory. Flags may be referenced when using [launchdarky/gha-flags](https://github.com/launchdarkly/gha-flags). If you would like to skip scanning these files, add `.github` to one of the ignore files.
140140

141141
To ignore additional files and directories, provide a `.ldignore` file in the root directory of your Git repository. All patterns specified in `.ldignore` file will be excluded by the scanner. Patterns must follow the `.gitignore` format as specified here: https://git-scm.com/docs/gitignore#_pattern_format

search/files.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,20 @@ func readFiles(ctx context.Context, files chan<- file, workspace string) error {
8080
path = filepath.ToSlash(path)
8181

8282
// Skip directories, hidden files, and ignored files
83-
if strings.HasPrefix(info.Name(), ".") || allIgnores.Match(path, isDir) {
83+
if allIgnores.Match(path, isDir) {
8484
if isDir {
8585
return filepath.SkipDir
8686
}
8787
return nil
88+
} else if strings.HasPrefix(info.Name(), ".") {
89+
if isDir {
90+
// don't skip github dir
91+
if strings.HasPrefix(info.Name(), ".github") {
92+
return nil
93+
}
94+
return filepath.SkipDir
95+
}
96+
return nil
8897
} else if !info.Mode().IsRegular() {
8998
return nil
9099
}

search/files_test.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,49 @@ import (
99
)
1010

1111
func Test_readFiles(t *testing.T) {
12-
files := make(chan file, 8)
13-
err := readFiles(context.Background(), files, "testdata")
14-
require.NoError(t, err)
15-
got := []file{}
16-
for file := range files {
17-
got = append(got, file)
18-
switch file.path {
19-
case "fileWithNoRefs":
20-
assert.Equal(t, []string{"fileWithNoRefs"}, file.lines)
21-
case "fileWithRefs":
22-
assert.Equal(t, testFile.lines, file.lines)
23-
case "ignoredFiles/included":
24-
assert.Equal(t, []string{"IGNORED BUT INCLUDED"}, file.lines)
25-
case "symlink":
26-
assert.Fail(t, "Should not read symlink contents")
27-
default:
28-
assert.Fail(t, "Read unexpected file", file)
12+
t.Run("don't ignore .github by default", func(t *testing.T) {
13+
files := make(chan file, 8)
14+
err := readFiles(context.Background(), files, "testdata/include-github-files")
15+
require.NoError(t, err)
16+
got := []file{}
17+
for file := range files {
18+
got = append(got, file)
19+
switch file.path {
20+
case "fileWithNoRefs":
21+
assert.Equal(t, []string{"fileWithNoRefs"}, file.lines)
22+
case "fileWithRefs", ".github/workflows/workflow.yml":
23+
assert.Equal(t, testFile.lines, file.lines)
24+
case "ignoredFiles/included":
25+
assert.Equal(t, []string{"IGNORED BUT INCLUDED"}, file.lines)
26+
case "symlink":
27+
assert.Fail(t, "Should not read symlink contents")
28+
default:
29+
assert.Fail(t, "Read unexpected file", file)
30+
}
2931
}
30-
}
31-
assert.Len(t, got, 3, "Expected 3 valid files to have been found")
32+
assert.Len(t, got, 4, "Expected 4 valid files to have been found")
33+
})
34+
35+
t.Run("explicitly ignore .github files", func(t *testing.T) {
36+
files := make(chan file, 8)
37+
err := readFiles(context.Background(), files, "testdata/exclude-github-files")
38+
require.NoError(t, err)
39+
got := []file{}
40+
for file := range files {
41+
got = append(got, file)
42+
switch file.path {
43+
case "fileWithNoRefs":
44+
assert.Equal(t, []string{"fileWithNoRefs"}, file.lines)
45+
case "fileWithRefs":
46+
assert.Equal(t, testFile.lines, file.lines)
47+
case "ignoredFiles/included":
48+
assert.Equal(t, []string{"IGNORED BUT INCLUDED"}, file.lines)
49+
case "symlink":
50+
assert.Fail(t, "Should not read symlink contents")
51+
default:
52+
assert.Fail(t, "Read unexpected file", file)
53+
}
54+
}
55+
assert.Len(t, got, 3, "Expected 3 valid files to have been found")
56+
})
3257
}

search/search_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,16 @@ func Test_processFiles(t *testing.T) {
385385
}
386386

387387
func Test_SearchForRefs(t *testing.T) {
388-
os.Symlink("testdata/fileWithRefs", "testdata/symlink")
388+
os.Symlink("testdata/exclude-github-files/fileWithRefs", "testdata/exclude-github-files/symlink")
389389
want := []ld.ReferenceHunksRep{{Path: testFile.path}}
390390
matcher := Matcher{
391391
ctxLines: 0,
392392
}
393393
matcher.Elements = append(matcher.Elements,
394394
NewElementMatcher("default", "", "", []string{testFlagKey, testFlagKey2}, nil),
395395
)
396-
t.Cleanup(func() { os.Remove("testdata/symlink") })
397-
got, err := SearchForRefs("testdata", matcher)
396+
t.Cleanup(func() { os.Remove("testdata/exclude-github-files/symlink") })
397+
got, err := SearchForRefs("testdata/exclude-github-files", matcher)
398398
require.NoError(t, err)
399399
require.Len(t, got, 1)
400400
require.Equal(t, want[0].Path, got[0].Path)

search/testdata/.ldignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
someFlag
2+
anotherFlag
3+
someFlaganotherFlag
4+
some-flag
5+
another-flag

search/testdata/.hiddenDir/dotDir renamed to search/testdata/exclude-github-files/.hiddenDir/dotDir

File renamed without changes.

search/testdata/.hiddenFile renamed to search/testdata/exclude-github-files/.hiddenFile

File renamed without changes.

search/testdata/.ignore renamed to search/testdata/exclude-github-files/.ignore

File renamed without changes.

0 commit comments

Comments
 (0)