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
/
Copy pathRepositoryService.cs
97 lines (85 loc) · 3.86 KB
/
RepositoryService.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using Octokit.GraphQL;
using static Octokit.GraphQL.Variable;
namespace GitHub.Services
{
[Export(typeof(IRepositoryService))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class RepositoryService: IRepositoryService
{
static ICompiledQuery<Tuple<string, string>> readParentOwnerLogin;
static ICompiledQuery<List<ProtectedBranch>> queryProtectedBranches;
readonly IGraphQLClientFactory graphqlFactory;
[ImportingConstructor]
public RepositoryService(IGraphQLClientFactory graphqlFactory)
{
Guard.ArgumentNotNull(graphqlFactory, nameof(graphqlFactory));
this.graphqlFactory = graphqlFactory;
}
/// <inheritdoc/>
public async Task<(string owner, string name)?> FindParent(HostAddress address, string owner, string name)
{
Guard.ArgumentNotNull(address, nameof(address));
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
if (readParentOwnerLogin == null)
{
readParentOwnerLogin = new Query()
.Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
.Select(r => r.Parent != null ? Tuple.Create(r.Parent.Owner.Login, r.Parent.Name) : null)
.Compile();
}
var vars = new Dictionary<string, object>
{
{ nameof(owner), owner },
{ nameof(name), name },
};
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
var result = await graphql.Run(readParentOwnerLogin, vars).ConfigureAwait(false);
return result != null ? (result.Item1, result.Item2) : ((string, string)?)null;
}
/// <inheritdoc/>
public async Task<IList<ProtectedBranch>> GetProtectedBranches(HostAddress address, string owner, string name)
{
Guard.ArgumentNotNull(address, nameof(address));
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
if (queryProtectedBranches == null)
{
queryProtectedBranches = new Query()
.Repository(Var(nameof(name)), Var(nameof(owner)))
.Select(r =>
r.ProtectedBranches(null, null, null, null)
.AllPages()
.Select(branch => new ProtectedBranch
{
Name = branch.Name,
RequiredStatusCheckContexts = branch.RequiredStatusCheckContexts.ToArray()
}).ToList()
).Compile();
}
var vars = new Dictionary<string, object>
{
{ nameof(owner), owner },
{ nameof(name), name },
};
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
return await graphql.Run(queryProtectedBranches, vars).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<ProtectedBranch> GetProtectedBranch(HostAddress address, string owner, string name, string branchName)
{
Guard.ArgumentNotNull(branchName, nameof(branchName));
var protectedBranches = await GetProtectedBranches(address, owner, name).ConfigureAwait(false);
return protectedBranches.FirstOrDefault(branch => branch.Name.Equals(branchName, StringComparison.InvariantCultureIgnoreCase));
}
}
}