Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pull request creation #123

Merged
merged 5 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ public async Task WhenNoPullRequestsExistForAStackWithMultipleBranches_CreatesPu
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
Expand All @@ -40,6 +48,7 @@ public async Task WhenNoPullRequestsExistForAStackWithMultipleBranches_CreatesPu
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-3", "branch-1")).Returns("PR Title for branch-3");
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-3")).Returns("PR Title for branch-5");
Expand Down Expand Up @@ -74,11 +83,19 @@ public async Task WhenCreatingPullRequestsForAStackWithMultipleBranches_EachPull
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
Expand All @@ -91,6 +108,7 @@ public async Task WhenCreatingPullRequestsForAStackWithMultipleBranches_EachPull
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-3", "branch-1")).Returns("PR Title for branch-3");
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-3")).Returns("PR Title for branch-5");
Expand Down Expand Up @@ -141,11 +159,19 @@ public async Task WhenAPullRequestExistForABranch_AndNoneForAnotherBranch_Create
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
Expand All @@ -158,6 +184,7 @@ public async Task WhenAPullRequestExistForABranch_AndNoneForAnotherBranch_Create
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-3")).Returns("PR Title for branch-5");
inputProvider.Text(Questions.PullRequestStackDescription, Arg.Any<string>()).Returns("A custom description");
Expand Down Expand Up @@ -207,11 +234,19 @@ public async Task WhenStackNameIsProvided_PullRequestsAreCreatedForThatStack()
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
Expand All @@ -224,6 +259,7 @@ public async Task WhenStackNameIsProvided_PullRequestsAreCreatedForThatStack()
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-3", "branch-1")).Returns("PR Title for branch-3");
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-3")).Returns("PR Title for branch-5");
Expand Down Expand Up @@ -258,18 +294,27 @@ public async Task WhenOnlyOneStackExists_DoesNotAskForStackName_PullRequestsAreC
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
new("Stack1", remoteUri, "branch-1", ["branch-3", "branch-5"])
]);
stackConfig.Load().Returns(stacks);

inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(2)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-3", "branch-1")).Returns("PR Title for branch-3");
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-3")).Returns("PR Title for branch-5");
Expand Down Expand Up @@ -335,11 +380,19 @@ public async Task WhenAPullRequestExistForABranch_AndHasBeenMerged_AndNoneForAno
var handler = new CreatePullRequestsCommandHandler(inputProvider, outputProvider, gitOperations, gitHubOperations, stackConfig);

var remoteUri = Some.HttpsUri().ToString();
outputProvider
.WhenForAnyArgs(o => o.Status(Arg.Any<string>(), Arg.Any<Action>()))
.Do(ci => ci.ArgAt<Action>(1)());

gitOperations.GetRemoteUri().Returns(remoteUri);
gitOperations.GetCurrentBranch().Returns("branch-1");
gitOperations.DoesRemoteBranchExist("branch-3").Returns(true);
gitOperations.DoesRemoteBranchExist("branch-5").Returns(true);
gitOperations
.GetBranchesThatExistInRemote(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

gitOperations
.GetBranchesThatExistLocally(Arg.Any<string[]>())
.Returns(["branch-1", "branch-3", "branch-5"]);

var stacks = new List<Config.Stack>(
[
Expand All @@ -352,6 +405,7 @@ public async Task WhenAPullRequestExistForABranch_AndHasBeenMerged_AndNoneForAno
.Do(ci => stacks = ci.ArgAt<List<Config.Stack>>(0));

inputProvider.Select(Questions.SelectStack, Arg.Any<string[]>()).Returns("Stack1");
inputProvider.Confirm(Questions.ConfirmStartCreatePullRequests(1)).Returns(true);
inputProvider.Confirm(Questions.ConfirmCreatePullRequests).Returns(true);
inputProvider.Text(Questions.PullRequestTitle("branch-5", "branch-1")).Returns("PR Title for branch-5");
inputProvider.Text(Questions.PullRequestStackDescription, Arg.Any<string>()).Returns("A custom description");
Expand Down
6 changes: 4 additions & 2 deletions src/Stack/Commands/Helpers/Questions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Humanizer;
using Stack.Infrastructure;

namespace Stack.Commands.Helpers;
Expand All @@ -16,8 +17,9 @@ public static class Questions
public const string ConfirmAddOrCreateBranch = "Do you want to add an existing branch or create a new branch and add it to the stack?";
public const string AddOrCreateBranch = "Add or create a branch:";
public const string ConfirmSwitchToBranch = "Do you want to switch to the new branch?";
public const string ConfirmCreatePullRequests = "Are you sure you want to create/update pull requests for branches in this stack?";
public static string PullRequestTitle(string sourceBranch, string targetBranch) => $"Pull request title for branch {sourceBranch.Branch()} to {targetBranch.Branch()}:";
public static string ConfirmStartCreatePullRequests(int numberOfBranchesWithoutPullRequests) => $"There {"are".ToQuantity(numberOfBranchesWithoutPullRequests, ShowQuantityAs.None)} {"branch".ToQuantity(numberOfBranchesWithoutPullRequests)} to create pull requests for. Do you want to continue?";
public const string ConfirmCreatePullRequests = "Are you sure you want to create pull requests for branches in this stack?";
public static string PullRequestTitle(string sourceBranch, string targetBranch) => $"Title for pull request from {sourceBranch.Branch()} -> {targetBranch.Branch()}:";
public const string PullRequestStackDescription = "Stack description for pull request:";
public const string OpenPullRequests = "Open the pull requests in the browser?";
}
Loading
Loading