Skip to content

Commit 11a4b1c

Browse files
authored
Merge pull request #8945 from GoogleCloudPlatform/main_sync
2 parents 75dcc62 + ffda752 commit 11a4b1c

File tree

164 files changed

+9555
-560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+9555
-560
lines changed

.ci/containers/membership-checker/membership.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var (
3030

3131
// This is for reviewers who are "on vacation": will not receive new review assignments but will still receive re-requests for assigned PRs.
3232
onVacationReviewers = []string{
33-
"zli82016",
3433
"slevenick",
34+
"roaks3",
3535
}
3636
)
3737

.ci/gcb-community-checker.yml

+62-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,71 @@
11
---
22
steps:
3-
- name: 'gcr.io/graphite-docker-images/membership-checker'
4-
id: community-checker
5-
secretEnv: ["GITHUB_TOKEN", "GENERATE_DIFFS_TRIGGER", "RAKE_TESTS_TRIGGER"]
6-
timeout: 8000s
7-
args:
8-
- "needs_approval"
9-
- $_PR_NUMBER
10-
- $COMMIT_SHA
11-
- $BRANCH_NAME
12-
- $_HEAD_REPO_URL
13-
- $_HEAD_BRANCH
14-
- $_BASE_BRANCH
3+
# The GCB / GH integration uses a shallow clone of the repo. We need to convert
4+
# that to a full clone in order to work with it properly.
5+
# https://cloud.google.com/source-repositories/docs/integrating-with-cloud-build#unshallowing_clones
6+
- name: "gcr.io/cloud-builders/git"
7+
args:
8+
- fetch
9+
- --unshallow
10+
11+
# Configure git
12+
- name: "gcr.io/cloud-builders/git"
13+
args:
14+
- config
15+
- --global
16+
- user.email
17+
18+
- name: "gcr.io/cloud-builders/git"
19+
args:
20+
- config
21+
- --global
22+
- user.name
23+
- "Modular Magician Diff Process"
24+
25+
# Fetch main (only if it's not already present)
26+
- name: "gcr.io/cloud-builders/git"
27+
args:
28+
- fetch
29+
- origin
30+
- main
31+
32+
# Display commit log for clarity
33+
- name: "gcr.io/cloud-builders/git"
34+
args:
35+
- log
36+
- "--oneline"
37+
- "-n 10"
38+
39+
# Find common ancestor commit and apply diff for the .ci folder
40+
- name: "gcr.io/cloud-builders/git"
41+
id: findMergeBase
42+
entrypoint: "bash"
43+
args:
44+
- "-c"
45+
- |
46+
base_commit=$(git merge-base origin/main HEAD)
47+
echo "Common ancestor commit: $base_commit"
48+
git diff $base_commit origin/main -- .ci/
49+
git diff $base_commit origin/main -- .ci/ > /workspace/ci.diff
50+
git apply /workspace/ci.diff --allow-empty
51+
52+
- name: 'gcr.io/graphite-docker-images/go-plus'
53+
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
54+
id: community-checker
55+
secretEnv: ["GITHUB_TOKEN", "GENERATE_DIFFS_TRIGGER"]
56+
timeout: 8000s
57+
args:
58+
- "community-checker"
59+
- $_PR_NUMBER
60+
- $COMMIT_SHA
61+
- $BRANCH_NAME
62+
- $_HEAD_REPO_URL
63+
- $_HEAD_BRANCH
64+
- $_BASE_BRANCH
1565

1666
availableSecrets:
1767
secretManager:
1868
- versionName: projects/673497134629/secrets/github-magician-token/versions/latest
1969
env: GITHUB_TOKEN
2070
- versionName: projects/673497134629/secrets/ci-trigger-generate-diffs/versions/latest
2171
env: GENERATE_DIFFS_TRIGGER
22-
- versionName: projects/673497134629/secrets/ci-trigger-rake-test/versions/latest
23-
env: RAKE_TESTS_TRIGGER
+63-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,74 @@
11
---
22
steps:
3-
- name: 'gcr.io/graphite-docker-images/membership-checker'
4-
id: contributor-membership-checker
5-
secretEnv: ["GITHUB_TOKEN", "GENERATE_DIFFS_TRIGGER", "RAKE_TESTS_TRIGGER", "COMMUNITY_CHECKER_TRIGGER"]
6-
timeout: 8000s
7-
args:
8-
- "auto_run"
9-
- $_PR_NUMBER
10-
- $COMMIT_SHA
11-
- $BRANCH_NAME
12-
- $_HEAD_REPO_URL
13-
- $_HEAD_BRANCH
14-
- $_BASE_BRANCH
3+
# The GCB / GH integration uses a shallow clone of the repo. We need to convert
4+
# that to a full clone in order to work with it properly.
5+
# https://cloud.google.com/source-repositories/docs/integrating-with-cloud-build#unshallowing_clones
6+
- name: "gcr.io/cloud-builders/git"
7+
args:
8+
- fetch
9+
- --unshallow
10+
11+
# Configure git
12+
- name: "gcr.io/cloud-builders/git"
13+
args:
14+
- config
15+
- --global
16+
- user.email
17+
18+
- name: "gcr.io/cloud-builders/git"
19+
args:
20+
- config
21+
- --global
22+
- user.name
23+
- "Modular Magician Diff Process"
24+
25+
# Fetch main (only if it's not already present)
26+
- name: "gcr.io/cloud-builders/git"
27+
args:
28+
- fetch
29+
- origin
30+
- main
31+
32+
# Display commit log for clarity
33+
- name: "gcr.io/cloud-builders/git"
34+
args:
35+
- log
36+
- "--oneline"
37+
- "-n 10"
38+
39+
# Find common ancestor commit and apply diff for the .ci folder
40+
- name: "gcr.io/cloud-builders/git"
41+
id: findMergeBase
42+
entrypoint: "bash"
43+
args:
44+
- "-c"
45+
- |
46+
base_commit=$(git merge-base origin/main HEAD)
47+
echo "Common ancestor commit: $base_commit"
48+
git diff $base_commit origin/main -- .ci/
49+
git diff $base_commit origin/main -- .ci/ > /workspace/ci.diff
50+
git apply /workspace/ci.diff --allow-empty
51+
52+
- name: "gcr.io/graphite-docker-images/go-plus"
53+
entrypoint: "/workspace/.ci/scripts/go-plus/magician/exec.sh"
54+
id: contributor-membership-checker
55+
secretEnv:
56+
["GITHUB_TOKEN", "GENERATE_DIFFS_TRIGGER", "COMMUNITY_CHECKER_TRIGGER"]
57+
timeout: 8000s
58+
args:
59+
- "membership-checker"
60+
- $_PR_NUMBER
61+
- $COMMIT_SHA
62+
- $BRANCH_NAME
63+
- $_HEAD_REPO_URL
64+
- $_HEAD_BRANCH
65+
- $_BASE_BRANCH
1566

1667
availableSecrets:
1768
secretManager:
1869
- versionName: projects/673497134629/secrets/github-magician-token/versions/latest
1970
env: GITHUB_TOKEN
2071
- versionName: projects/673497134629/secrets/ci-trigger-generate-diffs/versions/latest
2172
env: GENERATE_DIFFS_TRIGGER
22-
- versionName: projects/673497134629/secrets/ci-trigger-rake-test/versions/latest
23-
env: RAKE_TESTS_TRIGGER
2473
- versionName: projects/673497134629/secrets/ci-trigger-community-checker/versions/latest
2574
env: COMMUNITY_CHECKER_TRIGGER
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cloudbuild
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"google.golang.org/api/cloudbuild/v1"
9+
)
10+
11+
func (cb cloudBuild) TriggerMMPresubmitRuns(commitSha string, substitutions map[string]string) error {
12+
presubmitTriggerId, ok := os.LookupEnv("GENERATE_DIFFS_TRIGGER")
13+
if !ok {
14+
return fmt.Errorf("did not provide GENERATE_DIFFS_TRIGGER environment variable")
15+
}
16+
17+
err := triggerCloudBuildRun(PROJECT_ID, presubmitTriggerId, REPO_NAME, commitSha, substitutions)
18+
if err != nil {
19+
return err
20+
}
21+
22+
return nil
23+
}
24+
25+
func triggerCloudBuildRun(projectId, triggerId, repoName, commitSha string, substitutions map[string]string) error {
26+
ctx := context.Background()
27+
c, err := cloudbuild.NewService(ctx)
28+
if err != nil {
29+
return fmt.Errorf("failed to create Cloud Build service client: %s", err)
30+
}
31+
32+
repoSource := &cloudbuild.RepoSource{
33+
ProjectId: projectId,
34+
RepoName: repoName,
35+
CommitSha: commitSha,
36+
Substitutions: substitutions,
37+
}
38+
39+
_, err = c.Projects.Triggers.Run(projectId, triggerId, repoSource).Do()
40+
if err != nil {
41+
return fmt.Errorf("failed to create Cloud Build run: %s", err)
42+
}
43+
44+
fmt.Println("Started Cloud Build Run: ", triggerId)
45+
return nil
46+
}

.ci/magician/cloudbuild/community.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package cloudbuild
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"google.golang.org/api/cloudbuild/v1"
9+
)
10+
11+
func (cb cloudBuild) ApproveCommunityChecker(prNumber, commitSha string) error {
12+
buildId, err := getPendingBuildId(PROJECT_ID, commitSha)
13+
if err != nil {
14+
return err
15+
}
16+
17+
if buildId == "" {
18+
return fmt.Errorf("Failed to find pending build for PR %s", prNumber)
19+
}
20+
21+
err = approveBuild(PROJECT_ID, buildId)
22+
if err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
}
28+
29+
func (cb cloudBuild) GetAwaitingApprovalBuildLink(prNumber, commitSha string) (string, error) {
30+
buildId, err := getPendingBuildId(PROJECT_ID, commitSha)
31+
if err != nil {
32+
return "", err
33+
}
34+
35+
if buildId == "" {
36+
return "", fmt.Errorf("failed to find pending build for PR %s", prNumber)
37+
}
38+
39+
targetUrl := fmt.Sprintf("https://console.cloud.google.com/cloud-build/builds;region=global/%s?project=%s", buildId, PROJECT_ID)
40+
41+
return targetUrl, nil
42+
}
43+
44+
func getPendingBuildId(projectId, commitSha string) (string, error) {
45+
COMMUNITY_CHECKER_TRIGGER, ok := os.LookupEnv("COMMUNITY_CHECKER_TRIGGER")
46+
if !ok {
47+
return "", fmt.Errorf("Did not provide COMMUNITY_CHECKER_TRIGGER environment variable")
48+
}
49+
50+
ctx := context.Background()
51+
52+
c, err := cloudbuild.NewService(ctx)
53+
if err != nil {
54+
return "", err
55+
}
56+
57+
filter := fmt.Sprintf("trigger_id=%s AND status=PENDING", COMMUNITY_CHECKER_TRIGGER)
58+
// Builds will be sorted by createTime, descending order.
59+
// 50 should be enough to include the one needs auto approval
60+
pageSize := int64(50)
61+
62+
builds, err := c.Projects.Builds.List(projectId).Filter(filter).PageSize(pageSize).Do()
63+
if err != nil {
64+
return "", err
65+
}
66+
67+
for _, build := range builds.Builds {
68+
if build.Substitutions["COMMIT_SHA"] == commitSha {
69+
return build.Id, nil
70+
}
71+
}
72+
73+
return "", nil
74+
}
75+
76+
func approveBuild(projectId, buildId string) error {
77+
ctx := context.Background()
78+
79+
c, err := cloudbuild.NewService(ctx)
80+
if err != nil {
81+
return err
82+
}
83+
84+
name := fmt.Sprintf("projects/%s/builds/%s", projectId, buildId)
85+
86+
approveBuildRequest := &cloudbuild.ApproveBuildRequest{
87+
ApprovalResult: &cloudbuild.ApprovalResult{
88+
Decision: "APPROVED",
89+
},
90+
}
91+
92+
_, err = c.Projects.Builds.Approve(name, approveBuildRequest).Do()
93+
if err != nil {
94+
return err
95+
}
96+
97+
fmt.Println("Auto approved build ", buildId)
98+
99+
return nil
100+
}

.ci/magician/cloudbuild/constants.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package cloudbuild
2+
3+
const PROJECT_ID = "graphite-docker-images"
4+
const REPO_NAME = "magic-modules"

.ci/magician/cloudbuild/init.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cloudbuild
2+
3+
type cloudBuild bool
4+
5+
type CloudBuild interface {
6+
ApproveCommunityChecker(prNumber, commitSha string) error
7+
GetAwaitingApprovalBuildLink(prNumber, commitSha string) (string, error)
8+
TriggerMMPresubmitRuns(commitSha string, substitutions map[string]string) error
9+
}
10+
11+
func NewCloudBuildService() CloudBuild {
12+
var x cloudBuild = true
13+
return x
14+
}

0 commit comments

Comments
 (0)