@@ -22,6 +22,7 @@ import (
22
22
"encoding/json"
23
23
"fmt"
24
24
"os"
25
+ "os/exec"
25
26
"path/filepath"
26
27
"regexp"
27
28
"runtime/debug"
@@ -537,7 +538,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR(
537
538
// 1. Find an existing PR and switch to its branch, or create a new branch
538
539
// based on `prod`.
539
540
branch := "prod"
540
- headBranch := fmt . Sprintf ( "synchronize-cobradocs-for-%d" , prInfo .num )
541
+ headBranch := cobraDocsSyncBranchName ( prInfo .num )
541
542
headRef := fmt .Sprintf ("refs/heads/%s" , headBranch )
542
543
543
544
prodBranch , _ , err := client .Repositories .GetBranch (ctx , website .Owner , website .Name , branch , false )
@@ -628,30 +629,54 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR(
628
629
return nil , errors .Wrapf (err , "Failed to checkout %s:%s to %s for %s" , remote , ref , op , pr .GetHTMLURL ())
629
630
}
630
631
632
+ var (
633
+ tree * github.Tree
634
+ commit * github.Commit
635
+ skipFirstCommit bool
636
+ )
637
+
631
638
// 3. Run the sync script with `COBRADOC_VERSION_PAIRS="$(baseref):$(docsVersion)`.
632
639
_ , err = shell .NewContext (ctx , "./tools/sync_cobradocs.sh" ).InDir (website .LocalDir ).WithExtraEnv (
633
640
fmt .Sprintf ("VITESS_DIR=%s" , vitess .LocalDir ),
634
641
"COBRADOCS_SYNC_PERSIST=yes" ,
635
642
fmt .Sprintf ("COBRADOC_VERSION_PAIRS=HEAD:%s" , docsVersion ),
636
643
).Output ()
637
644
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
+ }
639
655
}
640
656
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
+ }
655
680
}
656
681
657
682
// 4. Switch vitess repo to the PR's head ref.
@@ -803,13 +828,46 @@ func (h *PullRequestHandler) updateDocs(ctx context.Context, event github.PullRe
803
828
}
804
829
}()
805
830
831
+ website := git .NewRepo (
832
+ prInfo .repoOwner ,
833
+ "website" ,
834
+ ).WithDefaultBranch ("prod" ).WithLocalDir (
835
+ filepath .Join (h .Workdir (), "website" ),
836
+ )
837
+
806
838
// Checks:
807
839
// - is vitessio/vitess:main branch
808
840
// - PR contains changes to either `go/cmd/**/*.go` OR `go/flags/endtoend/*.txt` (TODO)
809
841
if prInfo .base .GetRef () != "main" {
810
842
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.
812
844
// (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
+ }
813
871
return nil
814
872
}
815
873
@@ -828,13 +886,6 @@ func (h *PullRequestHandler) updateDocs(ctx context.Context, event github.PullRe
828
886
return nil
829
887
}
830
888
831
- website := git .NewRepo (
832
- prInfo .repoOwner ,
833
- "website" ,
834
- ).WithDefaultBranch ("prod" ).WithLocalDir (
835
- filepath .Join (h .Workdir (), "website" ),
836
- )
837
-
838
889
pr , err := h .synchronizeCobraDocs (ctx , client , vitess , website , event .GetPullRequest (), prInfo )
839
890
if err != nil {
840
891
return err
0 commit comments