Skip to content

Commit 3d87dac

Browse files
CopilotMalcolmnixon
andcommitted
Use GitHub API to get PR linked issues instead of parsing body
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent ee810f4 commit 3d87dac

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/DemaConsulting.BuildMark/RepoConnectors/GitHubRepoConnector.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,19 @@ public override async Task<List<string>> GetPullRequestsBetweenTagsAsync(Version
209209
/// <returns>List of issue IDs.</returns>
210210
public override async Task<List<string>> GetIssuesForPullRequestAsync(string pullRequestId)
211211
{
212-
// Validate and fetch PR body using GitHub CLI
213-
// Arguments: --json body (get body field), --jq .body (extract body value)
214-
// Output: raw PR description text which may contain issue references
212+
// Use GitHub API to get issues that are actually linked to close when PR merges
213+
// This is more reliable than parsing PR body text which could contain any #numbers
214+
// Arguments: --json closingIssuesReferences (get linked issues), --jq to extract numbers
215+
// Output: issue numbers (one per line)
215216
var validatedId = ValidateId(pullRequestId, nameof(pullRequestId));
216-
var output = await RunCommandAsync("gh", $"pr view {validatedId} --json body --jq .body");
217+
var output = await RunCommandAsync("gh", $"pr view {validatedId} --json closingIssuesReferences --jq .closingIssuesReferences[].number");
217218

218-
// Extract issue references (e.g., #123, #456) from PR body text
219-
var issues = new List<string>();
220-
var regex = NumberReferenceRegex();
221-
222-
foreach (Match match in regex.Matches(output))
223-
{
224-
issues.Add(match.Groups[1].Value);
225-
}
219+
// Parse output to get issue numbers
220+
var issues = output
221+
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
222+
.Select(n => n.Trim())
223+
.Where(n => !string.IsNullOrEmpty(n))
224+
.ToList();
226225

227226
return issues;
228227
}
@@ -326,11 +325,4 @@ public override async Task<List<string>> GetOpenIssuesAsync()
326325
/// <returns>Compiled regular expression.</returns>
327326
[GeneratedRegex(@"^\d+$", RegexOptions.Compiled)]
328327
private static partial Regex NumericIdRegex();
329-
330-
/// <summary>
331-
/// Regular expression to match number references (#123).
332-
/// </summary>
333-
/// <returns>Compiled regular expression.</returns>
334-
[GeneratedRegex(@"#(\d+)", RegexOptions.Compiled)]
335-
private static partial Regex NumberReferenceRegex();
336328
}

test/DemaConsulting.BuildMark.Tests/GitHubRepoConnectorTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ public async Task GitHubRepoConnector_GetIssuesForPullRequestAsync_ReturnsExpect
370370
var connector = new TestableGitHubRepoConnector();
371371
connector.AddCommandResult(
372372
"gh",
373-
"pr view 10 --json body --jq .body",
374-
"This PR fixes #123 and resolves #456");
373+
"pr view 10 --json closingIssuesReferences --jq .closingIssuesReferences[].number",
374+
"123\n456");
375375

376376
// Act
377377
var issues = await connector.GetIssuesForPullRequestAsync("10");
@@ -392,8 +392,8 @@ public async Task GitHubRepoConnector_GetIssuesForPullRequestAsync_ReturnsEmptyW
392392
var connector = new TestableGitHubRepoConnector();
393393
connector.AddCommandResult(
394394
"gh",
395-
"pr view 10 --json body --jq .body",
396-
"This PR has no issue references");
395+
"pr view 10 --json closingIssuesReferences --jq .closingIssuesReferences[].number",
396+
"");
397397

398398
// Act
399399
var issues = await connector.GetIssuesForPullRequestAsync("10");

0 commit comments

Comments
 (0)