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

feat: Improved sorting of pull requests to merge #43

Merged
merged 1 commit into from
Jul 20, 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
1 change: 1 addition & 0 deletions GitHub/GraphPullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@
{
class GraphPullRequest
{
public Uri Url;

Check warning on line 7 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequest.Url' is never assigned to, and will always have its default value null
public int Number;
public string Title;

Check warning on line 9 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequest.Title' is never assigned to, and will always have its default value null
public DateTimeOffset CreatedAt;
public GraphPullRequestAuthor Author;

Check warning on line 11 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequest.Author' is never assigned to, and will always have its default value null
public GraphPullRequestRef HeadRef;
public bool IsDraft;

Check warning on line 13 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequest.IsDraft' is never assigned to, and will always have its default value false
public GraphPullRequestLabels Labels;

Check warning on line 14 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequest.Labels' is never assigned to, and will always have its default value null
}

class GraphPullRequestAuthor
{
public Uri Url;
public string Login;

Check warning on line 20 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequestAuthor.Login' is never assigned to, and will always have its default value null
}

class GraphPullRequestRef
{
public string Prefix;

Check warning on line 25 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequestRef.Prefix' is never assigned to, and will always have its default value null
public string Name;
}

class GraphPullRequestLabels
{
public GraphPullRequestLabelNode[] Nodes;

Check warning on line 31 in GitHub/GraphPullRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Field 'GraphPullRequestLabels.Nodes' is never assigned to, and will always have its default value null
}

class GraphPullRequestLabelNode
Expand Down
1 change: 1 addition & 0 deletions GitHub/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public async Task<IReadOnlyList<GraphPullRequest>> GetOpenPullRequests(string or
prefix
name
}
isDraft
labels(first: 100) {
nodes {
name
Expand Down
15 changes: 12 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
Loading