Skip to content

Commit

Permalink
Add stack list to PR description
Browse files Browse the repository at this point in the history
  • Loading branch information
geofflamrock committed Nov 11, 2024
1 parent 7993e1d commit c90f829
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, CreatePullR
if (console.Prompt(new ConfirmationPrompt("Are you sure you want to create pull requests for branches in this stack?")))
{
var sourceBranch = stack.SourceBranch;
var pullRequestsInStack = new List<GitHubPullRequest>();

foreach (var branch in stack.Branches)
{
Expand All @@ -52,18 +53,20 @@ public override async Task<int> ExecuteAsync(CommandContext context, CreatePullR
if (existingPullRequest is not null && existingPullRequest.State != GitHubPullRequestStates.Closed)
{
console.MarkupLine($"Pull request [{existingPullRequest.GetPullRequestColor()} link={existingPullRequest.Url}]#{existingPullRequest.Number}: {existingPullRequest.Title}[/] already exists for branch [blue]{branch}[/] to [blue]{sourceBranch}[/]. Skipping...");
pullRequestsInStack.Add(existingPullRequest);
}
else
{
if (gitOperations.DoesRemoteBranchExist(branch, settings.GetGitOperationSettings()))
{
var prTitle = console.Prompt(new TextPrompt<string>($"Pull request title for branch [blue]{branch}[/] to [blue]{sourceBranch}[/]:"));
console.MarkupLine($"Creating pull request for branch [blue]{branch}[/] to [blue]{sourceBranch}[/]");
var pullRequest = gitHubOperations.CreatePullRequest(branch, sourceBranch, prTitle, "test", settings.GetGitHubOperationSettings());
var pullRequest = gitHubOperations.CreatePullRequest(branch, sourceBranch, prTitle, "", settings.GetGitHubOperationSettings());

if (pullRequest is not null)
{
console.MarkupLine($"Pull request [{pullRequest.GetPullRequestColor()} link={pullRequest.Url}]#{pullRequest.Number}: {pullRequest.Title}[/] created for branch [blue]{branch}[/] to [blue]{sourceBranch}[/]");
pullRequestsInStack.Add(pullRequest);
}

sourceBranch = branch;
Expand All @@ -75,6 +78,44 @@ public override async Task<int> ExecuteAsync(CommandContext context, CreatePullR
}
}
}

// Edit each PR and add to the top of the description
// the details of each PR in the stack
var stackMarkerStart = "<!-- stack-pr-list -->";
var stackMarkerEnd = "<!-- /stack-pr-list -->";
var prList = pullRequestsInStack
.Select(pr => $"- {pr.Url}")
.ToList();
var prListMarkdown = string.Join("\n", prList);
var prListHeader = $"This PR is part of a stack **{stack.Name}**:";
var prBodyMarkdown = $"{stackMarkerStart}\n{prListHeader}\n\n{prListMarkdown}\n{stackMarkerEnd}";

foreach (var pullRequest in pullRequestsInStack)
{
// Find the existing part of the PR body that has the PR list
// and replace it with the updated PR list
var prBody = pullRequest.Body;
console.WriteLine(prBody);

var prListStart = prBody.IndexOf(stackMarkerStart, StringComparison.OrdinalIgnoreCase);
var prListEnd = prBody.IndexOf(stackMarkerEnd, StringComparison.OrdinalIgnoreCase);

console.WriteLine($"{prListStart} {prListEnd}");

if (prListStart >= 0 && prListEnd >= 0)
{
prBody = prBody.Remove(prListStart, prListEnd - prListStart + stackMarkerEnd.Length);
}

if (prListStart == -1)
{
prListStart = 0;
}

prBody = prBody.Insert(prListStart, prBodyMarkdown);

gitHubOperations.EditPullRequest(pullRequest.Number, prBody, settings.GetGitHubOperationSettings());
}
}

return 0;
Expand Down
10 changes: 8 additions & 2 deletions src/Stack/Git/GitHubOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class GitHubPullRequestStates
public const string Merged = "MERGED";
}

internal record GitHubPullRequest(int Number, string Title, string State, Uri Url);
internal record GitHubPullRequest(int Number, string Title, string Body, string State, Uri Url);

internal static class GitHubPullRequestExtensionMethods
{
Expand All @@ -36,13 +36,14 @@ internal interface IGitHubOperations
{
GitHubPullRequest? GetPullRequest(string branch, GitHubOperationSettings settings);
GitHubPullRequest? CreatePullRequest(string headBranch, string baseBranch, string title, string body, GitHubOperationSettings settings);
void EditPullRequest(int number, string body, GitHubOperationSettings settings);
}

internal class GitHubOperations(IAnsiConsole console) : IGitHubOperations
{
public GitHubPullRequest? GetPullRequest(string branch, GitHubOperationSettings settings)
{
var output = ExecuteGitHubCommandAndReturnOutput($"pr list --json title,number,state,url --head {branch} --state all", settings);
var output = ExecuteGitHubCommandAndReturnOutput($"pr list --json title,number,body,state,url --head {branch} --state all", settings);
var pullRequests = System.Text.Json.JsonSerializer.Deserialize<List<GitHubPullRequest>>(output,
new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web))!;

Expand All @@ -61,6 +62,11 @@ internal class GitHubOperations(IAnsiConsole console) : IGitHubOperations
return GetPullRequest(headBranch, settings);
}

public void EditPullRequest(int number, string body, GitHubOperationSettings settings)
{
ExecuteGitHubCommand($"pr edit {number} --body \"{body}\"", settings);
}

private string ExecuteGitHubCommandAndReturnOutput(string command, GitHubOperationSettings settings)
{
if (settings.Verbose)
Expand Down

0 comments on commit c90f829

Please sign in to comment.