tgstation-server 6.3.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
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 protected override async ValueTask<Models.TestMerge> GetTestMergeImpl(
47 TestMergeParameters parameters,
48 RepositorySettings repositorySettings,
49 CancellationToken cancellationToken)
50 {
51 var gitHubService = repositorySettings.AccessToken != null
54
55 PullRequest? pr = null;
56 ApiException? exception = null;
57 string? errorMessage = null;
58 try
59 {
60 pr = await gitHubService.GetPullRequest(RemoteRepositoryOwner, RemoteRepositoryName, parameters.Number, cancellationToken);
61 }
62 catch (RateLimitExceededException ex)
63 {
64 // you look at your anonymous access and sigh
65 errorMessage = "GITHUB API ERROR: RATE LIMITED";
66 exception = ex;
67 }
68 catch (AuthorizationException ex)
69 {
70 errorMessage = "GITHUB API ERROR: BAD CREDENTIALS";
71 exception = ex;
72 }
73 catch (NotFoundException ex)
74 {
75 // you look at your shithub and sigh
76 errorMessage = "GITHUB API ERROR: PULL REQUEST NOT FOUND";
77 exception = ex;
78 }
79
80 if (exception != null)
81 Logger.LogWarning(exception, "Error retrieving pull request metadata!");
82
83 var revisionToUse = parameters.TargetCommitSha == null
84 || pr?.Head.Sha.StartsWith(parameters.TargetCommitSha, StringComparison.OrdinalIgnoreCase) == true
85 ? pr?.Head.Sha
86 : parameters.TargetCommitSha;
87
88 var testMerge = new Models.TestMerge
89 {
90 Author = pr?.User.Login ?? errorMessage,
91 BodyAtMerge = pr?.Body ?? errorMessage ?? String.Empty,
92 TitleAtMerge = pr?.Title ?? errorMessage ?? String.Empty,
93 Comment = parameters.Comment,
94 Number = parameters.Number,
95 TargetCommitSha = revisionToUse,
96 Url = pr?.HtmlUrl ?? errorMessage,
97 };
98
99 return testMerge;
100 }
101 }
102}
Represents configurable settings for a git repository.
string? AccessToken
The token/password to access the git repository with.
Parameters for creating a TestMerge.
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)
Implementation of GetTestMerge(TestMergeParameters, RepositorySettings, 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...
IGitHubService CreateService()
Create a IGitHubService.
Task< PullRequest > GetPullRequest(string repoOwner, string repoName, int pullRequestNumber, CancellationToken cancellationToken)
Get a given pullRequestNumber .
RemoteGitProvider
Indicates the remote git host.