-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcoderefs.go
More file actions
306 lines (270 loc) · 9.79 KB
/
Copy pathcoderefs.go
File metadata and controls
306 lines (270 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package coderefs
import (
"fmt"
"strings"
"github.com/launchdarkly/ld-find-code-refs/v2/aliases"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/git"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/helpers"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/ld"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/log"
"github.com/launchdarkly/ld-find-code-refs/v2/internal/validation"
"github.com/launchdarkly/ld-find-code-refs/v2/options"
"github.com/launchdarkly/ld-find-code-refs/v2/search"
)
func Run(opts options.Options, output bool) {
if len(opts.ProjKey) > 0 {
opts.Projects = append(opts.Projects, options.Project{
Key: opts.ProjKey,
})
}
absPath, err := validation.NormalizeAndValidatePath(opts.Dir)
if err != nil {
log.Error.Fatalf("could not validate directory option: %s", err)
}
log.Info.Printf("absolute directory path: %s", absPath)
ldApi := ld.InitApiClient(ld.ApiOptions{ApiKey: opts.AccessToken, BaseUri: opts.BaseUri, UserAgent: helpers.GetUserAgent(opts.UserAgent)})
branchName := opts.Branch
revision := opts.Revision
var gitClient *git.Client
var commitTime int64
if revision == "" {
gitClient, err = git.NewClient(absPath, branchName, opts.AllowTags)
if err != nil {
log.Error.Fatalf("%s", err)
}
branchName = gitClient.GitBranch
revision = gitClient.GitSha
commitTime = gitClient.GitTimestamp
}
repoParams := ld.RepoParams{
Type: opts.RepoType,
Name: opts.RepoName,
Url: opts.RepoUrl,
CommitUrlTemplate: opts.CommitUrlTemplate,
HunkUrlTemplate: opts.HunkUrlTemplate,
DefaultBranch: opts.DefaultBranch,
}
matcher, refs := search.Scan(opts, repoParams, absPath)
var updateId *int
if opts.UpdateSequenceId >= 0 {
updateIdOption := opts.UpdateSequenceId
updateId = &updateIdOption
}
branch := ld.BranchRep{
Name: strings.TrimPrefix(branchName, "refs/heads/"),
Head: revision,
UpdateSequenceId: updateId,
SyncTime: helpers.MakeTimestamp(),
References: refs,
CommitTime: commitTime,
}
if output {
generateHunkOutput(opts, matcher, branch, repoParams, ldApi)
}
if gitClient != nil {
runExtinctions(opts, matcher, branch, repoParams, gitClient, ldApi)
}
}
func Prune(opts options.Options, branches []string) {
ldApi := ld.InitApiClient(ld.ApiOptions{ApiKey: opts.AccessToken, BaseUri: opts.BaseUri, UserAgent: helpers.GetUserAgent(opts.UserAgent)})
err := ldApi.PostDeleteBranchesTask(opts.RepoName, branches)
if err != nil {
helpers.FatalServiceError(err, opts.IgnoreServiceErrors)
}
}
func deleteStaleBranches(ldApi ld.ApiClient, repoName string, remoteBranches map[string]bool) error {
branches, err := ldApi.GetCodeReferenceRepositoryBranches(repoName)
if err != nil {
return err
}
staleBranches := calculateStaleBranches(branches, remoteBranches)
if len(staleBranches) > 0 {
log.Debug.Printf("marking stale branches for code reference pruning: %v", staleBranches)
err = ldApi.PostDeleteBranchesTask(repoName, staleBranches)
if err != nil {
return err
}
}
return nil
}
func calculateStaleBranches(branches []ld.BranchRep, remoteBranches map[string]bool) []string {
staleBranches := []string{}
for _, branch := range branches {
if !remoteBranches[branch.Name] {
staleBranches = append(staleBranches, branch.Name)
}
}
log.Info.Printf("found %d stale branches to be marked for code reference pruning", len(staleBranches))
return staleBranches
}
func generateHunkOutput(opts options.Options, matcher search.Matcher, branch ld.BranchRep, repoParams ld.RepoParams, ldApi ld.ApiClient) {
outDir := opts.OutDir
projectKeys := make([]string, 1)
for _, project := range opts.Projects {
projectKeys = append(projectKeys, project.Key)
}
if outDir != "" {
outPath, err := branch.WriteToCSV(outDir, projectKeys[0], repoParams.Name, opts.Revision)
if err != nil {
log.Error.Fatalf("error writing code references to csv: %s", err)
}
log.Info.Printf("wrote code references to %s", outPath)
}
if opts.Debug {
branch.PrintReferenceCountTable()
}
if opts.DryRun {
totalFlags := 0
for _, searchElems := range matcher.Elements {
totalFlags += len(searchElems.Elements)
}
log.Info.Printf(
"dry run found %d code references across %d flags and %d files",
branch.TotalHunkCount(),
totalFlags,
len(branch.References),
)
return
}
log.Info.Printf(
"sending %d code references across %d flags and %d files to LaunchDarkly for project(s): %s",
branch.TotalHunkCount(),
len(matcher.Elements[0].Elements),
len(branch.References),
projectKeys,
)
err := ldApi.PutCodeReferenceBranch(branch, repoParams.Name)
switch {
case err == ld.BranchUpdateSequenceIdConflictErr:
if branch.UpdateSequenceId != nil {
log.Warning.Printf("updateSequenceId (%d) must be greater than previously submitted updateSequenceId", *branch.UpdateSequenceId)
}
case err == ld.EntityTooLargeErr:
log.Error.Fatalf("code reference payload too large for LaunchDarkly API - consider excluding more files with .ldignore or using fewer lines of context")
case err != nil:
helpers.FatalServiceError(fmt.Errorf("error sending code references to LaunchDarkly: %w", err), opts.IgnoreServiceErrors)
}
}
func runExtinctions(opts options.Options, matcher search.Matcher, branch ld.BranchRep, repoParams ld.RepoParams, gitClient *git.Client, ldApi ld.ApiClient) {
if opts.Lookback > 0 {
var removedFlags []ld.ExtinctionRep
flagCounts := branch.CountByProjectAndFlag(matcher.GetElements(), opts.GetProjectKeys())
for _, project := range opts.Projects {
missingFlags := []string{}
for flag, count := range flagCounts[project.Key] {
if count == 0 {
missingFlags = append(missingFlags, flag)
}
}
log.Info.Printf("checking if %d flags without references were removed in the last %d commits for project: %s", len(missingFlags), opts.Lookback, project.Key)
removedFlagsByProject, err := gitClient.FindExtinctions(project, missingFlags, matcher, opts.Lookback+1)
if err != nil {
log.Warning.Printf("unable to generate flag extinctions: %s", err)
} else {
log.Info.Printf("found %d removed flags", len(removedFlagsByProject))
}
removedFlags = append(removedFlags, removedFlagsByProject...)
}
if len(removedFlags) > 0 && !opts.DryRun {
err := ldApi.PostExtinctionEvents(removedFlags, repoParams.Name, branch.Name)
if err != nil {
log.Error.Printf("error sending extinction events to LaunchDarkly: %s", err)
}
}
}
if !opts.DryRun && opts.Prune {
log.Info.Printf("attempting to prune old code reference data from LaunchDarkly")
remoteBranches, err := gitClient.RemoteBranches()
if err != nil {
log.Warning.Printf("unable to retrieve branch list from remote, skipping code reference pruning: %s", err)
} else {
err = deleteStaleBranches(ldApi, repoParams.Name, remoteBranches)
if err != nil {
helpers.FatalServiceError(fmt.Errorf("failed to mark old branches for code reference pruning: %w", err), opts.IgnoreServiceErrors)
}
}
}
}
// GenerateAliases generates aliases for flags based on the provided options
func GenerateAliases(opts options.AliasOptions) error {
absPath, err := validation.NormalizeAndValidatePath(opts.Dir)
if err != nil {
return fmt.Errorf("could not validate directory option: %s", err)
}
log.Info.Printf("absolute directory path: %s", absPath)
var flagKeys []string
if opts.FlagKey != "" {
// Local Mode: Generate aliases for a single flag key
log.Info.Printf("generating aliases for flag key: %s", opts.FlagKey)
flagKeys = []string{opts.FlagKey}
} else {
// API-Connected Mode: Fetch all flags from the API
log.Info.Printf("fetching all flags from LaunchDarkly API")
ldApi := ld.InitApiClient(ld.ApiOptions{
ApiKey: opts.AccessToken,
BaseUri: opts.BaseUri,
UserAgent: helpers.GetUserAgent(opts.UserAgent),
})
// Determine project key
projKey := opts.ProjKey
if projKey == "" && len(opts.Projects) > 0 {
projKey = opts.Projects[0].Key
}
flags, err := ldApi.GetFlagKeyList(projKey, false) // Include archived flags
if err != nil {
return fmt.Errorf("could not retrieve flag keys from LaunchDarkly for project `%s`: %w", projKey, err)
}
flagKeys = flags
log.Info.Printf("retrieved %d flags from LaunchDarkly", len(flagKeys))
}
// Load alias configuration from .launchdarkly/coderefs.yaml
// We need to get the full options to access the aliases
fullOpts, err := options.GetOptions()
if err != nil {
return fmt.Errorf("error loading configuration: %w", err)
}
var aliasConfigs []options.Alias
// Check if we have project-specific aliases
if len(fullOpts.Projects) > 0 {
for _, project := range fullOpts.Projects {
aliasConfigs = append(aliasConfigs, project.Aliases...)
}
} else {
// Use global aliases
aliasConfigs = fullOpts.Aliases
}
if len(aliasConfigs) == 0 {
log.Warning.Printf("no alias configurations found in .launchdarkly/coderefs.yaml")
return nil
}
// Generate aliases
aliasMap, err := aliases.GenerateAliases(flagKeys, aliasConfigs, absPath)
if err != nil {
return fmt.Errorf("error generating aliases: %w", err)
}
// Print the results
if opts.FlagKey != "" {
// Local mode: print aliases for the single flag
if aliases, exists := aliasMap[opts.FlagKey]; exists && len(aliases) > 0 {
fmt.Printf("Aliases for flag '%s':\n", opts.FlagKey)
for _, alias := range aliases {
fmt.Printf(" - %s\n", alias)
}
} else {
fmt.Printf("No aliases found for flag '%s'\n", opts.FlagKey)
}
} else {
// API-connected mode: print aliases for all flags
fmt.Printf("Generated aliases for %d flags:\n\n", len(flagKeys))
for _, flagKey := range flagKeys {
if aliases, exists := aliasMap[flagKey]; exists && len(aliases) > 0 {
fmt.Printf("Flag '%s':\n", flagKey)
for _, alias := range aliases {
fmt.Printf(" - %s\n", alias)
}
fmt.Println()
}
}
}
return nil
}