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

moosetechnology / GitProjectHealth / 10699021710

04 Sep 2024 09:33AM UTC coverage: 18.305%. First build
10699021710

Pull #48

github

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

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

1590 of 8686 relevant lines covered (18.31%)

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.
×
255

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

×
260

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

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

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

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

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

×
NEW
279

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

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

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

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

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

×
320
        ^ itsProjects
×
321
]
×
322

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

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

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

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

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

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

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

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

×
NEW
363
                  metrics do: [ :metric |
×
NEW
364
                          newMetric := metric new
×
NEW
365
                                               setPeriodSince: (period at: #since)
×
NEW
366
                                               until: (period at: #until);
×
NEW
367
                                               over: aDateWeekMonthOrYear;
×
NEW
368
                                               user: entity.
×
369

×
370

×
NEW
371
                          result := newMetric calculate.
×
NEW
372
                          userAnalysisReport metrics at: newMetric name put: result ].
×
373

×
NEW
374
                  userAnalysisReport ]
×
375
]
×
376

377
{ #category : #utilities }
378
GitMetricExporter >> getAnalysisWithDate: date from: analysisCollection [
×
379

×
380
        ^analysisCollection
×
381
                detect: [ :groupAnalysis | (groupAnalysis period at: #since) = date ]
×
382
                ifNone: [ nil ]
×
383
]
×
384

385
{ #category : #accessing }
386
GitMetricExporter >> glhImporter: anImporter [
×
387

×
388
        glhImporter := anImporter withInitialCommits: false; yourself. 
×
389
]
×
390

391
{ #category : #adding }
392
GitMetricExporter >> importAnalysesFromS3: bucketName accessKey: accessKey secretKey: secretKey region: region [
×
393

×
394
        | s3 bucket xmlObjects contentElements filesContent |
×
395
        AWSS3Config default
×
396
                accessKeyId: accessKey;
×
397
                secretKey: secretKey;
×
398
                regionName: region.
×
399

×
400
        s3 := AWSS3 new.
×
401
        bucket := s3 bucketNamed: bucketName.
×
402
        xmlObjects := bucket listObjects.
×
403
        contentElements := xmlObjects root elementsSelect: [ :element |
×
404
                                   element isNamed: 'Contents' ].
×
405

×
406
        filesContent := contentElements collect: [ :contentElement |
×
407
                                | keyValue |
×
408
                                keyValue := contentElement contentStringAt: 'Key'.
×
409
                                bucket getObject: keyValue ].
×
410

×
411
        ^ analyses := (filesContent collect: [ :file | STON fromString: file ])
×
412
                              flattened
×
413
]
×
414

415
{ #category : #initialization }
416
GitMetricExporter >> initialize [
×
417

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

×
425
        analyses := OrderedCollection new.
×
426

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

444
{ #category : #accessing }
445
GitMetricExporter >> jiraImporter: anObject [
×
446

×
447
        jiraImporter := anObject
×
448
]
×
449

450
{ #category : #accessing }
451
GitMetricExporter >> label: aString [ 
×
452
        label := aString
×
453
]
×
454

455
{ #category : #accessing }
456
GitMetricExporter >> maxCommitWindow: anInteger [
×
457
        maxCommitWindow := anInteger abs
×
458
]
×
459

460
{ #category : #adding }
461
GitMetricExporter >> onlyImportProjectsOfGroup: groupId [
×
462

×
463
        | group |
×
464
        group := glhImporter importGroup: groupId.
×
465
        projectCache := group toScope: GLHProject
×
466
]
×
467

468
{ #category : #accessing }
NEW
469
GitMetricExporter >> projectAnalyses [
×
NEW
470

×
NEW
471
        ^ analyses select: [ :analysis | analysis class = ProjectAnalysisReport ]
×
NEW
472
]
×
473

474
{ #category : #accessing }
NEW
475
GitMetricExporter >> projectEntities [
×
NEW
476

×
NEW
477
        ^ entities select: [ :entity | entity class = GLHProject ]
×
NEW
478
]
×
479

480
{ #category : #accessing }
NEW
481
GitMetricExporter >> projectMetrics [
×
NEW
482

×
NEW
483
        ^ metrics select: [ :metric | metric inheritsFrom: ProjectMetric ]
×
NEW
484
]
×
485

486
{ #category : #setup }
NEW
487
GitMetricExporter >> setupAnalysisForProjectsWithIds: projectIds [
×
NEW
488

×
NEW
489
        | projects |
×
NEW
490
        projects := glhImporter importProjects: projectIds.
×
NEW
491

×
NEW
492
        entities addAll: projects
×
NEW
493
]
×
494

495
{ #category : #setup }
496
GitMetricExporter >> setupAnalysisForUsersWithNames: userNames [
×
497
        "import all the project since a certain time"
×
498

×
499
        | users |
×
500
        users := userNames collect: [ :username |
×
501
                         glhImporter importUserByUsername: username ].
×
502

×
503
        glhImporter userCatalogue scrapeAuthorNamesForUsers: users.
×
504
        
×
505
        users do: [ :user |
×
506
                |projects |
×
507
                projects := glhImporter importContributedProjectsOfUser: user.
×
508
                                projects do: [ :project |
×
509
                                glhImporter completeImportProject: project ].         
×
510
                
×
511
        ].
×
512

×
513
        entities addAll: users.
×
514

×
515
        ^ self
×
516
]
×
517

518
{ #category : #accessing }
NEW
519
GitMetricExporter >> userAnalyses [
×
NEW
520

×
NEW
521
        ^ analyses select: [ :analysis | analysis class = UserAnalysisReport ]
×
NEW
522
]
×
523

524
{ #category : #accessing }
NEW
525
GitMetricExporter >> userEntities [
×
NEW
526

×
NEW
527
        ^ entities select: [ :entity | entity class = GitMetric4User ]
×
NEW
528
]
×
529

530
{ #category : #accessing }
NEW
531
GitMetricExporter >> userMetrics [
×
NEW
532

×
NEW
533
        ^ metrics select: [ :metric | metric inheritsFrom: UserMetric ]
×
NEW
534
]
×
535

536
{ #category : #setup }
537
GitMetricExporter >> withEmailDomain: anEmailDomain [
×
538

×
539
        "define the email domain of your Git user. Usefull to link them between service (i.e. Jira)"
×
540
        emailDomain := anEmailDomain
×
541
]
×
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