-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIGitHubFlowWithPullRequest.cs
More file actions
172 lines (150 loc) · 6.47 KB
/
IGitHubFlowWithPullRequest.cs
File metadata and controls
172 lines (150 loc) · 6.47 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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;
using Nuke.Common.Git;
using Nuke.Common.Tools.GitHub;
using Octokit;
using static Nuke.Common.Tools.Git.GitTasks;
using static Nuke.Common.Utilities.ConsoleUtility;
using static Serilog.Log;
namespace Candoumbe.Pipelines.Components.GitHub
{
/// <summary>
/// This interface adds a target to open a pull request
/// </summary>
public interface IGitHubFlowWithPullRequest : IGitHubFlow, IPullRequest
{
/// <summary>
/// The title of the PR that will be created
/// </summary>
[Parameter("Title that will be used when creating a PR")]
string IPullRequest.Title => TryGetValue(() => Title) ?? ((GitRepository.IsOnFeatureBranch(), GitRepository.IsOnReleaseBranch(), GitRepository.IsOnHotfixBranch()) switch
{
(true, _, _) => $"✨ {GitRepository.Branch?.Replace($"{FeatureBranchPrefix}/", string.Empty).ToTitleCase()}",
(_, _, true) => $"🛠️ {GitRepository.Branch?.Replace($"{HotfixBranchPrefix}/", string.Empty).ToTitleCase()}",
_ => $"💪🏾 {GitRepository.Branch?.ToTitleCase()}"
}).Replace('-', ' ');
/// <summary>
/// Token that will be used to connect to GitHub
/// </summary>
[Parameter("Token used to create a pull request")]
[Secret]
string Token => TryGetValue(() => Token);
/// <summary>
/// Description of the pull request
/// </summary>
[Parameter("Description of the pull request")]
string IPullRequest.Description => TryGetValue(() => Description) ?? this.As<IHaveChangeLog>()?.ReleaseNotes;
///<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);
string title = Title;
string token = GitHubToken;
if (!SkipConfirmation)
{
Information(@"Title of the pull request (or ""{PullRequestName}"" if empty)", Title);
title = (Console.ReadLine()) switch
{
{ } value when !string.IsNullOrWhiteSpace(value) => value.Trim(),
_ => Title
};
token ??= PromptForInput("Token (leave empty to exit)", string.Empty);
}
else
{
Information(@"Title of the pull request : {PullRequestName}", Title);
}
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() =>
[
(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;
}
}
}
}
/// <inheritdoc />
async ValueTask IDoChoreWorkflow.FinishChore() => await FinishFeature();
}
}