aboutsummaryrefslogtreecommitdiffstats
path: root/dashboard/app/label.go
blob: de874cf50d2f9c1509d5fb7fba3fad3874c9e426 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package main

import (
	"context"
	"fmt"
	"sort"
	"strings"

	"github.com/google/syzkaller/pkg/report/crash"
)

const (
	EmptyLabel           BugLabelType = ""
	SubsystemLabel       BugLabelType = "subsystems"
	PriorityLabel        BugLabelType = "prio"
	NoRemindersLabel     BugLabelType = "no-reminders"
	OriginLabel          BugLabelType = "origin"
	MissingBackportLabel BugLabelType = "missing-backport"
	RaceLabel            BugLabelType = "race"
)

type BugPrio string

const (
	LowPrioBug    BugPrio = "low"
	NormalPrioBug BugPrio = "normal"
	HighPrioBug   BugPrio = "high"
)

const (
	BenignRace  = "benign"
	HarmfulRace = "harmful"
)

type oneOf []string
type subsetOf []string
type trueFalse struct{}

func makeLabelSet(c context.Context, bug *Bug) *labelSet {
	ret := map[BugLabelType]any{
		PriorityLabel: oneOf([]string{
			string(LowPrioBug),
			string(NormalPrioBug),
			string(HighPrioBug),
		}),
		NoRemindersLabel:     trueFalse{},
		MissingBackportLabel: trueFalse{},
	}
	typ := crash.TitleToType(bug.Title)
	if typ == crash.KCSANDataRace {
		ret[RaceLabel] = oneOf([]string{BenignRace, HarmfulRace})
	}
	cfg := getNsConfig(c, bug.Namespace)
	service := cfg.Subsystems.Service
	if service != nil {
		names := []string{}
		for _, item := range service.List() {
			names = append(names, item.Name)
		}
		ret[SubsystemLabel] = subsetOf(names)
	}

	originLabels := []string{}
	for _, repo := range cfg.Repos {
		if repo.LabelIntroduced != "" {
			originLabels = append(originLabels, repo.LabelIntroduced)
		}
		if repo.LabelReached != "" {
			originLabels = append(originLabels, repo.LabelReached)
		}
	}

	if len(originLabels) > 0 {
		ret[OriginLabel] = subsetOf(originLabels)
	}

	return &labelSet{
		c:      c,
		ns:     bug.Namespace,
		labels: ret,
	}
}

type labelSet struct {
	c      context.Context
	ns     string
	labels map[BugLabelType]any
}

func (s *labelSet) FindLabel(label BugLabelType) bool {
	_, ok := s.labels[label]
	return ok
}

func (s *labelSet) ValidateValues(label BugLabelType, values []BugLabel) string {
	rules := s.labels[label]
	if rules == nil {
		return ""
	}
	switch v := rules.(type) {
	case oneOf:
		if len(values) != 1 {
			return "You must specify only one of the allowed values"
		}
		if !stringInList([]string(v), values[0].Value) {
			return fmt.Sprintf("%q is not among the allowed values", values[0].Value)
		}
	case subsetOf:
		for _, item := range values {
			if !stringInList([]string(v), item.Value) {
				return fmt.Sprintf("%q is not among the allowed values", item.Value)
			}
		}
	case trueFalse:
		if len(values) != 1 || values[0].Value != "" {
			return "This label does not support any values"
		}
	}
	return ""
}

func (s *labelSet) Help() string {
	var sortedKeys []BugLabelType
	for key := range s.labels {
		sortedKeys = append(sortedKeys, key)
	}
	sort.Slice(sortedKeys, func(i, j int) bool {
		return string(sortedKeys[i]) < string(sortedKeys[j])
	})

	var help strings.Builder
	for _, key := range sortedKeys {
		if help.Len() > 0 {
			help.WriteString(", ")
		}
		if key == SubsystemLabel {
			help.WriteString(fmt.Sprintf("%s: {.. see below ..}", key))
			continue
		}
		switch v := s.labels[key].(type) {
		case oneOf:
			help.WriteString(string(key))
			help.WriteString(": {")
			list := []string(v)
			for i := range list {
				if i > 0 {
					help.WriteString(", ")
				}
				help.WriteString(list[i])
			}
			help.WriteByte('}')
		case trueFalse:
			help.WriteString(string(key))
		}
	}

	var sb strings.Builder
	writeWrapped(&sb, help.String())
	if _, ok := s.labels[SubsystemLabel]; ok {
		url := subsystemListURL(s.c, s.ns)
		if url != "" {
			sb.WriteString(fmt.Sprintf("\nThe list of subsystems: %s", url))
		}
	}
	return sb.String()
}

func writeWrapped(sb *strings.Builder, str string) {
	const limit = 80
	lineLen := 0
	for _, token := range strings.Fields(str) {
		if lineLen >= limit ||
			lineLen > 0 && lineLen+len(token) >= limit {
			sb.WriteByte('\n')
			lineLen = 0
		}
		if lineLen > 0 {
			sb.WriteString(" ")
		}
		sb.WriteString(token)
		lineLen += len(token)
	}
}

func (bug *Bug) HasLabel(label BugLabelType, value string) bool {
	for _, item := range bug.Labels {
		if item.Label == label && item.Value == value {
			return true
		}
	}
	return false
}

func (bug *Bug) LabelValues(label BugLabelType) []BugLabel {
	var ret []BugLabel
	for _, item := range bug.Labels {
		if item.Label == label {
			ret = append(ret, item)
		}
	}
	return ret
}

func (bug *Bug) SetLabels(set *labelSet, values []BugLabel) error {
	var label BugLabelType
	for _, v := range values {
		if label != EmptyLabel && label != v.Label {
			return fmt.Errorf("values of the same label type are expected")
		}
		label = v.Label
	}
	if errStr := set.ValidateValues(label, values); errStr != "" {
		return fmt.Errorf("%s", errStr)
	}
	bug.UnsetLabels(label)
	bug.Labels = append(bug.Labels, values...)
	return nil
}

func (bug *Bug) UnsetLabels(labels ...BugLabelType) map[BugLabelType]struct{} {
	toDelete := map[BugLabelType]struct{}{}
	notFound := map[BugLabelType]struct{}{}
	for _, name := range labels {
		toDelete[name] = struct{}{}
		notFound[name] = struct{}{}
	}
	var newList []BugLabel
	for _, item := range bug.Labels {
		if _, ok := toDelete[item.Label]; ok {
			delete(notFound, item.Label)
			continue
		}
		newList = append(newList, item)
	}
	bug.Labels = newList
	return notFound
}

func (bug *Bug) HasUserLabel(label BugLabelType) bool {
	for _, item := range bug.Labels {
		if item.Label == label && item.SetBy != "" {
			return true
		}
	}
	return false
}

func (bug *Bug) prio() BugPrio {
	for _, label := range bug.LabelValues(PriorityLabel) {
		return BugPrio(label.Value)
	}
	return NormalPrioBug
}

var bugPrioOrder = map[BugPrio]int{
	LowPrioBug:    1,
	NormalPrioBug: 2,
	HighPrioBug:   3,
}

func (bp BugPrio) LessThan(other BugPrio) bool {
	return bugPrioOrder[bp] < bugPrioOrder[other]
}