• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moosetechnology / GitProjectHealth / 13562032915

27 Feb 2025 08:29AM UTC coverage: 63.625% (+0.9%) from 62.725%
13562032915

Pull #136

github

web-flow
Merge 38d64247b into 29b562512
Pull Request #136: WIP Closed merge request duration project metric

5500 of 6814 new or added lines in 79 files covered. (80.72%)

10383 of 16319 relevant lines covered (63.63%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/GitLabHealth-Model-Analysis/GitAnalyzer.class.st
1
Class {
2
        #name : #GitAnalyzer,
3
        #superclass : #Object,
4
        #instVars : [
5
                'glModel',
6
                'fromCommit',
7
                'glhImporter',
8
                'onProject',
9
                'maxChildCommits'
10
        ],
11
        #category : #'GitLabHealth-Model-Analysis'
12
}
13

14
{ #category : #analyze }
15
GitAnalyzer >> analyseChurn [
1✔
16

1✔
17
        | commitFiles totalContribution childCommits access |
1✔
18
        access := ('' join: {
1✔
19
                                   'churn'.
1✔
20
                                   maxChildCommits }) asSymbol.
1✔
21

1✔
22
        ^ fromCommit cacheAt: access ifAbsentPut: [
1✔
23
                  ('GitAnalyzer, analyse chrun onProject: ' , onProject printString)
1✔
24
                          recordInfo.
1✔
25
                  childCommits := Set new.
1✔
26
                  totalContribution := self
1✔
27
                                               visitChildCommits: fromCommit childCommits
1✔
28
                                               toStoreThemIn: childCommits
1✔
29
                                               upto: self maxChildCommits.
1✔
30
                  totalContribution add: fromCommit. 
1✔
31
                  totalContribution := totalContribution sum: [ :commit | "nil if merge request commit"
1✔
32
                                               commit additions ifNil: [ 0 ] ].
1✔
33
                  commitFiles := self
1✔
34
                                         impactedFilesInFollowUpCommitsOf: fromCommit
1✔
35
                                         withMaxCommits: self maxChildCommits.
1✔
36
                        "a churned line is a line added on top of an already added line"
1✔
37
                  (self computeChurnOnFiles: commitFiles)
1✔
38
                          at: #totalContribution ifAbsentPut: totalContribution;
1✔
39
                          yourself ]
1✔
40
]
1✔
41

42
{ #category : #analyze }
43
GitAnalyzer >> analyseCommentContribution [
1✔
44

1✔
45
        | numberOfComments |
1✔
46
        ('GitAnalyzer, analyse comment contributions onProject: '
1✔
47
         , onProject printString) recordInfo.
1✔
48
        numberOfComments := 0.
1✔
49
        
1✔
50
        fromCommit diffs do: [ :diff |
1✔
51
                diff diffRanges do: [ :range |
1✔
52
                        range changes do: [ :change |
1✔
53
                                ((change isKindOf: GLHAddition) and: [
1✔
54
                                         (change sourceCode withoutPrefix: '+') trimLeft
1✔
55
                                                 beginsWithAnyOf: { '#'. '//'. '/*'. '*'. '*/' } ]) ifTrue: [
1✔
56
                                        numberOfComments := numberOfComments + 1 ] ] ] ].
1✔
57
        ^ numberOfComments
1✔
58
]
1✔
59

60
{ #category : #commit }
61
GitAnalyzer >> analyseCommitContribution [
1✔
62
        
1✔
63
        
1✔
64
        ('GitAnalyzer, analyse commit contribution of: ', fromCommit printString )
1✔
65
                recordInfo.
1✔
66
        
1✔
67
        ^ { (#addition -> fromCommit additions).
1✔
68
          (#deletion -> fromCommit deletions). } asDictionary 
1✔
69
]
1✔
70

71
{ #category : #analyze }
NEW
72
GitAnalyzer >> analyseCommitFrequencyFromCommits: initialCommits [
×
NEW
73

×
NEW
74
        | commits response |
×
NEW
75
        ('GitAnalyzer, analyse commit Frequency on: ' , onProject printString)
×
NEW
76
                recordInfo.
×
NEW
77

×
NEW
78
        response := {
×
NEW
79
                            (#numberOfCommit -> nil).
×
NEW
80
                            (#frequency -> nil) } asDictionary.
×
NEW
81

×
NEW
82

×
NEW
83
        commits := self arrangeCommitsByDate: initialCommits.
×
NEW
84
        commits := (commits associations sortAscending: [ :entry |
×
NEW
85
                            entry key asDate ]) asOrderedDictionary.
×
NEW
86

×
NEW
87
        ^ commits
×
NEW
88
]
×
89

90
{ #category : #analyze }
91
GitAnalyzer >> analyseCommitFrequencySince: since until: until [ 
1✔
92

1✔
93
        | commits response |
1✔
94
        
1✔
95
        ('GitAnalyzer, analyse commit Frequency on: ', onProject printString )
1✔
96
                recordInfo.
1✔
97
        
1✔
98
        response := {
1✔
99
                            (#numberOfCommit -> nil).
1✔
100
                            (#frequency -> nil) } asDictionary.
1✔
101

1✔
102
        commits := glhImporter
1✔
103
                           importCommitsOfProject: onProject
1✔
104
                           since: since
1✔
105
                           until: until.
1✔
106

1✔
107
        commits := self arrangeCommitsByDate: commits.
1✔
108
        commits := (commits associations sortAscending: [ :entry |
1✔
109
                            entry key asDate ]) asOrderedDictionary.
1✔
110

1✔
111
        ^ commits
1✔
112
]
1✔
113

114
{ #category : #analyze }
115
GitAnalyzer >> analyseDelayUntilFirstChurn [
1✔
116
        "return the first commit that modify the same lines of code as the fromCommit"
1✔
117

1✔
118
        | churn res access|
1✔
119
        
1✔
120
        access := ('' join: {
1✔
121
                                   'amandment'.
1✔
122
                                   maxChildCommits }) asSymbol.
1✔
123
        
1✔
124
        ^ fromCommit cacheAt: access ifPresent: [ :v | v ] ifAbsentPut: [
1✔
125
        
1✔
126
        ('GitAnalyzer, analyse amandment onProject: ', onProject printString )
1✔
127
                recordInfo.
1✔
128
        
1✔
129
        churn := self analyseChurn.
1✔
130

1✔
131
        res := self firstAmandmentFromChrun: churn.
1✔
132
         res]
1✔
133
]
1✔
134

135
{ #category : #analyze }
136
GitAnalyzer >> analyseMergeResquestValidation: aGLHPMergeRequest [
1✔
137

1✔
138
        | creationDate mergedDate closedDate response |
1✔
139
        ('GitAnalyzer, analyse merge request delay of: '
1✔
140
         , aGLHPMergeRequest printString) recordInfo.
1✔
141

1✔
142
        ^ aGLHPMergeRequest 
1✔
143
                  cacheAt: #mergeRequestValidation
1✔
144
                  ifPresent: [ :v | v ]
1✔
145
                  ifAbsentPut: [
1✔
146
                          response := {
1✔
147
                                              (#id_merge_resquest -> aGLHPMergeRequest iid).
1✔
148
                                              (#id_merge_commit -> nil).
1✔
149
                                              (#created_at -> aGLHPMergeRequest created_at).
1✔
150
                                              (#merged_at -> nil).
1✔
151
                                              (#closed_at -> nil).
1✔
152
                                              (#close_duration -> nil).
1✔
153
                                              (#duration -> nil).
1✔
154
                                              (#status -> aGLHPMergeRequest merge_status) }
1✔
155
                                              asDictionary.
1✔
156

1✔
157
                          creationDate := aGLHPMergeRequest created_at asDateAndTime.
1✔
158

1✔
159
            "Handle merged case"
1✔
160
            mergedDate := aGLHPMergeRequest merged_at ifNil: [ nil ].
1✔
161
            mergedDate ifNotNil: [
1✔
162
                response  
1✔
163
                    at: #duration put: mergedDate - creationDate;  
1✔
164
                    at: #id_merge_commit put: aGLHPMergeRequest merge_commit_sha;  
1✔
165
                    at: #merged_at put: aGLHPMergeRequest merged_at.  
1✔
166
            ].
1✔
167

1✔
168
            "Handle closed case"
1✔
169
            closedDate := aGLHPMergeRequest closed_at ifNil: [ ^ response ].  
1✔
170
            response  
1✔
171
                at: #close_duration put: closedDate - creationDate;  
1✔
172
                at: #closed_at put: aGLHPMergeRequest closed_at.  
1✔
173
            response
1✔
174
        ].
1✔
175
]
1✔
176

177
{ #category : #filter }
178
GitAnalyzer >> arrangeCommitsByDate: commits [
1✔
179

1✔
180
        | date2commits |
1✔
181
        date2commits := Dictionary new.
1✔
182

1✔
183
        commits do: [ :commit |
1✔
184
                | date |
1✔
185
                date := commit created_at asDate.
1✔
186
                date2commits
1✔
187
                        at: date printString
1✔
188
                        ifPresent: [ :v | v add: commit ]
1✔
189
                        ifAbsentPut: [
1✔
190
                                OrderedCollection new
1✔
191
                                        add: commit;
1✔
192
                                        yourself ] ].
1✔
193
        ^ date2commits
1✔
194
]
1✔
195

196
{ #category : #churn }
197
GitAnalyzer >> computeChurnOnFiles: aCollection [
1✔
198

1✔
199
        | changesDic perFileChanges churns initialAuthor followingAuthors|
1✔
200
        "1 -> (a GLPHEChange -> NumberOfChurnDetected)"
1✔
201
        changesDic := Dictionary new.
1✔
202
        initialAuthor := fromCommit commitCreator. 
1✔
203
        
1✔
204

1✔
205
        perFileChanges := aCollection associations collect: [ :assoc |
1✔
206
                                  assoc key
1✔
207
                                  -> (self computeSpecificChurnOf: assoc value) ].
1✔
208

1✔
209

1✔
210
        churns := perFileChanges collect: [ :assoc |
1✔
211
                          | churnData file aLineOfChanges |
1✔
212
                          file := assoc key.
1✔
213
                          aLineOfChanges := assoc value.
1✔
214
                          churnData := aLineOfChanges values ifEmpty: [  nil ] ifNotEmpty: [
1✔
215
                                  | churnedContribution churnSpecificFromCommit churnOfAuthorOnly |
1✔
216
                                
1✔
217
                                  "the total churn on any LoC affected"
1✔
218
                                  churnedContribution := aLineOfChanges select: [ :a |
1✔
219
                                                                 a value  > 1 ].
1✔
220

1✔
221
                                  "the churn that coccurs specifically on the loc introduced by the initial commit"
1✔
222
                                  churnSpecificFromCommit := churnedContribution select: [ :a |
1✔
223
                                                                     (a key collect: [ :loc |
1✔
224
                                                                              loc diffRange diff commit ])
1✔
225
                                                                             includes: fromCommit ].
1✔
226

1✔
227
                                                "the churn made the author of the initial commits "
1✔
228
                                  churnOfAuthorOnly := churnSpecificFromCommit select: [ :a |
1✔
229
                                                                     (((a key collect: [ :loc |
1✔
230
                                                                              loc diffRange diff commit commitCreator ]) asSet) difference: {initialAuthor} asSet ) isEmpty
1✔
231
                                                                             ].
1✔
232
                                                
1✔
233

1✔
234
                                  {
1✔
235
                                                   (#churnFromInitialCommitLines
1✔
236
                                                    ->
1✔
237
                                                            ((churnSpecificFromCommit sum: #value) - churnSpecificFromCommit size)).
1✔
238
                                                                                (#churnFromCommitCreatorOnly
1✔
239
                                                    ->
1✔
240
                                                            ((churnOfAuthorOnly sum: #value) - churnOfAuthorOnly size)).
1✔
241
                                                   (#churnLoC
1✔
242
                                                    ->
1✔
243
                                                            ((churnedContribution sum: #value)
1✔
244
                                                             - churnedContribution size)) 
1✔
245
                                        } asDictionary ].
1✔
246

1✔
247
                          file -> churnData ].
1✔
248
        churns := churns reject: [ :file2churn | file2churn value isNil ].
1✔
249

1✔
250
        ^ {
1✔
251
                  (#churns -> churns).
1✔
252
                  (#details -> perFileChanges) } asDictionary
1✔
253
]
1✔
254

255
{ #category : #churn }
256
GitAnalyzer >> computeSpecificChurnOf: commit2Changes [
1✔
257

1✔
258
        | changesDic |
1✔
259
        "1 -> (a GLPHEChange -> NumberOfChurnDetected)"
1✔
260
        changesDic := OrderedDictionary new.
1✔
261

1✔
262

1✔
263
        (commit2Changes sortAscending: [ :assoc | assoc key created_at ])
1✔
264
                do: [ :entry |
1✔
265
                        | commit diffRanges |
1✔
266
                        commit := entry key.
1✔
267
                        diffRanges := entry value.
1✔
268

1✔
269
                        diffRanges do: [ :diff |
1✔
270
                                | from |
1✔
271
                                from := (diff originalLineRange
1✔
272
                                                 copyFrom: (diff originalLineRange indexOf: $-) + 1
1✔
273
                                                 to: (diff originalLineRange
1✔
274
                                                                  indexOf: $,
1✔
275
                                                                  ifAbsent: [ diff originalLineRange size + 1 ]) - 1)
1✔
276
                                                asString asNumber.
1✔
277
                                from = 0 ifTrue: [ from := 1 ].
1✔
278
                                self insertDiff: diff into: changesDic startingFrom: from ] ].
1✔
279

1✔
280

1✔
281

1✔
282
        ^ self sortChangeDic: changesDic
1✔
283
]
1✔
284

285
{ #category : #accessing }
286
GitAnalyzer >> firstAmandmentFromChrun: aChurnAnalysis [ 
1✔
287
        |details whereChangesOccurs firstCommitsPerFile|
1✔
288
        
1✔
289
        whereChangesOccurs := (aChurnAnalysis at: #churns ) select: [ :file | (file value at: #churnLoC) > 0 ].
1✔
290
        
1✔
291
        details := whereChangesOccurs collect: [ :file |
1✔
292
                ((aChurnAnalysis at: #details) detect: [ :entry | entry key = file key] )
1✔
293
                 ].
1✔
294
        
1✔
295
        firstCommitsPerFile := details collect: [ :perFile |
1✔
296
                |changes firstCommits first|
1✔
297
                changes := perFile value.
1✔
298
                changes := changes select: [ :line2changes | line2changes value value > 1  ].
1✔
299
                firstCommits := (changes collect: [ :line2changes |  line2changes key second diffRange diff commit ]) values. 
1✔
300
                first := (firstCommits sortAscending: [:c | c created_at ]) first.
1✔
301
                 ].
1✔
302
        
1✔
303

1✔
304
        ^ (firstCommitsPerFile sortAscending: [:c | c created_at ]) ifEmpty: nil ifNotEmpty: [ :v | v first ]  . 
1✔
305

1✔
306
]
1✔
307

308
{ #category : #accessing }
309
GitAnalyzer >> fromCommit: aCommit [
1✔
310
        fromCommit := aCommit. 
1✔
311
]
1✔
312

313
{ #category : #accessing }
314
GitAnalyzer >> glhImporter: anImporter [
1✔
315
        glhImporter := anImporter .
1✔
316
]
1✔
317

318
{ #category : #'as yet unclassified' }
NEW
319
GitAnalyzer >> impactedFilesInFollowUpCommitsOf: aGLHCommit [
×
NEW
320

×
NEW
321
        ^ self
×
NEW
322
                  impactedFilesInFollowUpCommitsOf: aGLHCommit
×
NEW
323
                  withMaxCommits: self maxChildCommits. 
×
NEW
324
]
×
325

326
{ #category : #churn }
327
GitAnalyzer >> impactedFilesInFollowUpCommitsOf: aGLHCommit withMaxCommits: max [
1✔
328

1✔
329
        | commitFiles |
1✔
330
        commitFiles := (fromCommit diffs collect: [ :diff |
1✔
331
                                diff new_path -> (Set new
1✔
332
                                         add: aGLHCommit -> diff diffRanges;
1✔
333
                                         yourself) ]) asDictionary.
1✔
334

1✔
335
        self
1✔
336
                visitChildCommits: fromCommit childCommits
1✔
337
                lookingForFiles: commitFiles upto: max.
1✔
338

1✔
339
        ^ commitFiles
1✔
340
]
1✔
341

342
{ #category : #initialization }
343
GitAnalyzer >> initialize [
1✔
344

1✔
345
        glModel := GLHModel new.
1✔
346
        fromCommit := GLHCommit new.
1✔
347
        glhImporter := GitlabWithMergeRequestModelImporter new.
1✔
348
        onProject := GLHProject new.
1✔
349
        maxChildCommits := -1
1✔
350
]
1✔
351

352
{ #category : #insertion }
353
GitAnalyzer >> insertDiff: aGLPHEDiffRange into: fileChangesDic startingFrom: from [ 
1✔
354
        |index|
1✔
355
        index := from. 
1✔
356
        aGLPHEDiffRange changes do: [ :aChange |
1✔
357
        
1✔
358
                aChange isAddition ifTrue: [ 
1✔
359
                        fileChangesDic at: index ifPresent: [ :current | 
1✔
360
                         
1✔
361
                        current key add: aChange.
1✔
362
                        current value: current value + 1.  ] ifAbsentPut: [((OrderedCollection new add: aChange; yourself) -> 1 ) ].
1✔
363
                         ].
1✔
364
                
1✔
365
                aChange isDeletion ifFalse: [ index := index + 1 ]. 
1✔
366
                
1✔
367
                 ]
1✔
368
]
1✔
369

370
{ #category : #accessing }
371
GitAnalyzer >> maxChildCommit: max [ 
1✔
372
        maxChildCommits := max
1✔
373
]
1✔
374

375
{ #category : #accessing }
376
GitAnalyzer >> maxChildCommits [
1✔
377
        ^ maxChildCommits
1✔
378
]
1✔
379

380
{ #category : #'as yet unclassified' }
381
GitAnalyzer >> onModel: agitHealthModel [
1✔
382
        glModel := agitHealthModel
1✔
383
]
1✔
384

385
{ #category : #accessing }
386
GitAnalyzer >> onProject: aGLHProject [ 
1✔
387
        onProject := aGLHProject
1✔
388
]
1✔
389

390
{ #category : #sorting }
391
GitAnalyzer >> sortChangeDic: aCollection [ 
1✔
392
        ^ (aCollection associations sortAscending: [ :e | e key ] ) asOrderedDictionary 
1✔
393
]
1✔
394

395
{ #category : #visiting }
NEW
396
GitAnalyzer >> visitChildCommits: commits lookingForFiles: commitFiles [
×
NEW
397

×
NEW
398
        ^ self visitChildCommits:  commits lookingForFiles: commitFiles upto: -1 
×
NEW
399
]
×
400

401
{ #category : #visiting }
402
GitAnalyzer >> visitChildCommits: commits lookingForFiles: commitFiles upto: nCommits [
1✔
403

1✔
404
        commits ifEmpty: [ ^ commitFiles ].
1✔
405
        (nCommits = 0) ifTrue: [ ^ commitFiles ].
1✔
406

1✔
407
        commits do: [ :commit |
1✔
408
                | files |
1✔
409
                files := commit diffs collect: [ :diff | diff ].
1✔
410

1✔
411
                files do: [ :diff |
1✔
412
                        commitFiles
1✔
413
                                at: diff new_path
1✔
414
                                ifPresent: [ :v | v add: commit -> diff diffRanges ]
1✔
415
                                ifAbsent: [  ] ].
1✔
416

1✔
417
                self
1✔
418
                        visitChildCommits: commit childCommits
1✔
419
                        lookingForFiles: commitFiles
1✔
420
                        upto: nCommits - 1 ].
1✔
421

1✔
422
        ^ commitFiles
1✔
423
]
1✔
424

425
{ #category : #visiting }
426
GitAnalyzer >> visitChildCommits: commits toStoreThemIn: commitsFound upto: nCommits [
1✔
427

1✔
428
        commits ifEmpty: [ ^ commitsFound ].
1✔
429
        nCommits = 0 ifTrue: [ ^ commitsFound ].
1✔
430

1✔
431
        commits do: [ :commit |
1✔
432
                commitsFound add: commit.
1✔
433

1✔
434
                self
1✔
435
                        visitChildCommits: commit childCommits
1✔
436
                        toStoreThemIn: commitsFound
1✔
437
                        upto: nCommits - 1 ].
1✔
438

1✔
439
        ^ commitsFound
1✔
440
]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc