-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathcreate.go
179 lines (138 loc) · 3.8 KB
/
create.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
/*
Copyright 2020 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 proposal
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/sirupsen/logrus"
"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/repo"
)
type CreateOpts struct {
Repo *repo.Repo
// Proposal options
KEP string // KEP name sig-xxx/xxx-name
Name string
Number string
SIG string
// Create options
Title string
Approvers []string
Authors []string
Reviewers []string
Type string
State string
Stage string
ParticipatingSIGs []string
PRRApprovers []string
}
// Validate checks the args provided to the create command and parses the sig,
// kep # and name to populate the create opts
func (c *CreateOpts) Validate(args []string) error {
// TODO: Populate logic
// nolint:gocritic
/*
err := repoOpts.ValidateAndPopulateKEP(args)
if err != nil {
return err
}
if len(c.PRRApprovers) == 0 {
return errors.New("must provide at least one PRR Approver")
}
*/
return nil
}
// Create builds a new KEP based on the README.md and kep.yaml templates in the
// path specified by the command args. CreateOpts is used to populate the template
func Create(opts *CreateOpts) error {
r := opts.Repo
logrus.Infof("Creating KEP %s %s %s", opts.SIG, opts.Number, opts.Name)
kep := &api.Proposal{}
populateProposal(kep, opts)
errs := r.KEPHandler.Validate(kep)
if errs != nil {
return fmt.Errorf("invalid kep: %v", errs)
}
err := createKEP(kep, opts)
if err != nil {
return err
}
return nil
}
func createKEP(kep *api.Proposal, opts *CreateOpts) error {
r := opts.Repo
logrus.Infof("Generating new KEP %s in %s ===>", opts.Name, opts.SIG)
err := r.WriteKEP(kep)
if err != nil {
return fmt.Errorf("unable to create KEP: %s", err)
}
template := r.ProposalTemplate
newPath := filepath.Join(
r.ProposalPath,
opts.SIG,
opts.Name,
repo.ProposalFilename,
)
if writeErr := ioutil.WriteFile(newPath, template, os.ModePerm); writeErr != nil {
return fmt.Errorf("writing KEP data to file: %w", writeErr)
}
return nil
}
func populateProposal(p *api.Proposal, opts *CreateOpts) {
p.Name = opts.Name
if opts.State != "" {
p.Status = api.Status(opts.State)
}
now := time.Now()
layout := "2006-01-02"
p.CreationDate = now.Format(layout)
p.Number = opts.Number
p.Title = opts.Title
if len(opts.Authors) > 0 {
authors := []string{}
for _, author := range opts.Authors {
if !strings.HasPrefix(author, "@") {
author = fmt.Sprintf("@%s", author)
}
authors = append(authors, author)
}
p.Authors = authors
}
if len(opts.Approvers) > 0 {
p.Approvers = updatePersonReference(opts.Approvers)
}
if len(opts.Reviewers) > 0 {
p.Reviewers = updatePersonReference(opts.Reviewers)
}
p.OwningSIG = opts.SIG
// TODO(lint): appendAssign: append result not assigned to the same slice (gocritic)
//nolint:gocritic
p.ParticipatingSIGs = append(opts.ParticipatingSIGs, opts.SIG)
p.Filename = opts.Name
p.LastUpdated = "v1.19"
p.Stage = api.Stage(opts.Stage)
}
func updatePersonReference(names []string) []string {
persons := []string{}
for _, name := range names {
if !strings.HasPrefix(name, "@") {
name = fmt.Sprintf("@%s", name)
}
persons = append(persons, name)
}
return persons
}