Skip to content

Commit be6b65f

Browse files
maxildarturcic
authored andcommitted
Instantiation of all UsernamePasswordCredentials ensure non-null password.
It also supports null/empty password, but guards against passing null to libgit2sharp. This is to avoid runtime errors when using personal access tokens to authenticate when fetching etc. from private github repos. The standard way to authenticate: new UsernamePasswordCredentials { Username = "<github-personal-access-token>", // GITVERSION_REMOTE_USERNAME Password = string.Empty }; GitVersion should support this authentication scheme necessary when having 2FA. When using environment variables to propagate credentials password will end up being null, because GITVERSION_REMOTE_PASSWORD is not present when passing empty value. This commit guards against libgit2sharp throwing LibGit2SharpException: "UsernamePasswordCredentials contains a null Username or Password."
1 parent 93df5e2 commit be6b65f

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

src/GitVersionCore/Core/GitPreparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ private void CloneRepository(string repositoryUrl, string gitDirectory, Authenti
149149

150150
if (auth != null)
151151
{
152-
if (!string.IsNullOrWhiteSpace(auth.Username) && !string.IsNullOrWhiteSpace(auth.Password))
152+
if (!string.IsNullOrWhiteSpace(auth.Username))
153153
{
154154
log.Info($"Setting up credentials using name '{auth.Username}'");
155155

156156
credentials = new UsernamePasswordCredentials
157157
{
158158
Username = auth.Username,
159-
Password = auth.Password
159+
Password = auth.Password ?? string.Empty
160160
};
161161
}
162162
}

src/GitVersionCore/Extensions/RepositoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCr
196196
return repository.Network.ListReferences(remote, (url, fromUrl, types) => new UsernamePasswordCredentials
197197
{
198198
Username = username,
199-
Password = password
199+
Password = password ?? string.Empty
200200
}).Select(r => r.ResolveToDirectReference());
201201
}
202202

src/GitVersionCore/Model/AuthenticationInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public FetchOptions ToFetchOptions()
1717
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
1818
{
1919
Username = Username,
20-
Password = Password
20+
Password = Password ?? string.Empty
2121
};
2222
}
2323

0 commit comments

Comments
 (0)