Skip to content

Commit

Permalink
Merge branch 'GoogleCloudPlatform:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
niharika-98 authored Oct 28, 2024
2 parents 5e5d9f8 + 551f480 commit 780b1ad
Show file tree
Hide file tree
Showing 470 changed files with 7,489 additions and 2,278 deletions.
4 changes: 2 additions & 2 deletions .ci/containers/build-environment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ RUN git config --global user.email "[email protected]"
RUN go install golang.org/x/tools/cmd/goimports@d088b475e3360caabc032aaee1dc66351d4e729a
RUN go install github.com/github/[email protected]+incompatible

ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/main/mmv1/Gemfile" Gemfile
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/main/mmv1/Gemfile.lock" Gemfile.lock
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/refs/heads/legacy-ruby/mmv1/Gemfile" Gemfile
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/refs/heads/legacy-ruby/mmv1/Gemfile.lock" Gemfile.lock
RUN bundle install
RUN rm Gemfile Gemfile.lock

Expand Down
1 change: 1 addition & 0 deletions .ci/infra/terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ After applying this configuration:
- Enroll in Cloud Armor Managed Protection Plus tier
- Add Cloud Identity Premium Plan to the Google Workspace domain
- Perform the Privileged Access Manager set-up https://pantheon.corp.google.com/iam-admin/pam/setup
- (Org only) Enroll the org in the Premium tier of Security Control Center
Quotas that will need to be adjusted to support all tests:
- Project quota for the new service account
Expand Down
6 changes: 6 additions & 0 deletions .ci/infra/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ resource "google_organization_iam_member" "sa_securitycenter_bigquery_exports_ed
member = google_service_account.sa.member
}

resource "google_organization_iam_member" "sa_principal_access_boundary_admin" {
org_id = data.google_organization.org.org_id
role = "roles/iam.principalAccessBoundaryAdmin"
member = google_service_account.sa.member
}

resource "google_billing_account_iam_member" "sa_master_billing_admin" {
billing_account_id = data.google_billing_account.master_acct.id
role = "roles/billing.admin"
Expand Down
2 changes: 2 additions & 0 deletions .ci/magician/cmd/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ type GithubClient interface {
GetPullRequests(state, base, sort, direction string) ([]github.PullRequest, error)
GetPullRequestRequestedReviewers(prNumber string) ([]github.User, error)
GetPullRequestPreviousReviewers(prNumber string) ([]github.User, error)
GetPullRequestComments(prNumber string) ([]github.PullRequestComment, error)
GetUserType(user string) github.UserType
GetTeamMembers(organization, team string) ([]github.User, error)
MergePullRequest(owner, repo, prNumber, commitSha string) error
PostBuildStatus(prNumber, title, state, targetURL, commitSha string) error
PostComment(prNumber, comment string) error
UpdateComment(prNumber, comment string, id int) error
RequestPullRequestReviewers(prNumber string, reviewers []string) error
AddLabels(prNumber string, labels []string) error
RemoveLabel(prNumber, label string) error
Expand Down
23 changes: 17 additions & 6 deletions .ci/magician/cmd/mock_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
)

type mockGithub struct {
pullRequest github.PullRequest
userType github.UserType
requestedReviewers []github.User
previousReviewers []github.User
teamMembers map[string][]github.User
calledMethods map[string][][]any
pullRequest github.PullRequest
userType github.UserType
requestedReviewers []github.User
previousReviewers []github.User
pullRequestComments []github.PullRequestComment
teamMembers map[string][]github.User
calledMethods map[string][][]any
}

func (m *mockGithub) GetPullRequest(prNumber string) (github.PullRequest, error) {
Expand Down Expand Up @@ -55,6 +56,11 @@ func (m *mockGithub) GetPullRequestPreviousReviewers(prNumber string) ([]github.
return m.previousReviewers, nil
}

func (m *mockGithub) GetPullRequestComments(prNumber string) ([]github.PullRequestComment, error) {
m.calledMethods["GetPullRequestComments"] = append(m.calledMethods["GetPullRequestComments"], []any{prNumber})
return m.pullRequestComments, nil
}

func (m *mockGithub) GetTeamMembers(organization, team string) ([]github.User, error) {
m.calledMethods["GetTeamMembers"] = append(m.calledMethods["GetTeamMembers"], []any{organization, team})
if team == "" {
Expand All @@ -73,6 +79,11 @@ func (m *mockGithub) PostComment(prNumber string, comment string) error {
return nil
}

func (m *mockGithub) UpdateComment(prNumber, comment string, id int) error {
m.calledMethods["UpdateComment"] = append(m.calledMethods["UpdateComment"], []any{prNumber, comment, id})
return nil
}

func (m *mockGithub) AddLabels(prNumber string, labels []string) error {
m.calledMethods["AddLabels"] = append(m.calledMethods["AddLabels"], []any{prNumber, labels})
return nil
Expand Down
126 changes: 126 additions & 0 deletions .ci/magician/cmd/reassign_reviewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd

import (
"errors"
"fmt"
"magician/github"
"os"

"github.com/spf13/cobra"
)

// reassignReviewerCmd represents the reassignReviewer command
var reassignReviewerCmd = &cobra.Command{
Use: "reassign-reviewer PR_NUMBER [REVIEWER]",
Short: "Reassigns primary reviewer to the given reviewer or a random reviewer if none given",
Long: `This command reassigns reviewers when invoked via a comment on a pull request.
The command expects the following PR details as arguments:
1. PR_NUMBER
2. REVIEWER (optional)
It then performs the following operations:
1. Updates the reviewer comment to reflect the new primary reviewer.
2. Requests a review from the new primary reviewer.
`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
prNumber := args[0]
fmt.Println("PR Number: ", prNumber)

githubToken, ok := os.LookupEnv("GITHUB_TOKEN")
if !ok {
return fmt.Errorf("did not provide GITHUB_TOKEN environment variable")
}
gh := github.NewClient(githubToken)
var newPrimaryReviewer string
if len(args) > 1 {
newPrimaryReviewer = args[1]
}
return execReassignReviewer(prNumber, newPrimaryReviewer, gh)
},
}

func execReassignReviewer(prNumber, newPrimaryReviewer string, gh GithubClient) error {
comments, err := gh.GetPullRequestComments(prNumber)
if err != nil {
return err
}

reviewerComment, currentReviewer := github.FindReviewerComment(comments)

if currentReviewer == "" {
fmt.Println("No reviewer comment found, creating one")
newPrimaryReviewer, err = createReviewComment(prNumber, newPrimaryReviewer, gh)
if err != nil {
return err
}
} else {
fmt.Println("Reassigning to random reviewer")
newPrimaryReviewer, err = updateReviewComment(prNumber, currentReviewer, newPrimaryReviewer, reviewerComment.ID, gh)
if err != nil {
return err
}
}

fmt.Println("New primary reviewer is ", newPrimaryReviewer)

err = gh.RequestPullRequestReviewers(prNumber, []string{newPrimaryReviewer})
if err != nil {
return err
}

return nil
}

func createReviewComment(prNumber, newPrimaryReviewer string, gh GithubClient) (string, error) {
if newPrimaryReviewer == "" {
newPrimaryReviewer = github.GetRandomReviewer()
}

if newPrimaryReviewer == "" {
return "", errors.New("no primary reviewer found")
}

err := gh.PostComment(prNumber, github.FormatReviewerComment(newPrimaryReviewer))
if err != nil {
return "", err
}
return newPrimaryReviewer, nil
}

func updateReviewComment(prNumber, currentReviewer, newPrimaryReviewer string, reviewerCommentID int, gh GithubClient) (string, error) {
if newPrimaryReviewer == "" {
newPrimaryReviewer = github.GetNewRandomReviewer(currentReviewer)
}

if currentReviewer == newPrimaryReviewer {
return newPrimaryReviewer, fmt.Errorf("primary reviewer is already %s", newPrimaryReviewer)
}

err := gh.UpdateComment(prNumber, github.FormatReviewerComment(newPrimaryReviewer), reviewerCommentID)
if err != nil {
return "", err
}
return newPrimaryReviewer, nil
}

func init() {
rootCmd.AddCommand(reassignReviewerCmd)
}
5 changes: 0 additions & 5 deletions .ci/magician/cmd/request_reviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ var requestReviewerCmd = &cobra.Command{
The command expects the following pull request details as arguments:
1. PR Number
2. Commit SHA
3. Branch Name
4. Head Repo URL
5. Head Branch
6. Base Branch
It then performs the following operations:
1. Determines the author of the pull request
Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/scheduled_pr_reminders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ func TestFormatReminderComment(t *testing.T) {
&github.User{Login: github.String("other-reviewer")},
},
},
state: waitingForMerge,
state: waitingForMerge,
sinceDays: 5,
expectedStrings: []string{
"waiting for merge for 1 week",
Expand Down
27 changes: 21 additions & 6 deletions .ci/magician/cmd/templates/vcr/record_replay.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{{- if gt (len .RecordingResult.PassedTests) 0 -}}
{{color "green" "Tests passed during RECORDING mode:"}}
{{range .RecordingResult.PassedTests}}`{{.}}`[[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{range .RecordingResult.PassedTests -}}
`{{.}}` {{/* remove trailing whitespace */ -}}
[[Debug log](https://storage.cloud.google.com/{{$.LogBucket}}/{{$.Version}}/refs/heads/{{$.Head}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{/* remove trailing whitespace */ -}}
{{end}}

{{- if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 -}}
{{- if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 }}

{{color "red" "Tests failed when rerunning REPLAYING mode:"}}
{{range .ReplayingAfterRecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/replaying_build_after_recording/{{.}}_replaying_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/replaying_after_recording/{{.}}.log)]
{{range .ReplayingAfterRecordingResult.FailedTests -}}
`{{.}}` {{/* remove trailing whitespace */ -}}
[[Error message](https://storage.cloud.google.com/{{$.LogBucket}}/{{$.Version}}/refs/heads/{{$.Head}}/artifacts/{{$.BuildID}}/build-log/replaying_build_after_recording/{{.}}_replaying_test.log)] {{/* remove trailing whitespace */ -}}
[[Debug log](https://storage.cloud.google.com/{{$.LogBucket}}/{{$.Version}}/refs/heads/{{$.Head}}/artifacts/{{$.BuildID}}/replaying_after_recording/{{.}}.log)]
{{/* remove trailing whitespace */ -}}
{{end}}

Tests failed due to non-determinism or randomness when the VCR replayed the response after the HTTP request was made.
Expand All @@ -20,12 +28,19 @@ Please fix these to complete your PR. If you believe these test failures to be i

{{if gt (len .RecordingResult.FailedTests) 0 -}}
{{color "red" "Tests failed during RECORDING mode:"}}
{{range .RecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/recording_build/{{.}}_recording_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{range .RecordingResult.FailedTests -}}
`{{.}}` {{/* remove trailing whitespace */ -}}
[[Error message](https://storage.cloud.google.com/{{$.LogBucket}}/{{$.Version}}/refs/heads/{{$.Head}}/artifacts/{{$.BuildID}}/build-log/recording_build/{{.}}_recording_test.log)] {{/* remove trailing whitespace */ -}}
[[Debug log](https://storage.cloud.google.com/{{$.LogBucket}}/{{$.Version}}/refs/heads/{{$.Head}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{/* remove trailing whitespace */ -}}
{{end}}
{{end}} {{- /* end of if gt (len .RecordingResult.FailedTests) 0 */ -}}

{{if .HasTerminatedTests}}{{color "red" "Several tests got terminated during RECORDING mode."}}{{end}}
{{if .HasTerminatedTests}}{{color "red" "Several tests terminated during RECORDING mode."}}{{end}}

{{if .RecordingErr}}{{color "red" "Errors occurred during RECORDING mode. Please fix them to complete your PR."}}{{end}}

{{if .AllRecordingPassed}}{{color "green" "All tests passed!"}}{{end}}

View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/recording_test.log) or the [debug log](https://console.cloud.google.com/storage/browser/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/recording) for each test
View the [build log](https://storage.cloud.google.com/{{.LogBucket}}/{{.Version}}/refs/heads/{{.Head}}/artifacts/{{.BuildID}}/build-log/recording_test.log) {{/* remove trailing whitespace */ -}}
or the [debug log](https://console.cloud.google.com/storage/browser/{{.LogBucket}}/{{.Version}}/refs/heads/{{.Head}}/artifacts/{{.BuildID}}/recording) for each test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Affected tests list:
{{- end}}
{{if .ReplayingErr}}
#################################
Errors occurred during REPLAYING mode: {{.ReplayingErr}}.
Errors occurred during REPLAYING mode.
#################################
{{- end}}
{{if .AllReplayingPassed}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
{{color "green" "All tests passed!"}}
{{- end}}

View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/replaying_test.log)
View the [build log](https://storage.cloud.google.com/{{.LogBucket}}/{{.Version}}/refs/heads/{{.Head}}/artifacts/{{.BuildID}}/build-log/replaying_test.log)
12 changes: 6 additions & 6 deletions .ci/magician/cmd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ func TestColor(t *testing.T) {
want: "🟡 Test text",
},
{
name: "green",
name: "green",
color: "green",
text: "Test text",
want: "🟢 Test text",
text: "Test text",
want: "🟢 Test text",
},
{
name: "unsupported color",
name: "unsupported color",
color: "mauve",
text: "Test text",
want: "Test text",
text: "Test text",
want: "Test text",
},
{
name: "empty color",
Expand Down
Loading

0 comments on commit 780b1ad

Please sign in to comment.