Release Notes Manager
Loading...
Searching...
No Matches
Format.cpp File Reference

This file implements functions in Format.h. More...

#include <string>
#include <regex>
#include "Format.h"
#include "Config.h"
#include "Enums.h"
#include "Utils.h"

Functions

string indentAllLinesInString (string s)
 Indents (puts 4 spaces) before all lines in a string.
 
string replaceHashIdsWithLinks (string pullRequestBody)
 Replaces all plain text hash ids (issue ids and pull request ids (#2777)) with links to these issues/pull requests on GitHub.
 
string replaceCommitShasWithLinks (string pullRequestBody)
 Replaces all plain text commit SHAs (e.g., 219c2149) with links to these commits on GitHub.
 
string removeExtraNewLines (string pullRequestBody)
 Remove extra new lines in retrieved PR description to make it look identical to the PR description on GitHub.
 
string formatPullRequestBody (string pullRequestBody)
 Makes the formatting of the retrieved PR body look like the PR on GitHub.
 
string convertConventionalCommitTitleToReleaseNoteTitle (string conventionalCommitTitle, CommitTypeMatchResults matchResult, string markdownPrefix)
 Converts the given conventional commit title to a better markdown title that could be used in the release notes Example: "fix: fixed bug X" gets converted to "### Fixed bug X".
 

Variables

Config config
 

Detailed Description

This file implements functions in Format.h.

Author
Ahmed Khaled

Function Documentation

◆ convertConventionalCommitTitleToReleaseNoteTitle()

string convertConventionalCommitTitleToReleaseNoteTitle ( string conventionalCommitTitle,
CommitTypeMatchResults matchResult,
string markdownPrefix )

Converts the given conventional commit title to a better markdown title that could be used in the release notes Example: "fix: fixed bug X" gets converted to "### Fixed bug X".

Parameters
conventionalCommitTitleThe conventional commit title
matchResultCommitTypeMatchResult that this title got with it's conventional commit type (has subcategory or no)
markdownPrefixThe markdown prefix (e.g. -, ##, ###, etc.) that should be added before the release note title
Returns
The improved markdown title
140 {
141 string subCategoryText = "";
142
144 size_t startPos = conventionalCommitTitle.find("(") + 1;
145 // Adding the subcategory title
146 subCategoryText = "(" + conventionalCommitTitle.substr(startPos, conventionalCommitTitle.find(")") - startPos) + " Related) ";
147 // Capitalizing the first letter in the subcategory
148 subCategoryText[1] = toupper(subCategoryText[1]);
149 }
150
151 string releaseNoteTitle = "";
152
153 // Removing the commit type from the conventional commit title and capitalizing the first letter
154 size_t colonPosition = conventionalCommitTitle.find(":");
155 if (colonPosition != string::npos) {
156 releaseNoteTitle = conventionalCommitTitle.substr(colonPosition + 2);
157 }
158 releaseNoteTitle[0] = toupper(releaseNoteTitle[0]);
159
160 // Inserting the commit type subcategory (will be empty if there is no subcategory)
161 releaseNoteTitle.insert(0, subCategoryText);
162
163 // Adding the markdown prefix
164 releaseNoteTitle = markdownPrefix + releaseNoteTitle + "\n";
165
166 return releaseNoteTitle;
167}

◆ formatPullRequestBody()

string formatPullRequestBody ( string pullRequestBody)

Makes the formatting of the retrieved PR body look like the PR on GitHub.

Parameters
pullRequestBodyThe original body/description of the retrieved PR
Returns
PR body/description after formatting it
123 {
124 pullRequestBody = replaceHashIdsWithLinks(pullRequestBody);
125 pullRequestBody = replaceCommitShasWithLinks(pullRequestBody);
126 pullRequestBody = removeExtraNewLines(pullRequestBody);
127
128 return pullRequestBody;
129}
string replaceHashIdsWithLinks(string pullRequestBody)
Replaces all plain text hash ids (issue ids and pull request ids (#2777)) with links to these issues/...
Definition Format.cpp:44
string replaceCommitShasWithLinks(string pullRequestBody)
Replaces all plain text commit SHAs (e.g., 219c2149) with links to these commits on GitHub.
Definition Format.cpp:75
string removeExtraNewLines(string pullRequestBody)
Remove extra new lines in retrieved PR description to make it look identical to the PR description on...
Definition Format.cpp:106

◆ indentAllLinesInString()

string indentAllLinesInString ( string s)

Indents (puts 4 spaces) before all lines in a string.

Parameters
sThe input string
Returns
The indented string
24 {
25 bool isNewLine = 1;
26 string result;
27 for (char c : s) {
28 if (isNewLine) {
29 result += " ";
30 }
31 result += c;
32
33 isNewLine = (c == '\n');
34 }
35
36 return result;
37}

◆ removeExtraNewLines()

string removeExtraNewLines ( string pullRequestBody)

Remove extra new lines in retrieved PR description to make it look identical to the PR description on GitHub.

Parameters
pullRequestBodyThe original body/description of the retrieved PR
Returns
PR body/description after removing extra new lines
106 {
107 // New lines in the retrieved PR description are represented as "\r\n" and there is no problem with that
108 // BUT after the script writes the retrieved PR description in the markdown file and I observed the contents of the markdown file
109 // using a hexadecimal editor, I found out that for some reason an extra "\r" was added when the markdown was written, so new lines were "\r\r\n"
110 // for some reason that created extra new lines, so when I tried removing this extra "\r" and I added 2 spaces before the new line " \r\n"
111 // (these 2 spaces in markdown specify that a new line should occur), it worked!
112 regex pattern(R"(\r)");
113 pullRequestBody = regex_replace(pullRequestBody, pattern, " ");
114
115 return pullRequestBody;
116}

◆ replaceCommitShasWithLinks()

string replaceCommitShasWithLinks ( string pullRequestBody)

Replaces all plain text commit SHAs (e.g., 219c2149) with links to these commits on GitHub.

Parameters
pullRequestBodyThe original body/description of the pull request to do the replacements on
Returns
Pull request body/description after performing the replacements
75 {
76 // Here I match any commit SHA that starts at the beginning of a line or with a space or "(" before it
77 // and ends with either anything other than a number or a letter or the end of the pull request body
78 // the "?=" is a regex lookahead which detects this pattern but doesn't include it in the match
79 // I am using this specific regex pattern based on how GitHub markdown interprets commit SHAs
80 regex commitShaPattern(R"((^|[ (])([0-9a-f]{6,40})(?=[^0-9a-zA-Z]|$))");
81
82 string result = pullRequestBody;
83 size_t numberOfNewCharactersAdded = 0;
84 sregex_iterator firstSha(pullRequestBody.begin(), pullRequestBody.end(), commitShaPattern);
85 sregex_iterator lastSha;
86
87 for (sregex_iterator i = firstSha; i != lastSha; i++)
88 {
89 smatch currentShaMatch = *i;
90 // Here I have the number "2" as a parameter in the below functions since I am interested in the second capture group
91 // meaning the second "()" in the regex pattern which is the commit SHA itself
92 string currentSha = currentShaMatch.str(2);
93 result.erase(currentShaMatch.position(2) + numberOfNewCharactersAdded, currentShaMatch.length(2));
94 result.insert(currentShaMatch.position(2) + numberOfNewCharactersAdded, "[" + currentSha.substr(0, 6) + "](" + config.repoCommitsUrl + currentSha + ")");
95 numberOfNewCharactersAdded += 4 + config.repoCommitsUrl.length() + 6;
96 }
97
98 return result;
99}
Config config
Definition Main.cpp:40
string repoCommitsUrl
Definition Config.h:24

◆ replaceHashIdsWithLinks()

string replaceHashIdsWithLinks ( string pullRequestBody)

Replaces all plain text hash ids (issue ids and pull request ids (#2777)) with links to these issues/pull requests on GitHub.

Parameters
pullRequestBodyThe original body/description of the pull request to do the replacements on
Returns
Pull request body/description after performing the replacements
44 {
45 // The first brackets are not considered a capture group, they are a must when adding "R" to define this as a *raw string literal*
46 // We add this "R" to write regex patterns easier and increase readability by not needing to escape back slashes
47 regex hashIdPattern(R"(#(\d+))");
48
49 string result = pullRequestBody;
50 size_t numberOfNewCharactersAdded = 0;
51 sregex_iterator firstHashId(pullRequestBody.begin(), pullRequestBody.end(), hashIdPattern);
52 // This is an end of sequence iterator
53 sregex_iterator lastHashId;
54
55 for (sregex_iterator i = firstHashId; i != lastHashId; i++) {
56 smatch currentHashIdMatch = *i;
57 // I get from my current match the first capture group (brackets) of my pattern (\d+) which only contains the numerical id in the hash id
58 string currentNumericId = currentHashIdMatch.str(1);
59 // I remove the old hash id and create a new markdown link using it and insert the new link in its place
60 result.erase(currentHashIdMatch.position() + numberOfNewCharactersAdded, currentHashIdMatch.length());
61 result.insert(currentHashIdMatch.position() + numberOfNewCharactersAdded, "[#" + currentNumericId + "](" + config.repoIssuesUrl + currentNumericId + ")");
62 // Regex smatch.position() was assigned before we replaced hash ids with urls
63 // So we must account for that by counting number of new characters we have added, "4" is for the characters "[]()"
64 numberOfNewCharactersAdded += 4 + config.repoIssuesUrl.length() + currentNumericId.length();
65 }
66
67 return result;
68};
string repoIssuesUrl
Definition Config.h:23

Variable Documentation

◆ config

Config config
extern