Skip to content

Commit b0fd549

Browse files
committed
feat: Squash PR merges into one merge on a branch
1 parent 5ce085b commit b0fd549

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Git/Project.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Text;
67

78
namespace Open_Rails_Code_Bot.Git
@@ -40,6 +41,25 @@ public string ParseRef(string reference)
4041
throw new ApplicationException("Unable to find ref");
4142
}
4243

44+
public string Describe(string options)
45+
{
46+
foreach (var line in GetCommandOutput($"describe {options}"))
47+
{
48+
return line;
49+
}
50+
throw new ApplicationException("Unable to describe commit");
51+
}
52+
53+
public string CommitTree(string treeRef, IEnumerable<string> parentRefs, string message)
54+
{
55+
var parents = String.Join(" ", parentRefs.Select(parentRef => $"-p {parentRef}"));
56+
foreach (var line in GetCommandOutput($"commit-tree {treeRef} {parents} -m {message}"))
57+
{
58+
return line;
59+
}
60+
throw new ApplicationException("Unable to describe commit");
61+
}
62+
4363
public void CheckoutDetached(string reference)
4464
{
4565
RunCommand($"checkout --quiet --detach {reference}");
@@ -55,6 +75,11 @@ public void Merge(string reference)
5575
RunCommand($"merge --no-edit --no-ff {reference}");
5676
}
5777

78+
public void SetBranchRef(string branch, string reference)
79+
{
80+
RunCommand($"branch -f {branch} {reference}");
81+
}
82+
5883
void RunCommand(string command)
5984
{
6085
foreach (var line in GetCommandOutput(command))

Program.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ static async Task AsyncMain(IConfigurationRoot config)
4646
Console.WriteLine($"GitHub organisation: {gitHubConfig["organization"]}");
4747
Console.WriteLine($"GitHub team: {gitHubConfig["team"]}");
4848
Console.WriteLine($"GitHub repository: {gitHubConfig["repository"]}");
49+
Console.WriteLine($"GitHub base branch: {gitHubConfig["baseBranch"]}");
50+
Console.WriteLine($"GitHub new branch: {gitHubConfig["branch"]}");
4951

5052
var members = await query.GetTeamMembers(gitHubConfig["organization"], gitHubConfig["team"]);
5153
Console.WriteLine($"Team members ({members.Count}):");
@@ -83,8 +85,13 @@ static async Task AsyncMain(IConfigurationRoot config)
8385
git.Init($"https://github.com/{gitHubConfig["organization"]}/{gitHubConfig["repository"]}.git");
8486
git.Fetch();
8587
git.ResetHard();
86-
var baseCommit = git.ParseRef("master");
87-
git.CheckoutDetached(baseCommit);
88+
var baseBranchCommit = git.ParseRef(gitHubConfig["baseBranch"]);
89+
var mergeBranchCommit = git.ParseRef(gitHubConfig["branch"]);
90+
git.CheckoutDetached(baseBranchCommit);
91+
var baseBranchVersion = String.Format(gitHubConfig["versionFormat"] ?? "{0}", git.Describe(gitHubConfig["versionDescribeOptions"] ?? ""));
92+
var mergeBranchParents = new List<string>();
93+
mergeBranchParents.Add(baseBranchCommit);
94+
mergeBranchParents.Add(mergeBranchCommit);
8895
var autoMergePullRequestsSuccess = new List<GraphPullRequest>();
8996
var autoMergePullRequestsFailure = new List<GraphPullRequest>();
9097
foreach (var pullRequest in autoMergePullRequests)
@@ -93,6 +100,7 @@ static async Task AsyncMain(IConfigurationRoot config)
93100
try
94101
{
95102
git.Merge(mergeCommit);
103+
mergeBranchParents.Add(mergeCommit);
96104
autoMergePullRequestsSuccess.Add(pullRequest);
97105
}
98106
catch (ApplicationException)
@@ -101,7 +109,12 @@ static async Task AsyncMain(IConfigurationRoot config)
101109
git.ResetHard();
102110
}
103111
}
104-
Console.WriteLine($"Final commit: {git.ParseRef("HEAD")}");
112+
var mergedCommit = git.ParseRef("HEAD");
113+
var newMergeBranchCommit = git.CommitTree($"{mergedCommit}^{{tree}}", mergeBranchParents, "Auto-merge");
114+
git.SetBranchRef(gitHubConfig["branch"], newMergeBranchCommit);
115+
Console.WriteLine($"Base branch commit: {baseBranchCommit}");
116+
Console.WriteLine($"Base branch version: {baseBranchVersion}");
117+
Console.WriteLine($"Merge branch commit: {newMergeBranchCommit}");
105118

106119
Console.WriteLine($"Pull requests successfully auto-merged ({autoMergePullRequestsSuccess.Count}):");
107120
foreach (var pullRequest in autoMergePullRequestsSuccess)

0 commit comments

Comments
 (0)