Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -84,6 +85,7 @@ type diffCommentData struct {
MissingServiceLabels []string
MissingTests map[string]*MissingTestInfo
MissingDocs *MissingDocsSummary
AddedResources []string
Errors []Errors
}

Expand All @@ -93,6 +95,7 @@ type simpleSchemaDiff struct {

const allowBreakingChangesLabel = "override-breaking-change"
const allowMissingServiceLabelsLabel = "override-missing-service-labels"
const allowMultipleResourcesLabel = "override-multiple-resources"

var gcEnvironmentVariables = [...]string{
"BUILD_ID",
Expand Down Expand Up @@ -363,6 +366,25 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
})
data.BreakingChanges = breakingChangesSlice

// Check if multiple resources were added.
multipleResourcesState := "success"
if len(uniqueAddedResources) > 1 {
multipleResourcesState = "failure"
for _, label := range pullRequest.Labels {
if label.Name == allowMultipleResourcesLabel {
multipleResourcesState = "success"
break
}
}
}
targetURL := fmt.Sprintf("https://console.cloud.google.com/cloud-build/builds;region=global/%s;step=%s?project=%s", buildId, buildStep, projectId)
if err = gh.PostBuildStatus(strconv.Itoa(prNumber), "terraform-provider-multiple-resources", multipleResourcesState, targetURL, commitSha); err != nil {
fmt.Printf("Error posting terraform-provider-multiple-resources build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
errors["Other"] = append(errors["Other"], "Failed to update missing-service-labels status check with state: "+multipleResourcesState)
}
data.AddedResources = maps.Keys(uniqueAddedResources)
slices.Sort(data.AddedResources)

// Compute affected resources based on changed files
changedFilesAffectedResources := map[string]struct{}{}
for _, repo := range []source.Repo{tpgRepo, tpgbRepo} {
Expand Down Expand Up @@ -427,7 +449,6 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
}
}
}
targetURL := fmt.Sprintf("https://console.cloud.google.com/cloud-build/builds;region=global/%s;step=%s?project=%s", buildId, buildStep, projectId)
if err = gh.PostBuildStatus(strconv.Itoa(prNumber), "terraform-provider-breaking-change-test", breakingState, targetURL, commitSha); err != nil {
fmt.Printf("Error posting terraform-provider-breaking-change-test build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
errors["Other"] = append(errors["Other"], "Failed to update breaking-change status check with state: "+breakingState)
Expand Down
20 changes: 20 additions & 0 deletions .ci/magician/cmd/generate_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestExecGenerateComment(t *testing.T) {

for method, expectedCalls := range map[string][][]any{
"PostBuildStatus": {
{"123456", "terraform-provider-multiple-resources", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"},
{"123456", "terraform-provider-breaking-change-test", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"},
{"123456", "terraform-provider-missing-service-labels", "success", "https://console.cloud.google.com/cloud-build/builds;region=global/build1;step=17?project=project1", "sha1"},
},
Expand Down Expand Up @@ -242,6 +243,25 @@ func TestFormatDiffComment(t *testing.T) {
"## Missing test report",
},
},
"multiple resources are displayed": {
data: diffCommentData{
AddedResources: []string{"google_redis_instance", "google_alloydb_cluster"},
},
expectedStrings: []string{
"## Diff report",
"## Multiple resources added",
"`override-multiple-resources`",
"split it into multiple PRs",
"`google_redis_instance`, `google_alloydb_cluster`.",
},
notExpectedStrings: []string{
"generated some diffs",
"## Errors",
"## Missing test report",
"## Missing doc report",
"## Breaking Change(s) Detected",
},
},
"missing tests are displayed": {
data: diffCommentData{
MissingTests: map[string]*MissingTestInfo{
Expand Down
7 changes: 7 additions & 0 deletions .ci/magician/cmd/templates/DIFF_COMMENT.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ If you believe this detection to be incorrect please raise the concern with your
An `override-missing-service-label` label can be added to allow merging.
{{end}}

{{- if gt (len .AddedResources) 1 }}
## Multiple resources added

This PR adds multiple new resources: {{range $i, $resource := .AddedResources}}{{ if gt $i 0}}, {{end}}`{{$resource}}`{{end}}. This makes review significantly more difficult. Please split it into multiple PRs, one per resource.
An `override-multiple-resources` label can be added to allow merging.
{{end}}

{{- if and (.MissingDocs) (or .MissingDocs.Resource .MissingDocs.DataSource) }}
## Missing doc report (experimental)

Expand Down
Loading