@@ -20,7 +20,10 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"log"
23
+ "os"
23
24
"os/exec"
25
+ "strings"
26
+ "time"
24
27
25
28
"vitess.io/vitess-releaser/go/releaser"
26
29
"vitess.io/vitess-releaser/go/releaser/git"
@@ -42,37 +45,92 @@ const (
42
45
// Request must be forced-merged by a Vitess maintainer, this step cannot be automated.
43
46
func CodeFreeze (ctx * releaser.Context ) (* logging.ProgressLogging , func () string ) {
44
47
pl := & logging.ProgressLogging {
45
- TotalSteps : 6 ,
48
+ TotalSteps : 12 ,
46
49
}
47
50
51
+ waitForPRToBeMerged := func (nb int ) {
52
+ pl .NewStepf ("Waiting for the PR to be merged. You must enable bypassing the branch protection rules in: https://github.com/vitessio/vitess/settings/branches" )
53
+ outer:
54
+ for {
55
+ select {
56
+ case <- time .After (5 * time .Second ):
57
+ if github .IsPRMerged (ctx .VitessRepo , nb ) {
58
+ break outer
59
+ }
60
+ }
61
+ }
62
+ pl .NewStepf ("PR has been merged" )
63
+ }
64
+
65
+ var done bool
66
+ var url string
67
+ var nb int
48
68
return pl , func () string {
69
+ defer func () {
70
+ ctx .Issue .CodeFreeze .Done = done
71
+ ctx .Issue .CodeFreeze .URL = url
72
+ pl .NewStepf ("Update Issue %s on GitHub" , ctx .IssueLink )
73
+ _ , fn := ctx .UploadIssue ()
74
+ issueLink := fn ()
75
+
76
+ pl .NewStepf ("Issue updated, see: %s" , issueLink )
77
+ }()
78
+
49
79
git .CorrectCleanRepo (ctx .VitessRepo )
50
80
nextRelease , branchName := releaser .FindNextRelease (ctx .MajorRelease )
51
81
52
82
pl .NewStepf ("Fetch from git remote" )
53
83
remote := git .FindRemoteName (ctx .VitessRepo )
54
84
git .ResetHard (remote , branchName )
55
85
86
+ codeFreezePRName := fmt .Sprintf ("[%s] Code Freeze for `v%s`" , branchName , nextRelease )
87
+
88
+ // look for existing code freeze PRs
89
+ pl .NewStepf ("Look for existing an Code Freeze Pull Request" )
90
+ if nb , url = github .FindCodeFreezePR (ctx .VitessRepo , codeFreezePRName ); url != "" {
91
+ pl .TotalSteps = 7 // only 7 total steps in this situation
92
+ pl .NewStepf ("An opened Code Freeze Pull Request was found: %s" , url )
93
+ waitForPRToBeMerged (nb )
94
+ done = true
95
+ return url
96
+ }
97
+
98
+ // check if the branch is already frozen or not
99
+ pl .NewStepf ("Check if branch %s is already frozen" , branchName )
100
+ if isCurrentBranchFrozen () {
101
+ pl .TotalSteps = 6 // only 6 total steps in this situation
102
+ pl .NewStepf ("Branch %s is already frozen, no action needed." , branchName )
103
+ done = true
104
+ return ""
105
+ }
106
+
56
107
pl .NewStepf ("Create new branch based on %s/%s" , remote , branchName )
57
108
newBranchName := findNewBranchForCodeFreeze (remote , branchName )
58
109
59
110
pl .NewStepf ("Turn on code freeze on branch %s" , newBranchName )
60
111
activateCodeFreeze ()
61
112
62
113
pl .NewStepf ("Commit and push to branch %s" , newBranchName )
63
- git .CommitAll (fmt .Sprintf ("Code Freeze of %s" , branchName ))
114
+ if git .CommitAll (fmt .Sprintf ("Code Freeze of %s" , branchName )) {
115
+ pl .TotalSteps = 9 // only 9 total steps in this situation
116
+ pl .NewStepf ("Nothing to commit, seems like code freeze is already done." , newBranchName )
117
+ done = true
118
+ return ""
119
+ }
64
120
git .Push (remote , newBranchName )
65
121
66
122
pl .NewStepf ("Create Pull Request" )
67
123
pr := github.PR {
68
- Title : fmt . Sprintf ( "[%s] Code Freeze for `v%s`" , branchName , nextRelease ) ,
124
+ Title : codeFreezePRName ,
69
125
Body : fmt .Sprintf ("This Pull Request freezes the branch `%s` for `v%s`" , branchName , nextRelease ),
70
126
Branch : newBranchName ,
71
127
Base : branchName ,
72
128
Labels : []github.Label {{Name : "Component: General" }, {Name : "Type: Release" }},
73
129
}
74
- url : = pr .Create (ctx .VitessRepo )
130
+ nb , url = pr .Create (ctx .VitessRepo )
75
131
pl .NewStepf ("PR created %s" , url )
132
+ waitForPRToBeMerged (nb )
133
+ done = true
76
134
return url
77
135
}
78
136
}
@@ -95,6 +153,15 @@ func findNewBranchForCodeFreeze(remote, baseBranch string) string {
95
153
return newBranch
96
154
}
97
155
156
+ func isCurrentBranchFrozen () bool {
157
+ b , err := os .ReadFile (codeFreezeWorkflowFile )
158
+ if err != nil {
159
+ log .Fatal (err )
160
+ }
161
+ str := string (b )
162
+ return strings .Contains (str , "exit 1" )
163
+ }
164
+
98
165
func activateCodeFreeze () {
99
166
changeCodeFreezeWorkflow (codeFreezeActivated )
100
167
}
0 commit comments