cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/checks/alerts_annotation.go

73lines · modecode

1package checks
2
3import (
4 "fmt"
5 "regexp"
6
7 "github.com/cloudflare/pint/internal/parser"
8)
9
10const (
11 AnnotationCheckName = "alerts/annotation"
12)
13
14func NewAnnotationCheck(key string, valueRe *regexp.Regexp, isReguired bool, severity Severity) AnnotationCheck {
15 return AnnotationCheck{key: key, valueRe: valueRe, isReguired: isReguired, severity: severity}
16}
17
18type AnnotationCheck struct {
19 key string
20 valueRe *regexp.Regexp
21 isReguired bool
22 severity Severity
23}
24
25func (c AnnotationCheck) String() string {
26 return fmt.Sprintf("%s(%s)", AnnotationCheckName, c.key)
27}
28
29func (c AnnotationCheck) Check(rule parser.Rule) (problems []Problem) {
30 if rule.AlertingRule == nil {
31 return nil
32 }
33
34 if rule.AlertingRule.Annotations == nil {
35 if c.isReguired {
36 problems = append(problems, Problem{
37 Fragment: fmt.Sprintf("%s: %s", rule.AlertingRule.Alert.Key.Value, rule.AlertingRule.Alert.Value.Value),
38 Lines: rule.Lines(),
39 Reporter: AnnotationCheckName,
40 Text: fmt.Sprintf("%s annotation is required", c.key),
41 Severity: c.severity,
42 })
43 }
44 return
45 }
46
47 val := rule.AlertingRule.Annotations.GetValue(c.key)
48 if val == nil {
49 if c.isReguired {
50 problems = append(problems, Problem{
51 Fragment: fmt.Sprintf("%s:", rule.AlertingRule.Annotations.Key.Value),
52 Lines: rule.AlertingRule.Annotations.Lines(),
53 Reporter: AnnotationCheckName,
54 Text: fmt.Sprintf("%s annotation is required", c.key),
55 Severity: c.severity,
56 })
57 }
58 return
59 }
60
61 if c.valueRe != nil && !c.valueRe.MatchString(val.Value) {
62 problems = append(problems, Problem{
63 Fragment: fmt.Sprintf("%s: %s", c.key, val.Value),
64 Lines: val.Position.Lines,
65 Reporter: AnnotationCheckName,
66 Text: fmt.Sprintf("%s annotation value must match regex: %s", c.key, c.valueRe.String()),
67 Severity: c.severity,
68 })
69 return
70 }
71
72 return nil
73}
74