-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from vitessio/andrew/cobradocs-sync
- Loading branch information
Showing
4 changed files
with
245 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
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. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/go-github/v53/github" | ||
"github.com/pkg/errors" | ||
"github.com/vitess.io/vitess-bot/go/git" | ||
"github.com/vitess.io/vitess-bot/go/shell" | ||
) | ||
|
||
// synchronize cobradocs from main and release branches | ||
func synchronizeCobraDocs( | ||
ctx context.Context, | ||
client *github.Client, | ||
vitess *git.Repo, | ||
website *git.Repo, | ||
pr *github.PullRequest, | ||
prInfo prInformation, | ||
) (*github.PullRequest, error) { | ||
op := "update cobradocs" | ||
branch := "prod" | ||
|
||
websiteProdRef, _, err := client.Git.GetRef(ctx, website.Owner, website.Name, "heads/"+branch) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to fetch prod ref for repository %s/%s to %s on Pull Request %d", website.Owner, website.Name, op, prInfo.num) | ||
} | ||
|
||
newBranch := fmt.Sprintf("synchronize-cobradocs-for-%d", pr.GetNumber()) | ||
_, err = website.CreateBranch(ctx, client, websiteProdRef, newBranch) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to create git ref %s ref for repository %s/%s to %s on Pull Request %d", newBranch, website.Owner, website.Name, op, prInfo.num) | ||
} | ||
|
||
if err := setupRepo(ctx, vitess, prInfo, op); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := vitess.FetchRef(ctx, "origin", "--tags"); err != nil { | ||
return nil, errors.Wrapf(err, "Failed to fetch tags in repository %s/%s to %s on Pull Request %d", vitess.Owner, vitess.Name, op, prInfo.num) | ||
} | ||
|
||
if err := setupRepo(ctx, website, prInfo, op); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Checkout the new branch we created. | ||
if err := website.Checkout(ctx, newBranch); err != nil { | ||
return nil, errors.Wrapf(err, "Failed to checkout repository %s/%s to branch %s to %s on Pull Request %d", website.Owner, website.Name, newBranch, op, prInfo.num) | ||
} | ||
|
||
// Run the sync script (which authors the commit already). | ||
_, err = shell.NewContext(ctx, "./tools/sync_cobradocs.sh").InDir(website.LocalDir).WithExtraEnv( | ||
fmt.Sprintf("VITESS_DIR=%s", vitess.LocalDir), | ||
"COBRADOCS_SYNC_PERSIST=yes", | ||
).Output() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to run cobradoc sync script in repository %s/%s to %s on Pull Request %d", website.Owner, website.Name, newBranch, prInfo.num) | ||
} | ||
|
||
// TODO: do we need to amend the commit to change the author to the bot? | ||
|
||
// Push the branch | ||
if err := website.Push(ctx, git.PushOpts{ | ||
Remote: "origin", | ||
Refs: []string{newBranch}, | ||
Force: true, | ||
}); err != nil { | ||
return nil, errors.Wrapf(err, "Failed to push %s to %s on Pull Request %d", newBranch, op, prInfo.num) | ||
} | ||
|
||
// Create a Pull Request for the new branch | ||
newPR := &github.NewPullRequest{ | ||
Title: github.String(fmt.Sprintf("[cobradocs] synchronize with %s (vitess#%d)", pr.GetTitle(), pr.GetNumber())), | ||
Head: github.String(newBranch), | ||
Base: github.String(branch), | ||
Body: github.String(fmt.Sprintf("## Description\nThis is an automated PR to synchronize the cobradocs with %s", pr.GetHTMLURL())), | ||
MaintainerCanModify: github.Bool(true), | ||
} | ||
newPRCreated, _, err := client.PullRequests.Create(ctx, website.Owner, website.Name, newPR) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to create Pull Request using branch %s on %s/%s", newBranch, website.Owner, website.Name) | ||
} | ||
|
||
return newPRCreated, nil | ||
|
||
} | ||
|
||
func setupRepo(ctx context.Context, repo *git.Repo, prInfo prInformation, op string) error { | ||
if err := repo.Clone(ctx); err != nil { | ||
return errors.Wrapf(err, "Failed to clone repository %s/%s to %s on Pull Request %d", repo.Owner, repo.Name, op, prInfo.num) | ||
} | ||
|
||
if err := repo.Clean(ctx); err != nil { | ||
return errors.Wrapf(err, "Failed to clean the repository %s/%s to %s on Pull Request %d", repo.Owner, repo.Name, op, prInfo.num) | ||
} | ||
|
||
if err := repo.Fetch(ctx, "origin"); err != nil { | ||
return errors.Wrapf(err, "Failed to fetch origin on repository %s/%s to %s on Pull Request %d", repo.Owner, repo.Name, op, prInfo.num) | ||
} | ||
|
||
if err := repo.ResetHard(ctx, "HEAD"); err != nil { | ||
return errors.Wrapf(err, "Failed to reset the repository %s/%s to %s on Pull Request %d", repo.Owner, repo.Name, op, prInfo.num) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
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. | ||
*/ | ||
|
||
package git | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-github/v53/github" | ||
) | ||
|
||
const rowsPerPage = 100 | ||
|
||
// ListPRFiles returns a list of all files included in a given PR in the repo. | ||
func (r *Repo) ListPRFiles(ctx context.Context, client *github.Client, pr int) (allFiles []*github.CommitFile, err error) { | ||
cont := true | ||
for page := 1; cont; page++ { | ||
files, _, err := client.PullRequests.ListFiles(ctx, r.Owner, r.Name, pr, &github.ListOptions{ | ||
PerPage: rowsPerPage | ||
}) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "Failed to list changed files in Pull Request %s/%s#%d - at page %d", r.Owner, r.Name, page) | ||
} | ||
allFiles = append(allFiles, files...) | ||
if len(files) < rowsPerPage { | ||
cont = false | ||
break | ||
} | ||
} | ||
|
||
return allFiles | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters