Skip to content

Commit a5e6b65

Browse files
authored
Allow running code references in a specific subdirectory (#471)
This PR adds a new configuration option for specifying a location of the `.launchdarkly/coderefs.yaml` config file if it's not located in the root. This allows a monorepo to specify multiple config files, and will require individual invocation: ``` ld-find-code-refs \ --accessToken=<TOKEN> \ --repoName=<REPO_NAME> \ --dir="<LOCAL_DIR>" \ --subdirectory="path/to/subdirectory" \ --projKey="<PROJ_KEY>" ```
1 parent 8907421 commit a5e6b65

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

build/metadata/bitbucket/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Add the following snippet to the script section of your `bitbucket-pipelines.yml
1717
# LD_IGNORE_SERVICE_ERRORS "<boolean>" # Optional.
1818
# LD_LOOKBACK "<integer>" # Optional.
1919
# LD_ALLOW_TAGS "<boolean>" #Optional.
20+
# LD_SUBDIRECTORY "<string>" # Optional.
2021
```
2122

2223
## Variables

build/metadata/github-actions/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,20 @@ Once your workflow has been created, the best way to confirm that the workflow i
8383

8484
If the action fails, there may be a problem with your configuration. To investigate, dig into the action's logs to view any error messages.
8585

86-
<!-- action-docs-inputs -->
86+
<!-- action-docs-inputs source="action.yml" -->
8787
## Inputs
8888

89-
| parameter | description | required | default |
89+
| name | description | required | default |
9090
| --- | --- | --- | --- |
91-
| accessToken | A token with write access to the LaunchDarkly project. | `true` | |
92-
| allowTags | Enable storing references for tags. Lists the tag as a branch. | `false` | false |
93-
| baseUri | The base URL of the LaunchDarkly server for this configuration. | `false` | https://app.launchdarkly.com |
94-
| contextLines | The number of context lines above and below a code reference for the job to send to LaunchDarkly. By default, the flag finder will not send any context lines to LaunchDarkly. If < 0, it will send no source code to LaunchDarkly. If 0, it will send only the lines containing flag references. If > 0, it will send that number of context lines above and below the flag reference. You may provide a maximum of 5 context lines. | `false` | 2 |
95-
| debug | Enable verbose debug logging. | `false` | false |
96-
| ignoreServiceErrors | If enabled, the scanner will terminate with exit code 0 when the LaunchDarkly API is unreachable or returns an unexpected response. | `false` | false |
97-
| lookback | Set the number of commits to search in history for whether you removed a feature flag from code. You may set to 0 to disable this feature. Setting this option to a high value will increase search time. | `false` | 10 |
98-
| projKey | Key of the LaunchDarkly project associated with this repository. Found under Account Settings -> Projects in the LaunchDarkly dashboard. Cannot be combined with `projects` block in configuration file. | `false` | |
99-
| repoName | The repository name. Defaults to the current GitHub repository. | `false` | |
100-
| prune | There is a known issue where the GitHub Action will not prune deleted branch data in private repos. Only enable this if you are running the action in a public repo. | `false` | false |
101-
<!-- action-docs-inputs -->
91+
| `accessToken` | <p>A token with write access to the LaunchDarkly project.</p> | `true` | `""` |
92+
| `allowTags` | <p>Enable storing references for tags. Lists the tag as a branch.</p> | `false` | `false` |
93+
| `baseUri` | <p>The base URL of the LaunchDarkly server for this configuration.</p> | `false` | `https://app.launchdarkly.com` |
94+
| `contextLines` | <p>The number of context lines above and below a code reference for the job to send to LaunchDarkly. By default, the flag finder will not send any context lines to LaunchDarkly. If < 0, it will send no source code to LaunchDarkly. If 0, it will send only the lines containing flag references. If > 0, it will send that number of context lines above and below the flag reference. You may provide a maximum of 5 context lines.</p> | `false` | `2` |
95+
| `debug` | <p>Enable verbose debug logging.</p> | `false` | `false` |
96+
| `ignoreServiceErrors` | <p>If enabled, the scanner will terminate with exit code 0 when the LaunchDarkly API is unreachable or returns an unexpected response.</p> | `false` | `false` |
97+
| `lookback` | <p>Set the number of commits to search in history for whether you removed a feature flag from code. You may set to 0 to disable this feature. Setting this option to a high value will increase search time.</p> | `false` | `10` |
98+
| `projKey` | <p>Key of the LaunchDarkly project associated with this repository. Found under Account Settings -&gt; Projects in the LaunchDarkly dashboard. Cannot be combined with <code>projects</code> block in configuration file.</p> | `false` | `""` |
99+
| `repoName` | <p>The repository name. Defaults to the current GitHub repository.</p> | `false` | `""` |
100+
| `prune` | <p>There is a known issue where the GitHub Action will not prune deleted branch data in private repos. Only enable this if you are running the action in a public repo.</p> | `false` | `false` |
101+
| `subdirectory` | <p>The subdirectory to run the action in.</p> | `false` | `""` |
102+
<!-- action-docs-inputs source="action.yml" -->

build/metadata/github-actions/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ inputs:
4242
default: "false"
4343
description: "There is a known issue where the GitHub Action will not prune deleted branch data in private repos. Only enable this if you are running the action in a public repo."
4444
required: false
45+
subdirectory:
46+
description: "The subdirectory to run the action in."
47+
required: false
4548
runs:
4649
using: 'docker'
4750
image: 'Dockerfile'
@@ -56,3 +59,4 @@ runs:
5659
LD_IGNORE_SERVICE_ERRORS: ${{ inputs.ignoreServiceErrors }}
5760
LD_LOOKBACK: ${{ inputs.lookback }}
5861
LD_PRUNE: ${{ inputs.prune }}
62+
LD_SUBDIRECTORY: ${{ inputs.subdirectory }}

docs/CONFIGURATION.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,24 @@ ld-find-code-refs [flags]
2929

3030
Flags:
3131
```
32-
-t, --accessToken string LaunchDarkly personal access token with write-level access.
32+
-t, --accessToken string LaunchDarkly personal access token with write-level access.
3333
3434
--allowTags Enables storing references for tags. The tag will be listed as a branch.
3535
3636
-U, --baseUri string LaunchDarkly base URI. (default "https://app.launchdarkly.com")
3737
3838
-b, --branch string The currently checked out branch. If not provided, branch name will be auto-detected. Provide this option when using CI systems that leave the repository in a detached HEAD state.
39+
3940
--commitUrlTemplate string If provided, LaunchDarkly will attempt to generate links to your VCS service provider per commit. Example: https://github.com/launchdarkly/ld-find-code-refs/commit/${sha}. Allowed template variables: 'branchName', 'sha'. If "commitUrlTemplate" is not provided, but "repoUrl" is provided and "repoType" is not custom, LaunchDarkly will attempt to automatically generate source code links for the given "repoType".
41+
4042
-C, --contextLines int The number of context lines to send to LaunchDarkly. If < 0, no source code will be sent to LaunchDarkly. If 0, only the lines containing flag references will be sent. If > 0, will send that number of context lines above and below the flag reference. A maximum of 5 context lines may be provided. (default 2)
4143
4244
--debug Enables verbose debug logging
4345
4446
-B, --defaultBranch string The default branch. The LaunchDarkly UI will default to this branch. If not provided, will fallback to 'main'. (default "main")
4547
4648
-d, --dir string Path to existing checkout of the repository.
49+
4750
--dryRun If enabled, the scanner will run without sending code references to LaunchDarkly. Combine with the outDir option to output code references to a CSV.
4851
4952
-h, --help help for ld-find-code-refs
@@ -63,15 +66,18 @@ Flags:
6366
-r, --repoName string Repository name. Will be displayed in LaunchDarkly. Case insensitive. Repository names must only contain letters, numbers, '.', '_' or '-'."
6467
6568
-T, --repoType string The repo service provider. Used to correctly categorize repositories in the LaunchDarkly UI. Acceptable values: bitbucket|custom|github|gitlab. (default "custom")
66-
69+
6770
-u, --repoUrl string The URL for the repository. If provided and "repoType" is not custom, LaunchDarkly will attempt to automatically generate source code links for the given "repoType".
6871
6972
-R, --revision string Use this option to scan non-git codebases. The current revision of the repository to be scanned. If set, the version string for the scanned repository will not be inferred, and branch garbage collection will be disabled. The "branch" option is required when "revision" is set.
7073
74+
--subdirectory string If the .launchdarkly/coderefs.yaml file is not in the root of the repository, provide the path to the configuration file relative to the root.
75+
Code references will only run on this provided subdirectory.
76+
7177
-s, --updateSequenceId int An integer representing the order number of code reference updates. Used to version updates across concurrent executions of the flag finder. If not provided, data will always be updated. If provided, data will only be updated if the existing "updateSequenceId" is less than the new "updateSequenceId". Examples: the time a "git push" was initiated, CI build number, the current unix timestamp. (default -1)
7278
7379
--userAgent string (Internal) Platform where code references is run.
74-
80+
7581
-v, --version version for ld-find-code-refs
7682
```
7783

options/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ LaunchDarkly UI. Acceptable values: bitbucket|custom|github|gitlab.`,
142142
defaultValue: "",
143143
usage: `Use this option to scan non-git codebases. The current revision of the repository to be scanned. If set, the version string for the scanned repository will not be inferred, and branch garbage collection will be disabled. The "branch" option is required when "revision" is set.`,
144144
},
145+
{
146+
name: "subdirectory",
147+
defaultValue: "",
148+
usage: `If the .launchdarkly/coderefs.yaml file is not in the root of
149+
the repository, provide the path to the subdirectory containing the configuration,
150+
relative to the root. Code references will only run on this provided subdirectory.
151+
This allows a monorepo to have multiple configuration files, one per subdirectory.`,
152+
},
145153
{
146154
name: "updateSequenceId",
147155
short: "s",

options/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Options struct {
5757
RepoType string `mapstructure:"repoType"`
5858
RepoUrl string `mapstructure:"repoUrl"`
5959
Revision string `mapstructure:"revision"`
60+
Subdirectory string `mapstructure:"subdirectory"`
6061
UserAgent string `mapstructure:"userAgent"`
6162
ContextLines int `mapstructure:"contextLines"`
6263
Lookback int `mapstructure:"lookback"`
@@ -109,9 +110,11 @@ func InitYAML() error {
109110
if err != nil {
110111
return err
111112
}
113+
subdirectoryPath := viper.GetString("subdirectory")
112114
viper.SetConfigName("coderefs")
113115
viper.SetConfigType("yaml")
114-
viper.AddConfigPath(filepath.Join(absPath, ".launchdarkly"))
116+
configPath := filepath.Join(absPath, subdirectoryPath, ".launchdarkly")
117+
viper.AddConfigPath(configPath)
115118
err = viper.ReadInConfig()
116119
if err != nil && !errors.As(err, &viper.ConfigFileNotFoundError{}) {
117120
return err

search/scan.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package search
22

33
import (
4+
"path/filepath"
5+
46
"github.com/launchdarkly/ld-find-code-refs/v2/flags"
57
"github.com/launchdarkly/ld-find-code-refs/v2/internal/ld"
68
"github.com/launchdarkly/ld-find-code-refs/v2/internal/log"
@@ -12,7 +14,12 @@ func Scan(opts options.Options, repoParams ld.RepoParams, dir string) (Matcher,
1214
flagKeys := flags.GetFlagKeys(opts, repoParams)
1315
matcher := NewMultiProjectMatcher(opts, dir, flagKeys)
1416

15-
refs, err := SearchForRefs(dir, matcher)
17+
searchDir := dir
18+
if opts.Subdirectory != "" {
19+
searchDir = filepath.Join(dir, opts.Subdirectory)
20+
}
21+
22+
refs, err := SearchForRefs(searchDir, matcher)
1623
if err != nil {
1724
log.Error.Fatalf("error searching for flag key references: %s", err)
1825
}

0 commit comments

Comments
 (0)