-
Notifications
You must be signed in to change notification settings - Fork 1k
/
release.go
154 lines (130 loc) · 3.74 KB
/
release.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"os"
"strings"
"github.com/blang/semver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/release/pkg/git"
"k8s.io/release/pkg/github"
"sigs.k8s.io/release-utils/command"
"sigs.k8s.io/release-utils/env"
"sigs.k8s.io/release-utils/util"
"github.com/cri-o/cri-o/internal/version"
)
const (
githubTokenEnvKey = "GITHUB_TOKEN"
gitRemoteEnvKey = "REMOTE"
orgEnvKey = "ORG"
versionFile = "internal/version/version.go"
branchPrefix = "release-"
)
func main() {
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
if err := run(); err != nil {
logrus.Fatalf("Unable to %v", err)
}
}
func run() error {
if !env.IsSet(githubTokenEnvKey) {
return errors.Errorf(
"run: $%s environment variable is not set", githubTokenEnvKey,
)
}
if !env.IsSet(orgEnvKey) {
return errors.Errorf(
"run: $%s environment variable is not set %s",
orgEnvKey,
"(should be set to your CRI-O fork organization, like 'gh-name')",
)
}
remote := env.Default(gitRemoteEnvKey, "origin")
logrus.Infof("Using repository fork remote: %s", remote)
org := os.Getenv(orgEnvKey)
logrus.Infof("Using repository fork organization: %s", org)
repo, err := git.OpenRepo(".")
if err != nil {
return errors.Wrap(err, "open local repo")
}
currentBranch, err := repo.CurrentBranch()
if err != nil {
return errors.Wrap(err, "get current branch")
}
logrus.Infof("Using current branch: %s", currentBranch)
newVersion, err := incVersion(version.Version, currentBranch)
if err != nil {
return errors.Wrap(err, "increment version")
}
logrus.Infof("Using new version: %s", util.SemverToTagString(newVersion))
if err := updateVersionAndCreatePR(
repo, newVersion, currentBranch, org, remote,
); err != nil {
return errors.Wrap(err, "update version in local repository")
}
return nil
}
func incVersion(tag, branch string) (res semver.Version, err error) {
sv, err := util.TagStringToSemver(strings.TrimSpace(tag))
if err != nil {
return res, errors.Wrapf(err, "convert tag string %s to semver", tag)
}
// clear any suffix like `-dev`
sv.Pre = nil
if branch == git.DefaultBranch {
// New minor version
sv.Minor++
sv.Patch = 0
} else {
// New patch version
sv.Patch++
}
return sv, nil
}
func updateVersionAndCreatePR(
repo *git.Repo, newVersion semver.Version, branch, org, remote string,
) error {
logrus.Info("Updating repository")
newBranch := branchPrefix + newVersion.String()
logrus.Infof("Switching to branch: %s", newBranch)
if err := repo.Checkout("-B", newBranch); err != nil {
return errors.Wrapf(err, "checkout branch %s", newBranch)
}
logrus.Infof("Updating new tag in file %s", versionFile)
if err := command.
New(
"sed", "-i",
fmt.Sprintf("s/%s/%s/", version.Version, newVersion),
versionFile,
).RunSilentSuccess(); err != nil {
return errors.Wrap(err, "update version file")
}
logrus.Info("Committing changes")
if err := repo.Add(versionFile); err != nil {
return errors.Wrapf(err, "add file %s to repo", versionFile)
}
if err := repo.UserCommit(
"Bump version to " + newVersion.String(),
); err != nil {
return errors.Wrap(err, "commit changes")
}
logrus.Info("Pushing changes")
if err := repo.PushToRemote(remote, newBranch); err != nil {
return errors.Wrapf(err, "pushing to remote: %s", remote)
}
logrus.Info("Creating PR")
gh := github.New()
pr, err := gh.CreatePullRequest("cri-o", "cri-o", branch,
fmt.Sprintf("%s:%s", org, newBranch),
fmt.Sprintf("Bump version for %s", newVersion),
fmt.Sprintf(
"Automated version bump to version `%s`\n\n%s",
newVersion, "/release-note-none",
),
)
if err != nil {
return errors.Wrap(err, "create pull request")
}
logrus.Infof("Created PR #%d", pr.GetNumber())
return nil
}