8000 Merge branch 'master' into feature/open-current-pr-command · github/VisualStudio@75b3f93 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 75b3f93

Browse files
authored
Merge branch 'master' into feature/open-current-pr-command
2 parents 7fdbd65 + c502cf8 commit 75b3f93

19 files changed

+298
-153
lines changed

src/GitHub.Exports.Reactive/Services/IPullRequestSession.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public interface IPullRequestSession
4646
/// </remarks>
4747
string RepositoryOwner { get; }
4848

49-
/// <summary>
50-
/// Adds a new comment to the session.
51-
/// </summary>
52-
/// <param name="comment">The comment.</param>
53-
Task AddComment(IPullRequestReviewCommentModel comment);
54-
5549
/// <summary>
5650
/// Gets all files touched by the pull request.
5751
/// </summary>
@@ -92,6 +86,24 @@ Task<IPullRequestSessionFile> GetFile(
9286
/// </returns>
9387
string GetRelativePath(string path);
9488

89+
/// <summary>
90+
/// Posts a new PR review comment.
91+
/// </summary>
92+
/// <param name="body">The comment body.</param>
93+
/// <param name="commitId">THe SHA of the commit to comment on.</param>
94+
/// <param name="path">The relative path of the file to comment on.</param>
95+
/// <param name="position">The line index in the diff to comment on.</param>
96+
/// <returns>A comment model.</returns>
97+
Task<IPullRequestReviewCommentModel> PostReviewComment(string body, string commitId, string path, int position);
98+
99+
/// <summary>
100+
/// Posts a PR review comment reply.
101+
/// </summary>
102+
/// <param name="body">The comment body.</param>
103+
/// <param name="inReplyTo">The comment ID to reply to.</param>
104+
/// <returns></returns>
105+
Task<IPullRequestReviewCommentModel> PostReviewComment(string body, int inReplyTo);
106+
95107
/// <summary>
96108
/// Updates the pull request session with a new pull request model in response to a refresh
97109
/// from the server.

src/GitHub.Exports/Models/UsageModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public class UsageModel
3030
public int NumberOfWelcomeDocsClicks { get; set; }
3131
public int NumberOfWelcomeTrainingClicks { get; set; }
3232
public int NumberOfGitHubPaneHelpClicks { get; set; }
33+
public int NumberOfPRDetailsViewChanges { get; set; }
34+
public int NumberOfPRDetailsViewFile { get; set; }
35+
public int NumberOfPRDetailsCompareWithSolution { get; set; }
36+
public int NumberOfPRDetailsOpenFileInSolution { get; set; }
37+
public int NumberOfPRReviewDiffViewInlineCommentOpen { get; set; }
38+
public int NumberOfPRReviewDiffViewInlineCommentPost { get; set; }
3339

3440
public UsageModel Clone(bool includeWeekly, bool includeMonthly)
3541
{
@@ -61,6 +67,12 @@ public UsageModel Clone(bool includeWeekly, bool includeMonthly)
6167
NumberOfWelcomeDocsClicks = NumberOfWelcomeDocsClicks,
6268
NumberOfWelcomeTrainingClicks = NumberOfWelcomeTrainingClicks,
6369
NumberOfGitHubPaneHelpClicks = NumberOfGitHubPaneHelpClicks,
70+
NumberOfPRDetailsViewChanges = NumberOfPRDetailsViewChanges,
71+
NumberOfPRDetailsViewFile = NumberOfPRDetailsViewFile,
72+
NumberOfPRDetailsCompareWithSolution = NumberOfPRDetailsCompareWithSolution,
73+
NumberOfPRDetailsOpenFileInSolution = NumberOfPRDetailsOpenFileInSolution,
74+
NumberOfPRReviewDiffViewInlineCommentOpen = NumberOfPRReviewDiffViewInlineCommentOpen,
75+
NumberOfPRReviewDiffViewInlineCommentPost = NumberOfPRReviewDiffViewInlineCommentPost,
6476
};
6577
}
6678
}

src/GitHub.Exports/Services/IUsageTracker.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ public interface IUsageTracker
2323
Task IncrementWelcomeDocsClicks();
2424
Task IncrementWelcomeTrainingClicks();
2525
Task IncrementGitHubPaneHelpClicks();
26+
Task IncrementPRDetailsViewChanges();
27+
Task IncrementPRDetailsViewFile();
28+
Task IncrementPRDetailsCompareWithSolution();
29+
Task IncrementPRDetailsOpenFileInSolution();
30+
Task IncrementPRReviewDiffViewInlineCommentOpen();
31+
Task IncrementPRReviewDiffViewInlineCommentPost();
2632
}
2733
}

src/GitHub.InlineReviews/Services/IPullRequestSessionService.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,41 @@ Task<byte[]> ExtractFileFromGit(
8181
/// The merge base SHA for the PR.
8282
/// </returns>
8383
Task<string> GetPullRequestMergeBase(ILocalRepositoryModel repository, IPullRequestModel pullRequest);
84+
85+
/// <summary>
86+
/// Posts a new PR review comment.
87+
/// </summary>
88+
/// <param name="repository">The repository.</param>
89+
/// <param name="user">The user posting the comment.</param>
90+
/// <param name="number">The pull request number.</param>
91+
/// <param name="body">The comment body.</param>
92+
/// <param name="commitId">THe SHA of the commit to comment on.</param>
93+
/// <param name="path">The relative path of the file to comment on.</param>
94+
/// <param name="position">The line index in the diff to comment on.</param>
95+
/// <returns>A model representing the posted comment.</returns>
96+
Task<IPullRequestReviewCommentModel> PostReviewComment(
97+
ILocalRepositoryModel repository,
98+
IAccount user,
99+
int number,
100+
string body,
101+
string commitId,
102+
string path,
103+
int position);
104+
105+
/// <summary>
106+
/// Posts a PR review comment reply.
107+
/// </summary>
108+
/// <param name="repository">The repository.</param>
109+
/// <param name="user">The user posting the comment.</param>
110+
/// <param name="number">The pull request number.</param>
111+
/// <param name="body">The comment body.</param>
112+
/// <param name="inReplyTo">The comment ID to reply to.</param>
113+
/// <returns>A model representing the posted comment.</returns>
114+
Task<IPullRequestReviewCommentModel> PostReviewComment(
115+
ILocalRepositoryModel repository,
116+
IAccount user,
117+
int number,
118+
string body,
119+
int inReplyTo);
84120
}
85121
}

src/GitHub.InlineReviews/Services/InlineCommentPeekService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using GitHub.InlineReviews.Tags;
1111
using GitHub.Models;
1212
using GitHub.Primitives;
13+
using GitHub.Services;
1314
using Microsoft.VisualStudio.Language.Intellisense;
1415
using Microsoft.VisualStudio.Text;
1516
using Microsoft.VisualStudio.Text.Differencing;
@@ -28,16 +29,19 @@ class InlineCommentPeekService : IInlineCommentPeekService
2829
readonly IApiClientFactory apiClientFactory;
2930
readonly IOutliningManagerService outliningService;
3031
readonly IPeekBroker peekBroker;
32+
readonly IUsageTracker usageTracker;
3133

3234
[ImportingConstructor]
3335
public InlineCommentPeekService(
3436
IApiClientFactory apiClientFactory,
3537
IOutliningManagerService outliningManager,
36-
IPeekBroker peekBroker)
38+
IPeekBroker peekBroker,
39+
IUsageTracker usageTracker)
3740
{
3841
this.apiClientFactory = apiClientFactory;
3942
this.outliningService = outliningManager;
4043
this.peekBroker = peekBroker;
44+
this.usageTracker = usageTracker;
4145
}
4246

4347
/// <inheritdoc/>
@@ -152,6 +156,12 @@ Tuple<ITextSnapshotLine, ITrackingPoint> GetLineAndTrackingPoint(ITextView textV
152156

153157
var line = snapshot.GetLineFromLineNumber(tag.LineNumber);
154158
var trackingPoint = snapshot.CreateTrackingPoint(line.Start.Position, PointTrackingMode.Positive);
159+
160+
ExpandCollapsedRegions(textView, line.Extent);
161+
peekBroker.TriggerPeekSession(textView, trackingPoint, InlineCommentPeekRelationship.Instance.Name);
162+
163+
usageTracker.IncrementPRReviewDiffViewInlineCommentOpen().Forget();
164+
155165
return Tuple.Create(line, trackingPoint);
156166
}
157167

src/GitHub.InlineReviews/Services/PullRequestSession.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ public PullRequestSession(
5050
RepositoryOwner = repositoryOwner;
5151
}
5252

53-
/// <inheritdoc/>
54-
public async Task AddComment(IPullRequestReviewCommentModel comment)
55-
{
56-
PullRequest.ReviewComments = PullRequest.ReviewComments
57-
.Concat(new[] { comment })
58-
.ToList();
59-
await Update(PullRequest);
60-
}
61-
6253
/// <inheritdoc/>
6354
public async Task<IReadOnlyList<IPullRequestSessionFile>> GetAllFiles()
6455
{
@@ -125,6 +116,34 @@ public string GetRelativePath(string path)
125116
return null;
126117
}
127118

119+
/// <inheritdoc/>
120+
public async Task<IPullRequestReviewCommentModel> PostReviewComment(string body, string commitId, string path, int position)
121+
{
122+
var model = await service.PostReviewComment(
123+
LocalRepository,
124+
User,
125+
PullRequest.Number,
126+
body,
127+
commitId,
128+
path,
129+
position);
130+
await AddComment(model);
131+
return model;
132+
}
133+
134+
/// <inheritdoc/>
135+
public async Task<IPullRequestReviewCommentModel> PostReviewComment(string body, int inReplyTo)
136+
{
137+
var model = await service.PostReviewComment(
138+
LocalRepository,
139+
User,
140+
PullRequest.Number,
141+
body,
142+
inReplyTo);
143+
await AddComment(model);
144+
return model;
145+
}
146+
128147
/// <inheritdoc/>
129148
public async Task UpdateEditorContent(string relativePath)
130149
{
@@ -159,6 +178,14 @@ public async Task Update(IPullRequestModel pullRequest)
159178
}
160179
}
161180

181+
async Task AddComment(IPullRequestReviewCommentModel comment)
182+
{
183+
PullRequest.ReviewComments = PullRequest.ReviewComments
184+
.Concat(new[] { comment })
185+
.ToList();
186+
await Update(PullRequest);
187+
}
188+
162189
async Task UpdateFile(PullRequestSessionFile file)
163190
{
164191
// NOTE: We must call GetPullRequestMergeBase before GetFileContent.

src/GitHub.InlineReviews/Services/PullRequestSessionService.cs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
using System.Collections.Generic;
33
using System.ComponentModel.Composition;
44
using System.IO;
5+
using System.Reactive.Linq;
56
using System.Threading.Tasks;
7+
using GitHub.Factories;
68
using GitHub.Models;
9+
using GitHub.Primitives;
710
using GitHub.Services;
811
using LibGit2Sharp;
912

@@ -18,18 +21,24 @@ class PullRequestSessionService : IPullRequestSessionService
1821
readonly IGitService gitService;
1922
readonly IGitClient gitClient;
2023
readonly IDiffService diffService;
24+
readonly IApiClientFactory apiClientFactory;
25+
readonly IUsageTracker usageTracker;
2126

2227
readonly IDictionary<Tuple<string, string>, string> mergeBaseCache;
2328

2429
[ImportingConstructor]
2530
public PullRequestSessionService(
2631
IGitService gitService,
2732
IGitClient gitClient,
28-
IDiffService diffService)
33+
IDiffService diffService,
34+
IApiClientFactory apiClientFactory,
35+
IUsageTracker usageTracker)
2936
{
3037
this.gitService = gitService;
3138
this.gitClient = gitClient;
3239
this.diffService = diffService;
40+
this.apiClientFactory = apiClientFactory;
41+
this.usageTracker = usageTracker;
3342

3443
mergeBaseCache = new Dictionary<Tuple<string, string>, string>();
3544
}
@@ -125,6 +134,80 @@ public async Task<string> GetPullRequestMergeBase(ILocalRepositoryModel reposito
125134
throw new FileNotFoundException($"Couldn't find merge base between {baseSha} and {headSha}.");
126135
}
127136

137+
/// <inheritdoc/>
138+
public async Task<IPullRequestReviewCommentModel> PostReviewComment(
139+
ILocalRepositoryModel repository,
140+
IAccount user,
141+
int number,
142+
string body,
143+
string commitId,
144+
string path,
145+
int position)
146+
{
147+
var address = HostAddress.Create(repository.CloneUrl.Host);
148+
var apiClient = await apiClientFactory.Create(address);
149+
150+
var result = await apiClient.CreatePullRequestReviewComment(
151+
repository.Owner,
152+
repository.Name,
153+
number,
154+
body,
155+
commitId,
156+
path,
157+
position);
158+
159+
await usageTracker.IncrementPRReviewDiffViewInlineCommentPost();
160+
161+
return new PullRequestReviewCommentModel
162+
{
163+
Body = result.Body,
164+
CommitId = result.CommitId,
165+
DiffHunk = result.DiffHunk,
166+
Id = result.Id,
167+
OriginalCommitId = result.OriginalCommitId,
168+
OriginalPosition = result.OriginalPosition,
169+
Path = result.Path,
170+
Position = result.Position,
171+
CreatedAt = result.CreatedAt,
172+
User = user,
173+
};
174+
}
175+
176+
/// <inheritdoc/>
177+
public async Task<IPullRequestReviewCommentModel> PostReviewComment(
178+
ILocalRepositoryModel repository,
179+
IAccount user,
180+
int number,
181+
string body,
182+
int inReplyTo)
183+
{
184+
var address = HostAddress.Create(repository.CloneUrl.Host);
185+
var apiClient = await apiClientFactory.Create(address);
186+
187+
var result = await apiClient.CreatePullRequestReviewComment(
188+
repository.Owner,
189+
repository.Name,
190+
number,
191+
body,
192+
inReplyTo);
193+
194+
await usageTracker.IncrementPRReviewDiffViewInlineCommentPost();
195+
196+
return new PullRequestReviewCommentModel
197+
{
198+
Body = result.Body,
199+
CommitId = result.CommitId,
200+
DiffHunk = result.DiffHunk,
201+
Id = result.Id,
202+
OriginalCommitId = result.OriginalCommitId,
203+
OriginalPosition = result.OriginalPosition,
204+
Path = result.Path,
205+
Position = result.Position,
206+
CreatedAt = result.CreatedAt,
207+
User = user,
208+
};
209+
}
210+
128211
Task<IRepository> GetRepository(ILocalRepositoryModel repository)
F438
129212
{
130213
return Task.Factory.StartNew(() => gitService.GetRepository(repository.LocalPath));

src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,17 @@ async Task UpdateThread()
137137
var lineAndLeftBuffer = peekService.GetLineNumber(peekSession, triggerPoint);
138138
var lineNumber = lineAndLeftBuffer.Item1;
139139
var leftBuffer = lineAndLeftBuffer.Item2;
140-
var thread = file.InlineCommentThreads.FirstOrDefault(x =>
140+
var thread = file.InlineCommentThreads.FirstOrDefault(x =>
141141
x.LineNumber == lineNumber &&
142142
((leftBuffer && x.DiffLineType == DiffChangeType.Delete) || (!leftBuffer && x.DiffLineType != DiffChangeType.Delete)));
143-
var apiClient = await CreateApiClient(session.LocalRepository);
144143

145144
if (thread != null)
146145
{
147-
Thread = new InlineCommentThreadViewModel(apiClient, session, thread.Comments);
146+
Thread = new InlineCommentThreadViewModel(session, thread.Comments);
148147
}
149148
else
150149
{
151-
var newThread = new NewInlineCommentThreadViewModel(apiClient, session, file, lineNumber, leftBuffer);
150+
var newThread = new NewInlineCommentThreadViewModel(session, file, lineNumber, leftBuffer);
152151
threadSubscription = newThread.Finished.Subscribe(_ => UpdateThread().Forget());
153152
Thread = newThread;
154153
}
@@ -182,12 +181,6 @@ async Task SessionChanged(IPullRequestSession session)
182181
fileSubscription = file.WhenAnyValue(x => x.InlineCommentThreads).Subscribe(_ => UpdateThread().Forget());
183182
}
184183

185-
Task<IApiClient> CreateApiClient(ILocalRepositoryModel repository)
186-
{
187-
var hostAddress = HostAddress.Create(repository.CloneUrl.Host);
188-
return apiClientFactory.Create(hostAddress);
189-
}
190-
191184
async Task<string> GetPlaceholderBodyToPreserve()
192185
{
193186
var lastComment = Thread?.Comments.LastOrDefault();

0 commit comments

Comments
 (0)
0