tgstation-server 6.19.1
The /tg/station 13 server suite
Loading...
Searching...
No Matches
GitHubRemoteFeatures.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5using Microsoft.Extensions.Logging;
6
7using Octokit;
8
11
13{
18 {
20 public override string TestMergeRefSpecFormatter => "pull/{0}/head:{1}";
21
23 public override string TestMergeLocalBranchNameFormatter => "pull/{0}/headrefs/heads/{1}";
24
26 public override RemoteGitProvider? RemoteGitProvider => Api.Models.RemoteGitProvider.GitHub;
27
32
39 public GitHubRemoteFeatures(IGitHubServiceFactory gitHubServiceFactory, ILogger<GitHubRemoteFeatures> logger, Uri remoteUrl)
40 : base(logger, remoteUrl)
41 {
42 this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
43 }
44
46 public override async ValueTask<string?> TransformRepositoryPassword(string? rawPassword, CancellationToken cancellationToken)
47 {
48 if (rawPassword == null)
49 return null;
50
51 var gitHubService = await gitHubServiceFactory.CreateService(
52 rawPassword,
53 new RepositoryIdentifier(this),
54 cancellationToken);
55 return gitHubService?.GetGitPassword() ?? rawPassword;
56 }
57
59 protected override async ValueTask<Models.TestMerge> GetTestMergeImpl(
60 TestMergeParameters parameters,
61 RepositorySettings repositorySettings,
62 CancellationToken cancellationToken)
63 {
64 var gitHubService = repositorySettings.AccessToken != null
66 repositorySettings.AccessToken,
67 new RepositoryIdentifier(this),
68 cancellationToken)
69 : await gitHubServiceFactory.CreateService(cancellationToken);
70
71 PullRequest? pr = null;
72 ApiException? exception = null;
73 string? errorMessage = null;
74 if (gitHubService == null)
75 errorMessage = "GITHUB API ERROR: AUTH FAILURE";
76 else
77 try
78 {
79 pr = await gitHubService.GetPullRequest(RemoteRepositoryOwner, RemoteRepositoryName, parameters.Number, cancellationToken);
80 }
81 catch (RateLimitExceededException ex)
82 {
83 // you look at your anonymous access and sigh
84 errorMessage = "GITHUB API ERROR: RATE LIMITED";
85 exception = ex;
86 }
87 catch (AuthorizationException ex)
88 {
89 errorMessage = "GITHUB API ERROR: BAD CREDENTIALS";
90 exception = ex;
91 }
92 catch (NotFoundException ex)
93 {
94 // you look at your shithub and sigh
95 errorMessage = "GITHUB API ERROR: PULL REQUEST NOT FOUND";
96 exception = ex;
97 }
98
99 if (exception != null)
100 Logger.LogWarning(exception, "Error retrieving pull request metadata!");
101
102 var revisionToUse = parameters.TargetCommitSha == null
103 || pr?.Head.Sha.StartsWith(parameters.TargetCommitSha, StringComparison.OrdinalIgnoreCase) == true
104 ? pr?.Head.Sha
105 : parameters.TargetCommitSha;
106
107 var testMerge = new Models.TestMerge
108 {
109 Author = pr?.User.Login ?? errorMessage,
110 BodyAtMerge = pr?.Body ?? errorMessage ?? String.Empty,
111 TitleAtMerge = pr?.Title ?? errorMessage ?? String.Empty,
112 Comment = parameters.Comment,
113 Number = parameters.Number,
114 TargetCommitSha = revisionToUse,
115 Url = pr?.HtmlUrl ?? $"https://github.com/{RemoteRepositoryOwner}/{RemoteRepositoryName}/pull/{parameters.Number}",
116 };
117
118 return testMerge;
119 }
120 }
121}
Represents configurable settings for a git repository.
string? AccessToken
The token/password to access the git repository with. Can also be a TGS encoded app private key....
virtual ? string TargetCommitSha
The sha of the test merge revision to merge. If not specified, the latest commit from the source will...
string? Comment
Optional comment about the test.
int Number
The number of the test merge source.
override async ValueTask< string?> TransformRepositoryPassword(string? rawPassword, CancellationToken cancellationToken)
Transform a service's rawPassword into a password usable by git.A ValueTask<TResult> resulting in th...
GitHubRemoteFeatures(IGitHubServiceFactory gitHubServiceFactory, ILogger< GitHubRemoteFeatures > logger, Uri remoteUrl)
Initializes a new instance of the GitHubRemoteFeatures class.
override string TestMergeRefSpecFormatter
Gets a formatter string which creates the remote refspec for fetching the HEAD of passed in test merg...
override async ValueTask< Models.TestMerge > GetTestMergeImpl(TestMergeParameters parameters, RepositorySettings repositorySettings, CancellationToken cancellationToken)
readonly IGitHubServiceFactory gitHubServiceFactory
The IGitHubServiceFactory for the GitHubRemoteFeatures.
string RemoteRepositoryOwner
If RemoteGitProvider is not RemoteGitProvider.Unknown this will be set with the owner of the reposito...
ILogger< GitRemoteFeaturesBase > Logger
The ILogger for the GitRemoteFeaturesBase.
string RemoteRepositoryName
If RemoteGitProvider is not RemoteGitProvider.Unknown this will be set with the name of the repositor...
Identifies a repository either by its RepositoryId or Owner and Name.
ValueTask< IGitHubService > CreateService(CancellationToken cancellationToken)
Create a IGitHubService.
RemoteGitProvider
Indicates the remote git host.