diff --git a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs index 7131919..20e49a4 100644 --- a/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs +++ b/src/Stack/Commands/PullRequests/CreatePullRequestsCommand.cs @@ -44,6 +44,7 @@ public override async Task 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(); foreach (var branch in stack.Branches) { @@ -52,6 +53,7 @@ public override async Task 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 { @@ -59,11 +61,12 @@ public override async Task ExecuteAsync(CommandContext context, CreatePullR { var prTitle = console.Prompt(new TextPrompt($"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; @@ -75,6 +78,44 @@ public override async Task 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 = ""; + var stackMarkerEnd = ""; + 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; diff --git a/src/Stack/Git/GitHubOperations.cs b/src/Stack/Git/GitHubOperations.cs index 9d7d93b..e994b50 100644 --- a/src/Stack/Git/GitHubOperations.cs +++ b/src/Stack/Git/GitHubOperations.cs @@ -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 { @@ -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>(output, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web))!; @@ -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)