-
Notifications
You must be signed in to change notification settings - Fork 159
/
generate-qa-activity-report.js
216 lines (184 loc) · 5.99 KB
/
generate-qa-activity-report.js
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
import simpleGit from 'simple-git'
import path from 'path'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import { Command } from 'commander'
import fs from 'fs'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const git = simpleGit()
const program = new Command()
program
.option('-d, --days <number>', 'number of days to look back')
.option('-m, --month <number>', 'month to look back')
.option('-y, --year <number>', 'year to look back')
.parse(process.argv)
const options = program.opts()
function getGitDateFormat(date) {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
function getStartAndEndDate(month, year) {
const startDate = new Date(year, month - 1, 1)
const endDate = new Date(year, month, 0)
return { startDate, endDate }
}
async function getRecentChanges() {
try {
const repoPath = path.resolve(__dirname)
await git.cwd(repoPath)
let formattedSinceDate, formattedUntilDate, period
if (options.month && options.year) {
const { startDate, endDate } = getStartAndEndDate(options.month, options.year)
formattedSinceDate = getGitDateFormat(startDate)
formattedUntilDate = getGitDateFormat(endDate)
period = `${options.month}_${options.year}`
} else if (options.days) {
const today = new Date()
today.setDate(today.getDate() - options.days)
formattedSinceDate = getGitDateFormat(today)
formattedUntilDate = getGitDateFormat(new Date())
period = `Last_${options.days}_days`
} else {
console.error('Please provide either days or month and year.')
return
}
const logOptions = {
'--since': formattedSinceDate
}
if (formattedUntilDate) {
logOptions['--until'] = formattedUntilDate
}
const logs = await git.log(logOptions)
const csvRows = [
['Test-Type', 'Date', 'Tests Added', 'Tests Changed', 'Tests Deleted', 'commit-ID']
]
for (const log of logs.all) {
const e2eDiff = await git.diff([`${log.hash}~1`, log.hash, '--', 'tests/e2e/cucumber/features'])
const unitDiff = await git.diff([`${log.hash}~1`, log.hash, '--', 'packages/**/*.spec.ts'])
const e2eRow = analyzeE2eDiff(e2eDiff, log)
const unitRow = analyzeUnitDiff(unitDiff, log)
if (e2eRow) {
csvRows.push(e2eRow)
}
if (unitRow) {
csvRows.push(unitRow)
}
}
const csvContent = csvRows.map(row => row.join(',')).join('\n')
const reportsDir = path.join(__dirname, 'reports')
if (!fs.existsSync(reportsDir)) {
fs.mkdirSync(reportsDir)
}
const reportFilePath = path.join(reportsDir, `QA_Activity_Report_${period}.csv`)
fs.writeFile(reportFilePath, csvContent, (err) => {
if (err) {
console.error('Error writing CSV report:', err)
} else {
console.log(`CSV report generated successfully. You can find it in , ${reportFilePath}`)
}
})
} catch (error) {
console.error('Error:', error)
}
}
function analyzeE2eDiff(diff, log) {
const diffLines = diff.split('\n')
let commitAddedTests = 0
let commitChangedTests = 0
let commitDeletedTests = 0
let currentFile = null
for (const line of diffLines) {
if (line.startsWith('diff --git')) {
const match = line.match(/ b\/(tests\/e2e\/cucumber\/features\/[^\s]+)/)
if (match) {
currentFile = match[1]
}
} else if (line.startsWith('+') && !line.startsWith('+++')) {
// Consider only the addition of scenarios or features. Example: + Scenario: activity
if (line.includes('Scenario:')) {
commitAddedTests++
}
} else if (line.startsWith('-') && !line.startsWith('---')) {
// Consider only the deleting of scenarios or features. Example: - Scenario: activity
if (line.includes('Scenario:')) {
commitDeletedTests++
}
} else if ((line.includes('@@ Feature:')) && currentFile) {
// if line contains 'Feature', that is test change. Example @@ -17,8 +17,8 @@ Feature: Download
commitChangedTests++
}
}
if (commitAddedTests || commitChangedTests || commitDeletedTests) {
return [
'UI Test',
log.date,
commitAddedTests,
commitChangedTests,
commitDeletedTests,
log.hash
]
}
}
function analyzeUnitDiff(diff, log) {
const diffLines = diff.split('\n')
let commitAddedTests = 0
let commitChangedTests = 0
let commitDeletedTests = 0
let currentFile = null
let inChangeBlock = false
let inTest = false
for (let i = 0; i < diffLines.length; i++) {
const line = diffLines[i];
// Detect the file being diffed
if (line.startsWith('diff --git')) {
const match = line.match(/ b\/(packages\/[^\s]+\.spec\.ts)/)
if (match) {
currentFile = match[1]
}
}
// Start a new change block
if (line.startsWith('@@')) {
if (inChangeBlock && !inTest) {
commitChangedTests++
}
inChangeBlock = true
inTest = false
}
// Process changes in the current block
if (inChangeBlock) {
if (line.startsWith('+') && !line.startsWith('+++')) {
if (line.includes('it(') || line.includes('it.each(')) {
commitAddedTests++
inTest = true
}
} else if (line.startsWith('-') && !line.startsWith('---')) {
if (line.includes('it(') || line.includes('it.each(')) {
commitDeletedTests++
inTest = true
}
}
}
// End of a change block
if (line === '' || i === diffLines.length - 1) {
if (inChangeBlock && !inTest) {
commitChangedTests++
}
inChangeBlock = false
}
}
// Return results if there are any changes
if (commitAddedTests || commitChangedTests || commitDeletedTests) {
return [
'Unit Test',
log.date,
commitAddedTests,
commitChangedTests,
commitDeletedTests,
log.hash
]
}
}
getRecentChanges()