Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Added support for "org" mode for selfhosted BitBucket. (#589)
Browse files Browse the repository at this point in the history
You need to provide the --Api switch when using this mode otherwise Nukeeper will default to use GitHub
  • Loading branch information
Jens-H-Eriksen authored and AnthonySteele committed Dec 6, 2018
1 parent b62819d commit 09a9bac
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NuKeeper/Collaboration/CollaborationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void CreateForPlatform()

case Platform.BitbucketLocal:
CollaborationPlatform = new BitBucketLocalPlatform(_nuKeeperLogger);
RepositoryDiscovery = new BitbucketRepositoryDiscovery(_nuKeeperLogger);
RepositoryDiscovery = new BitbucketLocalRepositoryDiscovery(_nuKeeperLogger, CollaborationPlatform, Settings);
ForkFinder = new BitbucketForkFinder(CollaborationPlatform, _nuKeeperLogger, forkMode);
break;

Expand Down
4 changes: 2 additions & 2 deletions Nukeeper.BitBucketLocal/BitBucketLocalPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public async Task<IReadOnlyList<Repository>> GetRepositoriesForOrganisation(stri
return repos.Select(repo =>
new Repository(repo.Name, false,
new UserPermissions(true, true, true),
new Uri(repo.Links.Clone.First().Href),
new Uri(repo.Links.Clone.First().Href),
new Uri(repo.Links.Clone.First(x => x.Name.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)).Href),
new Uri(repo.Links.Clone.First(x => x.Name.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)).Href),
null, false, null))
.ToList();
}
Expand Down
4 changes: 2 additions & 2 deletions Nukeeper.BitBucketLocal/BitBucketLocalSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RepositorySettings RepositorySettings(Uri repositoryUri)
.Where(s => !string.IsNullOrWhiteSpace(s))
.ToList();

Username = repositoryUri.UserInfo;
Username = Concat.FirstValue(repositoryUri.UserInfo, Environment.UserName);

if (pathParts.Count < 2)
{
Expand All @@ -53,7 +53,7 @@ public RepositorySettings RepositorySettings(Uri repositoryUri)

public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings)
{
settings.Username = Username;
settings.Username = Concat.FirstValue(Username, Environment.UserName);
UpdateTokenSettings(settings);
settings.ForkMode = settings.ForkMode ?? ForkMode.SingleRepositoryOnly;
}
Expand Down
89 changes: 89 additions & 0 deletions Nukeeper.BitBucketLocal/BitbucketLocalRepositoryDiscovery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NuKeeper.Abstractions.CollaborationModels;
using NuKeeper.Abstractions.CollaborationPlatform;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Logging;

namespace NuKeeper.BitBucketLocal
{
public class BitbucketLocalRepositoryDiscovery : IRepositoryDiscovery
{
private readonly INuKeeperLogger _logger;
private readonly ICollaborationPlatform _collaborationPlatform;
private CollaborationPlatformSettings _setting;

public BitbucketLocalRepositoryDiscovery(INuKeeperLogger logger, ICollaborationPlatform collaborationPlatform, CollaborationPlatformSettings settings)
{
_logger = logger;
_collaborationPlatform = collaborationPlatform;
_setting = settings;
}

public async Task<IEnumerable<RepositorySettings>> GetRepositories(SourceControlServerSettings settings)
{
switch (settings.Scope)
{
case ServerScope.Global:
_logger.Error($"{settings.Scope} not yet implemented");
throw new NotImplementedException();

case ServerScope.Organisation:
return await FromOrganisation(settings.OrganisationName, settings);

case ServerScope.Repository:
return await Task.FromResult(new List<RepositorySettings> { settings.Repository }.AsEnumerable());

default:
_logger.Error($"Unknown Server Scope {settings.Scope}");
return await Task.FromResult(Enumerable.Empty<RepositorySettings>());
}
}



private async Task<IReadOnlyCollection<RepositorySettings>> FromOrganisation(string organisationName, SourceControlServerSettings settings)
{
var allOrgRepos = await _collaborationPlatform.GetRepositoriesForOrganisation(organisationName);

var usableRepos = allOrgRepos
.Where(r => MatchesIncludeExclude(r, settings))
.ToList();

if (allOrgRepos.Count > usableRepos.Count)
{
_logger.Detailed($"Can pull from {usableRepos.Count} repos out of {allOrgRepos.Count}");
}

return usableRepos
.Select(r => new RepositorySettings()
{
ApiUri = _setting.BaseApiUrl,
RepositoryUri = r.HtmlUrl,
RepositoryName = r.Name,
RepositoryOwner = organisationName
}).ToList();
}

private static bool MatchesIncludeExclude(Repository repo, SourceControlServerSettings settings)
{
return
MatchesInclude(settings.IncludeRepos, repo)
&& !MatchesExclude(settings.ExcludeRepos, repo);
}

private static bool MatchesInclude(Regex regex, Repository repo)
{
return regex == null || regex.IsMatch(repo.Name);
}

private static bool MatchesExclude(Regex regex, Repository repo)
{
return regex != null && regex.IsMatch(repo.Name);
}

}
}

0 comments on commit 09a9bac

Please sign in to comment.