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

Commit 1c3ba1c

Browse files
Query the pull request list from graphql
1 parent 4b2c16c commit 1c3ba1c

File tree

6 files changed

+141
-16
lines changed

6 files changed

+141
-16
lines changed

src/GitHub.App/Services/PullRequestService.cs

+126-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using static Octokit.GraphQL.Variable;
3131
using CheckConclusionState = GitHub.Models.CheckConclusionState;
3232
using CheckStatusState = GitHub.Models.CheckStatusState;
33+
using PullRequestState = GitHub.Models.PullRequestState;
3334
using StatusState = GitHub.Models.StatusState;
3435

3536
namespace GitHub.Services
@@ -84,15 +85,15 @@ public PullRequestService(
8485
this.tempFileMappings = new Dictionary<string, (string commitId, string repoPath)>(StringComparer.OrdinalIgnoreCase);
8586
}
8687

87-
public async Task<Page<PullRequestListItemModel>> ReadPullRequests(
88-
HostAddress address,
88+
public async Task<Page<PullRequestListItemModel>> ReadPullRequests(HostAddress address,
8989
string owner,
9090
string name,
9191
string after,
92-
Models.PullRequestState[] states)
92+
PullRequestState[] states, string author)
9393
{
94-
94+
string filter = null;
9595
ICompiledQuery<Page<PullRequestListItemModel>> query;
96+
ICompiledQuery<Page<PullRequestListItemModel>> query2;
9697

9798
if (address.IsGitHubDotCom())
9899
{
@@ -154,6 +155,56 @@ public async Task<Page<PullRequestListItemModel>> ReadPullRequests(
154155
}
155156

156157
query = readPullRequests;
158+
159+
query2 = new Query().Search(Var(nameof(filter)), first: 100, after: Var(nameof(after)), type: SearchType.Issue)
160+
.Select(page => new Page<PullRequestListItemModel>
161+
{
162+
EndCursor = page.PageInfo.EndCursor,
163+
HasNextPage = page.PageInfo.HasNextPage,
164+
TotalCount = page.IssueCount,
165+
Items = page.Nodes.Select(issueOrPr => issueOrPr
166+
.Switch<PullRequestListItemModel>(when => when.PullRequest(pr => new ListItemAdapter
167+
{
168+
Id = pr.Id.Value,
169+
LastCommit = pr.Commits(null, null, 1, null).Nodes.Select(commit =>
170+
new LastCommitSummaryAdapter
171+
{
172+
CheckSuites = commit.Commit.CheckSuites(null, null, null, null, null).AllPages(10)
173+
.Select(suite => new CheckSuiteSummaryModel
174+
{
175+
CheckRuns = suite.CheckRuns(null, null, null, null, null).AllPages(10)
176+
.Select(run => new CheckRunSummaryModel
177+
{
178+
Conclusion = run.Conclusion.FromGraphQl(),
179+
Status = run.Status.FromGraphQl()
180+
}).ToList(),
181+
}).ToList(),
182+
Statuses = commit.Commit.Status
183+
.Select(context =>
184+
context.Contexts.Select(statusContext => new StatusSummaryModel
185+
{
186+
State = statusContext.State.FromGraphQl(),
187+
}).ToList()
188+
).SingleOrDefault()
189+
}).ToList().FirstOrDefault(),
190+
Author = new ActorModel
191+
{
192+
Login = pr.Author.Login,
193+
AvatarUrl = pr.Author.AvatarUrl(null),
194+
},
195+
CommentCount = pr.Comments(0, null, null, null).TotalCount,
196+
Number = pr.Number,
197+
Reviews = pr.Reviews(null, null, null, null, null, null).AllPages().Select(review => new ReviewAdapter
198+
{
199+
Body = review.Body,
200+
CommentCount = review.Comments(null, null, null, null).TotalCount,
201+
}).ToList(),
202+
State = pr.State.FromGraphQl(),
203+
Title = pr.Title,
204+
UpdatedAt = pr.UpdatedAt,
205+
}))).ToList()
206+
})
207+
.Compile();
157208
}
158209
else
159210
{
@@ -207,6 +258,48 @@ public async Task<Page<PullRequestListItemModel>> ReadPullRequests(
207258
}
208259

209260
query = readPullRequestsEnterprise;
261+
262+
query2 = new Query().Search(Var(nameof(filter)), first: 100, after: Var(nameof(after)), type: SearchType.Issue)
263+
.Select(page => new Page<PullRequestListItemModel>
264+
{
265+
EndCursor = page.PageInfo.EndCursor,
266+
HasNextPage = page.PageInfo.HasNextPage,
267+
TotalCount = page.IssueCount,
268+
Items = page.Nodes.Select(issueOrPr => issueOrPr
269+
.Switch<PullRequestListItemModel>(when => when.PullRequest(pr => new ListItemAdapter
270+
{
271+
Id = pr.Id.Value,
272+
LastCommit = pr.Commits(null, null, 1, null).Nodes.Select(commit =>
273+
new LastCommitSummaryAdapter
274+
{
275+
Statuses = commit.Commit.Status.Select(context =>
276+
context == null
277+
? null
278+
: context.Contexts
279+
.Select(statusContext => new StatusSummaryModel
280+
{
281+
State = statusContext.State.FromGraphQl()
282+
}).ToList()
283+
).SingleOrDefault()
284+
}).ToList().FirstOrDefault(),
285+
Author = new ActorModel
286+
{
287+
Login = pr.Author.Login,
288+
AvatarUrl = pr.Author.AvatarUrl(null),
289+
},
290+
CommentCount = pr.Comments(0, null, null, null).TotalCount,
291+
Number = pr.Number,
292+
Reviews = pr.Reviews(null, null, null, null, null, null).AllPages().Select(review => new ReviewAdapter
293+
{
294+
Body = review.Body,
295+
CommentCount = review.Comments(null, null, null, null).TotalCount,
296+
}).ToList(),
297+
State = pr.State.FromGraphQl(),
298+
Title = pr.Title,
299+
UpdatedAt = pr.UpdatedAt,
300+
}))).ToList()
301+
})
302+
.Compile();
210303
}
211304

212305
var graphql = await graphqlFactory.CreateConnection(address);
@@ -220,6 +313,35 @@ public async Task<Page<PullRequestListItemModel>> ReadPullRequests(
220313

221314
var result = await graphql.Run(query, vars);
222315

316+
filter = $"type:pr repo:{owner}/{name}";
317+
if (states.Any())
318+
{
319+
var filterState = states.First();
320+
switch (filterState)
321+
{
322+
case PullRequestState.Open:
323+
filter += " is:open";
324+
break;
325+
case PullRequestState.Closed:
326+
filter += " is:unmerged is:closed";
327+
break;
328+
case PullRequestState.Merged:
329+
filter += " is:merged is:closed";
330+
break;
331+
332+
default:
333+
throw new ArgumentOutOfRangeException(nameof(filterState));
334+
}
335+
}
336+
337+
var vars2 = new Dictionary<string, object>
338+
{
339+
{ nameof(filter), filter },
340+
{ nameof(after), after },
341+
};
342+
343+
var result2 = await graphql.Run(query2, vars2);
344+
223345
foreach (var item in result.Items.Cast<ListItemAdapter>())
224346
{
225347
item.CommentCount += item.Reviews.Sum(x => x.Count);

src/GitHub.App/ViewModels/GitHubPane/IssueListViewModelBase.cs

-7
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,6 @@ bool FilterItem(object o)
286286
}
287287
}
288288

289-
if (result && AuthorFilter.Selected != null)
290-
{
291-
result = item.Author.Login.Equals(
292-
AuthorFilter.Selected.Login,
293-
StringComparison.CurrentCultureIgnoreCase);
294-
}
295-
296289
return result;
297290
}
298291

src/GitHub.App/ViewModels/GitHubPane/PullRequestListViewModel.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ protected override async Task<Page<PullRequestListItemModel>> LoadPage(string af
141141
break;
142142
}
143143

144+
var authorLogin = owner.AuthorFilter.Selected?.Login;
145+
144146
var result = await owner.service.ReadPullRequests(
145147
HostAddress.Create(owner.RemoteRepository.CloneUrl),
146148
owner.RemoteRepository.Owner,
147149
owner.RemoteRepository.Name,
148150
after,
149-
states).ConfigureAwait(false);
151+
states,
152+
authorLogin).ConfigureAwait(false);
150153
return result;
151154
}
152155
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ public interface IPullRequestService : IIssueishService
1919
/// <param name="name">The repository name.</param>
2020
/// <param name="after">The end cursor of the previous page, or null for the first page.</param>
2121
/// <param name="states">The pull request states to filter by</param>
22+
/// <param name="author"></param>
2223
/// <returns>A page of pull request item models.</returns>
23-
Task<Page<PullRequestListItemModel>> ReadPullRequests(
24-
HostAddress address,
24+
Task<Page<PullRequestListItemModel>> ReadPullRequests(HostAddress address,
2525
string owner,
2626
string name,
2727
string after,
28-
PullRequestState[] states);
28+
PullRequestState[] states,
29+
string author);
2930

3031
/// <summary>
3132
/// Reads a page of users that can be assigned to pull requests.

src/GitHub.Exports/Models/PullRequestListItemModel.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace GitHub.Models
45
{
6+
public class PullRequestSearchItemModel
7+
{
8+
public IReadOnlyList<PullRequestListItemModel> Result { get; set; }
9+
}
10+
511
/// <summary>
612
/// Holds an overview of a pull request for display in the PR list.
713
/// </summary>

test/GitHub.App.UnitTests/ViewModels/GitHubPane/IssueListViewModelBaseTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected static IPullRequestSessionManager CreateSessionManager(PullRequestDeta
118118
protected static IPullRequestService CreatePullRequestService(int itemCount = 10)
119119
{
120120
var result = Substitute.For<IPullRequestService>();
121-
result.ReadPullRequests(null, null, null, null, null).ReturnsForAnyArgs(
121+
result.ReadPullRequests(null, null, null, null, null, null).ReturnsForAnyArgs(
122122
new Page<PullRequestListItemModel>
123123
{
124124
Items = Enumerable.Range(0, itemCount).Select(x => new PullRequestListItemModel

0 commit comments

Comments
 (0)