-
Notifications
You must be signed in to change notification settings - Fork 5
/
open.go
90 lines (73 loc) · 2.41 KB
/
open.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package checklist
import (
"context"
"fmt"
"log"
"os"
"github.com/cilium/release/pkg/github"
"github.com/cilium/release/pkg/types"
gh "github.com/google/go-github/v50/github"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)
var cfg ChecklistConfig
type ChecklistConfig struct {
types.CommonConfig
TargetVer string
TemplatePath string
DryRun bool
}
func (cfg *ChecklistConfig) Sanitize() error {
if err := cfg.CommonConfig.Sanitize(); err != nil {
return err
}
if !semver.IsValid(cfg.TargetVer) {
return fmt.Errorf("invalid --target-version=%s. Expected form 'vX.Y.Z(-rc.W|-pre.N)'", cfg.TargetVer)
}
return nil
}
func OpenCommand(ctx context.Context, logger *log.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "open",
Short: "Open a new release checklist",
RunE: func(cmd *cobra.Command, _ []string) error {
cfg.TargetVer = semver.Canonical(cfg.TargetVer)
if err := cfg.Sanitize(); err != nil {
cmd.Usage()
return fmt.Errorf("Failed to validate configuration: %s", err)
}
tmpl, err := prepareChecklist(cfg)
if err != nil {
return fmt.Errorf("Failed to apply template configuration to template: %s", err)
}
if cfg.DryRun {
fmt.Printf("%s", tmpl)
return nil
}
ghClient := github.NewClient(os.Getenv("GITHUB_TOKEN"))
return CreateIssue(ctx, ghClient, cfg, tmpl)
},
}
cmd.Flags().StringVar(&cfg.TemplatePath, "template", "", "Template path to create release checklist")
cmd.Flags().StringVar(&cfg.TargetVer, "target-version", "", "Target version to release")
cmd.Flags().StringVar(&cfg.RepoName, "repo", "cilium/release", "GitHub organization and repository names separated by a slash")
cmd.Flags().BoolVar(&cfg.DryRun, "dry-run", false, "Print the template, but do not open an issue on GitHub")
for _, flag := range []string{"target-version", "template"} {
cobra.MarkFlagRequired(cmd.Flags(), flag)
}
return cmd
}
func CreateIssue(ctx context.Context, ghClient *gh.Client, cfg ChecklistConfig, tmpl string) error {
req, err := templateToRequest(tmpl)
if err != nil {
return fmt.Errorf("Issue template metadata unreadable: %w", err)
}
res, _, err := ghClient.Issues.Create(ctx, cfg.Owner, cfg.Repo, req)
if err != nil {
return fmt.Errorf("Failed to create release checklist: %w", err)
}
fmt.Fprintf(os.Stderr, "Created issue at %s\n", *res.HTMLURL)
return nil
}