-
Notifications
You must be signed in to change notification settings - Fork 34
/
rule_types.go
159 lines (130 loc) · 3.67 KB
/
rule_types.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
// Copyright 2023 Stacklok, Inc.
//
// 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 v1
import (
"bytes"
"encoding/json"
"fmt"
"io"
"google.golang.org/protobuf/proto"
"github.com/stacklok/minder/internal/util/jsonyaml"
)
const (
// IngestTypeDiff is the ingest type for a diff
IngestTypeDiff = "diff"
)
const (
// DiffTypeDep is the diff type for a dependency
DiffTypeDep = "dep"
// DiffTypeFull is the diff type for including all files from the PR diff
DiffTypeFull = "full"
)
// ParseRuleType parses a rule type from a reader
func ParseRuleType(r io.Reader) (*RuleType, error) {
// We transcode to JSON so we can decode it straight to the protobuf structure
w := &bytes.Buffer{}
if err := jsonyaml.TranscodeYAMLToJSON(r, w); err != nil {
return nil, fmt.Errorf("error converting yaml to json: %w", err)
}
rt := &RuleType{}
if err := json.NewDecoder(w).Decode(rt); err != nil {
return nil, fmt.Errorf("error decoding json: %w", err)
}
return rt, nil
}
// WithDefaultDisplayName sets the display name if it is not set
func (r *RuleType) WithDefaultDisplayName() *RuleType {
if r == nil {
return nil
}
if r.DisplayName == "" {
r.DisplayName = r.Name
}
return r
}
// GetContext returns the context from the nested RuleType
func (r *CreateRuleTypeRequest) GetContext() *Context {
if r != nil && r.RuleType != nil {
return r.RuleType.Context
}
return nil
}
// GetContext returns the context from the nested RuleType
func (r *UpdateRuleTypeRequest) GetContext() *Context {
if r != nil && r.RuleType != nil {
return r.RuleType.Context
}
return nil
}
// InitializedStringValue returns the string value of the severity
// with initialization done.
func (s *Severity) InitializedStringValue() string {
return s.EnsureDefault().GetValue().Enum().AsString()
}
// EnsureDefault ensures the rule type has a default value
func (s *Severity) EnsureDefault() *Severity {
if s == nil {
s = &Severity{}
}
if s.Value == Severity_VALUE_UNSPECIFIED {
s.Value = Severity_VALUE_UNKNOWN
}
return s
}
// AsString returns a human-readable string for the severity value
func (s *Severity_Value) AsString() string {
if s == nil {
return "unknown"
}
v := s.Descriptor().Values().ByNumber(s.Number())
if v == nil {
return ""
}
extension := proto.GetExtension(v.Options(), E_Name)
n, ok := extension.(string)
if !ok {
return ""
}
return n
}
// FromString sets the severity value from a string
func (s *Severity_Value) FromString(str string) error {
vals := s.Descriptor().Values()
for i := 0; i < vals.Len(); i++ {
v := vals.Get(i)
extension := proto.GetExtension(v.Options(), E_Name)
n, ok := extension.(string)
if !ok {
continue
}
if n == str {
num := v.Number()
*s = Severity_Value(num)
return nil
}
}
return fmt.Errorf("unknown severity value: %s", str)
}
// MarshalJSON marshals the severity value to a JSON string
func (s *Severity_Value) MarshalJSON() ([]byte, error) {
return json.Marshal(s.AsString())
}
// UnmarshalJSON unmarshals the severity value from a JSON string
func (s *Severity_Value) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
}
return s.FromString(str)
}