Release Notes Manager
Loading...
Searching...
No Matches
Utils.h File Reference

This file defines general utility functions, like error handling, checks, converting types, etc. More...

#include <string>
#include "Enums.h"

Go to the source code of this file.

Functions

void printInputError (InputErrors inputError)
 Prints error messages when user runs the script with incorrect parameters/input.
 
size_t handleApiCallBack (char *data, size_t size, size_t numOfBytes, string *buffer)
 Callback function that is required to handle API response in libcurl.
 
void handleGithubApiErrorCodes (long errorCode, string apiResponse)
 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.
 
CommitTypeMatchResults checkCommitTypeMatch (string commitMessage, int commitTypeIndex)
 Checks how the given commit message matches the expected conventional commit type.
 
string convertMarkdownToHtml (string markdownText, string githubToken)
 Converts markdown to HTML using the GitHub API markdown endpoint.
 
void writeGeneratedNotesInFiles (string markdownGeneratedNotes, string githubToken)
 Writes the generated markdown notes in the markdown file and converts these markdown notes to HTML and writes them in the HTML file.
 

Detailed Description

This file defines general utility functions, like error handling, checks, converting types, etc.

Author
Ahmed Khaled

Function Documentation

◆ checkCommitTypeMatch()

CommitTypeMatchResults checkCommitTypeMatch ( string commitMessage,
int commitTypeIndex )

Checks how the given commit message matches the expected conventional commit type.

Parameters
commitMessageThe commit message
commitTypeIndexIndex of the commit type to check against in the commit types 2d array
Returns
The type of match that happened between the two commit types
96 {
97 string correctCommitType = config.commitTypes[commitTypeIndex][(int)CommitTypeInfo::ConventionalName];
98
99 if (commitMessage.substr(0, commitMessage.find(":")) == correctCommitType) {
101 }
102 else if (commitMessage.substr(0, commitMessage.find("(")) == correctCommitType) {
104 }
105 else {
107 }
108}
Config config
Definition Main.cpp:40
string commitTypes[50][2]
2d array storing conventional commit types and their corresponding markdown titles The first dimensio...
Definition Config.h:31

◆ convertMarkdownToHtml()

string convertMarkdownToHtml ( string markdownText,
string githubToken )

Converts markdown to HTML using the GitHub API markdown endpoint.

Parameters
markdownTextThe markdown text to be converted to HTML
githubTokenThe GitHub token used to make authenticated requests to the GitHub API
Returns
The HTML text containing the exact same content as the given markdown
116 {
117 // Initializing libcurl
118 CURL* curl = curl_easy_init();
119 CURLcode resultCode;
120 string htmlText;
121 struct curl_slist* headers = NULL;
122 headers = curl_slist_append(headers, ((const string)"Accept: application/vnd.github+json").c_str());
123 headers = curl_slist_append(headers, ("Authorization: token " + githubToken).c_str());
124
125 if (curl) {
126 curl_easy_setopt(curl, CURLOPT_URL, config.githubMarkdownApiUrl.c_str());
127 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleApiCallBack);
128 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlText);
129 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Ahmed-Khaled-dev");
130 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
131
132 json postData;
133 postData["text"] = markdownText;
134 string postDataString = postData.dump();
135 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postDataString.c_str());
136
137 resultCode = curl_easy_perform(curl);
138
139 if (resultCode == CURLE_OK) {
140 // Get the HTTP response code
141 long httpCode;
142 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
143
144 if (httpCode == 200) {
145 return htmlText;
146 }
147 else if (httpCode == 404) {
148 throw runtime_error("Markdown API url not found");
149 }
150 else {
151 handleGithubApiErrorCodes(httpCode, htmlText);
152 }
153 }
154 else {
155 throw runtime_error(config.githubApiUnableToMakeRequestError);
156 }
157
158 curl_easy_cleanup(curl);
159 curl_slist_free_all(headers);
160 }
161 else {
162 throw runtime_error(config.githubApiLibcurlError);
163 }
164
165 return htmlText;
166}
void handleGithubApiErrorCodes(long errorCode, string apiResponse)
Throws runtime exceptions with appropriate messages that describe the given GitHub API error code All...
Definition Utils.cpp:78
size_t handleApiCallBack(char *data, size_t size, size_t numOfBytes, string *buffer)
Callback function that is required to handle API response in libcurl.
Definition Utils.cpp:66
string githubMarkdownApiUrl
Definition Config.h:22
string githubApiUnableToMakeRequestError
Definition Config.h:65
string githubApiLibcurlError
Definition Config.h:66

◆ handleApiCallBack()

size_t handleApiCallBack ( char * data,
size_t size,
size_t numOfBytes,
string * buffer )

Callback function that is required to handle API response in libcurl.

Parameters
dataPointer to the data received
sizeSize of each data element
numOfBytesNumber of bytes received
bufferPointer to the buffer storing the response
Returns
Total size of the received data
66 {
67 size_t totalSize = size * numOfBytes;
68 buffer->append(data, totalSize);
69 return totalSize;
70}

◆ handleGithubApiErrorCodes()

void handleGithubApiErrorCodes ( long errorCode,
string apiResponse )

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.

Parameters
errorCodeThe GitHub API error code that occurred
apiResponseThe GitHub API response
78 {
79 if (errorCode == 429 || errorCode == 403) {
80 throw runtime_error(config.githubApiRateLimitExceededError + apiResponse);
81 }
82 else if (errorCode == 401) {
83 throw runtime_error(config.githubApiUnauthorizedAccessError + apiResponse);
84 }
85 else if (errorCode == 400) {
86 throw runtime_error(config.githubApiBadRequestError + apiResponse);
87 }
88}
string githubApiUnauthorizedAccessError
Definition Config.h:63
string githubApiRateLimitExceededError
Definition Config.h:62
string githubApiBadRequestError
Definition Config.h:64

◆ printInputError()

void printInputError ( InputErrors inputError)

Prints error messages when user runs the script with incorrect parameters/input.

Parameters
inputErrorThe type of input error
27 {
28 if (inputError == InputErrors::NoReleaseNotesSource) {
29 cerr << config.noReleaseNotesSourceError << endl;
30 }
31 else if (inputError == InputErrors::IncorrectReleaseNotesSource) {
33 }
34 else if (inputError == InputErrors::NoReleaseNotesMode) {
35 cerr << config.noReleaseNotesModeError << endl;
36 }
37 else if (inputError == InputErrors::IncorrectReleaseNotesMode) {
39 }
40 else if (inputError == InputErrors::NoGithubToken) {
41 cerr << config.noGithubTokenError << endl;
42 }
43 else if (inputError == InputErrors::NoReleaseStartReference) {
45 }
46 else if (inputError == InputErrors::NoReleaseEndReference) {
47 cerr << config.noReleaseEndReferenceError << endl;
48 }
49 else if (inputError == InputErrors::NoPullRequestNumber) {
50 cerr << config.noPullRequestNumberError << endl;
51 }
52 else if(inputError == InputErrors::NoGithubRepository) {
53 cerr << config.noGithubRepositoryError << endl;
54 }
55 cerr << config.expectedSyntaxMessage << endl;
56}
@ NoReleaseStartReference
@ NoReleaseNotesSource
@ NoReleaseEndReference
@ IncorrectReleaseNotesMode
@ IncorrectReleaseNotesSource
string expectedSyntaxMessage
Definition Config.h:70
string noPullRequestNumberError
Definition Config.h:60
string noReleaseNotesModeError
Definition Config.h:55
string noReleaseStartReferenceError
Definition Config.h:58
string incorrectReleaseNotesModeError
Definition Config.h:56
string incorrectReleaseNotesSourceError
Definition Config.h:54
string noGithubRepositoryError
Definition Config.h:61
string noReleaseEndReferenceError
Definition Config.h:59
string noGithubTokenError
Definition Config.h:57
string noReleaseNotesSourceError
Definition Config.h:53

◆ writeGeneratedNotesInFiles()

void writeGeneratedNotesInFiles ( string markdownGeneratedNotes,
string githubToken )

Writes the generated markdown notes in the markdown file and converts these markdown notes to HTML and writes them in the HTML file.

Parameters
markdownGeneratedNotesThe generated markdown notes to be written
githubTokenThe GitHub token used to make authenticated requests to the GitHub API
174 {
175 ofstream markdownFileOutput(config.markdownOutputFileName);
176
177 if (!markdownFileOutput.is_open()) {
178 throw runtime_error(config.markdownFileError);
179 }
180
181 if (markdownGeneratedNotes.size() == 0) {
182 throw runtime_error(config.emptyReleaseNotesMessage);
183 }
184
185 markdownFileOutput << markdownGeneratedNotes;
186 markdownFileOutput.close();
187
188 ofstream htmlFileOutput(config.htmlOutputFileName);
189
190 if (!htmlFileOutput.is_open()) {
191 throw runtime_error(config.htmlFileError);
192 }
193
194 htmlFileOutput << convertMarkdownToHtml(markdownGeneratedNotes, githubToken);
195}
string convertMarkdownToHtml(string markdownText, string githubToken)
Converts markdown to HTML using the GitHub API markdown endpoint.
Definition Utils.cpp:116
string markdownOutputFileName
Definition Config.h:18
string emptyReleaseNotesMessage
Definition Config.h:73
string htmlOutputFileName
Definition Config.h:19
string htmlFileError
Definition Config.h:69
string markdownFileError
Definition Config.h:68