Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/GitHub.Exports.Reactive/Services/IPullRequestSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ public interface IPullRequestSession
/// </remarks>
string RepositoryOwner { get; }

/// <summary>
/// Adds a new comment to the session.
/// </summary>
/// <param name="comment">The comment.</param>
Task AddComment(IPullRequestReviewCommentModel comment);

/// <summary>
/// Gets all files touched by the pull request.
/// </summary>
Expand Down Expand Up @@ -92,6 +86,24 @@ Task<IPullRequestSessionFile> GetFile(
/// </returns>
string GetRelativePath(string path);

/// <summary>
/// Posts a new PR review comment.
/// </summary>
/// <param name="body">The comment body.</param>
/// <param name="commitId">THe SHA of the commit to comment on.</param>
/// <param name="path">The relative path of the file to comment on.</param>
/// <param name="position">The line index in the diff to comment on.</param>
/// <returns>A comment model.</returns>
Task<IPullRequestReviewCommentModel> PostReviewComment(string body, string commitId, string path, int position);

/// <summary>
/// Posts a PR review comment reply.
/// </summary>
/// <param name="body">The comment body.</param>
/// <param name="inReplyTo">The comment ID to reply to.</param>
/// <returns></returns>
Task<IPullRequestReviewCommentModel> PostReviewComment(string body, int inReplyTo);

/// <summary>
/// Updates the pull request session with a new pull request model in response to a refresh
/// from the server.
Expand Down
12 changes: 12 additions & 0 deletions src/GitHub.Exports/Models/UsageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class UsageModel
public int NumberOfWelcomeDocsClicks { get; set; }
public int NumberOfWelcomeTrainingClicks { get; set; }
public int NumberOfGitHubPaneHelpClicks { get; set; }
public int NumberOfPRDetailsViewChanges { get; set; }
public int NumberOfPRDetailsViewFile { get; set; }
public int NumberOfPRDetailsCompareWithSolution { get; set; }
public int NumberOfPRDetailsOpenFileInSolution { get; set; }
public int NumberOfPRReviewDiffViewInlineCommentOpen { get; set; }
public int NumberOfPRReviewDiffViewInlineCommentPost { get; set; }

public UsageModel Clone(bool includeWeekly, bool includeMonthly)
{
Expand Down Expand Up @@ -61,6 +67,12 @@ public UsageModel Clone(bool includeWeekly, bool includeMonthly)
NumberOfWelcomeDocsClicks = NumberOfWelcomeDocsClicks,
NumberOfWelcomeTrainingClicks = NumberOfWelcomeTrainingClicks,
NumberOfGitHubPaneHelpClicks = NumberOfGitHubPaneHelpClicks,
NumberOfPRDetailsViewChanges = NumberOfPRDetailsViewChanges,
NumberOfPRDetailsViewFile = NumberOfPRDetailsViewFile,
NumberOfPRDetailsCompareWithSolution = NumberOfPRDetailsCompareWithSolution,
NumberOfPRDetailsOpenFileInSolution = NumberOfPRDetailsOpenFileInSolution,
NumberOfPRReviewDiffViewInlineCommentOpen = NumberOfPRReviewDiffViewInlineCommentOpen,
NumberOfPRReviewDiffViewInlineCommentPost = NumberOfPRReviewDiffViewInlineCommentPost,
};
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/GitHub.Exports/Services/IUsageTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ public interface IUsageTracker
Task IncrementWelcomeDocsClicks();
Task IncrementWelcomeTrainingClicks();
Task IncrementGitHubPaneHelpClicks();
Task IncrementPRDetailsViewChanges();
Task IncrementPRDetailsViewFile();
Task IncrementPRDetailsCompareWithSolution();
Task IncrementPRDetailsOpenFileInSolution();
Task IncrementPRReviewDiffViewInlineCommentOpen();
Task IncrementPRReviewDiffViewInlineCommentPost();
}
}
36 changes: 36 additions & 0 deletions src/GitHub.InlineReviews/Services/IPullRequestSessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,41 @@ Task<byte[]> ExtractFileFromGit(
/// The merge base SHA for the PR.
/// </returns>
Task<string> GetPullRequestMergeBase(ILocalRepositoryModel repository, IPullRequestModel pullRequest);

/// <summary>
/// Posts a new PR review comment.
/// </summary>
/// <param name="repository">The repository.</param>
/// <param name="user">The user posting the comment.</param>
/// <param name="number">The pull request number.</param>
/// <param name="body">The comment body.</param>
/// <param name="commitId">THe SHA of the commit to comment on.</param>
/// <param name="path">The relative path of the file to comment on.</param>
/// <param name="position">The line index in the diff to comment on.</param>
/// <returns>A model representing the posted comment.</returns>
Task<IPullRequestReviewCommentModel> PostReviewComment(
ILocalRepositoryModel repository,
IAccount user,
int number,
string body,
string commitId,
string path,
int position);

/// <summary>
/// Posts a PR review comment reply.
/// </summary>
/// <param name="repository">The repository.</param>
/// <param name="user">The user posting the comment.</param>
/// <param name="number">The pull request number.</param>
/// <param name="body">The comment body.</param>
/// <param name="inReplyTo">The comment ID to reply to.</param>
/// <returns>A model representing the posted comment.</returns>
Task<IPullRequestReviewCommentModel> PostReviewComment(
ILocalRepositoryModel repository,
IAccount user,
int number,
string body,
int inReplyTo);
}
}
12 changes: 11 additions & 1 deletion src/GitHub.InlineReviews/Services/InlineCommentPeekService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using GitHub.InlineReviews.Tags;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Differencing;
Expand All @@ -28,16 +29,19 @@ class InlineCommentPeekService : IInlineCommentPeekService
readonly IApiClientFactory apiClientFactory;
readonly IOutliningManagerService outliningService;
readonly IPeekBroker peekBroker;
readonly IUsageTracker usageTracker;

[ImportingConstructor]
public InlineCommentPeekService(
IApiClientFactory apiClientFactory,
IOutliningManagerService outliningManager,
IPeekBroker peekBroker)
IPeekBroker peekBroker,
IUsageTracker usageTracker)
{
this.apiClientFactory = apiClientFactory;
this.outliningService = outliningManager;
this.peekBroker = peekBroker;
this.usageTracker = usageTracker;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -152,6 +156,12 @@ Tuple<ITextSnapshotLine, ITrackingPoint> GetLineAndTrackingPoint(ITextView textV

var line = snapshot.GetLineFromLineNumber(tag.LineNumber);
var trackingPoint = snapshot.CreateTrackingPoint(line.Start.Position, PointTrackingMode.Positive);

ExpandCollapsedRegions(textView, line.Extent);
peekBroker.TriggerPeekSession(textView, trackingPoint, InlineCommentPeekRelationship.Instance.Name);

usageTracker.IncrementPRReviewDiffViewInlineCommentOpen().Forget();

return Tuple.Create(line, trackingPoint);
}

Expand Down
45 changes: 36 additions & 9 deletions src/GitHub.InlineReviews/Services/PullRequestSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ public PullRequestSession(
RepositoryOwner = repositoryOwner;
}

/// <inheritdoc/>
public async Task AddComment(IPullRequestReviewCommentModel comment)
{
PullRequest.ReviewComments = PullRequest.ReviewComments
.Concat(new[] { comment })
.ToList();
await Update(PullRequest);
}

/// <inheritdoc/>
public async Task<IReadOnlyList<IPullRequestSessionFile>> GetAllFiles()
{
Expand Down Expand Up @@ -125,6 +116,34 @@ public string GetRelativePath(string path)
return null;
}

/// <inheritdoc/>
public async Task<IPullRequestReviewCommentModel> PostReviewComment(string body, string commitId, string path, int position)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these methods be added as part of this PR or have they snuck in from somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're part of the PR - I had to move things around a bit in order to be able to get hold of an IUsageTracker at the right place.

{
var model = await service.PostReviewComment(
LocalRepository,
User,
PullRequest.Number,
body,
commitId,
path,
position);
await AddComment(model);
return model;
}

/// <inheritdoc/>
public async Task<IPullRequestReviewCommentModel> PostReviewComment(string body, int inReplyTo)
{
var model = await service.PostReviewComment(
LocalRepository,
User,
PullRequest.Number,
body,
inReplyTo);
await AddComment(model);
return model;
}

/// <inheritdoc/>
public async Task UpdateEditorContent(string relativePath)
{
Expand Down Expand Up @@ -159,6 +178,14 @@ public async Task Update(IPullRequestModel pullRequest)
}
}

async Task AddComment(IPullRequestReviewCommentModel comment)
{
PullRequest.ReviewComments = PullRequest.ReviewComments
.Concat(new[] { comment })
.ToList();
await Update(PullRequest);
}

async Task UpdateFile(PullRequestSessionFile file)
{
// NOTE: We must call GetPullRequestMergeBase before GetFileContent.
Expand Down
85 changes: 84 additions & 1 deletion src/GitHub.InlineReviews/Services/PullRequestSessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Factories;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using LibGit2Sharp;

Expand All @@ -18,18 +21,24 @@ class PullRequestSessionService : IPullRequestSessionService
readonly IGitService gitService;
readonly IGitClient gitClient;
readonly IDiffService diffService;
readonly IApiClientFactory apiClientFactory;
readonly IUsageTracker usageTracker;

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

[ImportingConstructor]
public PullRequestSessionService(
IGitService gitService,
IGitClient gitClient,
IDiffService diffService)
IDiffService diffService,
IApiClientFactory apiClientFactory,
IUsageTracker usageTracker)
{
this.gitService = gitService;
this.gitClient = gitClient;
this.diffService = diffService;
this.apiClientFactory = apiClientFactory;
this.usageTracker = usageTracker;

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

/// <inheritdoc/>
public async Task<IPullRequestReviewCommentModel> PostReviewComment(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More new methods. This this PR depend on some other PR that hasn't been merged yet?

ILocalRepositoryModel repository,
IAccount user,
int number,
string body,
string commitId,
string path,
int position)
{
var address = HostAddress.Create(repository.CloneUrl.Host);
var apiClient = await apiClientFactory.Create(address);

var result = await apiClient.CreatePullRequestReviewComment(
repository.Owner,
repository.Name,
number,
body,
commitId,
path,
position);

await usageTracker.IncrementPRReviewDiffViewInlineCommentPost();

return new PullRequestReviewCommentModel
{
Body = result.Body,
CommitId = result.CommitId,
DiffHunk = result.DiffHunk,
Id = result.Id,
OriginalCommitId = result.OriginalCommitId,
OriginalPosition = result.OriginalPosition,
Path = result.Path,
Position = result.Position,
CreatedAt = result.CreatedAt,
User = user,
};
}

/// <inheritdoc/>
public async Task<IPullRequestReviewCommentModel> PostReviewComment(
ILocalRepositoryModel repository,
IAccount user,
int number,
string body,
int inReplyTo)
{
var address = HostAddress.Create(repository.CloneUrl.Host);
var apiClient = await apiClientFactory.Create(address);

var result = await apiClient.CreatePullRequestReviewComment(
repository.Owner,
repository.Name,
number,
body,
inReplyTo);

await usageTracker.IncrementPRReviewDiffViewInlineCommentPost();

return new PullRequestReviewCommentModel
{
Body = result.Body,
CommitId = result.CommitId,
DiffHunk = result.DiffHunk,
Id = result.Id,
OriginalCommitId = result.OriginalCommitId,
OriginalPosition = result.OriginalPosition,
Path = result.Path,
Position = result.Position,
CreatedAt = result.CreatedAt,
User = user,
};
}

Task<IRepository> GetRepository(ILocalRepositoryModel repository)
{
return Task.Factory.StartNew(() => gitService.GetRepository(repository.LocalPath));
Expand Down
13 changes: 3 additions & 10 deletions src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,17 @@ async Task UpdateThread()
var lineAndLeftBuffer = peekService.GetLineNumber(peekSession, triggerPoint);
var lineNumber = lineAndLeftBuffer.Item1;
var leftBuffer = lineAndLeftBuffer.Item2;
var thread = file.InlineCommentThreads.FirstOrDefault(x =>
var thread = file.InlineCommentThreads.FirstOrDefault(x =>
x.LineNumber == lineNumber &&
((leftBuffer && x.DiffLineType == DiffChangeType.Delete) || (!leftBuffer && x.DiffLineType != DiffChangeType.Delete)));
var apiClient = await CreateApiClient(session.LocalRepository);

if (thread != null)
{
Thread = new InlineCommentThreadViewModel(apiClient, session, thread.Comments);
Thread = new InlineCommentThreadViewModel(session, thread.Comments);
}
else
{
var newThread = new NewInlineCommentThreadViewModel(apiClient, session, file, lineNumber, leftBuffer);
var newThread = new NewInlineCommentThreadViewModel(session, file, lineNumber, leftBuffer);
threadSubscription = newThread.Finished.Subscribe(_ => UpdateThread().Forget());
Thread = newThread;
}
Expand Down Expand Up @@ -182,12 +181,6 @@ async Task SessionChanged(IPullRequestSession session)
fileSubscription = file.WhenAnyValue(x => x.InlineCommentThreads).Subscribe(_ => UpdateThread().Forget());
}

Task<IApiClient> CreateApiClient(ILocalRepositoryModel repository)
{
var hostAddress = HostAddress.Create(repository.CloneUrl.Host);
return apiClientFactory.Create(hostAddress);
}

async Task<string> GetPlaceholderBodyToPreserve()
{
var lastComment = Thread?.Comments.LastOrDefault();
Expand Down
Loading