Skip to content

Commit 4394555

Browse files
authored
chore(internal/postprocessor): be selective about what PRs to update (#7280)
This limits the scope of which PRs are updated by our postprocessor. In the future we may actually want our post processor to run some tidy commands and vet peoples code for them. But we should discuss this with the rest of the team first.
1 parent 447afdd commit 4394555

File tree

4 files changed

+109
-31
lines changed

4 files changed

+109
-31
lines changed

.github/.OwlBot.lock.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414
docker:
1515
image: gcr.io/cloud-devrel-public-resources/owlbot-go:latest
16-
digest: sha256:614ecada3e5d1610bf15439e389c455f26fb070d0a14aee87cf5b6a15d753153
16+
digest: sha256:989e97271b91bb0bd869bd5d714d1ea076136090657aab7ba30635bfa1b38395

internal/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ To kick off a build locally run from the repo root:
2424
```bash
2525
gcloud builds submit --project=cloud-devrel-kokoro-resources --config=internal/cloudbuild.yaml
2626
```
27+
28+
### Updating OwlBot SHA
29+
30+
You may want to manually update the which version of the post processor will be
31+
used -- to do this you need to update the SHA in the OwlBot lock file. Start by
32+
running the following commands:
33+
34+
```bash
35+
docker pull gcr.io/cloud-devrel-public-resources/owlbot-go:latest
36+
docker inspect --format='{{index .RepoDigests 0}}' gcr.io/cloud-devrel-public-resources/owlbot-go:latest
37+
```
38+
39+
This will give you a SHA. You can use this value to update the value in
40+
`.github/.OwlBot.lock.yaml`.
41+
42+
*Note*: OwlBot will eventually open a pull request to update this value if it
43+
discovers a new version of the container.

internal/postprocessor/README.md

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
11
# postprocessor
22

33
## Running OwlBot locally
4-
Follow instructions in [OwlBot Usage Guide - "How will I test my .github/.OwlBot.yaml file"](https://g3doc.corp.google.com/company/teams/cloud-client-libraries/team/automation/docs/owlbot-usage-guide.md?cl=head#how-will-i-test-my-githubowlbotyaml-file) using the instructions for **split repositories**.
5-
- Note, if you replace step 2 with a clone of your own fork of the `googleapis/googleapis-gen.git` repo, you can see how changes in your forked `googleapis-gen` repo are eventually propagated through to the library without making changes to the protos. Lack of permissions may also force you to clone a fork instead of the repo.
64

7-
After following these steps the generated code will have replaced corresponding files in the `google-cloud-go` repo.
5+
Follow instructions in [OwlBot Usage Guide - "How will I test my .github/.OwlBot.yaml file"](https://g3doc.corp.google.com/company/teams/cloud-client-libraries/team/automation/docs/owlbot-usage-guide.md?cl=head#how-will-i-test-my-githubowlbotyaml-file) using the instructions for
6+
**split repositories**.
7+
**Note**, if you replace step 2 with a clone of your own fork of the
8+
`googleapis/googleapis-gen.git` repo, you can see how changes in your forked
9+
`googleapis-gen` repo are eventually propagated through to the library without
10+
making changes to the protos. Lack of permissions may also force you to clone a
11+
fork instead of the repo.
12+
13+
After following these steps the generated code will have replaced corresponding
14+
files in the `google-cloud-go` repo.
815

916
## Docker container
10-
The Docker container needs to be built with the context of the entire `google-cloud-go/internal` directory. When building the container, do so from the `google-cloud-go/internal` directory
17+
18+
The Docker container needs to be built with the context of the entire
19+
`google-cloud-go/internal` directory. When building the container, do so from
20+
the `google-cloud-go/internal` directory
1121

1222
## Running the post-processor locally
13-
The Docker container name needed will be found in the `.github/OwlBot.yaml` and `github/OwlBot.lock.yaml` files.
14-
```
15-
- To run post-processor run:
16-
```sh
17-
docker pull <container-name>
18-
docker run --user $(id -u):$(id -g) --rm -v $(pwd):/repo -w /repo <container-name>
19-
```
23+
24+
The Docker container name needed will be found in the `.github/OwlBot.yaml` and
25+
`github/OwlBot.lock.yaml` files.
26+
To run post-processor run:
27+
28+
```bash
29+
docker pull <container-name>
30+
docker run --user $(id -u):$(id -g) --rm -v $(pwd):/repo -w /repo <container-name>
31+
```
2032

2133
## Testing the post-processor locally
22-
You can run the post-processor locally on selected directories or on all of the clients in the root directory.
34+
35+
You can run the post-processor locally on selected directories or on all of the
36+
clients in the root directory.
2337

2438
### Run post-processor on all clients
25-
From the `google-cloud-go/internal/postprocessor` directory run:
26-
```sh
39+
40+
From the `google-cloud-go/internal/postprocessor` directory run:
41+
42+
```bash
2743
go run main.go -stage-dir="../../owl-bot-staging/src/" -client-root="../.." -googleapis-dir="/path/to/local/googleapis"
2844
```
45+
2946
### Run post-processor on select clients
30-
From the `google-cloud-go/internal/postprocessor` directory run the same command, but with an added `dirs` flag containing a comma-separated list of the names of the clients on which to run the post-processor. The example below shows the command for running the post-processor on the `accessapproval` and `asset` libraries:
31-
```sh
47+
48+
From the `google-cloud-go/internal/postprocessor` directory run the same
49+
command, but with an added `dirs` flag containing a comma-separated list of the
50+
names of the clients on which to run the post-processor. The example below shows
51+
the command for running the post-processor on the `accessapproval` and `asset`
52+
libraries:
53+
54+
```bash
3255
go run main.go -stage-dir="../../owl-bot-staging/src/" -client-root="../.." -googleapis-dir="/path/to/local/googleapis" -dirs="accessapproval,asset"
3356
```

internal/postprocessor/main.go

+52-14
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,40 @@ import (
3737
"gopkg.in/yaml.v2"
3838
)
3939

40-
var apiNameOwlBotScope = "[REPLACEME]"
41-
42-
// hashFromLinePattern grabs the hash from the end of a github commit URL
43-
var hashFromLinePattern = regexp.MustCompile(`.*/(?P<hash>[a-zA-Z0-9]*).*`)
44-
45-
// firstPartTitlePattern grabs the existing commit title before the ': [REPLACEME]'
46-
var firstPartTitlePattern = regexp.MustCompile(`(?P<titleFirstPart>)(\: *\` + apiNameOwlBotScope + `)(.*)`)
40+
const (
41+
owlBotBranchPrefix = "owl-bot-copy"
42+
apiNameOwlBotScope = "[REPLACEME]"
43+
)
4744

48-
// secondPartTitlePattern grabs the commit title after the ': [REPLACME]'
49-
var secondPartTitlePattern = regexp.MustCompile(`.*\: *\` + apiNameOwlBotScope + ` *(?P<titleSecondPart>.*)`)
45+
var (
46+
// hashFromLinePattern grabs the hash from the end of a github commit URL
47+
hashFromLinePattern = regexp.MustCompile(`.*/(?P<hash>[a-zA-Z0-9]*).*`)
48+
// firstPartTitlePattern grabs the existing commit title before the ': [REPLACEME]'
49+
firstPartTitlePattern = regexp.MustCompile(`(?P<titleFirstPart>)(\: *\` + apiNameOwlBotScope + `)(.*)`)
50+
// secondPartTitlePattern grabs the commit title after the ': [REPLACME]'
51+
secondPartTitlePattern = regexp.MustCompile(`.*\: *\` + apiNameOwlBotScope + ` *(?P<titleSecondPart>.*)`)
52+
)
5053

5154
func main() {
5255
clientRoot := flag.String("client-root", "/workspace/google-cloud-go", "Path to clients.")
5356
googleapisDir := flag.String("googleapis-dir", "", "Path to googleapis/googleapis repo.")
5457
directories := flag.String("dirs", "", "Comma-separated list of module names to run (not paths).")
55-
branchPrefix := flag.String("branch", "owl-bot-copy", "The prefix of the branch that OwlBot opens when working on a PR.")
58+
branchOverride := flag.String("branch", "", "The branch that should be processed by this code")
5659
githubUsername := flag.String("gh-user", "googleapis", "GitHub username where repo lives.")
5760
prFilepath := flag.String("pr-file", "/workspace/new_pull_request_text.txt", "Path at which to write text file if changing PR title or body.")
5861

5962
flag.Parse()
6063

64+
runAll, err := runAll(*clientRoot, *branchOverride)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
6169
ctx := context.Background()
6270

6371
log.Println("client-root set to", *clientRoot)
6472
log.Println("googleapis-dir set to", *googleapisDir)
65-
log.Println("branch set to", *branchPrefix)
73+
log.Println("branch set to", *branchOverride)
6674
log.Println("prFilepath is", *prFilepath)
6775

6876
var modules []string
@@ -96,9 +104,10 @@ func main() {
96104
googleapisDir: *googleapisDir,
97105
googleCloudDir: *clientRoot,
98106
modules: modules,
99-
branchPrefix: *branchPrefix,
107+
branchOverride: *branchOverride,
100108
githubUsername: *githubUsername,
101109
prFilepath: *prFilepath,
110+
runAll: runAll,
102111
}
103112

104113
if err := c.run(ctx); err != nil {
@@ -112,12 +121,35 @@ type config struct {
112121
googleapisDir string
113122
googleCloudDir string
114123
modules []string
115-
branchPrefix string
124+
branchOverride string
116125
githubUsername string
117126
prFilepath string
127+
runAll bool
128+
}
129+
130+
// runAll uses git to tell if the PR being updated should run all post
131+
// processing logic.
132+
func runAll(dir, branchOverride string) (bool, error) {
133+
if branchOverride != "" {
134+
// This means we are running the post processor locally and want it to
135+
// fully function -- so we lie.
136+
return true, nil
137+
}
138+
c := execv.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
139+
c.Dir = dir
140+
b, err := c.Output()
141+
if err != nil {
142+
return false, err
143+
}
144+
branchName := strings.TrimSpace(string(b))
145+
return strings.HasPrefix(branchName, owlBotBranchPrefix), nil
118146
}
119147

120148
func (c *config) run(ctx context.Context) error {
149+
if !c.runAll {
150+
log.Println("exiting post processing early")
151+
return nil
152+
}
121153
if err := gocmd.ModTidyAll(c.googleCloudDir); err != nil {
122154
return err
123155
}
@@ -130,6 +162,8 @@ func (c *config) run(ctx context.Context) error {
130162
if _, err := c.Manifest(generator.MicrogenGapicConfigs); err != nil {
131163
return err
132164
}
165+
// TODO(codyoss): In the future we may want to make it possible to be able
166+
// to run this locally with a user defined remote branch.
133167
if err := c.AmendPRDescription(ctx); err != nil {
134168
return err
135169
}
@@ -300,8 +334,12 @@ func (c *config) getPR(ctx context.Context) (*github.PullRequest, error) {
300334
return nil, err
301335
}
302336
var owlbotPR *github.PullRequest
337+
branch := c.branchOverride
338+
if c.branchOverride == "" {
339+
branch = owlBotBranchPrefix
340+
}
303341
for _, pr := range prs {
304-
if strings.Contains(*pr.Head.Label, c.branchPrefix) {
342+
if strings.Contains(*pr.Head.Label, branch) {
305343
owlbotPR = pr
306344
}
307345
}

0 commit comments

Comments
 (0)