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

moosetechnology / GitProjectHealth / 10699184045

04 Sep 2024 09:43AM UTC coverage: 18.297%. First build
10699184045

Pull #48

github

web-flow
Merge 51d727fd0 into 75bc8e542
Pull Request #48: feat: add project metrics structure

0 of 183 new or added lines in 3 files covered. (0.0%)

1590 of 8690 relevant lines covered (18.3%)

0.18 hits per line

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

0.0
/src/GitLabHealth-Model-Analysis/GitMetricExporter.class.st
1
Class {
2
        #name : #GitMetricExporter,
3
        #superclass : #Object,
4
        #instVars : [
5
                'glhImporter',
6
                'jiraImporter',
7
                'label',
8
                'entities',
9
                'projectCache',
10
                'sinceTimeLimit',
11
                'runningPeriods',
12
                'maxCommitWindow',
13
                'over',
14
                'emailDomain',
15
                'metrics',
16
                'analyses'
17
        ],
18
        #category : #'GitLabHealth-Model-Analysis'
19
}
20

21
{ #category : #'as yet unclassified' }
22
GitMetricExporter class >> demoPeriod [
×
23

×
24
        ^ self new
×
25
                  addAPeriodFrom: '01 march 2023' to: '31 may 2023';
×
26
                  addAPeriodFrom: '01 december 2023' to: '29 february 2024';
×
27
                  addAPeriodFrom: '01 march 2024' to: '31 may 2024'
×
28
]
×
29

30
{ #category : #adding }
31
GitMetricExporter >> addAPeriodFrom: since to: until [
×
32

×
33
        runningPeriods add: {
×
34
                        (#since -> since asDate).
×
35
                        (#until -> until asDate) } asDictionary.
×
36
        
×
37
        ^runningPeriods 
×
38
]
×
39

40
{ #category : #adding }
41
GitMetricExporter >> addEntitiesFromUserNames: userNames [
×
42
        "import all the project since a certain time"
×
43
        
×
44
        self
×
45
                deprecated: 'Use #setupAnalysesForUsersWithNames instead'
×
46
                on: '03 Sept 2024'
×
47
                in:
×
48
                'Pharo-11.0.0+build.726.sha.aece1b5473acf3830a0e082c1bc3a15d4ff3522b (64 Bit)'.
×
49

×
50
        'Import all projects from the Git repository' recordInfo.
×
51
        projectCache := projectCache ifNil: [
×
52
                                glhImporter importProjectsSince: sinceTimeLimit ].
×
53

×
54
        "then collect the project in which the user has be seen commited"
×
55
        entities addAll: (userNames collect: [ :username |
×
56
                         | projects  i size |
×
57
                         projects := self
×
58
                                             findParticipationOfCommitAuthorNamed: username
×
59
                                             amongProjects: projectCache.
×
60

×
61
                        "metrics := GitMetric4User new.
×
62
                         metrics
×
63
                                 glhImporter: glhImporter;
×
64
                                 jiraImporter: jiraImporter;
×
65
                                 findUserNamed: username."
×
66

×
67

×
68

×
69
                         i := 0.
×
70
                         size := projects size.
×
71
                         metrics itsProjects: (projects collect: [ :p |
×
72
                                          (' ' join: {
×
73
                                                           'complete import of project:'.
×
74
                                                           p name printString.
×
75
                                                           '['.
×
76
                                                           (i := i + 1) printString.
×
77
                                                           '/'.
×
78
                                                           size.
×
79
                                                           ']' }) recordInfo.
×
80

×
81
                                          p id -> (glhImporter completeImportProject: p) ]) asDictionary.
×
82

×
83
                         metrics ]).
×
84

×
85
        ^ self
×
86
]
×
87

88
{ #category : #adding }
89
GitMetricExporter >> addEntitiesFromUserNamesAndProjects: usersWithProjects [
×
90

×
91
        self
×
92
                deprecated: 'Use #setupAnalysesForUsersWithNames instead'
×
93
                on: '03 Sept 2024'
×
94
                in:
×
95
                'Pharo-11.0.0+build.726.sha.aece1b5473acf3830a0e082c1bc3a15d4ff3522b (64 Bit)'.
×
96

×
97
        entities addAll: (usersWithProjects associations collect: [ :assoc |
×
98
                         | username projects  |
×
99
                         username := assoc key.
×
100
                         projects := assoc value.
×
101

×
102
                        " metrics := GitMetric4User new.
×
103
                         metrics
×
104
                                 glhImporter: glhImporter;
×
105
                                 jiraImporter: jiraImporter;
×
106
                                 findUserNamed: username.
×
107
                         metrics loadProjectsFromIds: projects."
×
108

×
109
                         username ]).
×
110

×
111
        ^ self 
×
112
]
×
113

114
{ #category : #utilities }
115
GitMetricExporter >> constructFilePath: runningOver [
×
116

×
117
        | file |
×
118
        file := (FileLocator home
×
119
                 /
×
120
                         ('GitlabHealth-' , label printString , '-' , runningOver printString
×
121
                          , '-'
×
122
                          , (DateAndTime now printString replaceAll: $: with: $-)
×
123
                          , '.csv')) asFileReference.
×
124
        ^ file
×
125
]
×
126

127
{ #category : #exporting }
NEW
128
GitMetricExporter >> csvMetricsFor: metricCollection at: date [
×
129

×
130
        | csvMetrics |
×
NEW
131
        csvMetrics := metricCollection collect: [ :metric |
×
132
                              metric new description , ' - ' , date asString
×
133
                              -> [ :groupAnalyses |
×
134
                                      | analysis |
×
135
                                      analysis := self
×
136
                                                          getAnalysisWithDate: date
×
137
                                                          from: groupAnalyses.
×
138
                                      analysis
×
139
                                              ifNil: [ nil ]
×
NEW
140
                                              ifNotNil: [ analysis metrics at: metric new name ] ] ].
×
141

×
142
        ^ csvMetrics asOrderedDictionary
×
143
]
×
144

145
{ #category : #accessing }
146
GitMetricExporter >> entities: aCollection [ 
×
147
        entities := aCollection
×
148
]
×
149

150
{ #category : #exporting }
151
GitMetricExporter >> exportFor: usersWithProjects [
×
152

×
153
        self exportFor: usersWithProjects over: { Date. Week . Month . Year }.
×
154
]
×
155

156
{ #category : #exporting }
157
GitMetricExporter >> exportFor: usersWithProjects over: aCollectionOfDateWeekMonthOrYear [
×
158

×
159
        entities ifNil: [
×
160
                self addEntitiesFromUserNamesAndProjects: usersWithProjects ].
×
161

×
162
        self exportFor: aCollectionOfDateWeekMonthOrYear. 
×
163
]
×
164

165
{ #category : #exporting }
166
GitMetricExporter >> exportInCSV [
×
167

×
NEW
168
        self exportUserAnalysesInCSV.
×
NEW
169
        self exportProjectAnalysesInCSV.
×
NEW
170
]
×
171

172
{ #category : #exporting }
NEW
173
GitMetricExporter >> exportInDB: repository [
×
NEW
174

×
NEW
175
        repository enableSingleton.
×
NEW
176
        analyses do: [ :analysis | analysis save ]
×
NEW
177
]
×
178

179
{ #category : #exporting }
NEW
180
GitMetricExporter >> exportInS3: bucketName accessKey: accessKey secretKey: secretKey region: region [
×
NEW
181

×
NEW
182
        | s3 bucket ston date fileName |
×
NEW
183
        AWSS3Config default
×
NEW
184
                accessKeyId: accessKey;
×
NEW
185
                secretKey: secretKey;
×
NEW
186
                regionName: region.
×
NEW
187

×
NEW
188
        s3 := AWSS3 new.
×
NEW
189
        bucket := s3 bucketNamed: bucketName.
×
NEW
190

×
NEW
191
        ston := STON toString: analyses.
×
NEW
192

×
NEW
193
        date := Date today asString copyReplaceAll: ' ' asString with: '-'.
×
NEW
194
        fileName := ('-' join: {
×
NEW
195
                                     'analyse'.
×
NEW
196
                                     label.
×
NEW
197
                                     over asString.
×
NEW
198
                                     date }) , '.ston'.
×
NEW
199

×
NEW
200
        bucket atKey: fileName putObject: ston
×
NEW
201
]
×
202

203
{ #category : #exporting }
NEW
204
GitMetricExporter >> exportProjectAnalysesInCSV [
×
NEW
205

×
NEW
206
        | exportBrowserModel groupedByOver group groupOver groupByName periods csvMetrics file |
×
207
        exportBrowserModel := MiExportModel new.
×
208

×
NEW
209
        groupedByOver := self projectAnalyses groupedBy: #over.
×
210

×
211
        groupedByOver associations do: [ :groupAssociation |
×
212
                group := groupAssociation value.
×
213
                groupOver := groupAssociation key.
×
214

×
215

×
NEW
216
                groupByName := group groupedBy: #projectName.
×
217
                exportBrowserModel entitiesList: groupByName.
×
218

×
219
                exportBrowserModel removeColumnForQueryNamed: #Type.
×
220
                exportBrowserModel removeColumnForQueryNamed: #Name.
×
221

×
222
                exportBrowserModel
×
223
                        addColumnForQuery: [ :groupAnalyses |
×
NEW
224
                        (groupAnalyses at: 1) projectName ]
×
NEW
225
                        withName: #'Project name'.
×
226

×
227
                periods := (group groupedBy: #period) keys.
×
228

×
229
                periods do: [ :period |
×
NEW
230
                        csvMetrics := self
×
NEW
231
                                              csvMetricsFor: self projectMetrics
×
NEW
232
                                              at: (period at: #since).
×
233

×
234

×
235
                        csvMetrics associations do: [ :association |
×
236
                                exportBrowserModel
×
237
                                        addColumnForQuery: association value
×
238
                                        withName: association key ] ].
×
239

×
240
                file := self constructFilePath: groupOver.
×
241
                file writeStreamDo: [ :aStream |
×
242
                        aStream
×
243
                                << 'sep=,';
×
244
                                << OSPlatform current lineEnding.
×
NEW
245
                        exportBrowserModel writeCSVOn: aStream ] ]
×
246
]
×
247

248
{ #category : #exporting }
NEW
249
GitMetricExporter >> exportUserAnalysesInCSV [
×
250

×
NEW
251
        | exportBrowserModel groupedByOver group groupOver groupByName periods csvMetrics file |
×
NEW
252
        exportBrowserModel := MiExportModel new.
×
253

×
NEW
254
        groupedByOver := self userAnalyses groupedBy: #over.
×
NEW
255
        
×
256

×
NEW
257
        groupedByOver associations do: [ :groupAssociation |
×
NEW
258
                group := groupAssociation value.
×
NEW
259
                groupOver := groupAssociation key.
×
260

×
261

×
NEW
262
                groupByName := group groupedBy: #username.
×
NEW
263
                exportBrowserModel entitiesList: groupByName.
×
264

×
NEW
265
                exportBrowserModel removeColumnForQueryNamed: #Type.
×
NEW
266
                exportBrowserModel removeColumnForQueryNamed: #Name.
×
267

×
NEW
268
                exportBrowserModel
×
NEW
269
                        addColumnForQuery: [ :groupAnalyses |
×
NEW
270
                        (groupAnalyses at: 1) username ]
×
NEW
271
                        withName: #'User name'.
×
272

×
NEW
273
                periods := (group groupedBy: #period) keys.
×
NEW
274

×
NEW
275
                periods do: [ :period |
×
NEW
276
                        csvMetrics := self
×
NEW
277
                                              csvMetricsFor: self userMetrics
×
NEW
278
                                              at: (period at: #since).
×
NEW
279

×
NEW
280

×
NEW
281
                        csvMetrics associations do: [ :association |
×
NEW
282
                                exportBrowserModel
×
NEW
283
                                        addColumnForQuery: association value
×
NEW
284
                                        withName: association key ] ].
×
NEW
285

×
NEW
286
                file := self constructFilePath: groupOver.
×
NEW
287
                file writeStreamDo: [ :aStream |
×
NEW
288
                        aStream
×
NEW
289
                                << 'sep=,';
×
NEW
290
                                << OSPlatform current lineEnding.
×
NEW
291
                        exportBrowserModel writeCSVOn: aStream ] ]
×
292
]
×
293

294
{ #category : #projects }
295
GitMetricExporter >> findParticipationOfCommitAuthorNamed: username amongProjects: aCollectionOfProjects [
×
296

×
297
        | response itsProjects i size |
×
298
        i := 1.
×
299
        size := aCollectionOfProjects size.
×
300
        itsProjects := aCollectionOfProjects select: [ :project |
×
301
                               (' ' join: {
×
302
                                                'search'.
×
303
                                                username printString.
×
304
                                                'in Project:'.
×
305
                                                project name printString.
×
306
                                                '['.
×
307
                                                (i := i + 1) printString.
×
308
                                                '/'.
×
309
                                                size.
×
310
                                                ']' }) recordInfo.
×
311

×
312
                               response := glhImporter glhApi
×
313
                                                   commitsOfUser: username
×
314
                                                   inProject: project id
×
315
                                                   since: '01 january 2023' asDateAndTime
×
316
                                                   until: Date today asDateAndTime.
×
317
                               response := glhImporter parseCommitsResult: response.
×
318
                               response isNotEmpty
×
319
                 ].
×
320

×
321
        ^ itsProjects
×
322
]
×
323

324
{ #category : #projects }
325
GitMetricExporter >> findProjectsOfUser: aCollection [ 
×
326
        self shouldBeImplemented.
×
327
]
×
328

329
{ #category : #analysis }
330
GitMetricExporter >> generateAnalysesOver: aDateWeekMonthOrYear [
×
331

×
NEW
332
        | userAnalyses projectAnalyses |
×
333
        runningPeriods do: [ :period |
×
NEW
334
                userAnalyses := self
×
NEW
335
                                        generateUsersAnalysesDuringPeriod: period
×
NEW
336
                                        over: aDateWeekMonthOrYear.
×
NEW
337
                projectAnalyses := self
×
NEW
338
                                           generateProjectsAnalysesDuringPeriod: period
×
NEW
339
                                           over: aDateWeekMonthOrYear ].
×
NEW
340

×
NEW
341
        ^ analyses := userAnalyses, projectAnalyses.
×
NEW
342
]
×
343

344
{ #category : #analysis }
NEW
345
GitMetricExporter >> generateProjectsAnalysesDuringPeriod: period over: aDateWeekMonthOrYear [
×
346

×
NEW
347
        ^self projectEntities collect: [ :project |
×
NEW
348
                ProjectAnalysisReport new
×
NEW
349
                        projectName: project name;
×
NEW
350
                        period: period;
×
NEW
351
                        over: aDateWeekMonthOrYear ]
×
NEW
352
]
×
353

354
{ #category : #analysis }
NEW
355
GitMetricExporter >> generateUsersAnalysesDuringPeriod: period over: aDateWeekMonthOrYear [
×
NEW
356

×
NEW
357
        | userAnalysisReport newMetric result |
×
NEW
358
        ^ self userEntities collect: [ :user |
×
NEW
359
                  userAnalysisReport := UserAnalysisReport new
×
NEW
360
                                                username: user name;
×
NEW
361
                                                period: period;
×
NEW
362
                                                over: aDateWeekMonthOrYear.
×
NEW
363

×
NEW
364
                  metrics do: [ :metric |
×
NEW
365
                          newMetric := metric new
×
NEW
366
                                               glhImporter: glhImporter;
×
NEW
367
                                               jiraImporter: jiraImporter;
×
NEW
368
                                               setPeriodSince: (period at: #since)
×
NEW
369
                                               until: (period at: #until);
×
NEW
370
                                               over: aDateWeekMonthOrYear;
×
NEW
371
                                               user: user;
×
NEW
372
                                                                        emailDomain: emailDomain.
×
NEW
373

×
NEW
374

×
NEW
375
                          result := newMetric calculate.
×
NEW
376
                          userAnalysisReport metrics at: newMetric name put: result ].
×
NEW
377

×
NEW
378
                  userAnalysisReport ]
×
379
]
×
380

381
{ #category : #utilities }
382
GitMetricExporter >> getAnalysisWithDate: date from: analysisCollection [
×
383

×
384
        ^analysisCollection
×
385
                detect: [ :groupAnalysis | (groupAnalysis period at: #since) = date ]
×
386
                ifNone: [ nil ]
×
387
]
×
388

389
{ #category : #accessing }
390
GitMetricExporter >> glhImporter: anImporter [
×
391

×
392
        glhImporter := anImporter withInitialCommits: false; yourself. 
×
393
]
×
394

395
{ #category : #adding }
396
GitMetricExporter >> importAnalysesFromS3: bucketName accessKey: accessKey secretKey: secretKey region: region [
×
397

×
398
        | s3 bucket xmlObjects contentElements filesContent |
×
399
        AWSS3Config default
×
400
                accessKeyId: accessKey;
×
401
                secretKey: secretKey;
×
402
                regionName: region.
×
403

×
404
        s3 := AWSS3 new.
×
405
        bucket := s3 bucketNamed: bucketName.
×
406
        xmlObjects := bucket listObjects.
×
407
        contentElements := xmlObjects root elementsSelect: [ :element |
×
408
                                   element isNamed: 'Contents' ].
×
409

×
410
        filesContent := contentElements collect: [ :contentElement |
×
411
                                | keyValue |
×
412
                                keyValue := contentElement contentStringAt: 'Key'.
×
413
                                bucket getObject: keyValue ].
×
414

×
415
        ^ analyses := (filesContent collect: [ :file | STON fromString: file ])
×
416
                              flattened
×
417
]
×
418

419
{ #category : #initialization }
420
GitMetricExporter >> initialize [
×
421

×
422
        entities := OrderedCollection new.
×
423
        "set up the minimal date from which we are looking for the commits a particular user in projects"
×
424
        sinceTimeLimit := '2024-01-01' asDateAndTime.
×
425
        runningPeriods := OrderedCollection new.
×
426
        maxCommitWindow := 3.
×
427
        over := Date.
×
428

×
429
        analyses := OrderedCollection new.
×
430

×
431
        metrics := {
×
432
                           CodeAdditionMetric.
×
433
                           CodeDeletionMetric.
×
434
                           CommentContributionMetric.
×
435
                           CommitFrequencyMetric.
×
436
                           CodeChurnMetric.
×
437
                           DelayUntilFirstChurnMetric.
×
438
                           TimeBetweenCommitMetric.
×
439
                           ContributedProjectMetric.
×
440
                           MergeRequestDurationMetric.
×
441
                           OpenedMergeRequestMetric.
×
442
                           ClosedMergeRequestMetric.
×
443
                           MergedMergeRequestMetric.
×
444
                           MergedMergeRequestWithoutReviewMetric.
×
445
                           ClosedTicketsMetric}
×
446
]
×
447

448
{ #category : #accessing }
449
GitMetricExporter >> jiraImporter: anObject [
×
450

×
451
        jiraImporter := anObject
×
452
]
×
453

454
{ #category : #accessing }
455
GitMetricExporter >> label: aString [ 
×
456
        label := aString
×
457
]
×
458

459
{ #category : #accessing }
460
GitMetricExporter >> maxCommitWindow: anInteger [
×
461
        maxCommitWindow := anInteger abs
×
462
]
×
463

464
{ #category : #adding }
465
GitMetricExporter >> onlyImportProjectsOfGroup: groupId [
×
466

×
467
        | group |
×
468
        group := glhImporter importGroup: groupId.
×
469
        projectCache := group toScope: GLHProject
×
470
]
×
471

472
{ #category : #accessing }
NEW
473
GitMetricExporter >> projectAnalyses [
×
NEW
474

×
NEW
475
        ^ analyses select: [ :analysis | analysis class = ProjectAnalysisReport ]
×
NEW
476
]
×
477

478
{ #category : #accessing }
NEW
479
GitMetricExporter >> projectEntities [
×
NEW
480

×
NEW
481
        ^ entities select: [ :entity | entity class = GLHProject ]
×
NEW
482
]
×
483

484
{ #category : #accessing }
NEW
485
GitMetricExporter >> projectMetrics [
×
NEW
486

×
NEW
487
        ^ metrics select: [ :metric | metric inheritsFrom: ProjectMetric ]
×
NEW
488
]
×
489

490
{ #category : #setup }
NEW
491
GitMetricExporter >> setupAnalysisForProjectsWithIds: projectIds [
×
NEW
492

×
NEW
493
        | projects |
×
NEW
494
        projects := glhImporter importProjects: projectIds.
×
NEW
495

×
NEW
496
        entities addAll: projects
×
NEW
497
]
×
498

499
{ #category : #setup }
500
GitMetricExporter >> setupAnalysisForUsersWithNames: userNames [
×
501
        "import all the project since a certain time"
×
502

×
503
        | users |
×
504
        users := userNames collect: [ :username |
×
505
                         glhImporter importUserByUsername: username ].
×
506

×
507
        glhImporter userCatalogue scrapeAuthorNamesForUsers: users.
×
508
        
×
509
        users do: [ :user |
×
510
                |projects |
×
511
                projects := glhImporter importContributedProjectsOfUser: user.
×
512
                                projects do: [ :project |
×
513
                                glhImporter completeImportProject: project ].         
×
514
                
×
515
        ].
×
516

×
517
        entities addAll: users.
×
518

×
519
        ^ self
×
520
]
×
521

522
{ #category : #accessing }
NEW
523
GitMetricExporter >> userAnalyses [
×
NEW
524

×
NEW
525
        ^ analyses select: [ :analysis | analysis class = UserAnalysisReport ]
×
NEW
526
]
×
527

528
{ #category : #accessing }
NEW
529
GitMetricExporter >> userEntities [
×
NEW
530

×
NEW
531
        ^ entities select: [ :entity | entity class = GLHUser ]
×
NEW
532
]
×
533

534
{ #category : #accessing }
NEW
535
GitMetricExporter >> userMetrics [
×
NEW
536

×
NEW
537
        ^ metrics select: [ :metric | metric inheritsFrom: UserMetric ]
×
NEW
538
]
×
539

540
{ #category : #setup }
541
GitMetricExporter >> withEmailDomain: anEmailDomain [
×
542

×
543
        "define the email domain of your Git user. Usefull to link them between service (i.e. Jira)"
×
544
        emailDomain := anEmailDomain
×
545
]
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc