|
16 | 16 | package cmd
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "encoding/json" |
19 | 20 | "fmt"
|
20 |
| - "magician/exec" |
21 |
| - "magician/github" |
22 |
| - "magician/provider" |
23 |
| - "magician/source" |
24 | 21 | "os"
|
25 | 22 | "path/filepath"
|
26 | 23 | "sort"
|
27 | 24 | "strconv"
|
28 | 25 | "strings"
|
29 | 26 | "text/template"
|
30 | 27 |
|
| 28 | + "magician/exec" |
| 29 | + "magician/github" |
| 30 | + "magician/provider" |
| 31 | + "magician/source" |
| 32 | + |
31 | 33 | "github.com/spf13/cobra"
|
32 | 34 | "golang.org/x/exp/maps"
|
33 | 35 |
|
@@ -141,6 +143,14 @@ func listGCEnvironmentVariables() string {
|
141 | 143 | }
|
142 | 144 |
|
143 | 145 | func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) {
|
| 146 | + errors := map[string][]string{"Other": []string{}} |
| 147 | + |
| 148 | + pullRequest, err := gh.GetPullRequest(strconv.Itoa(prNumber)) |
| 149 | + if err != nil { |
| 150 | + fmt.Printf("Error getting pull request: %v\n", err) |
| 151 | + errors["Other"] = append(errors["Other"], "Failed to fetch PR data") |
| 152 | + } |
| 153 | + |
144 | 154 | newBranch := fmt.Sprintf("auto-pr-%d", prNumber)
|
145 | 155 | oldBranch := fmt.Sprintf("auto-pr-%d-old", prNumber)
|
146 | 156 | wd := rnr.GetCWD()
|
@@ -174,8 +184,6 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
|
174 | 184 | data := diffCommentData{
|
175 | 185 | PrNumber: prNumber,
|
176 | 186 | }
|
177 |
| - errors := map[string][]string{"Other": []string{}} |
178 |
| - var err error |
179 | 187 | for _, repo := range []*source.Repo{&tpgRepo, &tpgbRepo, &tgcRepo, &tfoicsRepo} {
|
180 | 188 | errors[repo.Title] = []string{}
|
181 | 189 | repo.Branch = newBranch
|
@@ -210,6 +218,7 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
|
210 | 218 |
|
211 | 219 | // The breaking changes are unique across both provider versions
|
212 | 220 | uniqueBreakingChanges := map[string]struct{}{}
|
| 221 | + uniqueServiceLabels := map[string]struct{}{} |
213 | 222 | diffProcessorPath := filepath.Join(mmLocalPath, "tools", "diff-processor")
|
214 | 223 | diffProcessorEnv := map[string]string{
|
215 | 224 | "OLD_REF": oldBranch,
|
@@ -240,39 +249,43 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
|
240 | 249 | uniqueBreakingChanges[breakingChange] = struct{}{}
|
241 | 250 | }
|
242 | 251 |
|
243 |
| - addLabelsEnv := map[string]string{ |
244 |
| - "GITHUB_TOKEN_MAGIC_MODULES": ghTokenMagicModules, |
| 252 | + // If fetching the PR failed, Labels will be empty |
| 253 | + labels := make([]string, len(pullRequest.Labels)) |
| 254 | + for i, label := range pullRequest.Labels { |
| 255 | + labels[i] = label.Name |
245 | 256 | }
|
246 |
| - err = addLabels(prNumber, diffProcessorPath, addLabelsEnv, rnr) |
| 257 | + serviceLabels, err := changedSchemaLabels(prNumber, labels, diffProcessorPath, gh, rnr) |
247 | 258 | if err != nil {
|
248 |
| - fmt.Println("adding service labels: ", err) |
249 |
| - errors[repo.Title] = append(errors[repo.Title], "The diff processor crashed while adding labels.") |
| 259 | + fmt.Println("computing changed schema labels: ", err) |
| 260 | + errors[repo.Title] = append(errors[repo.Title], "The diff processor crashed while computing changed schema labels.") |
250 | 261 | }
|
251 |
| - err = cleanDiffProcessor(diffProcessorPath, rnr) |
252 |
| - if err != nil { |
253 |
| - fmt.Println("cleaning up diff processor: ", err) |
254 |
| - errors[repo.Title] = append(errors[repo.Title], "The diff processor failed to clean up properly.") |
| 262 | + for _, serviceLabel := range serviceLabels { |
| 263 | + uniqueServiceLabels[serviceLabel] = struct{}{} |
255 | 264 | }
|
256 | 265 | }
|
257 | 266 | breakingChangesSlice := maps.Keys(uniqueBreakingChanges)
|
258 | 267 | sort.Strings(breakingChangesSlice)
|
259 | 268 | data.BreakingChanges = breakingChangesSlice
|
260 | 269 |
|
| 270 | + // Add service labels to PR |
| 271 | + if len(uniqueServiceLabels) > 0 { |
| 272 | + serviceLabelsSlice := maps.Keys(uniqueServiceLabels) |
| 273 | + sort.Strings(serviceLabelsSlice) |
| 274 | + if err = gh.AddLabels(strconv.Itoa(prNumber), serviceLabelsSlice); err != nil { |
| 275 | + fmt.Printf("Error posting new service labels %q: %s", serviceLabelsSlice, err) |
| 276 | + errors["Other"] = append(errors["Other"], "Failed to update service labels") |
| 277 | + } |
| 278 | + } |
| 279 | + |
261 | 280 | // Update breaking changes status on PR
|
262 | 281 | breakingState := "success"
|
263 | 282 | if len(uniqueBreakingChanges) > 0 {
|
264 | 283 | breakingState = "failure"
|
265 |
| - |
266 |
| - pullRequest, err := gh.GetPullRequest(strconv.Itoa(prNumber)) |
267 |
| - if err != nil { |
268 |
| - fmt.Printf("Error getting pull request: %v\n", err) |
269 |
| - errors["Other"] = append(errors["Other"], "Failed to check for `override-breaking-change` label") |
270 |
| - } else { |
271 |
| - for _, label := range pullRequest.Labels { |
272 |
| - if label.Name == allowBreakingChangesLabel { |
273 |
| - breakingState = "success" |
274 |
| - break |
275 |
| - } |
| 284 | + // If fetching the PR failed, Labels will be empty |
| 285 | + for _, label := range pullRequest.Labels { |
| 286 | + if label.Name == allowBreakingChangesLabel { |
| 287 | + breakingState = "success" |
| 288 | + break |
276 | 289 | }
|
277 | 290 | }
|
278 | 291 | }
|
@@ -357,6 +370,11 @@ func computeDiff(repo *source.Repo, oldBranch string, ctlr *source.Controller) (
|
357 | 370 |
|
358 | 371 | // Build the diff processor for tpg or tpgb
|
359 | 372 | func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr ExecRunner) error {
|
| 373 | + for _, path := range []string{"old", "new", "bin"} { |
| 374 | + if err := rnr.RemoveAll(filepath.Join(diffProcessorPath, path)); err != nil { |
| 375 | + return err |
| 376 | + } |
| 377 | + } |
360 | 378 | if err := rnr.PushDir(diffProcessorPath); err != nil {
|
361 | 379 | return err
|
362 | 380 | }
|
@@ -387,25 +405,40 @@ func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]string,
|
387 | 405 | return strings.Split(strings.TrimSuffix(output, "\n"), "\n"), rnr.PopDir()
|
388 | 406 | }
|
389 | 407 |
|
390 |
| -func addLabels(prNumber int, diffProcessorPath string, env map[string]string, rnr ExecRunner) error { |
| 408 | +func changedSchemaLabels(prNumber int, currentLabels []string, diffProcessorPath string, gh GithubClient, rnr ExecRunner) ([]string, error) { |
391 | 409 | if err := rnr.PushDir(diffProcessorPath); err != nil {
|
392 |
| - return err |
| 410 | + return nil, err |
| 411 | + } |
| 412 | + |
| 413 | + // short-circuit if service labels have already been added to the PR |
| 414 | + hasServiceLabels := false |
| 415 | + oldLabels := make(map[string]struct{}, len(currentLabels)) |
| 416 | + for _, label := range currentLabels { |
| 417 | + oldLabels[label] = struct{}{} |
| 418 | + if strings.HasPrefix(label, "service/") { |
| 419 | + hasServiceLabels = true |
| 420 | + } |
| 421 | + } |
| 422 | + if hasServiceLabels { |
| 423 | + return nil, nil |
393 | 424 | }
|
394 |
| - output, err := rnr.Run("bin/diff-processor", []string{"add-labels", strconv.Itoa(prNumber)}, env) |
395 |
| - fmt.Println(output) |
| 425 | + |
| 426 | + output, err := rnr.Run("bin/diff-processor", []string{"changed-schema-labels"}, nil) |
396 | 427 | if err != nil {
|
397 |
| - return err |
| 428 | + return nil, err |
398 | 429 | }
|
399 |
| - return rnr.PopDir() |
400 |
| -} |
401 | 430 |
|
402 |
| -func cleanDiffProcessor(diffProcessorPath string, rnr ExecRunner) error { |
403 |
| - for _, path := range []string{"old", "new", "bin"} { |
404 |
| - if err := rnr.RemoveAll(filepath.Join(diffProcessorPath, path)); err != nil { |
405 |
| - return err |
406 |
| - } |
| 431 | + fmt.Println("Labels for changed schema: " + output) |
| 432 | + |
| 433 | + var labels []string |
| 434 | + if err = json.Unmarshal([]byte(output), &labels); err != nil { |
| 435 | + return nil, err |
| 436 | + } |
| 437 | + |
| 438 | + if err = rnr.PopDir(); err != nil { |
| 439 | + return nil, err |
407 | 440 | }
|
408 |
| - return nil |
| 441 | + return labels, nil |
409 | 442 | }
|
410 | 443 |
|
411 | 444 | // Run the missing test detector and return the results.
|
|
0 commit comments