/
cmd_release.go
210 lines (196 loc) · 5.49 KB
/
cmd_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package godzil
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"os"
"regexp"
"strings"
"github.com/Songmu/ghch"
"github.com/Songmu/prompter"
"github.com/x-motemen/gobump"
"golang.org/x/mod/semver"
)
type release struct {
allowDirty, dryRun bool
branch, remote, repoPath string
path string // version.go location
outStream, errStream io.Writer
}
func (re *release) run(argv []string, outStream, errStream io.Writer) error {
re.outStream = outStream
re.errStream = errStream
fs := flag.NewFlagSet("godzil release", flag.ContinueOnError)
fs.SetOutput(errStream)
fs.StringVar(&re.branch, "branch", "", "releasing branch (default branch is used by default)")
fs.StringVar(&re.remote, "remote", "", "remote repository name (Optional)")
fs.BoolVar(&re.allowDirty, "allow-dirty", false, "allow dirty index")
fs.BoolVar(&re.dryRun, "dry-run", false, "dry run")
fs.StringVar(&re.repoPath, "C", "", "repository path")
if err := fs.Parse(argv); err != nil {
return err
}
if re.repoPath == "" {
re.repoPath = "."
}
re.path = fs.Arg(0)
return re.do()
}
var headBranchReg = regexp.MustCompile(`(?m)^\s*HEAD branch: (.*)$`)
func defaultBranch(remote string) (string, error) {
if remote == "" {
var err error
remote, err = detectRemote()
if err != nil {
return "", err
}
}
// `git symbolic-ref refs/remotes/origin/HEAD` sometimes doesn't work
// So use `git remote show origin` for detecting default branch
show, _, err := git("remote", "show", remote)
if err != nil {
return "", fmt.Errorf("failed to detect defaut branch: %w", err)
}
m := headBranchReg.FindStringSubmatch(show)
if len(m) < 2 {
return "", fmt.Errorf("failed to detect default branch from remote: %s", remote)
}
return m[1], nil
}
func detectRemote() (string, error) {
remotesStr, _, err := git("remote")
if err != nil {
return "", fmt.Errorf("failed to detect remote: %s", err)
}
remotes := strings.Fields(remotesStr)
if len(remotes) == 1 {
return remotes[0], nil
}
for _, r := range remotes {
if r == "origin" {
return r, nil
}
}
return "", errors.New("failed to detect remote")
}
var gitReg = regexp.MustCompile(`^(?:git|https)(?:@|://)([^/:]+(?::[0-9]{1,5})?)[/:](.*)$`)
func (re *release) do() error {
if !re.allowDirty {
out, _, err := git("status", "--porcelain")
if err != nil {
return fmt.Errorf("faild to release when git status: %w", err)
}
if strings.TrimSpace(out) != "" {
return fmt.Errorf("can't release on dirty index (or you can use --allow-dirty)\n%s", out)
}
}
if re.branch == "" {
b, err := defaultBranch(re.remote)
if err != nil {
return err
}
re.branch = b
}
branch, _, err := git("symbolic-ref", "--short", "HEAD")
if err != nil {
return fmt.Errorf("faild to release when git symbolic-ref: %w", err)
}
if branch != re.branch {
return fmt.Errorf("you are not on releasing branch %q, current branch is %q",
re.branch, branch)
}
remote, _, err := git("config", fmt.Sprintf("branch.%s.remote", branch))
if err != nil {
return fmt.Errorf("can't find a remote branch of %q: %w", branch, err)
}
apibase := os.Getenv("GITHUB_API")
if apibase == "" {
remoteURL, _, err := git("config", fmt.Sprintf("remote.%s.url", remote))
if err != nil {
return fmt.Errorf("can't find a remote URL of %q: %w", remote, err)
}
m := gitReg.FindStringSubmatch(remoteURL)
if len(m) < 2 {
return fmt.Errorf("strange remote URL: %s", remoteURL)
}
if m[1] != "github.com" {
apibase = fmt.Sprintf("https://%s/api/v3", m[1])
}
}
buf := &bytes.Buffer{}
gb := &gobump.Gobump{
Show: true,
Raw: true,
Target: re.path,
OutStream: buf,
}
if _, err := gb.Run(); err != nil {
return fmt.Errorf("no version declaraion found: %w", err)
}
currVerStr := strings.TrimSpace(buf.String())
vers := strings.Split(currVerStr, "\n")
currVer := vers[0]
fmt.Fprintf(re.outStream, "current version: %s\n", currVer)
nextVer := prompter.Prompt("input next version", "")
if !semver.IsValid("v" + nextVer) {
return fmt.Errorf("invalid version: %s", nextVer)
}
if semver.Compare("v"+nextVer, "v"+currVer) != 1 {
return fmt.Errorf("next version %q isn't greather than current version %q",
nextVer,
currVer)
}
nextTag := fmt.Sprintf("v%s", nextVer)
out, _, err := git("-C", re.repoPath, "ls-remote", remote, "refs/tags/"+nextTag)
if err != nil {
return fmt.Errorf("failed to check remote tags: %w", err)
}
if out != "" {
return fmt.Errorf("tag %s already exists on remote %s", nextTag, remote)
}
gb2 := &gobump.Gobump{
Write: true,
Target: re.path,
Config: gobump.Config{
Exact: nextVer,
},
}
filesMap, err := gb2.Run()
if err != nil {
return err
}
var versions []string
for f := range filesMap {
versions = append(versions, f)
}
fmt.Fprintln(re.outStream, "following changes will be released")
buf2 := &bytes.Buffer{}
gh := &ghch.Ghch{
RepoPath: re.repoPath,
NextVersion: nextTag,
BaseURL: apibase,
Format: "markdown",
OutStream: io.MultiWriter(re.outStream, buf2),
}
if err := gh.Run(); err != nil {
return err
}
gh.Write = true
if err := gh.Run(); err != nil {
return err
}
c := &cmd{outStream: re.outStream, errStream: re.errStream, dir: re.repoPath}
c.git(append([]string{"add", gh.ChangelogMd}, versions...)...)
if re.dryRun {
return c.err
}
c.git("commit", "-m",
fmt.Sprintf("Checking in changes prior to tagging of version v%s\n\n%s",
nextVer,
strings.TrimSpace(buf2.String())))
c.git("tag", nextTag)
c.git("push", "--atomic", remote, branch, "tag", nextTag)
return c.err
}