Skip to content

Commit 05fd518

Browse files
authored
Added missing service label handling to generate_comment (#13143)
1 parent 3404bd4 commit 05fd518

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

.ci/magician/cmd/generate_comment.go

+54-18
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ type Errors struct {
6666
}
6767

6868
type diffCommentData struct {
69-
PrNumber int
70-
Diffs []Diff
71-
BreakingChanges []BreakingChange
72-
MissingTests map[string]*MissingTestInfo
73-
Errors []Errors
69+
PrNumber int
70+
Diffs []Diff
71+
BreakingChanges []BreakingChange
72+
MissingServiceLabels []string
73+
MissingTests map[string]*MissingTestInfo
74+
Errors []Errors
7475
}
7576

7677
type simpleSchemaDiff struct {
7778
AddedResources, ModifiedResources, RemovedResources []string
7879
}
7980

8081
const allowBreakingChangesLabel = "override-breaking-change"
82+
const allowMissingServiceLabelsLabel = "override-missing-service-labels"
8183

8284
var gcEnvironmentVariables = [...]string{
8385
"BUILD_ID",
@@ -263,6 +265,7 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
263265
data.Diffs = diffs
264266

265267
// The breaking changes are unique across both provider versions
268+
uniqueAddedResources := map[string]struct{}{}
266269
uniqueAffectedResources := map[string]struct{}{}
267270
uniqueBreakingChanges := map[string]BreakingChange{}
268271
diffProcessorPath := filepath.Join(mmLocalPath, "tools", "diff-processor")
@@ -309,12 +312,16 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
309312
data.MissingTests = missingTests
310313
}
311314

312-
affectedResources, err := computeAffectedResources(diffProcessorPath, rnr, repo)
315+
simpleDiff, err := computeAffectedResources(diffProcessorPath, rnr, repo)
313316
if err != nil {
314317
fmt.Println("computing changed resource schemas: ", err)
315318
errors[repo.Title] = append(errors[repo.Title], "The diff processor crashed while computing changed resource schemas.")
316319
}
317-
for _, resource := range affectedResources {
320+
for _, resource := range simpleDiff.AddedResources {
321+
uniqueAddedResources[resource] = struct{}{}
322+
uniqueAffectedResources[resource] = struct{}{}
323+
}
324+
for _, resource := range append(simpleDiff.ModifiedResources, simpleDiff.RemovedResources...) {
318325
uniqueAffectedResources[resource] = struct{}{}
319326
}
320327
}
@@ -390,10 +397,43 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
390397
}
391398
targetURL := fmt.Sprintf("https://console.cloud.google.com/cloud-build/builds;region=global/%s;step=%s?project=%s", buildId, buildStep, projectId)
392399
if err = gh.PostBuildStatus(strconv.Itoa(prNumber), "terraform-provider-breaking-change-test", breakingState, targetURL, commitSha); err != nil {
393-
fmt.Printf("Error posting build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
400+
fmt.Printf("Error posting terraform-provider-breaking-change-test build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
394401
errors["Other"] = append(errors["Other"], "Failed to update breaking-change status check with state: "+breakingState)
395402
}
396403

404+
// Flag missing service labels for added resources
405+
missingServiceLabels := []string{}
406+
if len(regexpLabels) > 0 {
407+
for resource, _ := range uniqueAddedResources {
408+
found := false
409+
for _, rl := range regexpLabels {
410+
if rl.Regexp.MatchString(resource) {
411+
found = true
412+
break
413+
}
414+
}
415+
if !found {
416+
missingServiceLabels = append(missingServiceLabels, resource)
417+
}
418+
}
419+
missingServiceLabelsState := "success"
420+
if len(missingServiceLabels) > 0 {
421+
missingServiceLabelsState = "failure"
422+
for _, label := range pullRequest.Labels {
423+
if label.Name == allowMissingServiceLabelsLabel {
424+
missingServiceLabelsState = "success"
425+
break
426+
}
427+
}
428+
}
429+
if err = gh.PostBuildStatus(strconv.Itoa(prNumber), "terraform-provider-missing-service-labels", missingServiceLabelsState, targetURL, commitSha); err != nil {
430+
fmt.Printf("Error posting terraform-provider-missing-service-labels build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
431+
errors["Other"] = append(errors["Other"], "Failed to update missing-service-labels status check with state: "+missingServiceLabelsState)
432+
}
433+
}
434+
sort.Strings(missingServiceLabels)
435+
data.MissingServiceLabels = missingServiceLabels
436+
397437
// Add errors to data as an ordered list
398438
errorsList := []Errors{}
399439
for _, repo := range []source.Repo{tpgRepo, tpgbRepo, tgcRepo, tfoicsRepo} {
@@ -466,31 +506,27 @@ func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]Breakin
466506
return changes, rnr.PopDir()
467507
}
468508

469-
func computeAffectedResources(diffProcessorPath string, rnr ExecRunner, repo source.Repo) ([]string, error) {
509+
func computeAffectedResources(diffProcessorPath string, rnr ExecRunner, repo source.Repo) (simpleSchemaDiff, error) {
470510
if err := rnr.PushDir(diffProcessorPath); err != nil {
471-
return nil, err
511+
return simpleSchemaDiff{}, err
472512
}
473513

474514
output, err := rnr.Run("bin/diff-processor", []string{"schema-diff"}, nil)
475515
if err != nil {
476-
return nil, err
516+
return simpleSchemaDiff{}, err
477517
}
478518

479519
fmt.Printf("Schema diff for %q: %s\n", repo.Name, output)
480520

481521
var simpleDiff simpleSchemaDiff
482522
if err = json.Unmarshal([]byte(output), &simpleDiff); err != nil {
483-
return nil, err
523+
return simpleSchemaDiff{}, err
484524
}
485525

486526
if err = rnr.PopDir(); err != nil {
487-
return nil, err
527+
return simpleSchemaDiff{}, err
488528
}
489-
var resources []string
490-
resources = append(resources, simpleDiff.AddedResources...)
491-
resources = append(resources, simpleDiff.ModifiedResources...)
492-
resources = append(resources, simpleDiff.RemovedResources...)
493-
return resources, nil
529+
return simpleDiff, nil
494530
}
495531

496532
// Run the missing test detector and return the results.

.ci/magician/cmd/generate_comment_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ func TestExecGenerateComment(t *testing.T) {
110110
}
111111

112112
for method, expectedCalls := range map[string][][]any{
113-
"PostBuildStatus": {{"123456", "terraform-provider-breaking-change-test", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"}},
114-
"PostComment": {{"123456", "Hi there, I'm the Modular magician. I've detected the following information about your changes:\n\n## Diff report\n\nYour PR generated some diffs in downstreams - here they are.\n\n`google` provider: [Diff](https://github.com/modular-magician/terraform-provider-google/compare/auto-pr-123456-old..auto-pr-123456) ( 2 files changed, 40 insertions(+))\n`google-beta` provider: [Diff](https://github.com/modular-magician/terraform-provider-google-beta/compare/auto-pr-123456-old..auto-pr-123456) ( 2 files changed, 40 insertions(+))\n`terraform-google-conversion`: [Diff](https://github.com/modular-magician/terraform-google-conversion/compare/auto-pr-123456-old..auto-pr-123456) ( 1 file changed, 10 insertions(+))\n\n\n\n## Missing test report\nYour PR includes resource fields which are not covered by any test.\n\nResource: `google_folder_access_approval_settings` (3 total tests)\nPlease add an acceptance test which includes these fields. The test should include the following:\n\n```hcl\nresource \"google_folder_access_approval_settings\" \"primary\" {\n uncovered_field = # value needed\n}\n\n```\n"}},
115-
"AddLabels": {{"123456", []string{"service/alloydb"}}},
113+
"PostBuildStatus": {
114+
{"123456", "terraform-provider-breaking-change-test", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"},
115+
{"123456", "terraform-provider-missing-service-labels", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"},
116+
},
117+
"PostComment": {{"123456", "Hi there, I'm the Modular magician. I've detected the following information about your changes:\n\n## Diff report\n\nYour PR generated some diffs in downstreams - here they are.\n\n`google` provider: [Diff](https://github.com/modular-magician/terraform-provider-google/compare/auto-pr-123456-old..auto-pr-123456) ( 2 files changed, 40 insertions(+))\n`google-beta` provider: [Diff](https://github.com/modular-magician/terraform-provider-google-beta/compare/auto-pr-123456-old..auto-pr-123456) ( 2 files changed, 40 insertions(+))\n`terraform-google-conversion`: [Diff](https://github.com/modular-magician/terraform-google-conversion/compare/auto-pr-123456-old..auto-pr-123456) ( 1 file changed, 10 insertions(+))\n\n\n\n## Missing test report\nYour PR includes resource fields which are not covered by any test.\n\nResource: `google_folder_access_approval_settings` (3 total tests)\nPlease add an acceptance test which includes these fields. The test should include the following:\n\n```hcl\nresource \"google_folder_access_approval_settings\" \"primary\" {\n uncovered_field = # value needed\n}\n\n```\n\n\n"}},
118+
"AddLabels": {{"123456", []string{"service/alloydb"}}},
116119
} {
117120
if actualCalls, ok := gh.calledMethods[method]; !ok {
118121
t.Fatalf("Found no calls for %s", method)

.ci/magician/cmd/templates/DIFF_COMMENT.md.tmpl

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ Your PR generated some diffs in downstreams - here they are.
1111
{{end -}}
1212
{{end -}}
1313

14-
{{- $breakingChangesLength := len .BreakingChanges }}
15-
{{- if gt $breakingChangesLength 0}}
14+
{{if gt (len .BreakingChanges) 0}}
1615
## Breaking Change(s) Detected
1716

1817
The following breaking change(s) were detected within your pull request.
@@ -40,6 +39,18 @@ Please add an acceptance test which includes these fields. The test should inclu
4039
{{- end }}
4140
{{end}}
4241

42+
{{if gt (len .MissingServiceLabels) 0}}
43+
## Missing service labels
44+
45+
The following new resources do not have corresponding service labels:
46+
47+
{{- range .MissingServiceLabels}}
48+
- {{.}}{{end}}
49+
50+
If you believe this detection to be incorrect please raise the concern with your reviewer.
51+
An `override-missing-service-label` label can be added to allow merging.
52+
{{end}}
53+
4354
{{- $errorsLength := len .Errors}}
4455
{{- if gt $errorsLength 0}}
4556
## Errors

0 commit comments

Comments
 (0)