aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/pkg/triage/tree.go
blob: 5d75c380ae848b722a37c10972862fceb908a91a (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
// Copyright 2024 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 triage

import (
	"sort"
	"strings"

	"github.com/google/syzkaller/syz-cluster/pkg/api"
)

// SelectTrees returns an ordered list of git trees to apply the series to.
func SelectTrees(series *api.Series, trees []*api.Tree) []*api.Tree {
	seriesCc := map[string]bool{}
	for _, cc := range series.Cc {
		seriesCc[strings.ToLower(cc)] = true
	}
	tagsMap := map[string]bool{}
	for _, tag := range series.SubjectTags {
		tagsMap[tag] = true
	}
	var result []*api.Tree
	for _, tree := range trees {
		if tagsMap[tree.Name] {
			// If the tree was directly mentioned in the patch subject, always take it.
			result = append(result, tree)
			continue
		}
		intersects := false
		for _, cc := range tree.EmailLists {
			if seriesCc[strings.ToLower(cc)] {
				intersects = true
				break
			}
		}
		if len(tree.EmailLists) > 0 && !intersects {
			continue
		}
		result = append(result, tree)
	}
	sort.SliceStable(result, func(i, j int) bool {
		// First the trees from the patch subject, then everything else.
		return tagsMap[result[i].Name] && !tagsMap[result[j].Name]
	})
	return result
}

func TreeFromBranch(trees []*api.Tree, branch string) *api.Tree {
	for _, tree := range trees {
		if strings.HasPrefix(branch, tree.Name+"/") {
			return tree
		}
	}
	return nil
}