This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPullRequestReviewCommentThreadViewModel.cs
More file actions
273 lines (230 loc) · 9.22 KB
/
PullRequestReviewCommentThreadViewModel.cs
File metadata and controls
273 lines (230 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Factories;
using GitHub.Models;
using GitHub.Models.Drafts;
using GitHub.Primitives;
using GitHub.Services;
using ReactiveUI;
using static System.FormattableString;
namespace GitHub.ViewModels
{
/// <summary>
/// A thread of pull request review comments.
/// </summary>
[Export(typeof(IPullRequestReviewCommentThreadViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PullRequestReviewCommentThreadViewModel : CommentThreadViewModel, IPullRequestReviewCommentThreadViewModel
{
readonly ReactiveList<ICommentViewModel> comments = new ReactiveList<ICommentViewModel>();
readonly IViewViewModelFactory factory;
readonly ObservableAsPropertyHelper<bool> needsPush;
IPullRequestSessionFile file;
bool isNewThread;
/// <summary>
/// Initializes a new instance of the <see cref="PullRequestReviewCommentThreadViewModel"/> class.
/// </summary>
/// <param name="draftStore">The message draft store.</param>
/// <param name="factory">The view model factory.</param>
[ImportingConstructor]
public PullRequestReviewCommentThreadViewModel(
IMessageDraftStore draftStore,
IViewViewModelFactory factory)
: base(draftStore)
{
Guard.ArgumentNotNull(factory, nameof(factory));
this.factory = factory;
needsPush = this.WhenAnyValue(
x => x.File.CommitSha,
x => x.IsNewThread,
(sha, isNew) => isNew && sha == null)
.ToProperty(this, x => x.NeedsPush);
}
/// <inheritdoc/>
public IReactiveList<ICommentViewModel> Comments => comments;
/// <inheritdoc/>
public IPullRequestSession Session { get; private set; }
/// <inheritdoc/>
public IPullRequestSessionFile File
{
get => file;
private set => this.RaiseAndSetIfChanged(ref file, value);
}
/// <inheritdoc/>
public int LineNumber { get; private set; }
/// <inheritdoc/>
public DiffSide Side { get; private set; }
/// <inheritdoc/>
public bool IsOutdated { get; private set; }
/// <inheritdoc/>
public bool IsResolved { get; private set; }
public bool IsNewThread
{
get => isNewThread;
private set => this.RaiseAndSetIfChanged(ref isNewThread, value);
}
/// <inheritdoc/>
public bool NeedsPush => needsPush.Value;
/// <inheritdoc/>
IReadOnlyReactiveList<ICommentViewModel> IPullRequestReviewCommentThreadViewModel.Comments => comments;
/// <inheritdoc/>
public async Task InitializeAsync(
IPullRequestSession session,
IPullRequestSessionFile file,
IInlineCommentThreadModel thread,
bool addPlaceholder)
{
Guard.ArgumentNotNull(session, nameof(session));
await base.InitializeAsync(session.User).ConfigureAwait(true);
Session = session;
File = file;
LineNumber = thread.LineNumber;
Side = thread.DiffLineType == DiffChangeType.Delete ? DiffSide.Left : DiffSide.Right;
IsResolved = thread.IsResolved;
IsOutdated = thread.IsOutdated;
foreach (var comment in thread.Comments)
{
var vm = factory.CreateViewModel<IPullRequestReviewCommentViewModel>();
await vm.InitializeAsync(
session,
this,
comment.Review,
comment.Comment,
CommentEditState.None).ConfigureAwait(false);
Comments.Add(vm);
}
if (addPlaceholder)
{
var vm = factory.CreateViewModel<IPullRequestReviewCommentViewModel>();
await vm.InitializeAsPlaceholderAsync(
session,
this,
session.HasPendingReview,
false).ConfigureAwait(true);
var (key, secondaryKey) = GetDraftKeys(vm);
var draft = await DraftStore.GetDraft<PullRequestReviewCommentDraft>(key, secondaryKey).ConfigureAwait(true);
if (draft?.Side == Side)
{
await vm.BeginEdit.Execute();
vm.Body = draft.Body;
}
InitializePlaceholder(vm);
comments.Add(vm);
}
}
/// <inheritdoc/>
public async Task InitializeNewAsync(
IPullRequestSession session,
IPullRequestSessionFile file,
int lineNumber,
DiffSide side,
bool isEditing)
{
Guard.ArgumentNotNull(session, nameof(session));
await base.InitializeAsync(session.User).ConfigureAwait(false);
Session = session;
File = file;
LineNumber = lineNumber;
Side = side;
IsNewThread = true;
var vm = factory.CreateViewModel<IPullRequestReviewCommentViewModel>();
await vm.InitializeAsPlaceholderAsync(session, this, session.HasPendingReview, isEditing).ConfigureAwait(false);
var (key, secondaryKey) = GetDraftKeys(vm);
var draft = await DraftStore.GetDraft<PullRequestReviewCommentDraft>(key, secondaryKey).ConfigureAwait(true);
if (draft?.Side == side)
{
vm.Body = draft.Body;
}
InitializePlaceholder(vm);
comments.Add(vm);
}
public override async Task PostComment(ICommentViewModel comment)
{
Guard.ArgumentNotNull(comment, nameof(comment));
await DeleteDraft(comment).ConfigureAwait(false);
try
{
if (IsNewThread)
{
var diffPosition = File.Diff
.SelectMany(x => x.Lines)
.FirstOrDefault(x =>
{
var line = Side == DiffSide.Left ? x.OldLineNumber : x.NewLineNumber;
return line == LineNumber + 1;
});
if (diffPosition == null)
{
throw new InvalidOperationException("Unable to locate line in diff.");
}
await Session.PostReviewComment(
comment.Body,
File.CommitSha,
File.RelativePath.Replace("\\", "/"),
File.Diff,
diffPosition.DiffLineNumber).ConfigureAwait(false);
}
else
{
var replyId = Comments[0].Id;
await Session.PostReviewComment(comment.Body, replyId).ConfigureAwait(false);
}
}
catch
{
UpdateDraft(comment).Forget();
throw;
}
}
public override async Task EditComment(ICommentViewModel comment)
{
Guard.ArgumentNotNull(comment, nameof(comment));
await Session.EditComment(comment.Id, comment.Body).ConfigureAwait(false);
}
public override async Task DeleteComment(ICommentViewModel comment)
{
Guard.ArgumentNotNull(comment, nameof(comment));
await Session.DeleteComment(comment.PullRequestId, comment.DatabaseId).ConfigureAwait(false);
}
public static (string key, string secondaryKey) GetDraftKeys(
UriString cloneUri,
int pullRequestNumber,
string relativePath,
int lineNumber)
{
relativePath = relativePath.Replace("\\", "/");
var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{relativePath}");
return (key, lineNumber.ToString(CultureInfo.InvariantCulture));
}
protected override CommentDraft BuildDraft(ICommentViewModel comment)
{
return !string.IsNullOrEmpty(comment.Body) ?
new PullRequestReviewCommentDraft
{
Body = comment.Body,
Side = Side,
UpdatedAt = DateTimeOffset.UtcNow,
} : null;
}
protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewModel comment)
{
return GetDraftKeys(
Session.LocalRepository.CloneUrl.WithOwner(Session.RepositoryOwner),
Session.PullRequest.Number,
File.RelativePath,
LineNumber);
}
async Task UpdateDraft(ICommentViewModel comment)
{
var draft = BuildDraft(comment);
var (key, secondaryKey) = GetDraftKeys(comment);
await DraftStore.UpdateDraft(key, secondaryKey, draft).ConfigureAwait(true);
}
}
}