forked from kubernetes/release
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
392 lines (343 loc) · 10.3 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/release/pkg/notes"
"k8s.io/release/pkg/notes/document"
"k8s.io/release/pkg/notes/options"
"k8s.io/release/pkg/release"
"sigs.k8s.io/mdtoc/pkg/mdtoc"
"sigs.k8s.io/release-sdk/git"
"sigs.k8s.io/release-utils/env"
"sigs.k8s.io/release-utils/log"
)
type releaseNotesOptions struct {
outputFile string
tableOfContents bool
dependencies bool
}
var (
releaseNotesOpts = &releaseNotesOptions{}
opts = options.New()
cmd = &cobra.Command{
Short: "release-notes - The Kubernetes Release Notes Generator",
Use: "release-notes",
SilenceUsage: true,
SilenceErrors: true,
RunE: run,
PreRunE: func(*cobra.Command, []string) error {
return opts.ValidateAndFinish()
},
}
)
func init() {
// githubBaseURL contains the github base URL.
cmd.PersistentFlags().StringVar(
&opts.GithubBaseURL,
"github-base-url",
env.Default("GITHUB_BASE_URL", ""),
"Base URL of github",
)
// githubUploadURL contains the github upload URL.
cmd.PersistentFlags().StringVar(
&opts.GithubUploadURL,
"github-upload-url",
env.Default("GITHUB_UPLOAD_URL", ""),
"Upload URL of github",
)
// githubOrg contains name of github organization that holds the repo to scrape.
cmd.PersistentFlags().StringVar(
&opts.GithubOrg,
"org",
env.Default("ORG", notes.DefaultOrg),
"Name of github organization",
)
// githubRepo contains name of github repository to scrape.
cmd.PersistentFlags().StringVar(
&opts.GithubRepo,
"repo",
env.Default("REPO", notes.DefaultRepo),
"Name of github repository",
)
// output contains the path on the filesystem to where the resultant
// release notes should be printed.
cmd.PersistentFlags().StringVar(
&releaseNotesOpts.outputFile,
"output",
env.Default("OUTPUT", ""),
"The path to the where the release notes will be printed",
)
// branch is which branch to scrape.
cmd.PersistentFlags().StringVar(
&opts.Branch,
"branch",
env.Default("BRANCH", git.DefaultBranch),
fmt.Sprintf("Select which branch to scrape. Defaults to `%s`", git.DefaultBranch),
)
// startSHA contains the commit SHA where the release note generation
// begins.
cmd.PersistentFlags().StringVar(
&opts.StartSHA,
"start-sha",
env.Default("START_SHA", ""),
"The commit hash to start at",
)
// endSHA contains the commit SHA where the release note generation ends.
cmd.PersistentFlags().StringVar(
&opts.EndSHA,
"end-sha",
env.Default("END_SHA", ""),
"The commit hash to end at",
)
// startRev contains any valid git object where the release note generation
// begins. Can be used as alternative to start-sha.
cmd.PersistentFlags().StringVar(
&opts.StartRev,
"start-rev",
env.Default("START_REV", ""),
"The git revision to start at. Can be used as alternative to start-sha.",
)
// endRev contains any valid git object where the release note generation
// ends. Can be used as alternative to start-sha.
cmd.PersistentFlags().StringVar(
&opts.EndRev,
"end-rev",
env.Default("END_REV", ""),
"The git revision to end at. Can be used as alternative to end-sha.",
)
// repoPath contains the path to a local Kubernetes repository to avoid the
// delay during git clone
cmd.PersistentFlags().StringVar(
&opts.RepoPath,
"repo-path",
env.Default("REPO_PATH", filepath.Join(os.TempDir(), "k8s-repo")),
"Path to a local Kubernetes repository, used only for tag discovery.",
)
// format is the output format to produce the notes in.
cmd.PersistentFlags().StringVar(
&opts.Format,
"format",
env.Default("FORMAT", options.FormatMarkdown),
fmt.Sprintf("The format for notes output (options: %s)",
options.FormatJSON+", "+options.FormatMarkdown,
),
)
// go-template is the go template to be used when the format is markdown
cmd.PersistentFlags().StringVar(
&opts.GoTemplate,
"go-template",
env.Default("GO_TEMPLATE", options.GoTemplateDefault),
fmt.Sprintf("The go template to be used if --format=markdown (options: %s)",
strings.Join([]string{
options.GoTemplateDefault,
options.GoTemplateInline + "<template>",
options.GoTemplatePrefix + "<file.template>",
}, ", "),
),
)
cmd.PersistentFlags().BoolVar(
&opts.AddMarkdownLinks,
"markdown-links",
env.IsSet("MARKDOWN_LINKS"),
"Add links for PRs and authors are added in the markdown format",
)
cmd.PersistentFlags().StringVar(
&opts.RequiredAuthor,
"required-author",
env.Default("REQUIRED_AUTHOR", "k8s-ci-robot"),
"Only commits from this GitHub user are considered. Set to empty string to include all users",
)
cmd.PersistentFlags().BoolVar(
&opts.Debug,
"debug",
env.IsSet("DEBUG"),
"Enable debug logging",
)
cmd.PersistentFlags().StringVar(
&opts.DiscoverMode,
"discover",
env.Default("DISCOVER", options.RevisionDiscoveryModeNONE),
fmt.Sprintf("The revision discovery mode for automatic revision retrieval (options: %s)",
strings.Join([]string{
options.RevisionDiscoveryModeNONE,
options.RevisionDiscoveryModeMergeBaseToLatest,
options.RevisionDiscoveryModePatchToPatch,
options.RevisionDiscoveryModeMinorToMinor,
}, ", "),
),
)
cmd.PersistentFlags().StringVar(
&opts.ReleaseBucket,
"release-bucket",
env.Default("RELEASE_BUCKET", release.ProductionBucket),
"Specify gs bucket to point to in generated notes",
)
cmd.PersistentFlags().StringVar(
&opts.ReleaseTars,
"release-tars",
env.Default("RELEASE_TARS", ""),
"Directory of tars to sha512 sum for display",
)
cmd.PersistentFlags().BoolVar(
&releaseNotesOpts.tableOfContents,
"toc",
env.IsSet("TOC"),
"Enable the rendering of the table of contents",
)
cmd.PersistentFlags().StringVar(
&opts.RecordDir,
"record",
env.Default("RECORD", ""),
"Record the API into a directory",
)
cmd.PersistentFlags().StringVar(
&opts.ReplayDir,
"replay",
env.Default("REPLAY", ""),
"Replay a previously recorded API from a directory",
)
cmd.PersistentFlags().BoolVar(
&releaseNotesOpts.dependencies,
"dependencies",
true,
"Add dependency report",
)
cmd.PersistentFlags().StringSliceVarP(
&opts.MapProviderStrings,
"maps-from",
"m",
[]string{},
"specify a location to recursively look for release notes *.y[a]ml file mappings",
)
cmd.PersistentFlags().BoolVar(
&opts.ListReleaseNotesV2,
"list-v2",
false,
"enable experimental implementation to list commits (ListReleaseNotesV2)",
)
}
func WriteReleaseNotes(releaseNotes *notes.ReleaseNotes) (err error) {
logrus.Infof(
"Got %d release notes, performing rendering",
len(releaseNotes.History()),
)
var (
// Open a handle to the file which will contain the release notes output
output *os.File
existingNotes notes.ReleaseNotesByPR
)
if releaseNotesOpts.outputFile != "" {
output, err = os.OpenFile(releaseNotesOpts.outputFile, os.O_RDWR|os.O_CREATE, os.FileMode(0o644))
if err != nil {
return fmt.Errorf("opening the supplied output file: %w", err)
}
} else {
output, err = os.CreateTemp("", "release-notes-")
if err != nil {
return fmt.Errorf("creating a temporary file to write the release notes to: %w", err)
}
}
// Contextualized release notes can be printed in a variety of formats
if opts.Format == options.FormatJSON {
byteValue, err := io.ReadAll(output)
if err != nil {
return err
}
if len(byteValue) > 0 {
if err := json.Unmarshal(byteValue, &existingNotes); err != nil {
return fmt.Errorf("unmarshalling existing notes: %w", err)
}
}
if len(existingNotes) > 0 {
if err := output.Truncate(0); err != nil {
return err
}
if _, err := output.Seek(0, 0); err != nil {
return err
}
for i := 0; i < len(existingNotes); i++ {
pr := existingNotes[i].PrNumber
if releaseNotes.Get(pr) == nil {
releaseNotes.Set(pr, existingNotes[i])
}
}
}
enc := json.NewEncoder(output)
enc.SetIndent("", " ")
if err := enc.Encode(releaseNotes.ByPR()); err != nil {
return fmt.Errorf("encoding JSON output: %w", err)
}
} else {
doc, err := document.New(releaseNotes, opts.StartRev, opts.EndRev)
if err != nil {
return fmt.Errorf("creating release note document: %w", err)
}
markdown, err := doc.RenderMarkdownTemplate(opts.ReleaseBucket, opts.ReleaseTars, "", opts.GoTemplate)
if err != nil {
return fmt.Errorf("rendering release note document with template: %w", err)
}
const nl = "\n"
if releaseNotesOpts.dependencies {
if opts.StartSHA == opts.EndSHA {
logrus.Info("Skipping dependency report because start and end SHA are the same")
} else {
url := git.GetRepoURL(opts.GithubOrg, opts.GithubRepo, false)
deps, err := notes.NewDependencies().ChangesForURL(
url, opts.StartSHA, opts.EndSHA,
)
if err != nil {
return fmt.Errorf("generating dependency report: %w", err)
}
markdown += strings.Repeat(nl, 2) + deps
}
}
if releaseNotesOpts.tableOfContents {
toc, err := mdtoc.GenerateTOC([]byte(markdown), mdtoc.Options{
Dryrun: false,
SkipPrefix: false,
MaxDepth: mdtoc.MaxHeaderDepth,
})
if err != nil {
return fmt.Errorf("generating table of contents: %w", err)
}
markdown = toc + nl + markdown
}
if _, err := output.WriteString(markdown); err != nil {
return fmt.Errorf("writing output file: %w", err)
}
}
logrus.Infof("Release notes written to file: %s", output.Name())
return nil
}
func run(*cobra.Command, []string) error {
releaseNotes, err := notes.GatherReleaseNotes(opts)
if err != nil {
return fmt.Errorf("gathering release notes: %w", err)
}
return WriteReleaseNotes(releaseNotes)
}
func main() {
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
logrus.AddHook(log.NewFilenameHook())
if err := cmd.Execute(); err != nil {
logrus.Fatal(err)
}
}