Skip to content

Use authentication in GitPreparer.GetRemoteReference #468

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

Merged
merged 1 commit into from
Jun 17, 2015
Merged
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
15 changes: 15 additions & 0 deletions GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace GitVersion
{
using System;
using LibGit2Sharp;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -164,6 +165,20 @@ static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Authent
repo.Checkout(fakeBranchName);
}

internal static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, string repoUrl, string username, string password)
{
// This is a work-around as long as https://github.com/libgit2/libgit2sharp/issues/1099 is not fixed
var remote = repo.Network.Remotes.Add(Guid.NewGuid().ToString(), repoUrl);
try
{
return GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, username, password);
}
finally
{
repo.Network.Remotes.Remove(remote.Name);
}
}

static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, Remote remote, string username, string password)
{
return repo.Network.ListReferences(remote, (url, fromUrl, types) => new UsernamePasswordCredentials
Expand Down
6 changes: 3 additions & 3 deletions GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti

if (newHead == null)
{
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl);
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl, authentication);
if (remoteReference != null)
{
repository.Network.Fetch(repositoryUrl, new[]
Expand Down Expand Up @@ -202,11 +202,11 @@ private static Reference GetLocalReference(Repository repository, string branchN
return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName));
}

private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl)
private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl, Authentication authentication)
{
var targetBranchName = branchName.GetCanonicalBranchName();
var remoteReferences = repository.Network.ListReferences(repositoryUrl);

var remoteReferences = GitHelper.GetRemoteTipsUsingUsernamePasswordCredentials(repository, repositoryUrl, authentication.Username, authentication.Password);
return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName));
}
}
Expand Down