Skip to content

Commit dd5c2e9

Browse files
committed
[github-nfbot] Improve PR content checks
- Now looking for motivation and context title. - Improved validation of linked issues to make sure devs are following the correct pattern.
1 parent 1a1323e commit dd5c2e9

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

github-nfbot/GitHub-nfbot/GitHub_nfbot.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static class GitHub_nfbot
6262

6363
// strings for PR content
6464
private const string _prDescription = "## Description";
65+
private const string _prMotivationAndContext = "## Motivation and Context";
6566
private const string _prTypesOfChanges = "## Types of changes";
6667
private const string _prChecklist = "## Checklist";
6768

@@ -1394,7 +1395,7 @@ private static async Task ManageLabelsAsync(Octokit.PullRequest pr, ILogger log)
13941395

13951396
private static async Task<bool> CheckLinkedIssuesAsync(Octokit.PullRequest pr, ILogger log)
13961397
{
1397-
string commentContent;
1398+
string commentContent = string.Empty;
13981399

13991400
// check for invalid link to issues
14001401
if (pr.Body.Contains("- Fixes/Closes/Resolves nanoFramework/Home#NNNN"))
@@ -1405,17 +1406,31 @@ private static async Task<bool> CheckLinkedIssuesAsync(Octokit.PullRequest pr, I
14051406
{
14061407
commentContent = "🤪 You have to make up your mind on how this PR addresses the issue. It either **fixes**, **closes** or **resolves** it. Can't have them all...";
14071408
}
1408-
// TODO replace this with a regex
1409-
//else if ( ( prBody.Contains("- Fixes") ||
1410-
// prBody.Contains("- Closes") ||
1411-
// prBody.Contains("- Resolves")) &&
1412-
// prBody.Contains(" #"))
1413-
//{
1414-
// commentContent = ":disappointed: All our issues are tracked in Home repo. If this PR addresses an issue, make sure the reference to it follows the correct pattern: `nanoFramework/Home#NNNN`.";
1415-
//}
14161409
else
14171410
{
1418-
return true;
1411+
// Define the regex pattern to match the GitHub link verbs and the pattern KEYWORD #NNNN
1412+
string pattern = @"\b(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s+#\d+\b";
1413+
string validPattern = @"\b(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s+nanoFramework/Home#\d+\b";
1414+
1415+
// Find all matches of the invalid pattern
1416+
var matches = Regex.Matches(pr.Body, pattern);
1417+
1418+
foreach (Match match in matches)
1419+
{
1420+
// Check if the match is not a valid pattern
1421+
if (!Regex.IsMatch(match.Value, validPattern))
1422+
{
1423+
commentContent = ":disappointed: All our issues are tracked in Home repo. If this PR addresses an issue, make sure the reference to it follows the correct pattern: `nanoFramework/Home#NNNN`.";
1424+
break;
1425+
}
1426+
}
1427+
1428+
// any comment content?
1429+
if (string.IsNullOrEmpty(commentContent))
1430+
{
1431+
// got here, so all is good
1432+
return true;
1433+
}
14191434
}
14201435

14211436
await _octokitClient.Issue.Comment.Create(pr.Base.Repository.Id, pr.Number, $"Hi @{pr.User.Login},\r\n\r\n{commentContent}.{_fixRequestTagComment}");
@@ -1469,6 +1484,7 @@ private static async Task<bool> ValidatePRContentAsync(dynamic payload, ILogger
14691484
// check for master PR template
14701485
if (
14711486
prBody.Contains(_prDescription) &&
1487+
prBody.Contains(_prMotivationAndContext) &&
14721488
prBody.Contains(_prTypesOfChanges) &&
14731489
prBody.Contains(_prChecklist))
14741490
{

0 commit comments

Comments
 (0)