-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
408 lines (345 loc) · 18.4 KB
/
Main.cpp
File metadata and controls
408 lines (345 loc) · 18.4 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* @file Main.cpp
* @author Ahmed Khaled
* @brief This file contains the main functions related to generating release notes from commit messages or pull requests
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <regex>
#include <curl/curl.h> // Used to make API requests
#include <json.hpp>
#include "Config.h"
#include "Enums.h"
#include "Utils.h"
#include "Format.h"
// To allow for code compatibility between different compilers/OS (Microsoft's Visual C++ compiler and GCC)
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
using namespace std;
using namespace nlohmann;
void addPullRequestInfoInNotes(json pullRequestInfo, string &pullRequestsReleaseNotes, ReleaseNoteModes releaseNotesMode,
int commitTypeIndex);
string getPullRequestInfo(string pullRequestUrl, string githubToken);
string getCommitsNotesFromPullRequests(int commitTypeIndex, string releaseStartRef, string releaseEndRef,
string githubToken, ReleaseNoteModes releaseNotesMode);
string getCommitsNotesFromCommitMessages(int commitTypeIndex, string releaseStartRef, string releaseEndRef);
void generateReleaseNotes(ReleaseNoteSources releaseNoteSource, string releaseStartRef, string releaseEndRef,
string githubToken, ReleaseNoteModes releaseNoteMode = ReleaseNoteModes::Short);
void generatePullRequestChangeNote(string pullRequestNumber, string githubToken);
Config config;
int main(int argc, char* argv[]){
// Reading values from the external configuration file
const string releaseNotesConfigFileName = "release_notes_config.json";
try {
config.load(releaseNotesConfigFileName);
}
catch (const exception& e) {
cerr << e.what() << endl;
return 1;
}
if (argc <= 1) {
printInputError(InputErrors::NoReleaseNotesSource);
return 1;
}
try {
if (strcmp(argv[1], config.singlePullRequestSourceCliInputName.c_str()) == 0) {
if (argc <= 2) {
printInputError(InputErrors::NoPullRequestNumber);
return 1;
}
else if (argc <= 3) {
printInputError(InputErrors::NoGithubToken);
return 1;
}
else if (argc <= 4) {
printInputError(InputErrors::NoGithubRepository);
return 1;
}
config.repoCommitsUrl = config.githubUrl + argv[4] + "/commit/";
config.repoIssuesUrl = config.githubUrl + argv[4] + "/issues/";
config.repoPullRequestsApiUrl = config.githubReposApiUrl + argv[4] + "/pulls/";
generatePullRequestChangeNote(argv[2], argv[3]);
}
else {
if (argc <= 2) {
printInputError(InputErrors::NoReleaseStartReference);
return 1;
}
else if (argc <= 3) {
printInputError(InputErrors::NoReleaseEndReference);
return 1;
}
else if (argc <= 4) {
printInputError(InputErrors::NoGithubToken);
return 1;
}
if (strcmp(argv[1], config.commitMessagesSourceCliInputName.c_str()) == 0
|| strcmp(argv[1], config.commitMessagesSourceGithubActionsInputName.c_str()) == 0) {
generateReleaseNotes(ReleaseNoteSources::CommitMessages, argv[2], argv[3], argv[4]);
}
else if (strcmp(argv[1], config.pullRequestsSourceCliInputName.c_str()) == 0
|| strcmp(argv[1], config.pullRequestsSourceGithubActionsInputName.c_str()) == 0) {
if (argc <= 5) {
printInputError(InputErrors::NoReleaseNotesMode);
return 1;
}
else if(argc <= 6) {
printInputError(InputErrors::NoGithubRepository);
return 1;
}
config.repoCommitsUrl = config.githubUrl + argv[6] + "/commit/";
config.repoIssuesUrl = config.githubUrl + argv[6] + "/issues/";
config.repoPullRequestsApiUrl = config.githubReposApiUrl + argv[6] + "/pulls/";
if (strcmp(argv[5], config.fullModeCliInputName.c_str()) == 0
|| strcmp(argv[5], config.fullModeGithubActionsInputName.c_str()) == 0) {
generateReleaseNotes(ReleaseNoteSources::PullRequests, argv[2], argv[3], argv[4], ReleaseNoteModes::Full);
}
else if (strcmp(argv[5], config.shortModeCliInputName.c_str()) == 0
|| strcmp(argv[5], config.shortModeGithubActionsInputName.c_str()) == 0) {
generateReleaseNotes(ReleaseNoteSources::PullRequests, argv[2], argv[3], argv[4], ReleaseNoteModes::Short);
}
else {
printInputError(InputErrors::IncorrectReleaseNotesMode);
return 1;
}
}
else {
printInputError(InputErrors::IncorrectReleaseNotesSource);
return 1;
}
}
}
catch (const exception& e) {
cerr << config.failedToGenerateReleaseNotesMessage << endl;
cerr << e.what() << endl;
return 1;
}
return 0;
}
/**
* @brief Formats given pull request information (title, body) into a nice looking markdown format,
* then adds them in the given current release notes, based on the release notes mode
* @param pullRequestInfo JSON object containing raw pull request information
* @param pullRequestsReleaseNotes Existing release notes generated from pull requests
* @param releaseNotesMode The release notes mode that will decide if the pull request body will be included or not
* @param commitTypeIndex Index of the commit type in the commit types 2d array that this pull request belongs to
*/
void addPullRequestInfoInNotes(json pullRequestInfo, string &pullRequestsReleaseNotes, ReleaseNoteModes releaseNotesMode,
int commitTypeIndex) {
if (!pullRequestInfo["title"].is_null()) {
string title = pullRequestInfo["title"];
CommitTypeMatchResults matchResult = checkCommitTypeMatch(title, commitTypeIndex);
if(releaseNotesMode == ReleaseNoteModes::Full)
pullRequestsReleaseNotes += convertConventionalCommitTitleToReleaseNoteTitle(title, matchResult,
config.markdownFullModeReleaseNotePrefix);
else
pullRequestsReleaseNotes += convertConventionalCommitTitleToReleaseNoteTitle(title, matchResult,
config.markdownReleaseNotePrefix);
}
if (releaseNotesMode == ReleaseNoteModes::Full && !pullRequestInfo["body"].is_null()) {
string body = pullRequestInfo["body"];
// Capitalizing the first letter of the body
body[0] = toupper(body[0]);
body = formatPullRequestBody(body);
pullRequestsReleaseNotes += indentAllLinesInString(body) + "\n";
}
pullRequestsReleaseNotes += "\n";
}
/**
* @brief Retrieves pull request info from the GitHub API using libcurl
* @param pullRequestUrl The GitHub API URL of the pull request
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API
* @return The pull request info in JSON
*/
string getPullRequestInfo(string pullRequestUrl, string githubToken) {
// Initializing libcurl
CURL* curl = curl_easy_init();
CURLcode resultCode;
string jsonResponse;
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, ("Authorization: token " + githubToken).c_str());
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, pullRequestUrl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleApiCallBack);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &jsonResponse);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Ahmed-Khaled-dev");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
resultCode = curl_easy_perform(curl);
if (resultCode == CURLE_OK) {
// Get the HTTP response code
long httpCode;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
// All info obtained from https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api?apiVersion=2022-11-28
// and https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request
if (httpCode == 200) {
return jsonResponse;
}
else if (httpCode == 503 || httpCode == 500 || httpCode == 422 || httpCode == 406) {
throw runtime_error("GitHub API request could not be processed to retrieve pull request " + pullRequestUrl
+ " Additional information : " + jsonResponse);
}
else if (httpCode == 404) {
throw runtime_error("Pull request " + pullRequestUrl + " not found "
+ "or you are accessing a private repository and the GitHub token used doesn't have permissions to access pull requests info. "
+ "Additional information : " + jsonResponse);
}
else {
handleGithubApiErrorCodes(httpCode, jsonResponse);
}
}
else {
throw runtime_error(config.githubApiUnableToMakeRequestError);
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
else {
throw runtime_error(config.githubApiLibcurlError);
}
return jsonResponse;
}
/**
* @brief Retrieves release notes from each commit's *pull request* between the start reference and the end reference
* based on the given conventional commit type, release notes mode, and using the given GitHub token
* @param commitTypeIndex Index of the commit type in the commit types 2d array, to only generate release notes from the given commit type (fix, feat, etc.)
* @param releaseStartRef The git reference (commit SHA or tag name) that references the commit directly before the
* commit that starts the commit messages of the release, for example, the tag name of the previous release
* @param releaseEndRef The git reference (commit SHA or tag name) that references the end of the commit messages of the release
* @param releaseNotesMode The release notes mode
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API
* @return The generated release notes
*/
string getCommitsNotesFromPullRequests(int commitTypeIndex, string releaseStartRef, string releaseEndRef,
string githubToken, ReleaseNoteModes releaseNotesMode) {
string commandToRetrieveCommitsMessages = "git log " + releaseStartRef + ".." + releaseEndRef +
" --oneline --format=\"%s\" --grep=\"^" + config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::ConventionalName] + "[:(]\"" +
" --grep=\"#[0-9]\" --all-match";
FILE* pipe = popen(commandToRetrieveCommitsMessages.c_str(), "r");
if (!pipe) {
throw runtime_error(config.gitLogError);
}
char buffer[150];
string commitMessage, commitPullRequestNumber, releaseNotesFromPullRequests = "";
// Add the title of this commit type section in the release notes
releaseNotesFromPullRequests += "\n" + config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::MarkdownTitle] + "\n";
bool commitTypeContainsReleaseNotes = 0;
// Reading commit messages line-by-line from the output of the git log command
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
commitTypeContainsReleaseNotes = 1;
commitMessage = buffer;
CommitTypeMatchResults matchResult = checkCommitTypeMatch(commitMessage, commitTypeIndex);
if (matchResult != CommitTypeMatchResults::NoMatch) {
// Regular expression to match # followed by one or more digits
regex prRegex(R"(#(\d+))");
smatch match;
// Validating that a hashtag exists
if (regex_search(commitMessage, match, prRegex))
{
// Extracting the PR number associated with the commit from the first capture group
commitPullRequestNumber = match.str(1);
string jsonResponse = getPullRequestInfo(config.repoPullRequestsApiUrl + commitPullRequestNumber, githubToken);
json pullRequestInfo = json::parse(jsonResponse);
addPullRequestInfoInNotes(pullRequestInfo, releaseNotesFromPullRequests, releaseNotesMode, commitTypeIndex);
}
}
}
// Remove the title of this commit type section if it doesn't contain any release notes
if (!commitTypeContainsReleaseNotes) {
releaseNotesFromPullRequests = "";
}
pclose(pipe);
return releaseNotesFromPullRequests;
}
/**
* @brief Retrieves release notes from each commit's *message* between the start reference and the end reference
* based on the given conventional commit type
* @param commitTypeIndex Index of the commit type in the commit types 2d array, to only generate release notes from the given commit type (fix, feat, etc.)
* @param releaseStartRef The git reference (commit SHA or tag name) that references the commit directly before the
* commit that starts the commit messages of the release, for example, the tag name of the previous release
* @param releaseEndRef The git reference (commit SHA or tag name) that references the end of the commit messages of the release
* @return The generated release notes
*/
string getCommitsNotesFromCommitMessages(int commitTypeIndex, string releaseStartRef, string releaseEndRef) {
string commandToRetrieveCommitsMessages = "git log " + releaseStartRef + ".." + releaseEndRef +
" --oneline --format=\"%s\" --grep=\"^" + config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::ConventionalName] + "[:(]\"";
FILE* pipe = popen(commandToRetrieveCommitsMessages.c_str(), "r");
if (!pipe) {
throw runtime_error(config.gitLogError);
}
char buffer[150];
string commitMessage, releaseNotesFromCommitMessages, subCategoryText;
// Add the title of this commit type section in the release notes
releaseNotesFromCommitMessages += "\n" + config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::MarkdownTitle] + "\n";
bool commitTypeContainsReleaseNotes = 0;
// Reading commit messages line-by-line from the output of the git log command
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
commitTypeContainsReleaseNotes = 1;
commitMessage = buffer;
CommitTypeMatchResults matchResult = checkCommitTypeMatch(commitMessage, commitTypeIndex);
if (matchResult != CommitTypeMatchResults::NoMatch) {
releaseNotesFromCommitMessages += convertConventionalCommitTitleToReleaseNoteTitle(commitMessage, matchResult,
config.markdownReleaseNotePrefix);
}
}
// Remove the title of this commit type section if it doesn't contain any release notes
if (!commitTypeContainsReleaseNotes) {
releaseNotesFromCommitMessages = "";
}
pclose(pipe);
return releaseNotesFromCommitMessages;
}
/**
* @brief Generates release notes using commit messages between the start reference and the end reference
* using the given release notes source and if the source is pull requests then generates them based on the release note mode
* and using the given GitHub token
* @param releaseNoteSource The source to generate release notes from (commit messages or pull requests)
* @param releaseStartRef The git reference (commit SHA or tag name) that references the commit directly before the
* commit that starts the commit messages of the release, for example, the tag name of the previous release
* @param releaseEndRef The git reference (commit SHA or tag name) that references the end of the commit messages of the release
* @param releaseNoteMode The release notes mode when the source is pull requests
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API when source is pull requests
*/
void generateReleaseNotes(ReleaseNoteSources releaseNoteSource, string releaseStartRef, string releaseEndRef,
string githubToken, ReleaseNoteModes releaseNoteMode) {
cout << config.generatingReleaseNotesMessage << endl;
string commandToRetrieveCommitsMessages;
string currentCommitMessage;
string markdownReleaseNotes = "";
for (int i = 0; i < config.commitTypesCount; i++)
{
if (releaseNoteSource == ReleaseNoteSources::CommitMessages) {
markdownReleaseNotes += getCommitsNotesFromCommitMessages(i, releaseStartRef, releaseEndRef);
}
else if (releaseNoteSource == ReleaseNoteSources::PullRequests) {
markdownReleaseNotes += getCommitsNotesFromPullRequests(i, releaseStartRef, releaseEndRef, githubToken, releaseNoteMode);
}
}
writeGeneratedNotesInFiles(markdownReleaseNotes, githubToken);
cout << "Release notes generated successfully, check " + config.markdownOutputFileName + " and " + config.htmlOutputFileName + " in the current directory" << endl;
}
/**
* @brief Generates a single change note with it's conventional commit type category
* for a single pull request using the GitHub API (Not using commit messages at all)
* @param pullRequestNumber The number of the pull request to generate change note for (e.g., 13, 144, 3722, etc.)
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API
*/
void generatePullRequestChangeNote(string pullRequestNumber, string githubToken) {
cout << config.generatingReleaseNotesMessage << endl;
string jsonResponse = getPullRequestInfo(config.repoPullRequestsApiUrl + pullRequestNumber, githubToken);
json pullRequestInfo = json::parse(jsonResponse);
string pullRequestChangeNote = "";
for (int commitTypeIndex = 0; commitTypeIndex < config.commitTypesCount; commitTypeIndex++)
{
if (checkCommitTypeMatch(pullRequestInfo["title"], commitTypeIndex) != CommitTypeMatchResults::NoMatch) {
pullRequestChangeNote += "\n" + config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::MarkdownTitle] + "\n";
addPullRequestInfoInNotes(pullRequestInfo, pullRequestChangeNote, ReleaseNoteModes::Full, commitTypeIndex);
break;
}
}
writeGeneratedNotesInFiles(pullRequestChangeNote, githubToken);
cout << "Pull request change note generated successfully, check " + config.markdownOutputFileName + " and " + config.htmlOutputFileName + " in the current directory" << endl;
}