-
Notifications
You must be signed in to change notification settings - Fork 10
/
Bean.go
335 lines (286 loc) · 9.14 KB
/
Bean.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package git
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/git-sensor/internals/sql"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"io"
"time"
"unicode/utf8"
)
type FetchScmChangesRequest struct {
PipelineMaterialId int `json:"pipelineMaterialId"`
From string `json:"from"`
To string `json:"to"`
Count int `json:"count"`
ShowAll bool `json:"showAll"`
}
type HeadRequest struct {
MaterialIds []int `json:"materialIds"`
}
type CiPipelineMaterialBean struct {
Id int
GitMaterialId int
Type sql.SourceType
Value string
Active bool
GitCommit *GitCommitBase
ExtraEnvironmentVariables map[string]string // extra env variables which will be used for CI
}
type GitRepository struct {
*git.Repository
rootDir string
}
type CommitIterator interface {
Next() (GitCommit, error)
}
type CommitCliIterator struct {
commits []GitCommit
index int
}
type CommitGoGitIterator struct {
object.CommitIter
}
func transformFileStats(stats object.FileStats) FileStats {
fileStatList := make([]FileStat, 0)
for _, stat := range stats {
fileStatList = append(fileStatList, FileStat{
Name: stat.Name,
Addition: stat.Addition,
Deletion: stat.Deletion,
})
}
return fileStatList
}
func (itr *CommitGoGitIterator) Next() (GitCommit, error) {
commit, err := itr.CommitIter.Next()
if err != nil {
return nil, err
}
gitCommit := GitCommitBase{
Author: commit.Author.String(),
Commit: commit.Hash.String(),
Date: commit.Author.When,
Message: commit.Message,
}
return &GitCommitGoGit{
GitCommitBase: gitCommit,
Cm: commit,
}, err
}
func (itr *CommitCliIterator) Next() (GitCommit, error) {
if itr.index < len(itr.commits) {
commit := itr.commits[itr.index]
itr.index++
return commit, nil
}
return nil, io.EOF
}
type MaterialChangeResp struct {
Commits []*GitCommitBase `json:"commits"`
LastFetchTime time.Time `json:"lastFetchTime"`
IsRepoError bool `json:"isRepoError"`
RepoErrorMsg string `json:"repoErrorMsg"`
IsBranchError bool `json:"isBranchError"`
BranchErrorMsg string `json:"branchErrorMsg"`
}
type GitCommit interface {
GetCommit() *GitCommitBase
}
type GitCommitCli struct {
GitCommitBase
}
type GitCommitGoGit struct {
GitCommitBase
Cm *object.Commit
}
func (gitCommit *GitCommitBase) GetCommit() *GitCommitBase {
return gitCommit
}
type GitCommitBase struct {
Commit string
Author string
Date time.Time
Message string
Changes []string `json:",omitempty"`
FileStats *FileStats `json:",omitempty"`
WebhookData *WebhookData `json:"webhookData"`
Excluded bool `json:",omitempty"`
}
func AppendOldCommitsFromHistory(newCommits []*GitCommitBase, commitHistory string, fetchedCount int) ([]*GitCommitBase, error) {
oldCommits := make([]*GitCommitBase, 0)
if len(commitHistory) > 0 {
err := json.Unmarshal([]byte(commitHistory), &oldCommits)
if err != nil {
return newCommits, fmt.Errorf("unmarshalling error %v", err)
}
}
totalCommits := append(newCommits, oldCommits...)
if len(totalCommits) > fetchedCount {
totalCommits = totalCommits[:fetchedCount]
}
return totalCommits, nil
}
func (gitCommit *GitCommitBase) SetFileStats(stats *FileStats) {
gitCommit.FileStats = stats
}
func (gitCommit *GitCommitBase) TruncateMessageIfExceedsMaxLength() {
maxLength := 1024
if len(gitCommit.Message) > maxLength {
gitCommit.Message = gitCommit.Message[:maxLength-3] + "..."
}
}
// IsMessageValidUTF8 checks if a string is valid UTF-8.
func (gitCommit *GitCommitBase) IsMessageValidUTF8() bool {
return utf8.ValidString(gitCommit.Message)
}
// FixInvalidUTF8Message replaces invalid UTF-8 sequences with the replacement character (U+FFFD).
func (gitCommit *GitCommitBase) FixInvalidUTF8Message() {
invalidUTF8 := []rune(gitCommit.Message)
for i, r := range invalidUTF8 {
if !utf8.ValidRune(r) {
invalidUTF8[i] = utf8.RuneError
}
}
gitCommit.Message = string(invalidUTF8)
}
type WebhookAndCiData struct {
ExtraEnvironmentVariables map[string]string `json:"extraEnvironmentVariables"` // extra env variables which will be used for CI
WebhookData *WebhookData `json:"webhookData"`
}
type WebhookData struct {
Id int `json:"id"`
EventActionType string `json:"eventActionType"`
Data map[string]string `json:"data"`
}
type CommitMetadataRequest struct {
PipelineMaterialId int `json:"pipelineMaterialId"`
GitHash string `json:"gitHash"`
GitTag string `json:"gitTag"`
BranchName string `json:"branchName"`
}
type WebhookDataRequest struct {
Id int `json:"id"`
CiPipelineMaterialId int `json:"ciPipelineMaterialId"`
}
type WebhookEventConfigRequest struct {
GitHostId int `json:"gitHostId"`
EventId int `json:"eventId"`
}
type RefreshGitMaterialRequest struct {
GitMaterialId int `json:"gitMaterialId"`
}
type RefreshGitMaterialResponse struct {
Message string `json:"message"`
ErrorMsg string `json:"errorMsg"`
LastFetchTime time.Time `json:"lastFetchTime"`
}
type WebhookEvent struct {
PayloadId int `json:"payloadId"`
RequestPayloadJson string `json:"requestPayloadJson"`
GitHostId int `json:"gitHostId"`
EventType string `json:"eventType"`
}
type WebhookEventResponse struct {
success bool
}
type WebhookEventConfig struct {
Id int `json:"id"`
GitHostId int `json:"gitHostId"`
Name string `json:"name"`
EventTypesCsv string `json:"eventTypesCsv"`
ActionType string `json:"actionType"`
IsActive bool `json:"isActive"`
CreatedOn time.Time `json:"createdOn"`
UpdatedOn time.Time `json:"updatedOn"`
Selectors []*WebhookEventSelectors `json:"selectors"`
}
type WebhookEventSelectors struct {
Id int `json:"id"`
EventId int `json:"eventId"`
Name string `json:"name"`
Selector string `json:"selector"`
ToShow bool `json:"toShow"`
ToShowInCiFilter bool `json:"toShowInCiFilter"`
FixValue string `json:"fixValue"`
PossibleValues string `json:"possibleValues"`
IsActive bool `json:"isActive"`
CreatedOn time.Time `json:"createdOn"`
UpdatedOn time.Time `json:"updatedOn"`
}
// key in condition is selectorId
type WebhookSourceTypeValue struct {
EventId int `json:"eventId,omitempty"`
Condition map[int]string `json:"condition,omitempty"`
}
type WebhookPayloadDataRequest struct {
CiPipelineMaterialId int `json:"ciPipelineMaterialId"`
Limit int `json:"limit"`
Offset int `json:"offset"`
EventTimeSortOrder string `json:"eventTimeSortOrder"`
}
type WebhookPayloadDataResponse struct {
Filters map[string]string `json:"filters"`
RepositoryUrl string `json:"repositoryUrl"`
Payloads []*WebhookPayloadDataPayloadsResponse `json:"payloads"`
}
type WebhookPayloadDataPayloadsResponse struct {
ParsedDataId int `json:"parsedDataId"`
EventTime time.Time `json:"eventTime"`
MatchedFiltersCount int `json:"matchedFiltersCount"`
FailedFiltersCount int `json:"failedFiltersCount"`
MatchedFilters bool `json:"matchedFilters"`
}
type WebhookPayloadFilterDataRequest struct {
CiPipelineMaterialId int `json:"ciPipelineMaterialId"`
ParsedDataId int `json:"parsedDataId"`
}
type WebhookPayloadFilterDataResponse struct {
PayloadId int `json:"payloadId"`
SelectorsData []*WebhookPayloadFilterDataSelectorResponse `json:"selectorsData"`
}
type WebhookPayloadFilterDataSelectorResponse struct {
SelectorName string `json:"selectorName"`
SelectorCondition string `json:"selectorCondition"`
SelectorValue string `json:"selectorValue"`
Match bool `json:"match"`
}
type GitChanges struct {
Commits []*Commit
FileStats FileStats
}
type FileStatsResult struct {
FileStats FileStats
Error error
}
// FileStat stores the status of changes in content of a file.
type FileStat struct {
Name string
Addition int
Deletion int
}
// FileStats is a collection of FileStat.
type FileStats []FileStat
type IteratorRequest struct {
BranchRef string
Branch string
CommitCount int
FromCommitHash string
ToCommitHash string
}