-
Notifications
You must be signed in to change notification settings - Fork 4
/
generics.go
199 lines (169 loc) · 5.38 KB
/
generics.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
package mapper
import (
"context"
"fmt"
"github.com/Interhyp/metadata-service/api"
"github.com/Interhyp/metadata-service/internal/acorn/errors/nochangeserror"
"github.com/StephanHCB/go-backend-service-common/web/middleware/requestid"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"time"
)
type Dtos interface {
openapi.OwnerDto | openapi.ServiceDto | openapi.RepositoryDto
}
type PatchDtos interface {
openapi.OwnerPatchDto | openapi.ServicePatchDto | openapi.RepositoryPatchDto
}
func SetCommitHash(dto interface{}, commitHash string) {
if i, ok := dto.(*openapi.OwnerDto); ok {
i.CommitHash = commitHash
} else if i, ok := dto.(*openapi.ServiceDto); ok {
i.CommitHash = commitHash
} else if i, ok := dto.(*openapi.RepositoryDto); ok {
i.CommitHash = commitHash
} else if i, ok := dto.(*openapi.OwnerPatchDto); ok {
i.CommitHash = commitHash
} else if i, ok := dto.(*openapi.ServicePatchDto); ok {
i.CommitHash = commitHash
} else if i, ok := dto.(*openapi.RepositoryPatchDto); ok {
i.CommitHash = commitHash
}
}
func SetTimeStamp(dto interface{}, rawTimeStamp time.Time) {
if i, ok := dto.(*openapi.OwnerDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
} else if i, ok := dto.(*openapi.ServiceDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
} else if i, ok := dto.(*openapi.RepositoryDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
} else if i, ok := dto.(*openapi.OwnerPatchDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
} else if i, ok := dto.(*openapi.ServicePatchDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
} else if i, ok := dto.(*openapi.RepositoryPatchDto); ok {
i.TimeStamp = timeStamp(rawTimeStamp)
}
}
func SetJiraIssue(dto interface{}, commitMessage string) {
if i, ok := dto.(*openapi.OwnerDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
} else if i, ok := dto.(*openapi.ServiceDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
} else if i, ok := dto.(*openapi.RepositoryDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
} else if i, ok := dto.(*openapi.OwnerPatchDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
} else if i, ok := dto.(*openapi.ServicePatchDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
} else if i, ok := dto.(*openapi.RepositoryPatchDto); ok {
i.JiraIssue = jiraIssue(commitMessage)
}
}
func GetT[T Dtos](_ context.Context, s *Impl, resultPtr *T, fullPath string) error {
yamlBytes, commitInfo, err := s.Metadata.ReadFile(fullPath)
if err != nil {
return fmt.Errorf("failed to read %s from metadata: %s", fullPath, err.Error())
}
err = yaml.Unmarshal(yamlBytes, resultPtr)
if err != nil {
return fmt.Errorf("failed to parse %s as yaml from metadata: %s", fullPath, err.Error())
}
SetCommitHash(resultPtr, commitInfo.CommitHash)
SetTimeStamp(resultPtr, commitInfo.TimeStamp)
SetJiraIssue(resultPtr, commitInfo.Message)
return nil
}
func (s *Impl) resetLocalClone(ctx context.Context) {
reqId := ctx.Value(requestid.RequestIDKey)
if reqId == nil {
reqId = requestid.NewRequestID()
}
newCtx := context.WithValue(context.Background(), requestid.RequestIDKey, reqId)
logger := log.Ctx(ctx)
newCtx = logger.WithContext(newCtx)
err := s.Metadata.Clone(newCtx)
if err != nil {
s.Logging.Logger().Ctx(newCtx).Error().WithErr(err).Print("failed to repair local clone - continuing")
}
}
func WriteT[T Dtos](ctx context.Context, s *Impl, resultPtr *T, path string, fileNameNoPath string, description string, jiraIssue string) error {
fileName := path + "/" + fileNameNoPath
yamlBytes, err := yaml.Marshal(*resultPtr)
if err != nil {
return err
}
err = s.Metadata.MkdirAll(path)
if err != nil {
s.resetLocalClone(ctx)
return err
}
err = s.Metadata.WriteFile(fileName, yamlBytes)
if err != nil {
s.resetLocalClone(ctx)
return err
}
message := fmt.Sprintf("%s: update %s", jiraIssue, description)
commitInfo, err := s.Metadata.Commit(ctx, message)
if err != nil {
if !nochangeserror.Is(err) {
// empty commits need no re-clone
s.resetLocalClone(ctx)
}
SetJiraIssue(resultPtr, "")
return err
}
SetCommitHash(resultPtr, commitInfo.CommitHash)
SetTimeStamp(resultPtr, commitInfo.TimeStamp)
SetJiraIssue(resultPtr, commitInfo.Message)
err = s.Metadata.Push(ctx)
if err != nil {
s.resetLocalClone(ctx)
return err
}
return nil
}
func DeleteT[T PatchDtos](ctx context.Context, s *Impl, resultPtr *T, fullPath string, description string, jiraIssue string) error {
err := s.Metadata.DeleteFile(fullPath)
if err != nil {
s.resetLocalClone(ctx)
return err
}
message := fmt.Sprintf("%s: delete %s", jiraIssue, description)
commitInfo, err := s.Metadata.Commit(ctx, message)
if err != nil {
if !nochangeserror.Is(err) {
// empty commits need no re-clone
s.resetLocalClone(ctx)
}
return err
}
SetCommitHash(resultPtr, commitInfo.CommitHash)
SetTimeStamp(resultPtr, commitInfo.TimeStamp)
SetJiraIssue(resultPtr, commitInfo.Message)
err = s.Metadata.Push(ctx)
if err != nil {
s.resetLocalClone(ctx)
return err
}
return nil
}
func Move(ctx context.Context, s *Impl, v interface{}, oldFullPath string, newPath string, newFileNameNoPath string) error {
err := s.Metadata.DeleteFile(oldFullPath)
if err != nil {
return err
}
err = s.Metadata.MkdirAll(newPath)
if err != nil {
return err
}
yamlBytes, err := yaml.Marshal(v)
if err != nil {
return err
}
err = s.Metadata.WriteFile(newPath+"/"+newFileNameNoPath, yamlBytes)
if err != nil {
return err
}
return nil
}