-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.cpp
More file actions
195 lines (170 loc) · 6.91 KB
/
Utils.cpp
File metadata and controls
195 lines (170 loc) · 6.91 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
/**
* @file Utils.cpp
* @author Ahmed Khaled
* @brief This file implements functions in Utils.h
*/
#include <iostream>
#include <string>
#include <fstream>
#include <curl/curl.h> // Used to make API requests
#include <json.hpp>
#include "Utils.h"
#include "Enums.h"
#include "Config.h"
using namespace std;
using namespace nlohmann;
extern Config config;
/**
* @brief Prints error messages when user runs the script with incorrect parameters/input
* @param inputError The type of input error
*/
void printInputError(InputErrors inputError) {
if (inputError == InputErrors::NoReleaseNotesSource) {
cerr << config.noReleaseNotesSourceError << endl;
}
else if (inputError == InputErrors::IncorrectReleaseNotesSource) {
cerr << config.incorrectReleaseNotesSourceError << endl;
}
else if (inputError == InputErrors::NoReleaseNotesMode) {
cerr << config.noReleaseNotesModeError << endl;
}
else if (inputError == InputErrors::IncorrectReleaseNotesMode) {
cerr << config.incorrectReleaseNotesModeError << endl;
}
else if (inputError == InputErrors::NoGithubToken) {
cerr << config.noGithubTokenError << endl;
}
else if (inputError == InputErrors::NoReleaseStartReference) {
cerr << config.noReleaseStartReferenceError << endl;
}
else if (inputError == InputErrors::NoReleaseEndReference) {
cerr << config.noReleaseEndReferenceError << endl;
}
else if (inputError == InputErrors::NoPullRequestNumber) {
cerr << config.noPullRequestNumberError << endl;
}
else if(inputError == InputErrors::NoGithubRepository) {
cerr << config.noGithubRepositoryError << endl;
}
cerr << config.expectedSyntaxMessage << endl;
}
/**
* @brief Callback function that is required to handle API response in libcurl
* @param data Pointer to the data received
* @param size Size of each data element
* @param numOfBytes Number of bytes received
* @param buffer Pointer to the buffer storing the response
* @return Total size of the received data
*/
size_t handleApiCallBack(char* data, size_t size, size_t numOfBytes, string* buffer) {
size_t totalSize = size * numOfBytes;
buffer->append(data, totalSize);
return totalSize;
}
/**
* @brief Throws runtime exceptions with appropriate messages that describe the given GitHub API error code
* All info obtained from https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api?apiVersion=2022-11-28
* @param errorCode The GitHub API error code that occurred
* @param apiResponse The GitHub API response
*/
void handleGithubApiErrorCodes(long errorCode, string apiResponse) {
if (errorCode == 429 || errorCode == 403) {
throw runtime_error(config.githubApiRateLimitExceededError + apiResponse);
}
else if (errorCode == 401) {
throw runtime_error(config.githubApiUnauthorizedAccessError + apiResponse);
}
else if (errorCode == 400) {
throw runtime_error(config.githubApiBadRequestError + apiResponse);
}
}
/**
* @brief Checks how the given commit message matches the expected conventional commit type
* @param commitMessage The commit message
* @param commitTypeIndex Index of the commit type to check against in the commit types 2d array
* @return The type of match that happened between the two commit types
*/
CommitTypeMatchResults checkCommitTypeMatch(string commitMessage, int commitTypeIndex) {
string correctCommitType = config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::ConventionalName];
if (commitMessage.substr(0, commitMessage.find(":")) == correctCommitType) {
return CommitTypeMatchResults::MatchWithoutSubCategory;
}
else if (commitMessage.substr(0, commitMessage.find("(")) == correctCommitType) {
return CommitTypeMatchResults::MatchWithSubCategory;
}
else {
return CommitTypeMatchResults::NoMatch;
}
}
/**
* @brief Converts markdown to HTML using the GitHub API markdown endpoint
* @param markdownText The markdown text to be converted to HTML
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API
* @return The HTML text containing the exact same content as the given markdown
*/
string convertMarkdownToHtml(string markdownText, string githubToken) {
// Initializing libcurl
CURL* curl = curl_easy_init();
CURLcode resultCode;
string htmlText;
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, ((const string)"Accept: application/vnd.github+json").c_str());
headers = curl_slist_append(headers, ("Authorization: token " + githubToken).c_str());
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, config.githubMarkdownApiUrl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleApiCallBack);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlText);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Ahmed-Khaled-dev");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
json postData;
postData["text"] = markdownText;
string postDataString = postData.dump();
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postDataString.c_str());
resultCode = curl_easy_perform(curl);
if (resultCode == CURLE_OK) {
// Get the HTTP response code
long httpCode;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
if (httpCode == 200) {
return htmlText;
}
else if (httpCode == 404) {
throw runtime_error("Markdown API url not found");
}
else {
handleGithubApiErrorCodes(httpCode, htmlText);
}
}
else {
throw runtime_error(config.githubApiUnableToMakeRequestError);
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
else {
throw runtime_error(config.githubApiLibcurlError);
}
return htmlText;
}
/**
* @brief Writes the generated markdown notes in the markdown file
* and converts these markdown notes to HTML and writes them in the HTML file
* @param markdownGeneratedNotes The generated markdown notes to be written
* @param githubToken The GitHub token used to make authenticated requests to the GitHub API
*/
void writeGeneratedNotesInFiles(string markdownGeneratedNotes, string githubToken) {
ofstream markdownFileOutput(config.markdownOutputFileName);
if (!markdownFileOutput.is_open()) {
throw runtime_error(config.markdownFileError);
}
if (markdownGeneratedNotes.size() == 0) {
throw runtime_error(config.emptyReleaseNotesMessage);
}
markdownFileOutput << markdownGeneratedNotes;
markdownFileOutput.close();
ofstream htmlFileOutput(config.htmlOutputFileName);
if (!htmlFileOutput.is_open()) {
throw runtime_error(config.htmlFileError);
}
htmlFileOutput << convertMarkdownToHtml(markdownGeneratedNotes, githubToken);
}