-
Notifications
You must be signed in to change notification settings - Fork 4
/
repository.go
215 lines (181 loc) · 6.94 KB
/
repository.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
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
package mapper
import (
"context"
"errors"
"fmt"
"github.com/Interhyp/metadata-service/api"
"github.com/Interhyp/metadata-service/internal/acorn/errors/nochangeserror"
"github.com/Interhyp/metadata-service/internal/service/util"
"github.com/StephanHCB/go-backend-service-common/api/apierrors"
"sort"
"strings"
)
func (s *Impl) replaceRepositoryOwnerCache(newCache map[string]string) {
s.muOwnerCaches.Lock()
defer s.muOwnerCaches.Unlock()
s.repositoryOwnerCache = newCache
}
func (s *Impl) lookupInRepositoryOwnerCache(repoKey string) (string, bool) {
s.muOwnerCaches.Lock()
defer s.muOwnerCaches.Unlock()
owner, ok := s.repositoryOwnerCache[repoKey]
return owner, ok
}
func (s *Impl) GetSortedRepositoryKeys(ctx context.Context) ([]string, error) {
ownerAliases, err := s.GetSortedOwnerAliases(ctx)
if err != nil {
return []string{}, err
}
result := make([]string, 0)
newCache := make(map[string]string)
for _, ownerAlias := range ownerAliases {
fileInfos, err := s.Metadata.ReadDir(fmt.Sprintf("owners/%s/repositories", ownerAlias))
if err == nil {
// acceptable to not have a repositories dir
for i := range fileInfos {
repoKey := fileInfos[i].Name()
if !fileInfos[i].IsDir() && strings.HasSuffix(repoKey, ".yaml") {
repoKey = repoKey[:len(repoKey)-len(".yaml")]
result = append(result, repoKey)
newCache[repoKey] = ownerAlias
}
}
}
}
s.replaceRepositoryOwnerCache(newCache)
sort.Strings(result)
return result, nil
}
func (s *Impl) lookupRepositoryOwnerWithRefresh(ctx context.Context, repoKey string) (string, error) {
ownerAlias, ok := s.lookupInRepositoryOwnerCache(repoKey)
if !ok {
_, err := s.GetSortedRepositoryKeys(ctx)
if err != nil {
return "", fmt.Errorf("failed to refresh repository owner list from metadata: %s", err.Error())
}
ownerAlias, ok = s.lookupInRepositoryOwnerCache(repoKey)
if !ok {
return "", apierrors.NewNotFoundError(fmt.Sprintf("repository not found %s", repoKey), "repository.notfound", nil, s.Timestamp.Now())
}
}
return ownerAlias, nil
}
func (s *Impl) GetRepository(ctx context.Context, repoKey string) (openapi.RepositoryDto, error) {
result := openapi.RepositoryDto{}
ownerAlias, err := s.lookupRepositoryOwnerWithRefresh(ctx, repoKey)
if err != nil {
return result, err
}
fullPath := fmt.Sprintf("owners/%s/repositories/%s.yaml", ownerAlias, repoKey)
err = GetT[openapi.RepositoryDto](ctx, s, &result, fullPath)
if err == nil && result.Configuration != nil && result.Configuration.Approvers != nil {
approversGroupsMap := result.Configuration.Approvers
for approversGroupName, approversGroup := range *approversGroupsMap {
users, groups := util.SplitUsersAndGroups(approversGroup)
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)
}
(*approversGroupsMap)[approversGroupName] = append(filteredExistingUsers, groups...)
} else {
s.Logging.Logger().Ctx(ctx).Error().Printf("Error checking existing bitbucket users: %s", err2.Error())
}
if len((*approversGroupsMap)[approversGroupName]) <= 0 && len(users) > 0 {
s.Logging.Logger().Ctx(ctx).Warn().Printf("Fallback to predefined reviewers")
(*approversGroupsMap)[approversGroupName] = append((*approversGroupsMap)[approversGroupName], s.CustomConfiguration.BitbucketReviewerFallback())
}
}
}
}
result.Owner = ownerAlias
return result, err
}
func (s *Impl) WriteRepository(ctx context.Context, repoKey string, repository openapi.RepositoryDto) (openapi.RepositoryDto, error) {
if repository.Owner == "" {
return openapi.RepositoryDto{}, errors.New("internal error - cannot write repository with no owner")
}
err := s.Metadata.Pull(ctx)
if err != nil {
return openapi.RepositoryDto{}, err
}
// moving owners is handled at another level
currentOwner, err := s.lookupRepositoryOwnerWithRefresh(ctx, repoKey)
if err != nil {
// this is fine, could be a new repository that isn't in the lookup cache yet
} else {
if repository.Owner != currentOwner {
return openapi.RepositoryDto{}, errors.New("internal error - cannot change owners at this low level")
}
}
path := fmt.Sprintf("owners/%s/repositories", repository.Owner)
fileName := repoKey + ".yaml"
description := "repository " + repoKey
err = WriteT[openapi.RepositoryDto](ctx, s, &repository, path, fileName, description, repository.JiraIssue)
return repository, err
}
func (s *Impl) DeleteRepository(ctx context.Context, repoKey string, jiraIssue string) (openapi.RepositoryPatchDto, error) {
result := openapi.RepositoryPatchDto{}
err := s.Metadata.Pull(ctx)
if err != nil {
return result, err
}
ownerAlias, err := s.lookupRepositoryOwnerWithRefresh(ctx, repoKey)
if err != nil {
return result, err
}
fullPath := fmt.Sprintf("owners/%s/repositories/%s.yaml", ownerAlias, repoKey)
description := "repository " + repoKey
err = DeleteT[openapi.RepositoryPatchDto](ctx, s, &result, fullPath, description, jiraIssue)
// remove repository from owner cache by rebuilding the cache
_, _ = s.GetSortedRepositoryKeys(ctx)
return result, err
}
func (s *Impl) WriteRepositoryWithChangedOwner(ctx context.Context, repoKey string, repository openapi.RepositoryDto) (openapi.RepositoryDto, error) {
if repository.Owner == "" {
return openapi.RepositoryDto{}, errors.New("internal error - cannot write repository with no owner")
}
err := s.Metadata.Pull(ctx)
if err != nil {
return openapi.RepositoryDto{}, err
}
// rebuild the owner cache after pull
_, err = s.GetSortedRepositoryKeys(ctx)
if err != nil {
return openapi.RepositoryDto{}, err
}
oldOwnerAlias, err := s.lookupRepositoryOwnerWithRefresh(ctx, repoKey)
if err != nil {
return openapi.RepositoryDto{}, err
}
if oldOwnerAlias == repository.Owner {
return openapi.RepositoryDto{}, errors.New("internal error - owner is the same")
}
oldFullPath := fmt.Sprintf("owners/%s/repositories/%s.yaml", oldOwnerAlias, repoKey)
newPath := fmt.Sprintf("owners/%s/repositories", repository.Owner)
err = Move(ctx, s, repository, oldFullPath, newPath, repoKey+".yaml")
if err != nil {
s.resetLocalClone(ctx)
return openapi.RepositoryDto{}, err
}
message := fmt.Sprintf("%s: move repository %s from owner %s to owner %s", repository.JiraIssue, repoKey, oldOwnerAlias, repository.Owner)
commitInfo, err := s.Metadata.Commit(ctx, message)
if err != nil {
if !nochangeserror.Is(err) {
// empty commits need no re-clone
s.resetLocalClone(ctx)
}
return openapi.RepositoryDto{}, err
}
repository.CommitHash = commitInfo.CommitHash
repository.TimeStamp = timeStamp(commitInfo.TimeStamp)
repository.JiraIssue = jiraIssue(commitInfo.Message)
err = s.Metadata.Push(ctx)
if err != nil {
s.resetLocalClone(ctx)
return openapi.RepositoryDto{}, err
}
return repository, nil
}