private async Task <bool> HasMergeConflicts(BitbucketPullRequest pullRequest) { BitbucketMergeStatus Response = await this.bitbucketClient.TestMerge(pullRequest); // TODO, may need to handle vetos return(Response.IsConflicted); }
public void ValidPullRequestIsSent() { var bitbucketPullRequest = new BitbucketPullRequest(); var reviewers = GivenDefaultReviewers(); var nureOptions = GivenNureOptions(); m_ClientMock.Setup(client => client.GetCurrentUser()).Returns(new BitbucketUser()); m_ClientMock.Setup(client => client.GetDefaultReviewers()).Returns(reviewers); m_ClientMock.Setup(client => client.SendPullRequest(It.IsAny <BitbucketPullRequest>())) .Callback <BitbucketPullRequest>(pr => bitbucketPullRequest = pr); var pullRequestWriter = new BitbucketPullRequestWriter(nureOptions, m_ClientMock.Object); pullRequestWriter.WritePullRequest(A_BRANCH_NAME); m_ClientMock.Verify(client => client.GetCurrentUser()); m_ClientMock.Verify(client => client.GetDefaultReviewers()); m_ClientMock.Verify(client => client.SendPullRequest(It.IsAny <BitbucketPullRequest>())); Assert.Equal(nureOptions.PullRequestTitle, bitbucketPullRequest.Title); Assert.Equal(nureOptions.PullRequestDescription, bitbucketPullRequest.Description); Assert.Equal(A_BRANCH_NAME, bitbucketPullRequest.SourceBranch.Branch.Name); Assert.Equal(nureOptions.DefaultBranch, bitbucketPullRequest.DestinationBranch.Branch.Name); Assert.Equal(reviewers, bitbucketPullRequest.Reviewers); }
public void WritePullRequest(string p_BranchName) { s_Logger.Info("Preparing Bitbucket pull request..."); BitbucketUser currentUser = new BitbucketUser(); try { currentUser = m_BitbucketClient.GetCurrentUser(); } catch (AggregateException e) when((e.InnerException as FlurlHttpException)?.Call.HttpStatus == HttpStatusCode.Forbidden) { s_Logger.Warn("Unable to get the current user from Bitbucket. Attempting to create the pull request anyway."); } List <BitbucketUser> defaultReviewers = m_BitbucketClient.GetDefaultReviewers().Where(reviewer => reviewer.UserId != currentUser.UserId).ToList(); BitbucketPullRequest pullRequest = new BitbucketPullRequest { Title = m_NureOptions.PullRequestTitle, Description = m_NureOptions.PullRequestDescription, SourceBranch = new BitbucketBranchInfo { Branch = new BitbucketBranch { Name = p_BranchName } }, DestinationBranch = new BitbucketBranchInfo { Branch = new BitbucketBranch { Name = m_NureOptions.DefaultBranch } }, Reviewers = defaultReviewers, CloseBranchAfterMerge = true }; m_BitbucketClient.SendPullRequest(pullRequest); }
private async Task <FishEyeChangesets> FindReviewsByCommits(BitbucketPullRequest pullRequest) { BitbucketPageResponse <BitbucketPullRequestCommit> Response = await this.bitbucketClient.GetCommits(pullRequest); return(await this.fishEyeClient.GetReviewsForChangesets(pullRequest.FromReference.Repository.Slug, Response.Values.Select(s => s.Id).ToList())); }
public async Task <HttpResponseMessage> SetReviewerStatus(BitbucketPullRequest pullRequest, BitbucketParticipant reviewer) { Guard.AgainstNullArgument <BitbucketParticipant>(nameof(reviewer), reviewer); string RequestUri = $"{this.GetBaseUri(pullRequest)}/participants/{reviewer.User.Slug}"; return(await this.client.PutAsync(RequestUri, new JsonContent <BitbucketParticipant>(reviewer))); }
public async Task <HttpResponseMessage> AddComment(BitbucketPullRequest pullRequest, BitbucketComment comment) { Guard.AgainstNullArgument <BitbucketComment>(nameof(comment), comment); string RequestUri = $"{this.GetBaseUri(pullRequest)}/comments"; return(await this.client.PostAsync(RequestUri, new JsonContent <BitbucketComment>(comment))); }
public void SendPullRequest(BitbucketPullRequest p_BitbucketPullRequest) { s_Logger.Info($"Sending pull request merging {p_BitbucketPullRequest.SourceBranch.Branch.Name} into {p_BitbucketPullRequest.DestinationBranch.Branch.Name}"); new Url(m_BaseUrl) .AppendPathSegments(API_VERSION, "repositories", m_WorkspaceId, m_RepositoryId, "pullrequests") .WithBasicAuth(m_Username, m_Password) .WithClient(m_FlurlClient) .PostJsonAsync(p_BitbucketPullRequest) .Wait(); }
public void CurrentUserIsFilteredFromDefaultReviewers() { BitbucketPullRequest bitbucketPullRequest = new BitbucketPullRequest(); m_ClientMock.Setup(client => client.GetCurrentUser()).Returns(new BitbucketUser { UserId = A_CURRENT_USER_ID }); m_ClientMock.Setup(client => client.GetDefaultReviewers()).Returns(GivenDefaultReviewers); m_ClientMock.Setup(client => client.SendPullRequest(It.IsAny <BitbucketPullRequest>())).Callback <BitbucketPullRequest>(pr => bitbucketPullRequest = pr); BitbucketPullRequestWriter pullRequestWriter = new BitbucketPullRequestWriter(GivenNureOptions(), m_ClientMock.Object); pullRequestWriter.WritePullRequest(A_BRANCH_NAME); Assert.All(bitbucketPullRequest.Reviewers, reviewer => Assert.NotEqual(A_CURRENT_USER_ID, reviewer.UserId)); }
private async Task <bool> AreAllCommitsReviewed(BitbucketPullRequest pullRequest, FishEyeChangesets changesets) { List <FishEyeChangeset> ChangesetsMissingReviews = changesets.Changesets.FindAll(c => c.Reviews.Count <= 0); if (ChangesetsMissingReviews.Count > 0) { // list commits that are not associated with a review await this.bitbucketClient.AddComment(pullRequest, new BitbucketComment() { Text = string.Format(Resources.PullRequest_CommitsNotAssociatedWithAReview, string.Join("\n", ChangesetsMissingReviews.Select(s => $"* {s.ChangesetId}"))) }); } return(false); }
private async Task ValidateReviewConditions(BitbucketPullRequest pullRequest, FishEyeChangesets changesets) { // find distinctive reviews from changesets List <FishEyeReview> FishEyeReviews = new List <FishEyeReview>(); List <Problem> Problems; foreach (FishEyeChangeset Changeset in changesets.Changesets) { FishEyeReviews.AddRange(Changeset.Reviews.GroupBy(g => g.ProjectKey).Select(r => r.First())); } // get details on the reviews List <CrucibleReview> CrucibleReviews = new List <CrucibleReview>(); foreach (FishEyeReview Review in FishEyeReviews) { CrucibleReviews.Add(await this.crucibleClient.GetReviewDetails(Review.PermaId["id"].ToString())); } // check if all reviews are closed and have 2 completed reviewers Problems = this.ValidateStateForAssociatedReviews(CrucibleReviews); Problems = Problems.Merge(this.ValidateCompletedReviewersForAssociatedReviews(CrucibleReviews)); // if failure found, comment to the pr the problem, make sure to check both though if (Problems.Count > 0) { await this.bitbucketClient.SetReviewerStatus(pullRequest, new BitbucketParticipant() { User = new BitbucketUser() { Name = this.integrations.Bitbucket.Credentials.UserName, Slug = this.integrations.Bitbucket.Credentials.UserName }, IsApproved = false, Status = BitbucketStatus.NeedsWork }); await this.bitbucketClient.AddComment(pullRequest, new BitbucketComment() { Text = string.Format(Resources.PullRequest_NeedsWorkReviewConditions, pullRequest.Author.User.Name, string.Join("\n", Problems.Select(s => $"* {s.Message}"))) }); } }
private string GetBaseUri(BitbucketPullRequest pullRequest) { Guard.AgainstNullArgument <BitbucketPullRequest>(nameof(pullRequest), pullRequest); return($"projects/{pullRequest.ToReference.Repository.Project.Key}/repos/{pullRequest.ToReference.Repository.Slug}/pull-requests/{pullRequest.Id}"); }
public async Task <BitbucketMergeStatus> TestMerge(BitbucketPullRequest pullRequest) { string RequestUri = $"{this.GetBaseUri(pullRequest)}/merge"; return(await this.client.GetAsync <BitbucketMergeStatus>(RequestUri)); }
public async Task <BitbucketPageResponse <BitbucketPullRequestCommit> > GetCommits(BitbucketPullRequest pullRequest) { string RequestUri = $"{this.GetBaseUri(pullRequest)}/commits"; return(await this.client.GetAsync <BitbucketPageResponse <BitbucketPullRequestCommit> >(RequestUri)); }