Skip to content

Commit 9222941

Browse files
authored
Merge pull request #15 from vitessio/pre-release-create-gh-milestones
2 parents 1e97c43 + fc38ee6 commit 9222941

File tree

7 files changed

+264
-0
lines changed

7 files changed

+264
-0
lines changed

go/cmd/pre_release/milestone.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2023 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pre_release
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
"vitess.io/vitess-releaser/go/releaser/pre_release"
24+
)
25+
26+
var createMilestone = &cobra.Command{
27+
Use: "create-milestone",
28+
Short: "Create the next milestone for the future release.",
29+
Run: func(cmd *cobra.Command, args []string) {
30+
_, newMilestone := pre_release.NewMilestone()
31+
out := newMilestone()
32+
fmt.Println("New milestone created or found on GitHub:", out)
33+
},
34+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2023 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package interactive
18+
19+
import (
20+
tea "github.com/charmbracelet/bubbletea"
21+
"vitess.io/vitess-releaser/go/releaser/pre_release"
22+
)
23+
24+
type createMilestone string
25+
26+
func createMilestoneMenuItem() menuItem {
27+
return menuItem{
28+
name: "Create a new GitHub Milestone",
29+
act: createMilestoneAct,
30+
update: createMilestoneUpdate,
31+
}
32+
}
33+
34+
func createMilestoneUpdate(mi menuItem, msg tea.Msg) (menuItem, tea.Cmd) {
35+
milestoneLink, ok := msg.(createMilestone)
36+
if !ok {
37+
return mi, nil
38+
}
39+
40+
mi.state = string(milestoneLink)
41+
return mi, nil
42+
}
43+
44+
func createMilestoneAct(mi menuItem) (menuItem, tea.Cmd) {
45+
mi.state = "running..."
46+
pl, create := pre_release.NewMilestone()
47+
return mi, tea.Batch(func() tea.Msg {
48+
return createMilestone(create())
49+
}, push(newProgressDialog("Creating new GitHub Milestone", pl)))
50+
}

go/interactive/main_menu.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func MainScreen() {
3434
prerelease := newMenu(
3535
"Pre Release",
3636
codeFreezeMenuItem(),
37+
createMilestoneMenuItem(),
3738
)
3839

3940
postRelease := newMenu(

go/releaser/github/milestone.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2023 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package github
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"log"
23+
"strings"
24+
25+
"github.com/cli/go-gh"
26+
"vitess.io/vitess-releaser/go/releaser/state"
27+
)
28+
29+
type Milestone struct {
30+
URL string `json:"url"`
31+
}
32+
33+
func GetMilestonesByName(name string) []Milestone {
34+
stdOut, _, err := gh.Exec(
35+
"milestone", "list",
36+
"--query", name,
37+
"--repo", state.VitessRepo,
38+
"--json", "url",
39+
"--state", "all",
40+
)
41+
if err != nil {
42+
log.Fatal(err.Error())
43+
}
44+
str := stdOut.String()
45+
str = str[strings.Index(str, "]")+1:]
46+
var ms []Milestone
47+
err = json.Unmarshal([]byte(str), &ms)
48+
if err != nil {
49+
log.Fatal(err.Error())
50+
}
51+
return ms
52+
}
53+
54+
func CreateNewMilestone(name string) string {
55+
stdOut, _, err := gh.Exec(
56+
"milestone", "create",
57+
"--repo", state.VitessRepo,
58+
"--title", name,
59+
)
60+
if err != nil {
61+
log.Fatal(err.Error())
62+
}
63+
out := strings.ReplaceAll(stdOut.String(), "\n", "")
64+
idx := strings.LastIndex(out, fmt.Sprintf("https://github.com/%s/milestone/", state.VitessRepo))
65+
return out[idx:]
66+
}

go/releaser/logging/logging.go

+7
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ func (pl *ProgressLogging) GetStepInProgress() []string {
5454

5555
return slices.Clone(pl.StepsDone)
5656
}
57+
58+
func (pl *ProgressLogging) SetTotalStep(v int) {
59+
pl.mu.Lock()
60+
defer pl.mu.Unlock()
61+
62+
pl.TotalSteps = v
63+
}

go/releaser/pre_release/milestone.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2023 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pre_release
18+
19+
import (
20+
"fmt"
21+
22+
"vitess.io/vitess-releaser/go/releaser/github"
23+
"vitess.io/vitess-releaser/go/releaser/logging"
24+
"vitess.io/vitess-releaser/go/releaser/vitess"
25+
)
26+
27+
func NewMilestone() (*logging.ProgressLogging, func() string) {
28+
pl := &logging.ProgressLogging{
29+
TotalSteps: 3,
30+
}
31+
32+
return pl, func() string {
33+
pl.NewStepf("Finding the next Milestone")
34+
nextNextRelease := vitess.FindVersionAfterNextRelease()
35+
newMilestone := fmt.Sprintf("v%s", nextNextRelease)
36+
37+
ms := github.GetMilestonesByName(newMilestone)
38+
if len(ms) > 0 {
39+
pl.SetTotalStep(2)
40+
pl.NewStepf("Found an existing Milestone: %s", ms[0].URL)
41+
return ms[0].URL
42+
}
43+
44+
pl.NewStepf("Creating Milestone %s on GitHub", newMilestone)
45+
link := github.CreateNewMilestone(newMilestone)
46+
47+
pl.NewStepf("New Milestone %s created: %s", newMilestone, link)
48+
return link
49+
}
50+
}

go/releaser/vitess/milestone.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package vitess
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"strconv"
23+
"strings"
24+
25+
"vitess.io/vitess-releaser/go/releaser/state"
26+
)
27+
28+
func FindVersionAfterNextRelease() string {
29+
CorrectCleanRepo()
30+
nextRelease, _ := FindNextRelease(state.MajorRelease)
31+
32+
if strings.Contains(nextRelease, "rc") {
33+
panic("RC releases not supported for now")
34+
}
35+
36+
segments := strings.Split(nextRelease, ".")
37+
if len(segments) != 3 {
38+
return ""
39+
}
40+
41+
segmentInts := make([]int, 0, len(segments))
42+
for _, segment := range segments {
43+
v, err := strconv.Atoi(segment)
44+
if err != nil {
45+
log.Fatal(err.Error())
46+
}
47+
segmentInts = append(segmentInts, v)
48+
}
49+
50+
// if it is a major release
51+
if segmentInts[1] == 0 && segmentInts[2] == 0 {
52+
return fmt.Sprintf("%d.0.0", segmentInts[0]+1)
53+
}
54+
// if a patch release
55+
return fmt.Sprintf("%d.%d.%d", segmentInts[0], segmentInts[1], segmentInts[2]+1)
56+
}

0 commit comments

Comments
 (0)