tgstation-server 5.12.7
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
29 public override string RemoteRepositoryOwner { get; }
30
32 public override string RemoteRepositoryName { get; }
33
38
45 public GitHubRemoteFeatures(IGitHubServiceFactory gitHubServiceFactory, ILogger<GitHubRemoteFeatures> logger, Uri remoteUrl)
46 : base(logger, remoteUrl)
47 {
48 this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
49
50 ArgumentNullException.ThrowIfNull(remoteUrl);
51
52 RemoteRepositoryOwner = remoteUrl.Segments[1].TrimEnd('/');
53 RemoteRepositoryName = remoteUrl.Segments[2].TrimEnd('/');
54 if (RemoteRepositoryName.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
56 }
57
59 protected override async Task<Models.TestMerge> GetTestMergeImpl(
60 TestMergeParameters parameters,
61 RepositorySettings repositorySettings,
62 CancellationToken cancellationToken)
63 {
64 var gitHubService = repositorySettings.AccessToken != null
67
68 PullRequest pr = null;
69 ApiException exception = null;
70 string errorMessage = null;
71 try
72 {
73 pr = await gitHubService.GetPullRequest(RemoteRepositoryOwner, RemoteRepositoryName, parameters.Number, cancellationToken);
74 }
75 catch (RateLimitExceededException ex)
76 {
77 // you look at your anonymous access and sigh
78 errorMessage = "GITHUB API ERROR: RATE LIMITED";
79 exception = ex;
80 }
81 catch (AuthorizationException ex)
82 {
83 errorMessage = "GITHUB API ERROR: BAD CREDENTIALS";
84 exception = ex;
85 }
86 catch (NotFoundException ex)
87 {
88 // you look at your shithub and sigh
89 errorMessage = "GITHUB API ERROR: PULL REQUEST NOT FOUND";
90 exception = ex;
91 }
92
93 if (exception != null)
94 Logger.LogWarning(exception, "Error retrieving pull request metadata!");
95
96 var revisionToUse = parameters.TargetCommitSha == null
97 || pr?.Head.Sha.StartsWith(parameters.TargetCommitSha, StringComparison.OrdinalIgnoreCase) == true
98 ? pr?.Head.Sha
99 : parameters.TargetCommitSha;
100
101 var testMerge = new Models.TestMerge
102 {
103 Author = pr?.User.Login ?? errorMessage,
104 BodyAtMerge = pr?.Body ?? errorMessage ?? String.Empty,
105 TitleAtMerge = pr?.Title ?? errorMessage ?? String.Empty,
106 Comment = parameters.Comment,
107 Number = parameters.Number,
108 TargetCommitSha = revisionToUse,
109 Url = pr?.HtmlUrl ?? errorMessage,
110 };
111
112 return testMerge;
113 }
114 }
115}
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.
override string RemoteRepositoryOwner
If RemoteGitProvider is not RemoteGitProvider.Unknown this will be set with the owner of the reposito...
override string RemoteRepositoryName
If RemoteGitProvider is not RemoteGitProvider.Unknown this will be set with the name of the repositor...
override async Task< Models.TestMerge > GetTestMergeImpl(TestMergeParameters parameters, RepositorySettings repositorySettings, CancellationToken cancellationToken)
Implementation of GetTestMerge(TestMergeParameters, RepositorySettings, CancellationToken)....
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...
readonly IGitHubServiceFactory gitHubServiceFactory
The IGitHubServiceFactory for the GitHubRemoteFeatures.
ILogger< GitRemoteFeaturesBase > Logger
The ILogger for the GitRemoteFeaturesBase.
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.