-
Notifications
You must be signed in to change notification settings - Fork 444
/
generate_docs.go
374 lines (340 loc) · 9.63 KB
/
generate_docs.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
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/gob"
"fmt"
"io/ioutil"
"os"
"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v32/github"
"github.com/rotisserie/eris"
. "github.com/solo-io/gloo/docs/cmd/securityscanutils"
changelogdocutils "github.com/solo-io/go-utils/changeloggenutils"
"github.com/solo-io/go-utils/githubutils"
"github.com/spf13/cobra"
)
func main() {
ctx := context.Background()
app := rootApp(ctx)
if err := app.Execute(); err != nil {
fmt.Printf("unable to run: %v\n", err)
os.Exit(1)
}
}
type options struct {
ctx context.Context
}
func rootApp(ctx context.Context) *cobra.Command {
opts := &options{
ctx: ctx,
}
app := &cobra.Command{
Use: "docs-util",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
app.AddCommand(changelogMdFromGithubCmd(opts))
app.AddCommand(securityScanMdFromCmd(opts))
app.AddCommand(enterpriseHelmValuesMdFromGithubCmd(opts))
app.AddCommand(getReleasesCmd(opts))
return app
}
// Serializes github repository release and prints serialized releases to stdout
// To be used for caching release data for changelog/security scan docsgen.
func getReleasesCmd(opts *options) *cobra.Command {
app := &cobra.Command{
Use: "gen-releases",
Short: "cache github releases for gloo edge repository",
RunE: fetchAndSerializeReleases(opts),
}
return app
}
func fetchAndSerializeReleases(opts *options) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if !useCachedReleases() {
return nil
}
if len(args) != 1 {
return InvalidInputError(fmt.Sprintf("%v", len(args)-1))
}
client, err := githubutils.GetClient(opts.ctx)
if err != nil {
return err
}
target := args[0]
switch target {
case glooDocGen:
err = getRepoReleases(opts.ctx, glooOpenSourceRepo, client)
case glooEDocGen:
err = getRepoReleases(opts.ctx, glooEnterpriseRepo, client)
default:
return InvalidInputError(target)
}
return err
}
}
func securityScanMdFromCmd(opts *options) *cobra.Command {
app := &cobra.Command{
Use: "gen-security-scan-md",
Short: "generate a markdown file from gcloud bucket",
RunE: func(cmd *cobra.Command, args []string) error {
if os.Getenv(skipSecurityScan) != "" {
return nil
}
return generateSecurityScanMd(args)
},
}
return app
}
func changelogMdFromGithubCmd(opts *options) *cobra.Command {
app := &cobra.Command{
Use: "gen-changelog-md",
Short: "generate a markdown file from Github Release pages API",
RunE: func(cmd *cobra.Command, args []string) error {
if os.Getenv(skipChangelogGeneration) != "" {
return nil
}
return generateChangelogMd(args)
},
}
return app
}
func enterpriseHelmValuesMdFromGithubCmd(opts *options) *cobra.Command {
app := &cobra.Command{
Use: "get-enterprise-helm-values",
Short: "Get documentation of valid helm values from Gloo Enterprise github",
RunE: func(cmd *cobra.Command, args []string) error {
return fetchEnterpriseHelmValues(args)
},
}
return app
}
// Serialized github RepositoryRelease array to be written to file
func getRepoReleases(ctx context.Context, repo string, client *github.Client) error {
allReleases, err := githubutils.GetAllRepoReleases(ctx, client, "solo-io", repo)
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(allReleases)
if err != nil {
return err
}
fmt.Print(buf.String())
return nil
}
const (
glooDocGen = "gloo"
glooEDocGen = "glooe"
skipChangelogGeneration = "SKIP_CHANGELOG_GENERATION"
skipSecurityScan = "SKIP_SECURITY_SCAN"
)
const (
glooOpenSourceRepo = "gloo"
glooEnterpriseRepo = "solo-projects"
)
const (
glooCachedReleasesFile = "opensource.out"
glooeCachedReleasesFile = "enterprise.out"
)
var (
InvalidInputError = func(arg string) error {
return eris.Errorf("invalid input, must provide exactly one argument, either '%v' or '%v', (provided %v)",
glooDocGen,
glooEDocGen,
arg)
}
MissingGithubTokenError = func(envVar string) error {
return eris.Errorf("Must either set GITHUB_TOKEN or set %s environment variable to true", envVar)
}
FileNotFoundError = func(path string, branch string) error {
return eris.Errorf("Could not find file at path %s on branch %s", path, branch)
}
)
// Generates changelog for releases as fetched from Github
// Github defaults to a chronological order
func generateChangelogMd(args []string) error {
if len(args) != 1 {
return InvalidInputError(fmt.Sprintf("%v", len(args)-1))
}
client := github.NewClient(nil)
target := args[0]
switch target {
case glooDocGen:
generator := changelogdocutils.NewMinorReleaseGroupedChangelogGenerator(changelogdocutils.Options{
MainRepo: "gloo",
RepoOwner: "solo-io",
MainRepoReleases: getCachedReleases(glooCachedReleasesFile),
}, client)
out, err := generator.GenerateJSON(context.Background())
if err != nil {
return err
}
fmt.Println(out)
case glooEDocGen:
err := generateGlooEChangelog()
if err != nil {
return err
}
default:
return InvalidInputError(target)
}
return nil
}
// Fetches Gloo Enterprise releases, merges in open source release notes, and orders them by version
func generateGlooEChangelog() error {
// Initialize Auth
ctx := context.Background()
ghToken := os.Getenv("GITHUB_TOKEN")
if ghToken == "" {
return MissingGithubTokenError(skipChangelogGeneration)
}
client, err := githubutils.GetClient(ctx)
if err != nil {
return err
}
opts := changelogdocutils.Options{
NumVersions: 200,
MainRepo: "solo-projects",
DependentRepo: "gloo",
RepoOwner: "solo-io",
MainRepoReleases: getCachedReleases(glooeCachedReleasesFile),
DependentRepoReleases: getCachedReleases(glooCachedReleasesFile),
}
depFn, err := changelogdocutils.GetOSDependencyFunc("solo-io", "solo-projects", "gloo", ghToken)
if err != nil {
return err
}
generator := changelogdocutils.NewMergedReleaseGeneratorWithDepFn(opts, client, depFn)
out, err := generator.GenerateJSON(context.Background())
if err != nil {
return err
}
fmt.Println(out)
return nil
}
func getCachedReleases(fileName string) []*github.RepositoryRelease {
bArray, err := ioutil.ReadFile(fileName)
if err != nil {
return nil
}
buf := bytes.NewBuffer(bArray)
enc := gob.NewDecoder(buf)
var releases []*github.RepositoryRelease
err = enc.Decode(&releases)
if err != nil {
return nil
}
return releases
}
func useCachedReleases() bool {
if os.Getenv("USE_CACHED_RELEASES") == "false" {
return false
}
return true
}
// Generates security scan log for releases
func generateSecurityScanMd(args []string) error {
if len(args) != 1 {
return InvalidInputError(fmt.Sprintf("%v", len(args)-1))
}
target := args[0]
var (
err error
)
switch target {
case glooDocGen:
err = generateSecurityScanGloo(context.Background())
case glooEDocGen:
err = generateSecurityScanGlooE(context.Background())
default:
return InvalidInputError(target)
}
return err
}
func generateSecurityScanGloo(ctx context.Context) error {
client := github.NewClient(nil)
var (
allReleases []*github.RepositoryRelease
err error
)
if useCachedReleases() {
allReleases = getCachedReleases(glooCachedReleasesFile)
} else {
allReleases, err = githubutils.GetAllRepoReleases(ctx, client, "solo-io", glooOpenSourceRepo)
if err != nil {
return err
}
}
githubutils.SortReleasesBySemver(allReleases)
var tagNames []string
for _, release := range allReleases {
// ignore beta releases when display security scan results
test, err := semver.NewVersion(release.GetTagName())
stableOnlyConstraint, _ := semver.NewConstraint(">= 1.4.0")
if err == nil && stableOnlyConstraint.Check(test) {
tagNames = append(tagNames, release.GetTagName())
}
}
return BuildSecurityScanReportGloo(tagNames)
}
func generateSecurityScanGlooE(ctx context.Context) error {
// Initialize Auth
client, err := githubutils.GetClient(ctx)
if err != nil {
return err
}
var allReleases []*github.RepositoryRelease
if useCachedReleases() {
allReleases = getCachedReleases(glooeCachedReleasesFile)
} else {
allReleases, err = githubutils.GetAllRepoReleases(ctx, client, "solo-io", glooEnterpriseRepo)
if err != nil {
return err
}
}
githubutils.SortReleasesBySemver(allReleases)
var tagNames []string
for _, release := range allReleases {
// ignore beta releases when display security scan results
test, err := semver.NewVersion(release.GetTagName())
stableOnlyConstraint, _ := semver.NewConstraint(">= 1.4.0")
if err == nil && stableOnlyConstraint.Check(test) {
tagNames = append(tagNames, release.GetTagName())
}
}
return BuildSecurityScanReportGlooE(tagNames)
}
func fetchEnterpriseHelmValues(args []string) error {
ctx := context.Background()
client, err := githubutils.GetClient(ctx)
if err != nil {
return err
}
// Download the file at the specified path on the latest released branch of solo-projects
path := "install/helm/gloo-ee/reference/values.txt"
semverReleaseTag, err := ioutil.ReadFile("../_output/gloo-enterprise-version")
if err != nil {
return err
}
version, err := semver.NewVersion(string(semverReleaseTag))
if err != nil {
return err
}
minorReleaseTag := fmt.Sprintf("v%d.%d.x", version.Major(), version.Minor())
files, err := githubutils.GetFilesFromGit(ctx, client, "solo-io", glooEnterpriseRepo, minorReleaseTag, path)
if err != nil {
return err
}
if len(files) <= 0 {
return FileNotFoundError(path, minorReleaseTag)
}
// Decode the file and log to the console
decodedContents, err := base64.StdEncoding.DecodeString(*files[0].Content)
if err != nil {
return err
}
fmt.Printf("%s", decodedContents)
return nil
}