Skip to content
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

adds httpPath variable for Bitbucket DC #1584

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using Atlassian.Bitbucket.DataCenter;
using GitCredentialManager;
using Moq;
using System;
using Xunit;

namespace Atlassian.Bitbucket.Tests
{
public class BitbucketHelperTest
{


private Mock<ISettings> settings = new Mock<ISettings>(MockBehavior.Loose);

[Theory]
[InlineData(null, false)]
[InlineData("", false)]
Expand Down Expand Up @@ -56,5 +63,36 @@ public void BitbucketHelper_IsBitbucketOrg_Uri(string str, bool expected)
bool actual = BitbucketHelper.IsBitbucketOrg(new Uri(str));
Assert.Equal(expected, actual);
}

[Theory]
// old behavior
[InlineData("http://bitbucket.org", null, "http://bitbucket.org:80")]
[InlineData("https://bitbucket.org", null, "https://bitbucket.org:443")]
[InlineData("https://bitbucket.org/project/repo.git", null, "https://bitbucket.org:443/project")]
// with http path
[InlineData("http://bitbucket.org", "/bitbucket", "http://bitbucket.org:80/bitbucket")]
[InlineData("https://bitbucket.org", "/bitbucket", "https://bitbucket.org:443/bitbucket")]
// usehttppath takes preference over httpPath
[InlineData("https://bitbucket.org/project/repo.git", "/bitbucket", "https://bitbucket.org:443/project")]
public void BitbucketHelper_GetBaseUri(string uri, string httpPath, string expected)
{

settings.Setup(s => s.RemoteUri).Returns(new Uri(uri));
if(httpPath != null)
{
MockHttpPath(httpPath);
}
var actual = BitbucketHelper.GetBaseUri(settings.Object);
Assert.Equal(expected, actual);
}

private string MockHttpPath(string value)
{
settings.Setup(s => s.TryGetSetting(
DataCenterConstants.EnvironmentVariables.HttpPath,
Constants.GitConfiguration.Credential.SectionName, DataCenterConstants.GitConfiguration.Credential.HttpPath,
out value)).Returns(true);
return value;
}
}
}
23 changes: 22 additions & 1 deletion src/shared/Atlassian.Bitbucket/BitbucketHelper.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
using System;
using Atlassian.Bitbucket.Cloud;
using Atlassian.Bitbucket.DataCenter;
using GitCredentialManager;

namespace Atlassian.Bitbucket
{
public static class BitbucketHelper
{
public static string GetBaseUri(Uri remoteUri)

private static bool TryGetHttpPath(ISettings settings, out string httpPath)
{
return settings.TryGetSetting(
DataCenterConstants.EnvironmentVariables.HttpPath,
Constants.GitConfiguration.Credential.SectionName, DataCenterConstants.GitConfiguration.Credential.HttpPath,
out httpPath);
}

public static string GetBaseUri(ISettings settings)
{
var remoteUri = settings?.RemoteUri;
if (remoteUri == null)
{
throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC Rest/OAuth endpoints");
}

var pathParts = remoteUri.PathAndQuery.Split('/');
var pathPart = remoteUri.PathAndQuery.StartsWith("/") ? pathParts[1] : pathParts[0];
var path = !string.IsNullOrWhiteSpace(pathPart) ? "/" + pathPart : null;
if(path == null && TryGetHttpPath(settings, out string httpPath) && !string.IsNullOrEmpty(httpPath))
{
path = httpPath;
}
return $"{remoteUri.Scheme}://{remoteUri.Host}:{remoteUri.Port}{path}";

}

public static bool IsBitbucketOrg(InputArguments input)
Expand Down
12 changes: 2 additions & 10 deletions src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using GitCredentialManager;
using GitCredentialManager.Authentication.OAuth;

Expand Down Expand Up @@ -68,15 +66,9 @@ private static string GetClientSecret(ISettings settings)

private static OAuth2ServerEndpoints GetEndpoints(ISettings settings)
{
var remoteUri = settings.RemoteUri;
if (remoteUri == null)
{
throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC OAuth2 endpoint Urls");
}

return new OAuth2ServerEndpoints(
new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/oauth2/latest/authorize"),
new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/oauth2/latest/token")
new Uri(BitbucketHelper.GetBaseUri(settings) + "/rest/oauth2/latest/authorize"),
new Uri(BitbucketHelper.GetBaseUri(settings) + "/rest/oauth2/latest/token")
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,7 @@ private Uri ApiUri
{
get
{
var remoteUri = _context.Settings?.RemoteUri;
if (remoteUri == null)
{
throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC OAuth2 endpoint Urls");
}

return new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/");
return new Uri(BitbucketHelper.GetBaseUri(_context.Settings) + "/rest/");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static class EnvironmentVariables
public const string OAuthClientId = "GCM_BITBUCKET_DATACENTER_CLIENTID";
public const string OAuthClientSecret = "GCM_BITBUCKET_DATACENTER_CLIENTSECRET";
public const string OAuthRedirectUri = "GCM_BITBUCKET_DATACENTER_OAUTH_REDIRECTURI";
public const string HttpPath = "GCM_BITBUCKET_DATACENTER_HTTP_PATH";
}

public static class GitConfiguration
Expand All @@ -39,6 +40,7 @@ public static class Credential
public const string OAuthClientId = "bitbucketDataCenterOAuthClientId";
public const string OAuthClientSecret = "bitbucketDataCenterOAuthClientSecret";
public const string OAuthRedirectUri = "bitbucketDataCenterOauthRedirectUri";
public const string HttpPath = "bitbucketDataCenterHttpPath";
}
}
}
Expand Down