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 pathIssueService.cs
More file actions
163 lines (151 loc) · 6.51 KB
/
IssueService.cs
File metadata and controls
163 lines (151 loc) · 6.51 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
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Models;
using GitHub.Primitives;
using Octokit.GraphQL;
using Octokit.GraphQL.Model;
using static Octokit.GraphQL.Variable;
using IssueState = GitHub.Models.IssueState;
namespace GitHub.Services
{
[Export(typeof(IIssueService))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class IssueService : IIssueService
{
static ICompiledQuery<Page<ActorModel>> readAssignableUsers;
static ICompiledQuery<Page<IssueListItemModel>> readIssues;
static ICompiledQuery<IssueDetailModel> readIssue;
readonly IGraphQLClientFactory graphqlFactory;
[ImportingConstructor]
public IssueService(IGraphQLClientFactory graphqlFactory)
{
this.graphqlFactory = graphqlFactory;
}
public async Task<Page<IssueListItemModel>> ReadIssues(
HostAddress address,
string owner,
string name,
string after,
IssueState[] states)
{
if (readIssues == null)
{
readIssues = new Query()
.Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
.Issues(
first: 100,
after: Var(nameof(after)),
orderBy: new IssueOrder { Direction = OrderDirection.Desc, Field = IssueOrderField.CreatedAt },
states: Var(nameof(states)))
.Select(page => new Page<IssueListItemModel>
{
EndCursor = page.PageInfo.EndCursor,
HasNextPage = page.PageInfo.HasNextPage,
TotalCount = page.TotalCount,
Items = page.Nodes.Select(issue => new IssueListItemModel
{
Id = issue.Id.Value,
Author = new ActorModel
{
Login = issue.Author.Login,
AvatarUrl = issue.Author.AvatarUrl(null),
},
CommentCount = issue.Comments(0, null, null, null).TotalCount,
Number = issue.Number,
State = issue.State.FromGraphQl(),
Title = issue.Title,
UpdatedAt = issue.UpdatedAt,
}).ToList(),
}).Compile();
}
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
var vars = new Dictionary<string, object>
{
{ nameof(owner), owner },
{ nameof(name), name },
{ nameof(after), after },
{ nameof(states), states.Select(x => x.ToGraphQL()).ToList() },
};
return await graphql.Run(readIssues, vars).ConfigureAwait(false);
}
public async Task<IssueDetailModel> ReadIssue(HostAddress address, string owner, string name, int number)
{
if (readIssue == null)
{
readIssue = new Query()
.Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
.Issue(Var(nameof(number)))
.Select(issue => new IssueDetailModel
{
Id = issue.Id.Value,
Number = issue.Number,
Author = new ActorModel
{
Login = issue.Author.Login,
AvatarUrl = issue.Author.AvatarUrl(null),
},
Title = issue.Title,
Body = issue.Body,
UpdatedAt = issue.UpdatedAt,
Comments = issue.Comments(null, null, null, null).AllPages().Select(comment => new CommentModel
{
Id = comment.Id.Value,
DatabaseId = comment.DatabaseId.Value,
Author = new ActorModel
{
Login = comment.Author.Login,
AvatarUrl = comment.Author.AvatarUrl(null),
},
Body = comment.Body,
CreatedAt = comment.CreatedAt,
Url = comment.Url,
}).ToList(),
}).Compile();
}
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
var vars = new Dictionary<string, object>
{
{ nameof(owner), owner },
{ nameof(name), name },
{ nameof(number), number },
};
return await graphql.Run(readIssue, vars).ConfigureAwait(false);
}
public async Task<Page<ActorModel>> ReadAssignableUsers(
HostAddress address,
string owner,
string name,
string after)
{
if (readAssignableUsers == null)
{
readAssignableUsers = new Query()
.Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
.AssignableUsers(first: 100, after: Var(nameof(after)))
.Select(connection => new Page<ActorModel>
{
EndCursor = connection.PageInfo.EndCursor,
HasNextPage = connection.PageInfo.HasNextPage,
TotalCount = connection.TotalCount,
Items = connection.Nodes.Select(user => new ActorModel
{
AvatarUrl = user.AvatarUrl(30),
Login = user.Login,
}).ToList(),
}).Compile();
}
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
var vars = new Dictionary<string, object>
{
{ nameof(owner), owner },
{ nameof(name), name },
{ nameof(after), after },
};
return await graphql.Run(readAssignableUsers, vars).ConfigureAwait(false);
}
}
}