-
Notifications
You must be signed in to change notification settings - Fork 497
/
main.go
198 lines (166 loc) · 5.07 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
/*
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"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"sigs.k8s.io/mdtoc/pkg/mdtoc"
"sigs.k8s.io/release-sdk/git"
"sigs.k8s.io/release-utils/log"
"sigs.k8s.io/release-utils/version"
"k8s.io/release/pkg/notes"
"k8s.io/release/pkg/notes/document"
"k8s.io/release/pkg/notes/options"
)
type releaseNotesOptions struct {
outputFile string
tableOfContents bool
dependencies bool
}
var (
releaseNotesOpts = &releaseNotesOptions{}
opts = options.New()
)
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 _, existingNote := range existingNotes {
pr := existingNote.PrNumber
if releaseNotes.Get(pr) == nil {
releaseNotes.Set(pr, existingNote)
}
}
}
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
}
// hackDefaultSubcommand is a utility function that hacks the "generate"
// subcommand as default to avoid breaking compatibility with previoud
// versions of release-notes.
func hackDefaultSubcommand(cmd *cobra.Command) {
if len(os.Args) > 1 {
if os.Args[1] == "completion" {
return
}
// Check if the first arg corresponds to a registered subcommand
for _, command := range cmd.Commands() {
if command.Use == os.Args[1] {
return
}
}
}
logrus.Warn("No subcommand specified, running \"generate\" ")
os.Args = append([]string{os.Args[0], "generate"}, os.Args[1:]...)
}
func main() {
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
logrus.AddHook(log.NewFilenameHook())
cmd := &cobra.Command{
Short: "release-notes - The Kubernetes Release Notes Generator",
Use: "release-notes",
SilenceUsage: true,
SilenceErrors: true,
}
addGenerate(cmd)
addCheckPR(cmd)
cmd.AddCommand(version.WithFont("slant"))
hackDefaultSubcommand(cmd)
if err := cmd.Execute(); err != nil {
logrus.Fatal(err)
}
}