| 1 |
// Copyright 2020 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
//go:build !windows |
| 16 |
// +build !windows |
| 17 |
|
| 18 |
package main |
| 19 |
|
| 20 |
import ( |
| 21 |
"context" |
| 22 |
"fmt" |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
|
| 27 |
"cloud.google.com/go/internal/gapicgen/generator" |
| 28 |
"cloud.google.com/go/internal/gapicgen/git" |
| 29 |
"golang.org/x/sync/errgroup" |
| 30 |
) |
| 31 |
|
| 32 |
// generate downloads sources and generates pull requests for go-genproto and |
| 33 |
// google-cloud-go if needed. |
| 34 |
func generate(ctx context.Context, githubClient *git.GithubClient, forceAll bool) error { |
| 35 |
log.Println("creating temp dir") |
| 36 |
tmpDir, err := os.MkdirTemp("", "update-genproto") |
| 37 |
if err != nil { |
| 38 |
return err |
| 39 |
} |
| 40 |
defer os.RemoveAll(tmpDir) |
| 41 |
|
| 42 |
log.Printf("working out %s\n", tmpDir) |
| 43 |
|
| 44 |
googleapisDir := filepath.Join(tmpDir, "googleapis") |
| 45 |
genprotoDir := filepath.Join(tmpDir, "genproto") |
| 46 |
protoDir := filepath.Join(tmpDir, "proto") |
| 47 |
|
| 48 |
// Clone repositories. |
| 49 |
grp, _ := errgroup.WithContext(ctx) |
| 50 |
grp.Go(func() error { |
| 51 |
return git.DeepClone("https://github.com/googleapis/googleapis", googleapisDir) |
| 52 |
}) |
| 53 |
grp.Go(func() error { |
| 54 |
return git.DeepClone("https://github.com/googleapis/go-genproto", genprotoDir) |
| 55 |
}) |
| 56 |
|
| 57 |
grp.Go(func() error { |
| 58 |
return git.DeepClone("https://github.com/protocolbuffers/protobuf", protoDir) |
| 59 |
}) |
| 60 |
if err := grp.Wait(); err != nil { |
| 61 |
log.Println(err) |
| 62 |
} |
| 63 |
|
| 64 |
// Regen. |
| 65 |
conf := &generator.Config{ |
| 66 |
GoogleapisDir: googleapisDir, |
| 67 |
GenprotoDir: genprotoDir, |
| 68 |
ProtoDir: protoDir, |
| 69 |
ForceAll: forceAll, |
| 70 |
} |
| 71 |
changes, err := generator.Generate(ctx, conf) |
| 72 |
if err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
|
| 76 |
// Create PR |
| 77 |
genprotoHasChanges, err := git.HasChanges(genprotoDir) |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
if !genprotoHasChanges { |
| 83 |
log.Println("no changes detected") |
| 84 |
return nil |
| 85 |
} |
| 86 |
genprotoPRNum, err := githubClient.CreateGenprotoPR(ctx, genprotoDir, false, changes) |
| 87 |
if err != nil { |
| 88 |
return fmt.Errorf("error creating PR for genproto (may need to check logs for more errors): %w", err) |
| 89 |
} |
| 90 |
log.Printf("https://github.com/googleapis/go-genproto/pull/%d", genprotoPRNum) |
| 91 |
return nil |
| 92 |
} |