Skip to content

Commit f3bde9c

Browse files
Parameters are now being passed around to ensure that the username / password are first read from the environment variables, but can be overridden by command line arguments
1 parent 981c4f6 commit f3bde9c

13 files changed

+88
-65
lines changed

GitVersion/Arguments.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
namespace GitVersion
22
{
3-
class Arguments
3+
using System;
4+
5+
public class Arguments
46
{
7+
public Arguments()
8+
{
9+
Username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
10+
Password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
11+
}
12+
513
public string TargetPath;
614

715
public string TargetUrl;

GitVersion/BuildServers/BuildServerList.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55

66
public static class BuildServerList
77
{
8-
public static List<IBuildServer> BuildServers = new List<IBuildServer>
9-
{
10-
new ContinuaCi(),
11-
new TeamCity()
12-
};
8+
static List<IBuildServer> BuildServers;
139

14-
public static Func<IEnumerable<IBuildServer>> Selector = () => DefaultSelector();
10+
public static Func<Arguments, IEnumerable<IBuildServer>> Selector = arguments => DefaultSelector(arguments);
1511

1612
public static void ResetSelector()
1713
{
1814
Selector = DefaultSelector;
1915
}
2016

21-
public static IEnumerable<IBuildServer> GetApplicableBuildServers()
17+
public static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
2218
{
23-
return Selector();
19+
return Selector(arguments);
2420
}
2521

26-
static IEnumerable<IBuildServer> DefaultSelector()
22+
static IEnumerable<IBuildServer> DefaultSelector(Arguments arguments)
2723
{
24+
if (BuildServers == null)
25+
{
26+
BuildServers = new List<IBuildServer>
27+
{
28+
new ContinuaCi(arguments),
29+
new TeamCity(arguments)
30+
};
31+
}
32+
2833
foreach (var buildServer in BuildServers)
2934
{
3035
if (buildServer.CanApplyToCurrentContext())

GitVersion/BuildServers/ContinuaCi.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
public class ContinuaCi : IBuildServer
66
{
7+
readonly Arguments _arguments;
8+
9+
public ContinuaCi(Arguments arguments)
10+
{
11+
_arguments = arguments;
12+
}
13+
714
public bool CanApplyToCurrentContext()
815
{
916
using (var registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\VSoft Technologies\\Continua CI Agent"))
@@ -19,7 +26,7 @@ public void PerformPreProcessingSteps(string gitDirectory)
1926
throw new ErrorException("Failed to find .git directory on agent");
2027
}
2128

22-
GitHelper.NormalizeGitDirectory(gitDirectory);
29+
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
2330
}
2431

2532
public string[] GenerateSetParameterMessage(string name, string value)

GitVersion/BuildServers/GitHelper.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace GitVersion
22
{
3-
using System;
43
using System.Linq;
54
using LibGit2Sharp;
65

76
public static class GitHelper
87
{
9-
public static void NormalizeGitDirectory(string gitDirectory)
8+
public static void NormalizeGitDirectory(string gitDirectory, Arguments arguments)
109
{
1110
using (var repo = new Repository(gitDirectory))
1211
{
@@ -15,7 +14,7 @@ public static void NormalizeGitDirectory(string gitDirectory)
1514
Logger.WriteInfo(string.Format("Fetching from remote '{0}' using the following refspecs: {1}.",
1615
remote.Name, string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))));
1716

18-
var fetchOptions = BuildFetchOptions();
17+
var fetchOptions = BuildFetchOptions(arguments.Username, arguments.Password);
1918
repo.Network.Fetch(remote, fetchOptions);
2019

2120
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
@@ -32,17 +31,17 @@ public static void NormalizeGitDirectory(string gitDirectory)
3231
}
3332
}
3433

35-
static FetchOptions BuildFetchOptions()
34+
static FetchOptions BuildFetchOptions(string username, string password)
3635
{
37-
// TODO: Respect username/password of arguments?
38-
var username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
39-
var password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
40-
4136
var fetchOptions = new FetchOptions();
4237

4338
if (!string.IsNullOrEmpty(username))
4439
{
45-
fetchOptions.Credentials = new Credentials { Username = username, Password = password };
40+
fetchOptions.Credentials = new Credentials
41+
{
42+
Username = username,
43+
Password = password
44+
};
4645
}
4746

4847
return fetchOptions;

GitVersion/BuildServers/TeamCity.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
public class TeamCity : IBuildServer
66
{
7+
readonly Arguments _arguments;
8+
9+
public TeamCity(Arguments arguments)
10+
{
11+
_arguments = arguments;
12+
}
13+
14+
715
public bool CanApplyToCurrentContext()
816
{
917
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
@@ -16,7 +24,7 @@ public void PerformPreProcessingSteps(string gitDirectory)
1624
throw new ErrorException("Failed to find .git directory on agent. Please make sure agent checkout mode is enabled for you VCS roots - http://confluence.jetbrains.com/display/TCD8/VCS+Checkout+Mode");
1725
}
1826

19-
GitHelper.NormalizeGitDirectory(gitDirectory);
27+
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
2028
}
2129

2230
public string[] GenerateSetParameterMessage(string name, string value)

GitVersion/GitPreparer.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,13 @@
55

66
public class GitPreparer
77
{
8-
public GitPreparer(string targetPath, string url, string branchName, string username, string password)
8+
readonly Arguments _arguments;
9+
10+
public GitPreparer(Arguments arguments)
911
{
10-
TargetPath = targetPath;
11-
Url = url;
12-
BranchName = branchName;
13-
Username = username;
14-
Password = password;
12+
_arguments = arguments;
1513
}
1614

17-
public string TargetPath { get; private set; }
18-
19-
public string Url { get; private set; }
20-
21-
public string BranchName { get; private set; }
22-
23-
public string Username { get; private set; }
24-
25-
public string Password { get; private set; }
26-
2715
public bool IsDynamicGitRepository
2816
{
2917
get { return !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath); }
@@ -33,9 +21,9 @@ public bool IsDynamicGitRepository
3321

3422
public string Prepare()
3523
{
36-
var gitPath = TargetPath;
24+
var gitPath = _arguments.TargetPath;
3725

38-
if (!string.IsNullOrWhiteSpace(Url))
26+
if (!string.IsNullOrWhiteSpace(_arguments.TargetUrl))
3927
{
4028
gitPath = GetGitInfoFromUrl();
4129
}
@@ -45,7 +33,7 @@ public string Prepare()
4533

4634
private string GetGitInfoFromUrl()
4735
{
48-
var gitDirectory = Path.Combine(TargetPath, "_dynamicrepository", ".git");
36+
var gitDirectory = Path.Combine(_arguments.TargetPath, "_dynamicrepository", ".git");
4937
if (Directory.Exists(gitDirectory))
5038
{
5139
Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory));
@@ -54,32 +42,32 @@ private string GetGitInfoFromUrl()
5442
}
5543

5644
Credentials credentials = null;
57-
if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password))
45+
if (!string.IsNullOrWhiteSpace(_arguments.Username) && !string.IsNullOrWhiteSpace(_arguments.Password))
5846
{
59-
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", Username));
47+
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", _arguments.Username));
6048

6149
credentials = new Credentials()
6250
{
63-
Username = Username,
64-
Password = Password
51+
Username = _arguments.Username,
52+
Password = _arguments.Password
6553
};
6654
}
6755

68-
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", Url));
56+
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", _arguments.TargetUrl));
6957

70-
Repository.Clone(Url, gitDirectory, checkout: false, credentials: credentials);
58+
Repository.Clone(_arguments.TargetUrl, gitDirectory, checkout: false, credentials: credentials);
7159

72-
if (!string.IsNullOrWhiteSpace(BranchName))
60+
if (!string.IsNullOrWhiteSpace(_arguments.TargetBranch))
7361
{
7462
// Normalize (download branches) before using the branch
75-
GitHelper.NormalizeGitDirectory(gitDirectory);
63+
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
7664

7765
using (var repository = new Repository(gitDirectory))
7866
{
79-
var targetBranchName = string.Format("refs/heads/{0}", BranchName);
67+
var targetBranchName = string.Format("refs/heads/{0}", _arguments.TargetBranch);
8068
if (!string.Equals(repository.Head.CanonicalName, targetBranchName))
8169
{
82-
Logger.WriteInfo(string.Format("Switching to branch '{0}'", BranchName));
70+
Logger.WriteInfo(string.Format("Switching to branch '{0}'", _arguments.TargetBranch));
8371

8472
repository.Refs.UpdateTarget("HEAD", targetBranchName);
8573
}

GitVersion/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ static void Main()
2323

2424
ConfigureLogging(arguments);
2525

26-
var gitPreparer = new GitPreparer(arguments.TargetPath, arguments.TargetUrl,
27-
arguments.TargetBranch, arguments.Username, arguments.Password);
26+
var gitPreparer = new GitPreparer(arguments);
2827
var gitDirectory = gitPreparer.Prepare();
2928
if (string.IsNullOrEmpty(gitDirectory))
3029
{
3130
Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath);
3231
Environment.Exit(1);
3332
}
3433

35-
var applicableBuildServers = GetApplicableBuildServers().ToList();
34+
var applicableBuildServers = GetApplicableBuildServers(arguments).ToList();
3635

3736
foreach (var buildServer in applicableBuildServers)
3837
{
@@ -91,9 +90,9 @@ static void Main()
9190
Environment.Exit(exitCode.Value);
9291
}
9392

94-
static IEnumerable<IBuildServer> GetApplicableBuildServers()
93+
static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
9594
{
96-
return BuildServerList.BuildServers.Where(buildServer => buildServer.CanApplyToCurrentContext());
95+
return BuildServerList.GetApplicableBuildServers(arguments);
9796
}
9897

9998
static void ConfigureLogging(Arguments arguments)

GitVersionTask/VersionAndBranchFinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static bool TryGetVersion(string directory, out VersionAndBranchAndDate v
2121
return false;
2222
}
2323

24-
foreach (var buildServer in BuildServerList.GetApplicableBuildServers())
24+
var arguments = new Arguments();
25+
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(arguments))
2526
{
2627
Logger.WriteInfo(string.Format("Executing PerformPreProcessingSteps for '{0}'.", buildServer.GetType().Name));
2728
buildServer.PerformPreProcessingSteps(gitDirectory);

GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public void InnerExecute()
5353
return;
5454
}
5555

56-
WriteIntegrationParameters(versionAndBranch, BuildServerList.GetApplicableBuildServers());
56+
var arguments = new Arguments();
57+
WriteIntegrationParameters(versionAndBranch, BuildServerList.GetApplicableBuildServers(arguments));
5758
}
5859

5960
public void WriteIntegrationParameters(VersionAndBranch versionAndBranch, IEnumerable<IBuildServer> applicableBuildServers)

Tests/BuildServers/ContinuaCiTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public class ContinuaCiTests
88
[Test]
99
public void GenerateBuildVersion()
1010
{
11-
var versionBuilder = new ContinuaCi();
11+
var arguments = new Arguments();
12+
var versionBuilder = new ContinuaCi(arguments);
1213
var continuaCiVersion = versionBuilder.GenerateSetVersionMessage("0.0.0-Beta4.7");
1314
Assert.AreEqual("@@continua[setBuildVersion value='0.0.0-Beta4.7']", continuaCiVersion);
1415
}

Tests/BuildServers/TeamCityTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ public class TeamCityTests
77
[Test]
88
public void Develop_branch()
99
{
10-
var versionBuilder = new TeamCity();
10+
var arguments = new Arguments();
11+
var versionBuilder = new TeamCity(arguments);
1112
var tcVersion = versionBuilder.GenerateSetVersionMessage("0.0.0-Unstable4");
1213
Assert.AreEqual("##teamcity[buildNumber '0.0.0-Unstable4']", tcVersion);
1314
}
1415

1516
[Test]
1617
public void EscapeValues()
1718
{
18-
var versionBuilder = new TeamCity();
19+
var arguments = new Arguments();
20+
var versionBuilder = new TeamCity(arguments);
1921
var tcVersion = versionBuilder.GenerateSetParameterMessage("Foo", "0.8.0-unstable568 Branch:'develop' Sha:'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb'");
2022
Assert.AreEqual("##teamcity[setParameter name='GitFlowVersion.Foo' value='0.8.0-unstable568 Branch:|'develop|' Sha:|'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb|'']", tcVersion[0]);
2123
Assert.AreEqual("##teamcity[setParameter name='GitVersion.Foo' value='0.8.0-unstable568 Branch:|'develop|' Sha:|'ee69bff1087ebc95c6b43aa2124bd58f5722e0cb|'']", tcVersion[1]);

Tests/GitHelperTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public void CanDetermineTheVersionFromAFetchedMaster()
1212
{
1313
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/master");
1414

15-
GitHelper.NormalizeGitDirectory(gitDirectory);
15+
var arguments = new Arguments();
16+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
1617

1718
using (var repository = new Repository(gitDirectory))
1819
{
@@ -29,7 +30,8 @@ public void CanDetermineTheVersionFromAPullRequest()
2930

3031
var gitDirectory = FakeTeamCityFetchAndCheckout(repoPath, "refs/pull/1735/merge");
3132

32-
GitHelper.NormalizeGitDirectory(gitDirectory);
33+
var arguments = new Arguments();
34+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
3335

3436
using (var repository = new Repository(gitDirectory))
3537
{
@@ -44,7 +46,8 @@ public void CanDetermineTheVersionFromAFetchedDevelop()
4446
{
4547
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/develop");
4648

47-
GitHelper.NormalizeGitDirectory(gitDirectory);
49+
var arguments = new Arguments();
50+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
4851

4952
using (var repository = new Repository(gitDirectory))
5053
{
@@ -57,7 +60,8 @@ public void CanDetermineTheVersionFromAFetchedFeature()
5760
{
5861
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/feature/one");
5962

60-
GitHelper.NormalizeGitDirectory(gitDirectory);
63+
var arguments = new Arguments();
64+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
6165

6266
using (var repository = new Repository(gitDirectory))
6367
{

Tests/UpdateAssemblyInfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void StandardExecutionMode_CannotDetermineTheVersionFromADetachedHead()
106106
public void SetUp()
107107
{
108108
//avoid buildserver detection to make the tests pass on the buildserver
109-
BuildServerList.Selector = () => new List<IBuildServer>();
109+
BuildServerList.Selector = arguments => new List<IBuildServer>();
110110
}
111111

112112

0 commit comments

Comments
 (0)