-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (231 loc) · 8.63 KB
/
main.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"github.com/alecthomas/kong"
"github.com/sethvargo/go-githubactions"
"github.com/willabides/actionslog"
"github.com/willabides/actionslog/human"
"golang.org/x/exp/slog"
"gopkg.in/yaml.v3"
)
var version = "dev"
var flagHelp = kong.Vars{
"generate_action_help": `Ignore all other flags and generate a GitHub action.`,
"ref_help": `git ref.`,
"checkout_dir_help": `The directory where the repository is checked out.`,
"create_tag_help": `Whether to create a tag for the release.`,
"create_release_help": `Whether to create a release. Implies create-tag.`,
"initial_tag_help": `The tag to use if no previous version can be found. Set to "" to cause an error instead.`,
"tag_prefix_help": `The prefix to use for the tag.`,
"label_help": `PR label alias in the form of "<alias>=<label>" where <label> is a canonical label.`,
"output_format_help": `Output either json our GitHub action output.`,
"debug_help": `Enable debug logging.`,
"draft_help": `Leave the release as a draft.`,
"tempdir_help": `The prefix to use with mktemp to create a temporary directory.`,
"pushremote_help": `The remote to push tags to.`,
"repo_help": `GitHub repository in the form of owner/repo.`,
"github_token_help": "The GitHub token to use for authentication. Must have `contents: write` permission if creating a release or tag.",
"github_api_url_help": `GitHub API URL.`,
"check_pr_help": `
Operates as if the given PR has already been merged. Useful for making sure the PR is properly labeled.
Skips tag and release.
`,
"pre_tag_hook_help": `
Command to run before tagging the release. You may abort the release by exiting with a non-zero exit code. Exit code 0
will continue the release. Exit code 10 will skip the release without error. Any other exit code will abort the release
with an error.
Environment variables available to the hook:
RELEASE_VERSION
The semantic version being released (e.g. 1.2.3).
RELEASE_TAG
The tag being created (e.g. v1.2.3).
PREVIOUS_VERSION
The previous semantic version (e.g. 1.2.2). Empty on
first release.
FIRST_RELEASE
Whether this is the first release. Either "true" or
"false".
GITHUB_TOKEN
The GitHub token that was provided to release-train.
RELEASE_NOTES_FILE
A file path where you can write custom release notes.
When nothing is written to this file, release-train
will use GitHub's default release notes.
RELEASE_TARGET
A file path where you can write an alternate git ref
to release instead of HEAD.
ASSETS_DIR
A directory where you can write release assets. All
files in this directory will be uploaded as release
assets.
In addition to the above environment variables, all variables from release-train's environment are available to the
hook.
When the hook creates a tag named $RELEASE_TAG, it will be used as the release target instead of either HEAD or the
value written to $RELEASE_TARGET.
`,
"pre_release_hook_help": `
*deprecated* Will be removed in a future release. Alias for pre-tag-hook.
`,
"v0_help": `
Assert that current major version is 0 and treat breaking changes as minor changes.
Errors if the major version is not 0.
`,
"release_ref_help": `
Only allow tags and releases to be created from matching refs. Refs can be patterns accepted by git-show-ref.
If undefined, any branch can be used.
`,
}
func main() {
ctx := context.Background()
vars := flagHelp
vars["version"] = version
var root rootCmd
k := kong.Parse(
&root,
vars,
kong.BindTo(ctx, (*context.Context)(nil)),
kong.Description("Release every PR merge. No magic commit message required."),
)
k.FatalIfErrorf(k.Run())
}
type rootCmd struct {
Version kong.VersionFlag `action:"-"`
GenerateAction bool `hidden:"true" help:"${generate_action_help}"`
Repo string `action:",${{ github.repository }}" help:"${repo_help}"`
CheckPR int `action:"check-pr,${{ github.event.number }}" help:"${check_pr_help}"`
Label map[string]string `action:"labels" help:"${label_help}" placeholder:"<alias>=<label>;..."`
CheckoutDir string `action:",${{ github.workspace }}" short:"C" default:"." help:"${checkout_dir_help}"`
Ref string `default:"HEAD" help:"${ref_help}"`
GithubToken string `action:"github-token,${{ github.token }}" hidden:"true" env:"GITHUB_TOKEN" help:"${github_token_help}"`
CreateTag bool `help:"${create_tag_help}"`
CreateRelease bool `help:"${create_release_help}"`
Draft bool `help:"${draft_help}"`
TagPrefix string `default:"v" help:"${tag_prefix_help}"`
V0 bool `name:"v0" help:"${v0_help}"`
InitialTag string `action:"initial-release-tag" help:"${initial_tag_help}" default:"v0.0.0"`
PreTagHook string `placeholder:"<command>" help:"${pre_tag_hook_help}"`
PreReleaseHook string `placeholder:"<command>" help:"${pre_release_hook_help}"`
ReleaseRef []string `action:"release-refs" placeholder:"<branch>" help:"${release_ref_help}"`
PushRemote string `action:"-" default:"origin" help:"${pushremote_help}"`
Tempdir string `help:"${tempdir_help}"`
GithubApiUrl string `action:"-" help:"${github_api_url_help}" default:"https://api.github.com"`
OutputFormat string `action:"-" default:"json" help:"${output_format_help}" enum:"json,action"`
Debug bool `help:"${debug_help}"`
}
func (c *rootCmd) GithubClient(ctx context.Context) (GithubClient, error) {
return NewGithubClient(ctx, c.GithubApiUrl, c.GithubToken, fmt.Sprintf("release-train/%s", version))
}
func (c *rootCmd) Run(ctx context.Context, kongCtx *kong.Context) error {
var slogOpts slog.HandlerOptions
if c.Debug {
slogOpts.Level = slog.LevelDebug
}
var logHandler slog.Handler = slog.NewTextHandler(os.Stderr, &slogOpts)
// In actions, we always log at debug level, but when --debug is set, we output the debug logs as notices.
if c.OutputFormat == "action" {
logHandler = &actionslog.Wrapper{
ActionsLogger: func(level slog.Level) actionslog.ActionsLog {
l := actionslog.DefaultActionsLog(level)
if c.Debug && l == actionslog.LogDebug {
l = actionslog.LogNotice
}
return l
},
Handler: (&human.Handler{
Level: slog.LevelDebug,
}).WithOutput,
}
}
ctx = withLogger(ctx, slog.New(logHandler))
if c.GenerateAction {
return c.generateAction(kongCtx)
}
return c.runRelease(ctx, kongCtx.Stdout, kongCtx.Stderr)
}
func (c *rootCmd) generateAction(kongCtx *kong.Context) error {
enc := yaml.NewEncoder(os.Stdout)
enc.SetIndent(2)
got, err := getAction(kongCtx)
if err != nil {
return err
}
return enc.Encode(got)
}
func (c *rootCmd) runRelease(ctx context.Context, stdout, stderr io.Writer) (errOut error) {
logger := getLogger(ctx)
defer func() {
if errOut != nil {
logger.Error(errOut.Error())
}
}()
logger.Debug("starting runRelease")
client, err := c.GithubClient(ctx)
if err != nil {
return err
}
tempDir, err := os.MkdirTemp(c.Tempdir, "release-train-*")
if err != nil {
return err
}
defer func() {
errOut = errors.Join(errOut, os.RemoveAll(tempDir))
}()
createTag := c.CreateTag
if c.CreateRelease {
createTag = true
}
repo := c.Repo
if repo == "" {
repo, err = getGithubRepoFromRemote(ctx, c.CheckoutDir, c.PushRemote)
if err != nil {
return err
}
}
preTagHook := c.PreTagHook
if c.PreReleaseHook != "" {
if preTagHook != "" {
return fmt.Errorf("cannot specify both --pre-tag-hook and --pre-release-hook")
}
preTagHook = c.PreReleaseHook
}
runner := &Runner{
CheckoutDir: c.CheckoutDir,
Ref: c.Ref,
GithubToken: c.GithubToken,
CreateTag: createTag,
CreateRelease: c.CreateRelease,
Draft: c.Draft,
V0: c.V0,
TagPrefix: c.TagPrefix,
InitialTag: c.InitialTag,
PreTagHook: preTagHook,
Repo: repo,
PushRemote: c.PushRemote,
TempDir: tempDir,
ReleaseRefs: c.ReleaseRef,
LabelAliases: c.Label,
CheckPR: c.CheckPR,
GithubClient: client,
Stdout: stdout,
Stderr: stderr,
}
result, err := runner.Run(ctx)
if err != nil {
return err
}
if c.OutputFormat == "json" {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(result)
}
action := githubactions.New()
for _, item := range outputItems {
action.SetOutput(item.name, item.value(result))
}
return nil
}