-
Notifications
You must be signed in to change notification settings - Fork 0
/
IGitFlowWithPullRequest.cs
147 lines (128 loc) · 5.78 KB
/
IGitFlowWithPullRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Candoumbe.Pipelines.Components.Workflows;
using Nuke.Common.Git;
using Nuke.Common.Tools.GitHub;
using Octokit;
using static Nuke.Common.Tools.Git.GitTasks;
using static Nuke.Common.Tools.GitHub.GitHubTasks;
using static Nuke.Common.Utilities.ConsoleUtility;
using static Serilog.Log;
namespace Candoumbe.Pipelines.Components.GitHub;
/// <summary>
/// An <see cref="IWorkflow"/> implementation that opens a github pull request
/// </summary>
public interface IGitFlowWithPullRequest : IGitFlow, IPullRequest
{
string IPullRequest.Title => TryGetValue(() => Title) ?? ((GitRepository.IsOnFeatureBranch(), GitRepository.IsOnReleaseBranch(), GitRepository.IsOnHotfixBranch(), GitRepository.Branch.Like($"{ColdfixBranchPrefix}/*")) switch
{
(true, _, _, _) => $"✨ {GitRepository.Branch?.Replace($"{FeatureBranchPrefix}/", string.Empty).ToTitleCase()}",
(_, _, true, _) => $"🛠️ {GitRepository.Branch?.Replace($"{HotfixBranchPrefix}/", string.Empty).ToTitleCase()}",
(_, _, _, true) => $"🧹 {GitRepository.Branch?.Replace($"{ColdfixBranchPrefix}/", string.Empty).ToTitleCase()}",
_ => $"💪🏾 {GitRepository.Branch?.ToTitleCase()}"
}).Replace('-', ' ');
///<inheritdoc/>
async ValueTask IDoFeatureWorkflow.FinishFeature()
{
string linkToIssueKeyWord = Issues.AtLeastOnce()
? string.Join(',', Issues.Select(issueNumber => $"Resolves #{issueNumber}").ToArray())
: null;
// Push to the remote branch
GitPushToRemote();
string repositoryName = GitRepository.GetGitHubName();
string branchName = GitCurrentBranch();
string owner = GitRepository.GetGitHubOwner();
Information("Creating a pull request for {Repository}", repositoryName);
Information(@"Title of the pull request (or ""{PullRequestName}"" if empty)", Title);
string title = Title;
string token = GitHubToken;
if (!SkipConfirmation)
{
title = (Console.ReadLine()) switch
{
{ } value when !string.IsNullOrWhiteSpace(value) => value.Trim(),
_ => Title
};
token ??= PromptForInput("Token (leave empty to exit)", string.Empty);
}
Information("Creating {PullRequestName} for {Repository}", title, repositoryName);
if (!string.IsNullOrWhiteSpace(token))
{
Information("{SourceBranch} ==> {TargetBranch}", branchName, FeatureBranchSourceName);
GitHubClient gitHubClient = new(new ProductHeaderValue(repositoryName))
{
Credentials = new Credentials(token)
};
NewPullRequest newPullRequest = new(title, branchName, FeatureBranchSourceName)
{
Draft = Draft,
Body = linkToIssueKeyWord is not null
? $"{Description}{Environment.NewLine}{Environment.NewLine}{linkToIssueKeyWord}"
: Description
};
PullRequest pullRequest = await gitHubClient.PullRequest.Create(owner, repositoryName, newPullRequest);
if (SkipConfirmation)
{
DeleteLocalBranchIf(DeleteLocalOnSuccess
&& PromptForChoice("Delete branch {BranchName} ? (Y/N)", BuildChoices()) == ConsoleKey.Y, branchName, switchToBranchName: FeatureBranchSourceName);
}
else
{
DeleteLocalBranchIf(DeleteLocalOnSuccess, branchName, switchToBranchName: FeatureBranchSourceName);
}
Information("PR {PullRequestUrl} created successfully", pullRequest.HtmlUrl);
OpenUrl(pullRequest.HtmlUrl);
}
return;
static void DeleteLocalBranchIf(in bool condition, in string branchName, in string switchToBranchName)
{
if (!condition)
{
return;
}
Git($"switch {switchToBranchName}");
Git($"branch -D {branchName}");
}
static (ConsoleKey key, string description)[] BuildChoices() => new[]
{
(key: ConsoleKey.Y, "Delete the local branch"),
(key: ConsoleKey.N, "Keep the local branch"),
};
static void GitPushToRemote()
{
Git($"push origin --set-upstream {GitCurrentBranch()}");
}
static void OpenUrl(string url)
{
try
{
Process.Start(url);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
}
}