From 873deafda4d22325216d16e731257e924ae310ae Mon Sep 17 00:00:00 2001 From: James Ross Date: Sat, 20 Jul 2024 00:19:37 +0100 Subject: [PATCH] feat: Improved sorting of pull requests to merge --- GitHub/GraphPullRequest.cs | 1 + GitHub/Query.cs | 1 + Program.cs | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/GitHub/GraphPullRequest.cs b/GitHub/GraphPullRequest.cs index 3277edf..dcb7c8a 100644 --- a/GitHub/GraphPullRequest.cs +++ b/GitHub/GraphPullRequest.cs @@ -10,6 +10,7 @@ class GraphPullRequest public DateTimeOffset CreatedAt; public GraphPullRequestAuthor Author; public GraphPullRequestRef HeadRef; + public bool IsDraft; public GraphPullRequestLabels Labels; } diff --git a/GitHub/Query.cs b/GitHub/Query.cs index 051219f..545fcc2 100644 --- a/GitHub/Query.cs +++ b/GitHub/Query.cs @@ -73,6 +73,7 @@ public async Task> GetOpenPullRequests(string or prefix name } + isDraft labels(first: 100) { nodes { name diff --git a/Program.cs b/Program.cs index 307aca4..b858585 100644 --- a/Program.cs +++ b/Program.cs @@ -63,12 +63,14 @@ static async Task AsyncMain(IConfigurationRoot config) Console.WriteLine($"Open pull requests ({pullRequests.Count}):"); foreach (var pullRequest in pullRequests) { - var autoMerge = - (memberLogins.Contains(pullRequest.Author?.Login) && !pullRequest.Labels.Nodes.Any(label => label.Name == gitHubConfig["excludeLabel"])) - || pullRequest.Labels.Nodes.Any(label => label.Name == gitHubConfig["includeLabel"]); + var isMember = memberLogins.Contains(pullRequest.Author?.Login); + var isIncluded = pullRequest.Labels.Nodes.Any(label => label.Name == gitHubConfig["includeLabel"]); + var isExcluded = pullRequest.Labels.Nodes.Any(label => label.Name == gitHubConfig["excludeLabel"]); + var autoMerge = (isMember && !isExcluded) || isIncluded; Console.WriteLine($" #{pullRequest.Number} {pullRequest.Title}"); Console.WriteLine($" By: {pullRequest.Author?.Login}"); Console.WriteLine($" Branch: {pullRequest.HeadRef?.Name}"); + Console.WriteLine($" Draft: {pullRequest.IsDraft}"); Console.WriteLine($" Labels: {String.Join(' ', pullRequest.Labels.Nodes.Select(label => label.Name))}"); Console.WriteLine($" Allowed to auto-merge? {autoMerge}"); if (autoMerge) @@ -77,6 +79,13 @@ static async Task AsyncMain(IConfigurationRoot config) } } + // Sort pull requests by draft status (non-draft first), inclusion label (present first), and number + autoMergePullRequests = autoMergePullRequests.OrderBy(pullRequest => + { + var isIncluded = pullRequest.Labels.Nodes.Any(label => label.Name == gitHubConfig["includeLabel"]); + return $"{(pullRequest.IsDraft ? "2" : "1")}{(isIncluded ? "1" : "2")}{pullRequest.Number,10}"; + }).ToList(); + Console.WriteLine($"Pull requests suitable for auto-merging ({autoMergePullRequests.Count}):"); foreach (var pullRequest in autoMergePullRequests) {