forked from kubernetes/enhancements
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproposal.go
179 lines (143 loc) · 4.84 KB
/
proposal.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 2021 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 api
import (
"bufio"
"bytes"
"crypto/md5"
"fmt"
"io"
"strings"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
var ValidStages = []string{
"alpha",
"beta",
"stable",
}
type Proposals []*Proposal
func (p *Proposals) AddProposal(proposal *Proposal) {
*p = append(*p, proposal)
}
// TODO(api): json fields are not using consistent casing
type Proposal struct {
ID string `json:"id"`
PRNumber string `json:"prNumber,omitempty"`
Name string `json:"name,omitempty"`
Title string `json:"title" yaml:"title" validate:"required"`
Number string `json:"kep-number" yaml:"kep-number" validate:"required"`
Authors []string `json:"authors" yaml:",flow"`
OwningSIG string `json:"owningSig" yaml:"owning-sig" validate:"required"`
ParticipatingSIGs []string `json:"participatingSigs" yaml:"participating-sigs,flow,omitempty"`
Reviewers []string `json:"reviewers" yaml:",flow"`
Approvers []string `json:"approvers" yaml:",flow"`
PRRApprovers []string `json:"prrApprovers" yaml:"prr-approvers,flow"`
Editor string `json:"editor" yaml:"editor,omitempty"`
CreationDate string `json:"creationDate" yaml:"creation-date"`
LastUpdated string `json:"lastUpdated" yaml:"last-updated"`
Status string `json:"status" yaml:"status" validate:"required"`
SeeAlso []string `json:"seeAlso" yaml:"see-also,omitempty"`
Replaces []string `json:"replaces" yaml:"replaces,omitempty"`
SupersededBy []string `json:"supersededBy" yaml:"superseded-by,omitempty"`
Stage string `json:"stage" yaml:"stage"`
LatestMilestone string `json:"latestMilestone" yaml:"latest-milestone"`
Milestone Milestone `json:"milestone" yaml:"milestone"`
FeatureGates []FeatureGate `json:"featureGates" yaml:"feature-gates"`
DisableSupported bool `json:"disableSupported" yaml:"disable-supported"`
Metrics []string `json:"metrics" yaml:"metrics"`
Filename string `json:"-" yaml:"-"`
Error error `json:"-" yaml:"-"`
Contents string `json:"markdown" yaml:"-"`
}
func (p *Proposal) Validate() error {
v := validator.New()
if err := v.Struct(p); err != nil {
return errors.Wrap(err, "running validation")
}
return nil
}
func (p *Proposal) IsMissingMilestone() bool {
return p.LatestMilestone == ""
}
func (p *Proposal) IsMissingStage() bool {
return p.Stage == ""
}
type KEPHandler Parser
func NewKEPHandler() (*KEPHandler, error) {
handler := &KEPHandler{}
groups, err := FetchGroups()
if err != nil {
return nil, errors.Wrap(err, "fetching groups")
}
handler.Groups = groups
approvers, err := FetchPRRApprovers()
if err != nil {
return nil, errors.Wrap(err, "fetching PRR approvers")
}
handler.PRRApprovers = approvers
return handler, nil
}
// TODO(api): Make this a generic parser for all `Document` types
func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
scanner := bufio.NewScanner(in)
count := 0
metadata := []byte{}
var body bytes.Buffer
for scanner.Scan() {
line := scanner.Text() + "\n"
if strings.Contains(line, "---") {
count++
continue
}
if count == 1 {
metadata = append(metadata, []byte(line)...)
} else {
body.WriteString(line)
}
}
kep := &Proposal{
Contents: body.String(),
}
if err := scanner.Err(); err != nil {
return kep, errors.Wrap(err, "reading file")
}
// this file is just the KEP metadata
if count == 0 {
metadata = body.Bytes()
kep.Contents = ""
}
if err := yaml.Unmarshal(metadata, &kep); err != nil {
k.Errors = append(k.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return kep, errors.Wrap(err, "unmarshalling YAML")
}
if valErr := kep.Validate(); valErr != nil {
k.Errors = append(k.Errors, errors.Wrap(valErr, "validating KEP"))
return kep, errors.Wrap(valErr, "validating KEP")
}
kep.ID = hash(kep.OwningSIG + ":" + kep.Title)
return kep, nil
}
type Milestone struct {
Alpha string `json:"alpha" yaml:"alpha"`
Beta string `json:"beta" yaml:"beta"`
Stable string `json:"stable" yaml:"stable"`
}
type FeatureGate struct {
Name string `json:"name" yaml:"name"`
Components []string `json:"components" yaml:"components"`
}
func hash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}