/// <summary> /// Creates a pull request, and then adds a reviewer to it. /// </summary> /// <param name="gitHttpClient"> GitHttpClient that is created for accessing vsts</param> /// <param name="gitPullRequest"> the pull request to be created</param> /// <param name="repositoryId"> the unique identifier of the repository</param> /// <param name="reviewerAlias"> reviewer's alias in vsts</param> /// <param name="vstsAccountUrl">vsts account's url</param> /// <param name="personalToken"> personal access token to access the vsts account. </param> public static async void CreatePullRequestAndAddReviewer( GitHttpClient gitHttpClient, GitPullRequest gitPullRequest, string repositoryId, string reviewerAlias, Uri vstsAccountUrl, string personalToken) { // 1- Create the pull request. GitPullRequest pullRequest = gitHttpClient.CreatePullRequestAsync(gitPullRequest, repositoryId, cancellationToken: CancellationToken.None).Result; // 2- Create an Identity Client to get the reviewer's vsts id IdentityHttpClient identityHttpClient = CreateIdentityClient(vstsAccountUrl, personalToken); // 3- Find the reviewer's vsts identity. Identity identity = SearchForReviewerVstsIdentity(identityHttpClient, reviewerAlias).Result; // 4- Create a IdentityRefWithVote for the reviewer IdentityRefWithVote identityRefWithVote = new IdentityRefWithVote { Id = identity.Id.ToString(), IsRequired = true // false otherwise. }; // 5- Finally add the reviewer to the pull request. await AddReviewerToPullRequest(gitHttpClient, pullRequest, identityRefWithVote); }
public void TestHasFinalVoteBeenCastExtension() { IdentityRefWithVote identity = new IdentityRefWithVote(); // No vote should not be a valid final vote. // identity.Vote = 0; Assert.False(identity.HasFinalVoteBeenCast(), "No Vote incorrectly marked as final."); // Waiting should not be a valid final vote. // identity.Vote = -5; Assert.False(identity.HasFinalVoteBeenCast(), "Waiting incorrectly marked as final."); // Rejected should be a valid final vote. // identity.Vote = -10; Assert.True(identity.HasFinalVoteBeenCast(), "Rejected incorrectly marked as not a final vote."); // Approved should be a valid final vote. // identity.Vote = 10; Assert.True(identity.HasFinalVoteBeenCast(), "Approved incorrectly marked as not a final vote."); // Approved with suggestions should be a valid final vote. // identity.Vote = 5; Assert.True(identity.HasFinalVoteBeenCast(), "Approved with suggestions incorrectly marked as not a final vote."); }
public string AddReviewer(int pullRequestId, AutoMergeReviewer reviewer) { using (var client = CreateGitClient(out var authorizedIdenity)) { var pr = client.GetPullRequestByIdAsync(pullRequestId).Result; _log.Verbose("Pull Request Details\nId: {0}\nStatus: {1}\nMerge Status: {2}", pr.PullRequestId, pr.Status, pr.MergeStatus); if (pr.Reviewers.Any(t => t.Id == reviewer.Id)) { return(reviewer.Id); } _log.Verbose("Attempt to add reviewer to Pull Request"); var reviewerIdentity = new IdentityRefWithVote( new IdentityRef { Id = reviewer.Id }) { Vote = (short)reviewer.Vote.GetValueOrDefault() }; var result = client.CreatePullRequestReviewerAsync( reviewerIdentity, _settings.ProjectName, _settings.RepositoryName, pullRequestId, authorizedIdenity.Id.ToString()).Result; return(result.Id); } }
public void TestHasFinalVoteBeenCastOnNull() { IdentityRefWithVote nullIdentity = null; Assert.Throws <ArgumentNullException>(() => { nullIdentity.HasFinalVoteBeenCast(); }); }
/// <summary> /// Extension method that determines whether the final vote has been cast on a pull request. /// </summary> /// <param name="identityRef">The identity reference.</param> /// <returns> /// <c>true</c> if [has final vote been cast] [the specified identity reference]; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">identityRef</exception> public static bool HasFinalVoteBeenCast(this IdentityRefWithVote identityRef) { if (identityRef == null) { throw new ArgumentNullException(nameof(identityRef)); } return(identityRef.IsSignedOff() || identityRef.Vote == -10); // Rejected }
public void TestIsWaitingOnNull() { IdentityRefWithVote nullIdentity = null; Assert.Throws <ArgumentNullException>(() => { nullIdentity.IsWaiting(); }); }
/// <summary> /// Extension method that determines whether the vote has signed off on a pull request. /// </summary> /// <param name="identityRef">The identity reference.</param> /// <returns> /// <c>true</c> if [vote has signed off] [the specified identity reference]; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">identityRef</exception> public static bool IsSignedOff(this IdentityRefWithVote identityRef) { if (identityRef == null) { throw new ArgumentNullException(nameof(identityRef)); } return(identityRef.Vote == 10 || // Approved identityRef.Vote == 5); // Approved With Suggestions }
/// <summary> /// Extension method that determines whether this instance is waiting. /// </summary> /// <param name="identityRef">The identity reference.</param> /// <returns> /// <c>true</c> if the specified identity reference is waiting; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">identityRef</exception> public static bool IsWaiting(this IdentityRefWithVote identityRef) { if (identityRef == null) { throw new ArgumentNullException(nameof(identityRef)); } // Vote -5 == waiting. // return(identityRef.Vote == -5); }
public static bool HasFinalVoteBeenCast(this IdentityRefWithVote identityRef) { if (identityRef == null) { throw new ArgumentNullException(nameof(identityRef)); } return(identityRef.Vote == 10 || // Approved identityRef.Vote == 5 || // Approved with suggestions identityRef.Vote == -10); // Rejected }
public void TestIsWaitingExtension() { IdentityRefWithVote identity = new IdentityRefWithVote(); // Verify waiting vote value. // identity.Vote = -5; Assert.True(identity.IsWaiting(), "Approved incorrectly marked as not a final vote."); // No vote should not be a valid final vote. // identity.Vote = 0; Assert.False(identity.IsWaiting(), "No Vote incorrectly marked as waiting."); }
public int VotePullRequest(int pullRequestId, AutoMergeVote vote) { using (var client = CreateGitClient(out var authorizedIdenity)) { var reviewer = new IdentityRefWithVote { Id = authorizedIdenity.Id.ToString(), Vote = (short)vote }; var result = client.CreatePullRequestReviewerAsync( reviewer, _settings.ProjectName, _settings.RepositoryName, pullRequestId, authorizedIdenity.Id.ToString()).Result; _log.Verbose("Voted for pull request with '{0}'.", result.Vote); return(result.Vote); } }
/// <summary> /// Adds a reviewer to a an already created pull request. /// </summary> /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts</param> /// <param name="pullRequest"> pull request that is already created.</param> /// <param name="identity">identity of the reviewer that we want to add to the pull request.</param> public static async Task AddReviewerToPullRequest(GitHttpClient gitHttpClient, GitPullRequest pullRequest, IdentityRefWithVote identity) { identity = await gitHttpClient.CreatePullRequestReviewerAsync( identity, pullRequest.Repository.Id, pullRequest.PullRequestId, identity.Id).ConfigureAwait(false); }
/// <summary> /// Tries to get the current users reviewer object from a pull request. /// </summary> /// <param name="pullRequest">The pull request we want to look our selves up in.</param> /// <param name="currentUserId">The <see cref="Guid"/> of our current user.</param> /// <param name="reviewer">Output parameter that points to our own reviewer object.</param> /// <returns></returns> private static bool TryGetReviewer(GitPullRequest pullRequest, Guid currentUserId, out IdentityRefWithVote reviewer) { foreach (IdentityRefWithVote r in pullRequest.Reviewers) { if (currentUserId.Equals(Guid.Parse(r.Id))) { reviewer = r; return(true); } } reviewer = null; return(false); }