forked from promyze/gitlab-mr-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.ts
191 lines (163 loc) · 6.45 KB
/
extract.ts
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
import axios from 'axios';
import * as fs from 'fs';
require('dotenv').config();
// Configuration variables (set these accordingly)
const gitlabToken = process.env.GITLAB_TOKEN; // Your GitLab personal access token
if (!gitlabToken) {
throw new Error('Please set the GITLAB_TOKEN environment variable');
}
const gitlabApiUrl = process.env.GITLAB_API_URL || 'https://gitlab.com/api/v4'; // GitLab API URL
const projectId = process.env.GITLAB_PROJECT_ID?.toString() || ''; // The ID of the GitLab project
if (!projectId?.length) {
throw new Error('Please set the GITLAB_PROJECT_ID environment variable');
}
const startDate = process.env.START_DATE; // Start date in ISO format (e.g., '2023-01-01T00:00:00Z')
if (!startDate) {
throw new Error('Please set the START_DATE environment variable');
}
const endDate = process.env.END_DATE; // End date in ISO format (e.g., '2023-12-31T23:59:59Z')
if (!endDate) {
throw new Error('Please set the END_DATE environment variable');
}
// Function to fetch merge requests with pagination
async function fetchMergeRequests(page = 1): Promise<any[]> {
const perPage = 100; // Maximum items per page
const url = `${gitlabApiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests`;
const response = await axios.get(url, {
headers: {
'Private-Token': gitlabToken,
},
params: {
state: 'all',
created_after: startDate,
created_before: endDate,
per_page: perPage,
page: page,
},
});
const mergeRequests = response.data;
const totalPages = parseInt(response.headers['x-total-pages'] || '1', 10);
if (page < totalPages) {
const nextPageMergeRequests = await fetchMergeRequests(page + 1);
return mergeRequests.concat(nextPageMergeRequests);
} else {
return mergeRequests;
}
}
async function getMergeRequestCommentsCount(mergeRequestIid) {
const headers = {
'Private-Token': gitlabToken,
};
try {
// Fetch Notes
let notes = [];
let page = 1;
let perPage = 100;
let totalPages = 1;
do {
const notesResponse = await axios.get(
`${gitlabApiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${mergeRequestIid}/notes`,
{
headers,
params: {
page,
per_page: perPage,
},
}
);
notes = notes.concat(notesResponse.data);
// Get total pages from headers
totalPages = parseInt(notesResponse.headers['x-total-pages'], 10) || 1;
page += 1;
} while (page <= totalPages);
// Filter out system notes if desired
const userNotes = notes.filter(note => !note.system);
// Fetch Discussions
let discussions = [];
page = 1;
totalPages = 1;
do {
const discussionsResponse = await axios.get(
`${gitlabApiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${mergeRequestIid}/discussions`,
{
headers,
params: {
page,
per_page: perPage,
},
}
);
discussions = discussions.concat(discussionsResponse.data);
// Get total pages from headers
totalPages = parseInt(discussionsResponse.headers['x-total-pages'], 10) || 1;
page += 1;
} while (page <= totalPages);
// Count total notes in discussions
let discussionNotesCount = 0;
discussions.forEach(discussion => {
// Exclude system notes within discussions if desired
const notesInDiscussion = discussion.notes.filter(note => !note.system);
discussionNotesCount += notesInDiscussion.length;
});
// Total comments count
const totalCommentsCount = userNotes.length + discussionNotesCount;
return {
totalCommentsCount,
overviewCommentsCount: userNotes.length,
codeDiscussionCommentsCount: discussionNotesCount,
};
} catch (error) {
console.error('Error fetching comments:', error.response ? error.response.data : error.message);
throw error;
}
}
// Function to get details of a merge request
async function getMergeRequestDetails(iid: number): Promise<any> {
const url = `${gitlabApiUrl}/projects/${encodeURIComponent(projectId)}/merge_requests/${iid}`;
const response = await axios.get(url, {
headers: {
'Private-Token': gitlabToken,
},
});
return response.data;
}
// Main function to execute the script
async function main() {
try {
const mergeRequests = await fetchMergeRequests();
console.log(`Fetched ${mergeRequests.length} merge requests`);
// Fetch merge request details concurrently
const promises = mergeRequests.map(async (mr) => {
const commentsCount = await getMergeRequestCommentsCount(mr.iid);
const mrId = mr.iid;
const mrUrl = mr.web_url;
const openedDate = mr.created_at;
const closedDate = mr.closed_at || mr.merged_at || 'opened';
const state = mr.state;
return {
'Merge Request ID': mrId,
'Merge Request URL': mrUrl,
'State': state,
'Opened Date': openedDate,
'Closed Date': closedDate,
'Comments': commentsCount.totalCommentsCount,
};
});
const results = await Promise.all(promises);
// Generate CSV content
const csvHeader = 'Merge Request ID,Merge Request URL,State,Opened Date,Closed Date,Comments\n';
const csvLines = results.map((result) => {
// Escape commas in URLs
const escapedUrl = `"${result['Merge Request URL']}"`;
return `${result['Merge Request ID']},${escapedUrl},${result['State']},${result['Opened Date']},${result['Closed Date']},${result['Comments']}`;
});
const csvContent = csvHeader + csvLines.join('\n');
// Write CSV file
fs.writeFileSync('merge_requests.csv', csvContent);
console.log('CSV file generated: merge_requests.csv');
} catch (error) {
console.error('Error:', error);
}
}
// Execute the main function
main();