tgstation-server 6.12.0
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;
6using Octokit;
7
10
12{
17 {
19 public override string TestMergeRefSpecFormatter => "pull/{0}/head:{1}";
20
22 public override string TestMergeLocalBranchNameFormatter => "pull/{0}/headrefs/heads/{1}";
23
25 public override RemoteGitProvider? RemoteGitProvider => Api.Models.RemoteGitProvider.GitHub;
26
31
38 public GitHubRemoteFeatures(IGitHubServiceFactory gitHubServiceFactory, ILogger<GitHubRemoteFeatures> logger, Uri remoteUrl)
39 : base(logger, remoteUrl)
40 {
41 this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
42 }
43
45 protected override async ValueTask<Models.TestMerge> GetTestMergeImpl(
46 TestMergeParameters parameters,
47 RepositorySettings repositorySettings,
48 CancellationToken cancellationToken)
49 {
50 var gitHubService = repositorySettings.AccessToken != null
52 repositorySettings.AccessToken,
53 new RepositoryIdentifier(this),
54 cancellationToken)
55 : await gitHubServiceFactory.CreateService(cancellationToken);
56
57 PullRequest? pr = null;
58 ApiException? exception = null;
59 string? errorMessage = null;
60 if (gitHubService == null)
61 errorMessage = "GITHUB API ERROR: AUTH FAILURE";
62 else
63 try
64 {
65 pr = await gitHubService.GetPullRequest(RemoteRepositoryOwner, RemoteRepositoryName, parameters.Number, cancellationToken);
66 }
67 catch (RateLimitExceededException ex)
68 {
69 // you look at your anonymous access and sigh
70 errorMessage = "GITHUB API ERROR: RATE LIMITED";
71 exception = ex;
72 }
73 catch (AuthorizationException ex)
74 {
75 errorMessage = "GITHUB API ERROR: BAD CREDENTIALS";
76 exception = ex;
77 }
78 catch (NotFoundException ex)
79 {
80 // you look at your shithub and sigh
81 errorMessage = "GITHUB API ERROR: PULL REQUEST NOT FOUND";
82 exception = ex;
83 }
84
85 if (exception != null)
86 Logger.LogWarning(exception, "Error retrieving pull request metadata!");
87
88 var revisionToUse = parameters.TargetCommitSha == null
89 || pr?.Head.Sha.StartsWith(parameters.TargetCommitSha, StringComparison.OrdinalIgnoreCase) == true
90 ? pr?.Head.Sha
91 : parameters.TargetCommitSha;
92
93 var testMerge = new Models.TestMerge
94 {
95 Author = pr?.User.Login ?? errorMessage,
96 BodyAtMerge = pr?.Body ?? errorMessage ?? String.Empty,
97 TitleAtMerge = pr?.Title ?? errorMessage ?? String.Empty,
98 Comment = parameters.Comment,
99 Number = parameters.Number,
100 TargetCommitSha = revisionToUse,
101 Url = pr?.HtmlUrl ?? $"https://github.com/{RemoteRepositoryOwner}/{RemoteRepositoryName}/pull/{parameters.Number}",
102 };
103
104 return testMerge;
105 }
106 }
107}
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.
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.