-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
update.go
98 lines (86 loc) · 2.92 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020 Google LLC
//
// 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.
// +build !windows
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
)
func updateGocloudPR(ctx context.Context, githubClient *GithubClient, pr *PullRequest) error {
if pr.Author != githubClient.Username {
return fmt.Errorf("Pull request author %q does not match authenticated user %q", pr.Author, githubClient.Username)
}
// Checkout PR and update go.mod
if err := updateGocloudGoMod(pr); err != nil {
return err
}
if pr.IsDraft {
if err := githubClient.MarkPRReadyForReview(ctx, pr.Repo, pr.NodeID); err != nil {
return fmt.Errorf("unable to mark PR %v ready for review: %v", pr.Number, err)
}
}
// Done!
log.Printf("done updating google-cloud-go PR: %s\n", pr.URL)
return nil
}
// updateGocloudGoMod updates the go.mod to include latest version of genproto
// for the given gocloud ref.
func updateGocloudGoMod(pr *PullRequest) error {
tmpDir, err := ioutil.TempDir("", "finalize-gerrit-cl")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
c := exec.Command("/bin/bash", "-c", `
set -ex
git init
git remote add origin https://github.com/googleapis/google-cloud-go
git fetch --all
git checkout $BRANCH_NAME
# tidyall
go mod tidy
for i in $(find . -name go.mod); do
pushd $(dirname $i);
# Update genproto and api to latest for every module (latest version is
# always correct version). tidy will remove the dependencies if they're not
# actually used by the client.
go get -d google.golang.org/api | true # We don't care that there's no files at root.
go get -d google.golang.org/genproto | true # We don't care that there's no files at root.
go mod tidy;
popd;
done
git add -A
filesUpdated=$( git status --short | wc -l )
if [ $filesUpdated -gt 0 ];
then
git config credential.helper store # cache creds from ~/.git-credentials
git commit --amend --no-edit
git push -f origin $BRANCH_NAME
fi
`)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin // Prevents "the input device is not a TTY" error.
c.Env = []string{
fmt.Sprintf("BRANCH_NAME=%s", gocloudBranchName),
fmt.Sprintf("PATH=%s", os.Getenv("PATH")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands.
fmt.Sprintf("HOME=%s", os.Getenv("HOME")), // TODO(deklerk): Why do we need to do this? Doesn't seem to be necessary in other exec.Commands.
}
c.Dir = tmpDir
return c.Run()
}