|
| 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 | +} |
0 commit comments