Skip to content

Commit 3415871

Browse files
authored
Merge pull request #79 from vitessio/close-doc-preview-on-release-merge
Close doc preview on release merge
2 parents 7d68022 + 4883329 commit 3415871

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

go/cobradocs_sync.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
"github.com/vitess.io/vitess-bot/go/shell"
3030
)
3131

32+
func cobraDocsSyncBranchName(prNum int) string {
33+
return fmt.Sprintf("synchronize-cobradocs-for-%d", prNum)
34+
}
35+
3236
// synchronize cobradocs from main and release branches
3337
func (h *PullRequestHandler) synchronizeCobraDocs(
3438
ctx context.Context,
@@ -41,7 +45,7 @@ func (h *PullRequestHandler) synchronizeCobraDocs(
4145
logger := zerolog.Ctx(ctx)
4246
op := "update cobradocs"
4347
branch := "prod"
44-
headBranch := fmt.Sprintf("synchronize-cobradocs-for-%d", pr.GetNumber())
48+
headBranch := cobraDocsSyncBranchName(pr.GetNumber())
4549
headRef := fmt.Sprintf("refs/heads/%s", headBranch)
4650

4751
prodBranch, _, err := client.Repositories.GetBranch(ctx, website.Owner, website.Name, branch, false)

go/pull_request.go

+75-24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os"
25+
"os/exec"
2526
"path/filepath"
2627
"regexp"
2728
"runtime/debug"
@@ -537,7 +538,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR(
537538
// 1. Find an existing PR and switch to its branch, or create a new branch
538539
// based on `prod`.
539540
branch := "prod"
540-
headBranch := fmt.Sprintf("synchronize-cobradocs-for-%d", prInfo.num)
541+
headBranch := cobraDocsSyncBranchName(prInfo.num)
541542
headRef := fmt.Sprintf("refs/heads/%s", headBranch)
542543

543544
prodBranch, _, err := client.Repositories.GetBranch(ctx, website.Owner, website.Name, branch, false)
@@ -628,30 +629,54 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR(
628629
return nil, errors.Wrapf(err, "Failed to checkout %s:%s to %s for %s", remote, ref, op, pr.GetHTMLURL())
629630
}
630631

632+
var (
633+
tree *github.Tree
634+
commit *github.Commit
635+
skipFirstCommit bool
636+
)
637+
631638
// 3. Run the sync script with `COBRADOC_VERSION_PAIRS="$(baseref):$(docsVersion)`.
632639
_, err = shell.NewContext(ctx, "./tools/sync_cobradocs.sh").InDir(website.LocalDir).WithExtraEnv(
633640
fmt.Sprintf("VITESS_DIR=%s", vitess.LocalDir),
634641
"COBRADOCS_SYNC_PERSIST=yes",
635642
fmt.Sprintf("COBRADOC_VERSION_PAIRS=HEAD:%s", docsVersion),
636643
).Output()
637644
if err != nil {
638-
return nil, errors.Wrapf(err, "Failed to run cobradocs sync script against %s:%s to %s for %s", remote, ref, op, pr.GetHTMLURL())
645+
var exitErr *exec.ExitError
646+
if errors.As(err, &exitErr) &&
647+
bytes.Contains(exitErr.Stderr, []byte("No changes to cobradocs detected")) {
648+
logger.Info().Msgf("No cobradocs changed for PR %s/%s#%d at base %s. Skipping first commit ...", remote, vitess.Name, pr.GetNumber(), ref)
649+
skipFirstCommit = true
650+
} else {
651+
logger.Err(err).Msgf("%T", err)
652+
return nil, errors.Wrapf(err, "Failed to run cobradocs sync script against %s:%s to %s for %s", remote, ref, op, pr.GetHTMLURL())
653+
654+
}
639655
}
640656

641-
tree, commit, err := h.writeAndCommitTree(
642-
ctx,
643-
client,
644-
website,
645-
pr,
646-
branch,
647-
"HEAD",
648-
baseTree,
649-
parent,
650-
fmt.Sprintf("Generate cobradocs preview against %s:%s", remote, ref),
651-
op,
652-
)
653-
if err != nil {
654-
return nil, err
657+
if !skipFirstCommit {
658+
tree, commit, err = h.writeAndCommitTree(
659+
ctx,
660+
client,
661+
website,
662+
pr,
663+
branch,
664+
"HEAD",
665+
baseTree,
666+
parent,
667+
fmt.Sprintf("Generate cobradocs preview against %s:%s", remote, ref),
668+
op,
669+
)
670+
if err != nil {
671+
return nil, err
672+
}
673+
} else {
674+
tree = &github.Tree{
675+
SHA: &baseTree,
676+
}
677+
commit = &github.Commit{
678+
SHA: &parent,
679+
}
655680
}
656681

657682
// 4. Switch vitess repo to the PR's head ref.
@@ -803,13 +828,46 @@ func (h *PullRequestHandler) updateDocs(ctx context.Context, event github.PullRe
803828
}
804829
}()
805830

831+
website := git.NewRepo(
832+
prInfo.repoOwner,
833+
"website",
834+
).WithDefaultBranch("prod").WithLocalDir(
835+
filepath.Join(h.Workdir(), "website"),
836+
)
837+
806838
// Checks:
807839
// - is vitessio/vitess:main branch
808840
// - PR contains changes to either `go/cmd/**/*.go` OR `go/flags/endtoend/*.txt` (TODO)
809841
if prInfo.base.GetRef() != "main" {
810842
logger.Debug().Msgf("PR %d is merged to %s, not main, skipping website cobradocs sync", prInfo.num, prInfo.base.GetRef())
811-
// TODO: close any potentially open PR against website.
843+
// Close any potentially open PR against website.
812844
// (see https://github.com/vitessio/vitess-bot/issues/76).
845+
prs, err := website.FindPRs(ctx, client, github.PullRequestListOptions{
846+
State: "open",
847+
Head: fmt.Sprintf("%s:%s", website.Owner, cobraDocsSyncBranchName(prInfo.num)),
848+
Base: website.DefaultBranch,
849+
Sort: "created",
850+
Direction: "desc",
851+
}, func(pr *github.PullRequest) bool {
852+
return pr.GetUser().GetLogin() == h.botLogin
853+
}, 1)
854+
if err != nil {
855+
return err
856+
}
857+
858+
if len(prs) == 0 {
859+
// No open PRs.
860+
return nil
861+
}
862+
863+
openPR := prs[0]
864+
logger.Info().Msgf("closing open PR %s/%s#%d", website.Owner, website.Name, openPR.GetNumber())
865+
_, _, err = client.PullRequests.Edit(ctx, website.Owner, website.Name, openPR.GetNumber(), &github.PullRequest{
866+
State: github.String("closed"),
867+
})
868+
if err != nil {
869+
return errors.Wrapf(err, "Failed to close PR %s/%s#%d", website.Owner, website.Name, openPR.GetNumber())
870+
}
813871
return nil
814872
}
815873

@@ -828,13 +886,6 @@ func (h *PullRequestHandler) updateDocs(ctx context.Context, event github.PullRe
828886
return nil
829887
}
830888

831-
website := git.NewRepo(
832-
prInfo.repoOwner,
833-
"website",
834-
).WithDefaultBranch("prod").WithLocalDir(
835-
filepath.Join(h.Workdir(), "website"),
836-
)
837-
838889
pr, err := h.synchronizeCobraDocs(ctx, client, vitess, website, event.GetPullRequest(), prInfo)
839890
if err != nil {
840891
return err

go/shell/shell.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func (c *cmd) Output() ([]byte, error) {
106106

107107
func wrapErr(err error, out []byte) error {
108108
if execErr, ok := err.(*exec.ExitError); ok {
109-
err := fmt.Errorf("%s\nstderr: %s", err.Error(), execErr.Stderr)
109+
err := fmt.Errorf("%w\nstderr: %s", err, execErr.Stderr)
110110
if out != nil {
111-
err = fmt.Errorf("%s\nstdout: %s", err.Error(), out)
111+
err = fmt.Errorf("%w\nstdout: %s", err, out)
112112
}
113113

114114
return err

0 commit comments

Comments
 (0)