From 1e7eb59e729663d2776634cde395086e1a897a4b Mon Sep 17 00:00:00 2001 From: Lucas Vertrees Date: Wed, 21 Jul 2021 13:19:49 -0400 Subject: [PATCH 1/2] Blocker comment and fix anchor issue --- .../Core/Projects/BitbucketClient.cs | 93 +++++++++++++++++-- 1 file changed, 85 insertions(+), 8 deletions(-) diff --git a/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs b/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs index a77bd23..9ab4bf6 100644 --- a/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs +++ b/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs @@ -30,7 +30,52 @@ private IFlurlRequest GetProjectUrl(string projectKey) => GetProjectsUrl() private IFlurlRequest GetProjectsReposUrl(string projectKey, string repositorySlug, string path) => GetProjectsReposUrl(projectKey, repositorySlug) .AppendPathSegment(path); - public async Task> GetProjectsAsync( + private static object GetCreatePullRequestCommentPayload( + string text, + string parentId = null, + DiffTypes? diffType = null, + string fromHash = null, + string path = null, + string srcPath = null, + string toHash = null, + int? line = null, + FileTypes? fileType = null, + LineTypes? lineType = null + ) + { + var parent = parentId == null ? null : new { id = parentId }; + + // We only want to include the anchor if there's anchor data available. Otherwise we'll get a 500 HTTP response when POSTing + if (diffType != null || fromHash != null || path != null || srcPath != null + || toHash != null || line != null || fileType != null || lineType != null) + { + return new + { + text, + parent = parent, + anchor = new + { + diffType = BitbucketHelpers.DiffTypeToString(diffType), + fromHash, + path, + srcPath, + toHash, + line, + fileType = BitbucketHelpers.FileTypeToString(fileType), + lineType = BitbucketHelpers.LineTypeToString(lineType) + } + }; + } + + return new + { + text, + parent = parent, + }; + } + + + public async Task> GetProjectsAsync( int? maxPages = null, int? limit = null, int? start = null, @@ -1254,19 +1299,18 @@ public async Task CreatePullRequestCommentAsync(string projectKey, s FileTypes? fileType = null, LineTypes? lineType = null) { - var data = new - { + var data = GetCreatePullRequestCommentPayload( text, - parent = parentId == null ? null : new { id = parentId }, - diffType = BitbucketHelpers.DiffTypeToString(diffType), + parentId, + diffType, fromHash, path, srcPath, toHash, line, - fileType = BitbucketHelpers.FileTypeToString(fileType), - lineType = BitbucketHelpers.LineTypeToString(lineType) - }; + fileType, + lineType + ); var response = await GetProjectsReposUrl(projectKey, repositorySlug) .AppendPathSegment($"/pull-requests/{pullRequestId}/comments") @@ -1347,6 +1391,39 @@ public async Task DeletePullRequestCommentAsync(string projectKey, string return await HandleResponseAsync(response).ConfigureAwait(false); } + public async Task CreatePullRequestBlockerCommentAsync(string projectKey, string repositorySlug, long pullRequestId, + string text, + string parentId = null, + DiffTypes? diffType = null, + string fromHash = null, + string path = null, + string srcPath = null, + string toHash = null, + int? line = null, + FileTypes? fileType = null, + LineTypes? lineType = null) + { + var data = GetCreatePullRequestCommentPayload( + text, + parentId, + diffType, + fromHash, + path, + srcPath, + toHash, + line, + fileType, + lineType + ); + + var response = await GetProjectsReposUrl(projectKey, repositorySlug) + .AppendPathSegment($"/pull-requests/{pullRequestId}/blocker-comments") + .PostJsonAsync(data) + .ConfigureAwait(false); + + return await HandleResponseAsync(response).ConfigureAwait(false); + } + public async Task> GetPullRequestCommitsAsync(string projectKey, string repositorySlug, long pullRequestId, bool withCounts = false, int? maxPages = null, From e17e84a4b013fa28ab382476e98e5fda05ae9e1b Mon Sep 17 00:00:00 2001 From: Lucas Vertrees Date: Wed, 21 Jul 2021 14:52:18 -0400 Subject: [PATCH 2/2] Small refactoring when creating the payload --- src/Bitbucket.Net/Core/Projects/BitbucketClient.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs b/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs index 9ab4bf6..5604f42 100644 --- a/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs +++ b/src/Bitbucket.Net/Core/Projects/BitbucketClient.cs @@ -52,7 +52,7 @@ private static object GetCreatePullRequestCommentPayload( return new { text, - parent = parent, + parent, anchor = new { diffType = BitbucketHelpers.DiffTypeToString(diffType), @@ -69,8 +69,8 @@ private static object GetCreatePullRequestCommentPayload( return new { - text, - parent = parent, + text, + parent, }; }