This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support specifying threshold for open PRs (#1043)
Before, NuKeeper would only generate a single pull request as it would reselect the same updateset every single time, unless some outside factors would influence the result of its prioritization algorithm. Now, you can specify the max number of open pull requests on a per-repository basis. This is currently only supported for Azure Devops/TFS, however only Azure Devops Server has been tested. Since there are no straightforward APIs for figuring out the number of open pull requests, especially when using a PAT with `Code (Read & Write)`, heuristics are used to figure out the number of open pull requests as best as possible. First, the current user is fetched. If this fails, a user by the name of `[email protected]` will be fetched. If this fails, all pull requests in the repository will be considered that have the label `nukeeper`. If none of these things were possible, it's assumed that there are 0 open pull requests. When the parameter `--consolidate` is specified, the default value for `--maxopenpullrequests` is 1, otherwise it is `maxpackageupdates`.
- Loading branch information
1 parent
4c71eeb
commit 18d6cdb
Showing
23 changed files
with
823 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ namespace NuKeeper.Abstractions.CollaborationModels | |
{ | ||
public class User | ||
{ | ||
public static readonly User Default = new User("[email protected]", "", ""); | ||
|
||
public User(string login, string name, string email) | ||
{ | ||
Login = login; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using NuKeeper.Abstractions; | ||
using NuKeeper.Abstractions.CollaborationModels; | ||
using NuKeeper.Abstractions.CollaborationPlatform; | ||
using NuKeeper.Abstractions.Configuration; | ||
|
@@ -32,9 +33,42 @@ public void Initialise(AuthSettings settings) | |
_client = new AzureDevOpsRestClient(_clientFactory, _logger, settings.Token, settings.ApiBase); | ||
} | ||
|
||
public Task<User> GetCurrentUser() | ||
public async Task<User> GetCurrentUser() | ||
{ | ||
return Task.FromResult(new User("[email protected]", "", "")); | ||
try | ||
{ | ||
var currentAccounts = await _client.GetCurrentUser(); | ||
var account = currentAccounts.value.FirstOrDefault(); | ||
|
||
if (account == null) | ||
return User.Default; | ||
|
||
return new User(account.accountId, account.accountName, account.Mail); | ||
|
||
} | ||
catch (NuKeeperException) | ||
{ | ||
return User.Default; | ||
} | ||
} | ||
|
||
public async Task<User> GetUserByMail(string email) | ||
{ | ||
try | ||
{ | ||
var currentAccounts = await _client.GetUserByMail(email); | ||
var account = currentAccounts.value.FirstOrDefault(); | ||
|
||
if (account == null) | ||
return User.Default; | ||
|
||
return new User(account.accountId, account.accountName, account.Mail); | ||
|
||
} | ||
catch (NuKeeperException) | ||
{ | ||
return User.Default; | ||
} | ||
} | ||
|
||
public async Task<bool> PullRequestExists(ForkData target, string headBranch, string baseBranch) | ||
|
@@ -180,5 +214,58 @@ public async Task<SearchCodeResult> Search(SearchCodeRequest searchRequest) | |
|
||
return new SearchCodeResult(totalCount); | ||
} | ||
|
||
public async Task<int> GetNumberOfOpenPullRequests(string projectName, string repositoryName) | ||
{ | ||
var user = await GetCurrentUser(); | ||
|
||
if (user == User.Default) | ||
{ | ||
// TODO: allow this to be configurable | ||
user = await GetUserByMail("[email protected]"); | ||
} | ||
|
||
var prs = await GetPullRequestsForUser( | ||
projectName, | ||
repositoryName, | ||
user == User.Default ? | ||
string.Empty | ||
: user.Login | ||
); | ||
|
||
if (user == User.Default) | ||
{ | ||
var relevantPrs = prs? | ||
.Where( | ||
pr => pr.labels | ||
?.FirstOrDefault( | ||
l => l.name.Equals( | ||
"nukeeper", | ||
StringComparison.InvariantCultureIgnoreCase | ||
) | ||
)?.active ?? false | ||
); | ||
|
||
return relevantPrs?.Count() ?? 0; | ||
} | ||
else | ||
{ | ||
return prs?.Count() ?? 0; | ||
} | ||
} | ||
|
||
private async Task<IEnumerable<PullRequest>> GetPullRequestsForUser(string projectName, string repositoryName, string userName) | ||
{ | ||
try | ||
{ | ||
return await _client.GetPullRequests(projectName, repositoryName, userName); | ||
|
||
} | ||
catch (NuKeeperException ex) | ||
{ | ||
_logger.Error($"Failed to get pull requests for name {userName}", ex); | ||
return Enumerable.Empty<PullRequest>(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.