-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubGraphQLClient.cs
More file actions
334 lines (303 loc) · 13.5 KB
/
GitHubGraphQLClient.cs
File metadata and controls
334 lines (303 loc) · 13.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2026 DEMA Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Net.Http.Headers;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
namespace DemaConsulting.BuildMark.RepoConnectors.GitHub;
/// <summary>
/// Helper class for executing GitHub GraphQL queries.
/// </summary>
internal sealed class GitHubGraphQLClient : IDisposable
{
/// <summary>
/// Default GitHub GraphQL API endpoint.
/// </summary>
private const string DefaultGitHubGraphQLEndpoint = "https://api.github.com/graphql";
/// <summary>
/// GraphQL HTTP client for making GraphQL requests.
/// </summary>
private readonly GraphQLHttpClient _graphqlClient;
/// <summary>
/// Indicates whether this instance owns the GraphQL client and should dispose it.
/// </summary>
private readonly bool _ownsGraphQLClient;
/// <summary>
/// Initializes a new instance of the <see cref="GitHubGraphQLClient"/> class.
/// </summary>
/// <param name="token">GitHub authentication token.</param>
/// <param name="graphqlEndpoint">Optional GraphQL endpoint URL. Defaults to public GitHub API. For GitHub Enterprise, use https://your-github-enterprise/api/graphql.</param>
public GitHubGraphQLClient(string token, string? graphqlEndpoint = null)
{
// Initialize HTTP client with authentication and user agent headers
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
httpClient.DefaultRequestHeaders.UserAgent.Add(
new ProductInfoHeaderValue("BuildMark", "1.0"));
// Create GraphQL HTTP client with the configured HTTP client
var options = new GraphQLHttpClientOptions
{
EndPoint = new Uri(graphqlEndpoint ?? DefaultGitHubGraphQLEndpoint)
};
_graphqlClient = new GraphQLHttpClient(options, new SystemTextJsonSerializer(), httpClient);
_ownsGraphQLClient = true;
}
/// <summary>
/// Initializes a new instance of the <see cref="GitHubGraphQLClient"/> class with a pre-configured HTTP client.
/// </summary>
/// <param name="httpClient">Pre-configured HTTP client for making requests. Useful for testing with mocked responses.</param>
/// <param name="graphqlEndpoint">Optional GraphQL endpoint URL. Defaults to public GitHub API. For GitHub Enterprise, use https://your-github-enterprise/api/graphql.</param>
/// <remarks>
/// This constructor is intended for testing scenarios where you need to inject a mocked HttpClient with pre-canned responses.
/// The caller is responsible for disposing the provided HttpClient.
/// </remarks>
internal GitHubGraphQLClient(HttpClient httpClient, string? graphqlEndpoint = null)
{
// Use provided HTTP client (typically a mocked one for testing)
var options = new GraphQLHttpClientOptions
{
EndPoint = new Uri(graphqlEndpoint ?? DefaultGitHubGraphQLEndpoint)
};
_graphqlClient = new GraphQLHttpClient(options, new SystemTextJsonSerializer(), httpClient);
_ownsGraphQLClient = false;
}
/// <summary>
/// Gets all commits for a branch using GraphQL with pagination.
/// </summary>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="branch">Branch name (e.g., 'main'). Will be automatically converted to fully qualified ref name.</param>
/// <returns>List of commit SHAs on the branch.</returns>
public async Task<List<string>> GetCommitsAsync(
string owner,
string repo,
string branch)
{
try
{
var allCommitShas = new List<string>();
string? afterCursor = null;
bool hasNextPage;
// Convert branch name to fully qualified ref name if needed
var qualifiedBranch = branch.StartsWith("refs/") ? branch : $"refs/heads/{branch}";
// Paginate through all commits on the branch
do
{
// Create GraphQL request to get commits for a branch with pagination support
var request = new GraphQLRequest
{
Query = @"
query($owner: String!, $repo: String!, $branch: String!, $after: String) {
repository(owner: $owner, name: $repo) {
ref(qualifiedName: $branch) {
target {
... on Commit {
history(first: 100, after: $after) {
nodes {
oid
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
}
}",
Variables = new
{
owner,
repo,
branch = qualifiedBranch,
after = afterCursor
}
};
// Execute GraphQL query
var response = await _graphqlClient.SendQueryAsync<GetCommitsResponse>(request);
// Extract commit SHAs from the GraphQL response, filtering out null or invalid values
var pageCommitShas = response.Data?.Repository?.Ref?.Target?.History?.Nodes?
.Where(n => !string.IsNullOrEmpty(n.Oid))
.Select(n => n.Oid!)
.ToList() ?? [];
allCommitShas.AddRange(pageCommitShas);
// Check if there are more pages
var pageInfo = response.Data?.Repository?.Ref?.Target?.History?.PageInfo;
hasNextPage = pageInfo?.HasNextPage ?? false;
afterCursor = pageInfo?.EndCursor;
}
while (hasNextPage);
// Return list of all commit SHAs
return allCommitShas;
}
catch
{
// If GraphQL query fails, return empty list
return [];
}
}
/// <summary>
/// Gets all releases for a repository using GraphQL with pagination.
/// </summary>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <returns>List of release nodes.</returns>
public async Task<List<ReleaseNode>> GetReleasesAsync(
string owner,
string repo)
{
try
{
var allReleaseNodes = new List<ReleaseNode>();
string? afterCursor = null;
bool hasNextPage;
// Paginate through all releases
do
{
// Create GraphQL request to get releases for a repository with pagination support
var request = new GraphQLRequest
{
Query = @"
query($owner: String!, $repo: String!, $after: String) {
repository(owner: $owner, name: $repo) {
releases(first: 100, after: $after, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
tagName
}
pageInfo {
hasNextPage
endCursor
}
}
}
}",
Variables = new
{
owner,
repo,
after = afterCursor
}
};
// Execute GraphQL query
var response = await _graphqlClient.SendQueryAsync<GetReleasesResponse>(request);
// Extract release nodes from the GraphQL response, filtering out null or invalid values
var pageReleaseNodes = response.Data?.Repository?.Releases?.Nodes?
.Where(n => !string.IsNullOrEmpty(n.TagName))
.ToList() ?? [];
allReleaseNodes.AddRange(pageReleaseNodes);
// Check if there are more pages
var pageInfo = response.Data?.Repository?.Releases?.PageInfo;
hasNextPage = pageInfo?.HasNextPage ?? false;
afterCursor = pageInfo?.EndCursor;
}
while (hasNextPage);
// Return list of all release nodes
return allReleaseNodes;
}
catch
{
// If GraphQL query fails, return empty list
return [];
}
}
/// <summary>
/// Finds issue IDs linked to a pull request via closingIssuesReferences.
/// </summary>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="prNumber">Pull request number.</param>
/// <returns>List of issue IDs linked to the pull request.</returns>
public async Task<List<int>> FindIssueIdsLinkedToPullRequestAsync(
string owner,
string repo,
int prNumber)
{
try
{
var allIssueNumbers = new List<int>();
string? afterCursor = null;
bool hasNextPage;
// Paginate through all closing issues
do
{
// Create GraphQL request to get closing issues for a pull request with pagination support
var request = new GraphQLRequest
{
Query = @"
query($owner: String!, $repo: String!, $prNumber: Int!, $after: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 100, after: $after) {
nodes {
number
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}",
Variables = new
{
owner,
repo,
prNumber,
after = afterCursor
}
};
// Execute GraphQL query
var response = await _graphqlClient.SendQueryAsync<FindIssueIdsResponse>(request);
// Extract issue numbers from the GraphQL response, filtering out null or invalid values
var pageIssueNumbers = response.Data?.Repository?.PullRequest?.ClosingIssuesReferences?.Nodes?
.Where(n => n.Number.HasValue)
.Select(n => n.Number!.Value)
.ToList() ?? [];
allIssueNumbers.AddRange(pageIssueNumbers);
// Check if there are more pages
var pageInfo = response.Data?.Repository?.PullRequest?.ClosingIssuesReferences?.PageInfo;
hasNextPage = pageInfo?.HasNextPage ?? false;
afterCursor = pageInfo?.EndCursor;
}
while (hasNextPage);
// Return list of all linked issue numbers
return allIssueNumbers;
}
catch
{
// If GraphQL query fails, return empty list
return [];
}
}
/// <summary>
/// Disposes the GraphQL client if owned by this instance.
/// </summary>
public void Dispose()
{
// Clean up GraphQL client resources only if we own it
if (_ownsGraphQLClient)
{
_graphqlClient.Dispose();
}
}
}