-
Notifications
You must be signed in to change notification settings - Fork 116
Argo/Git: Update Vendor Identification Logic to poll http endpoint in the event of non-deterministic URL (gitlab only) #1839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using System; | ||
|
|
||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters.AzureDevOps | ||
| { | ||
| public class AzureDevOpsAgnosticApiAdapterFactory : IResolvingGitVendorApiAdapterFactory | ||
| { | ||
| public bool CanHandleAsCloudHosted(IRepositoryConnection repositoryConnection) | ||
| { | ||
| return AzureDevOpsApiAdapter.CanInvokeWith(repositoryConnection.Url); | ||
| } | ||
| public IGitVendorApiAdapter? Create(IRepositoryConnection repositoryConnection) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System; | ||
|
|
||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters.BitBucket | ||
| { | ||
| public class BitBucketAgnosticApiAdapterFactory: IResolvingGitVendorApiAdapterFactory | ||
| { | ||
| static Uri baseUrl = new Uri("https://bitbucket.org"); | ||
|
|
||
| public bool CanHandleAsCloudHosted(IRepositoryConnection repositoryConnection) | ||
| { | ||
| return repositoryConnection.Url.Host.Equals(baseUrl.Host, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| public IGitVendorApiAdapter Create(IRepositoryConnection repositoryConnection) | ||
| { | ||
| return new BitBucketApiAdapter(repositoryConnection, baseUrl); | ||
| } | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| using NGitLab; | ||
| using NGitLab.Models; | ||
|
|
||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters | ||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters.GitLab | ||
| { | ||
| public class GitLabApiAdapter : IGitVendorApiAdapter | ||
| { | ||
|
|
@@ -28,18 +28,35 @@ public async Task<PullRequest> CreatePullRequest(string pullRequestTitle, | |
| GitBranchName destinationBranch, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| await Task.CompletedTask; | ||
|
|
||
| await Task.CompletedTask; | ||
|
|
||
| var gitlabSrcBranch = StripBranchPrefix(sourceBranch.Value); | ||
| var gitlabDestBranch = StripBranchPrefix(destinationBranch.Value); | ||
|
Comment on lines
+34
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reasoning for this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. coz it doesn't work if we don't :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gitlab accepts the literal branch name with no prefixing.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So broken to begin it sounds like?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct. |
||
|
|
||
|
|
||
| var mergeRequest = gitLabClient.GetMergeRequest(projectPath).Create(new MergeRequestCreate() | ||
| { | ||
| Title = pullRequestTitle, | ||
| SourceBranch = sourceBranch.Value, | ||
| TargetBranch = destinationBranch.Value, | ||
| SourceBranch = gitlabSrcBranch, | ||
| TargetBranch = gitlabDestBranch, | ||
| Description = body | ||
| }); | ||
| return new PullRequest(mergeRequest.Title, mergeRequest.Id, mergeRequest.WebUrl); | ||
| } | ||
|
|
||
| static string StripBranchPrefix(string input) | ||
| { | ||
| const string branchPrefix = "refs/heads/"; | ||
| if (input.StartsWith(branchPrefix)) | ||
| { | ||
| return input.Substring(branchPrefix.Length); | ||
| } | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public string GenerateCommitUrl(string commit) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Threading; | ||
| using NGitLab; | ||
|
|
||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters.GitLab | ||
| { | ||
| public class GitLabApiAdapterFactory : IResolvingGitVendorApiAdapterFactory | ||
| { | ||
| readonly string CloudHost = "https://gitlab.com"; | ||
|
|
||
| readonly SelfHostedGitLabInspector selfHostedGitLabInspector; | ||
|
|
||
| public GitLabApiAdapterFactory(SelfHostedGitLabInspector selfHostedGitLabInspector) | ||
| { | ||
| this.selfHostedGitLabInspector = selfHostedGitLabInspector; | ||
| } | ||
|
|
||
| public bool CanHandleAsCloudHosted(Uri repositoryUri) | ||
| { | ||
| return repositoryUri.Host.Equals("gitlab.com", StringComparison.OrdinalIgnoreCase) | ||
| //Handle www.gitlab.com | ||
| || repositoryUri.Host.EndsWith(".gitlab.com", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| public bool CanHandleAsSelfHosted(IRepositoryConnection repositoryConnection) | ||
| { | ||
| return selfHostedGitLabInspector.IsSelfHostedGitLabInstance(repositoryConnection.Url, CancellationToken.None).Result; | ||
| } | ||
|
|
||
| public IGitVendorApiAdapter Create(IRepositoryConnection repositoryConnection) | ||
| { | ||
| var host = CanHandleAsCloudHosted(repositoryConnection.Url) | ||
| ? CloudHost | ||
| : SelfHostedGitLabInspector.GetSelfHostedBaseRepositoryUrl(repositoryConnection.Url); | ||
|
|
||
| var client = new GitLabClient(host, repositoryConnection.Password); | ||
|
|
||
| return new GitLabApiAdapter(client, repositoryConnection, new Uri(host)); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
|
|
||
| // This is duplicated from the OctopusDeploy server repo | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Helpful to add this same note to the server copy too |
||
| // source/Octopus.Core/Features/Git/PullRequests/Vendors/GitLab/SelfHostedGitLabInspector.cs | ||
| // Adjustments required: | ||
| // * Namespace | ||
| // * removed references to IOctopusHttpClientFactory - construct HtpClient directly | ||
| // * removed memory cache | ||
| // * removed semaphoreslim | ||
|
|
||
| namespace Calamari.ArgoCD.Git.GitVendorApiAdapters.GitLab; | ||
|
|
||
| public class SelfHostedGitLabInspector() | ||
| { | ||
| public static string GetSelfHostedBaseRepositoryUrl(Uri repositoryUri) => repositoryUri.GetLeftPart(UriPartial.Authority); //this elides the path & query | ||
|
|
||
| public async Task<bool> IsSelfHostedGitLabInstance(Uri repositoryUri, CancellationToken cancellationToken) | ||
| { | ||
| var selfHostedUri = GetSelfHostedBaseRepositoryUrl(repositoryUri); | ||
| var key = $"gitlab_{selfHostedUri}"; | ||
|
|
||
| using var httpClient = new HttpClient(); | ||
|
|
||
| //we can make an anonymous HTTP call to `/api/v4` and inspect the headers for `X-GitLab-Meta` | ||
| var apiUrl = new UriBuilder(selfHostedUri) | ||
| { | ||
| Path = "/api/v4" | ||
| }; | ||
|
|
||
| bool hasGitLabHeader; | ||
| try | ||
| { | ||
| var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, apiUrl.Uri), | ||
| HttpCompletionOption.ResponseHeadersRead, | ||
| cancellationToken); | ||
|
|
||
| //all API requests from GitLab have this header (afaik) | ||
| hasGitLabHeader = response.Headers.Contains("x-gitlab-meta"); | ||
| } | ||
| catch | ||
| { | ||
| //any exception we ignore | ||
| hasGitLabHeader = false; | ||
| } | ||
|
|
||
| return hasGitLabHeader; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should tests have been deleted from here?