-
Notifications
You must be signed in to change notification settings - Fork 4
/
owner.go
119 lines (97 loc) · 3.21 KB
/
owner.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
package mapper
import (
"context"
"github.com/Interhyp/metadata-service/api"
"sort"
"github.com/Interhyp/metadata-service/internal/service/util"
)
func (s *Impl) GetSortedOwnerAliases(_ context.Context) ([]string, error) {
fileInfos, err := s.Metadata.ReadDir("owners/")
if err != nil {
return []string{}, err
}
result := make([]string, 0)
for i := range fileInfos {
alias := fileInfos[i].Name()
if fileInfos[i].IsDir() {
// check presence of owner.info.yaml to be sure
_, err := s.Metadata.Stat("owners/" + alias + "/owner.info.yaml")
if err == nil {
if s.CustomConfiguration.OwnerFilterAliasRegex().MatchString(alias) {
result = append(result, alias)
}
}
}
}
sort.Strings(result)
return result, nil
}
func (s *Impl) GetOwner(ctx context.Context, ownerAlias string) (openapi.OwnerDto, error) {
result := openapi.OwnerDto{}
fullPath := "owners/" + ownerAlias + "/owner.info.yaml"
err := GetT[openapi.OwnerDto](ctx, s, &result, fullPath)
if nil == err {
if result.Groups != nil {
s.processGroupMap(ctx, result.Groups)
}
}
return result, err
}
func (s *Impl) processGroupMap(ctx context.Context, groupsMap *map[string][]string) {
for groupName, groupMembers := range *groupsMap {
users, groups := util.SplitUsersAndGroups(groupMembers)
if len(users) > 0 {
filteredExistingUsers, err2 := s.Bitbucket.FilterExistingUsernames(ctx, users)
if err2 == nil {
userDifference := util.Difference(users, filteredExistingUsers)
if len(userDifference) > 0 {
s.Logging.Logger().Ctx(ctx).Warn().Printf("Found unknown users in configuration: %v", userDifference)
}
(*groupsMap)[groupName] = append(filteredExistingUsers, groups...)
} else {
s.Logging.Logger().Ctx(ctx).Error().Printf("Error checking existing bitbucket users: %s", err2.Error())
}
if len((*groupsMap)[groupName]) <= 0 && len(users) > 0 {
s.Logging.Logger().Ctx(ctx).Warn().Printf("Fallback to predefined reviewers")
(*groupsMap)[groupName] = append((*groupsMap)[groupName], s.CustomConfiguration.BitbucketReviewerFallback())
}
}
}
}
func (s *Impl) WriteOwner(ctx context.Context, ownerAlias string, owner openapi.OwnerDto) (openapi.OwnerDto, error) {
err := s.Metadata.Pull(ctx)
if err != nil {
return owner, err
}
path := "owners/" + ownerAlias
fileName := "owner.info.yaml"
description := "owner " + ownerAlias
err = WriteT[openapi.OwnerDto](ctx, s, &owner, path, fileName, description, owner.JiraIssue)
return owner, err
}
func (s *Impl) DeleteOwner(ctx context.Context, ownerAlias string, jiraIssue string) (openapi.OwnerPatchDto, error) {
result := openapi.OwnerPatchDto{}
err := s.Metadata.Pull(ctx)
if err != nil {
return result, err
}
fullPath := "owners/" + ownerAlias + "/owner.info.yaml"
description := "owner " + ownerAlias
err = DeleteT[openapi.OwnerPatchDto](ctx, s, &result, fullPath, description, jiraIssue)
return result, err
}
func (s *Impl) IsOwnerEmpty(_ context.Context, ownerAlias string) bool {
s.muOwnerCaches.Lock()
defer s.muOwnerCaches.Unlock()
for _, owner := range s.serviceOwnerCache {
if owner == ownerAlias {
return false
}
}
for _, owner := range s.repositoryOwnerCache {
if owner == ownerAlias {
return false
}
}
return true
}