Skip to content

Commit 7ef98d4

Browse files
committed
introduce the concept of Authentication
reduces the use of the Arguments god class
1 parent e1d76da commit 7ef98d4

17 files changed

+75
-61
lines changed

GitVersionCore/ArgumentParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
8484

8585
if (IsSwitch("u", name))
8686
{
87-
arguments.Username = value;
87+
arguments.Authentication.Username = value;
8888
continue;
8989
}
9090

9191
if (IsSwitch("p", name))
9292
{
93-
arguments.Password = value;
93+
arguments.Authentication.Password = value;
9494
continue;
9595
}
9696

GitVersionCore/Arguments.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
namespace GitVersion
22
{
3-
using System;
4-
53
public class Arguments
64
{
75
public Arguments()
86
{
9-
Username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
10-
Password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
7+
Authentication = new Authentication();
118
Output = OutputType.Json;
129
}
1310

11+
public Authentication Authentication;
12+
1413
public string TargetPath;
1514

1615
public string TargetUrl;
1716
public string TargetBranch;
1817

19-
public string Username;
20-
public string Password;
2118

2219
public bool IsHelp;
2320
public string LogFilePath;

GitVersionCore/Authentication.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
5+
public class Authentication
6+
{
7+
public Authentication()
8+
{
9+
Username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
10+
Password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
11+
}
12+
public string Username;
13+
public string Password;
14+
}
15+
}

GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
public class AppVeyor : BuildServerBase
88
{
9-
Arguments arguments;
9+
Authentication authentication;
1010

11-
public AppVeyor(Arguments arguments)
11+
public AppVeyor(Authentication authentication)
1212
{
13-
this.arguments = arguments;
13+
this.authentication = authentication;
1414
}
1515

1616
public override bool CanApplyToCurrentContext()
@@ -27,7 +27,7 @@ public override void PerformPreProcessingSteps(string gitDirectory)
2727

2828
var repoBranch = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH");
2929

30-
GitHelper.NormalizeGitDirectory(gitDirectory, arguments, repoBranch);
30+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, repoBranch);
3131
}
3232

3333
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)

GitVersionCore/BuildServers/BuildServerList.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ public static class BuildServerList
77
{
88
static List<IBuildServer> BuildServers;
99

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

1212
public static void ResetSelector()
1313
{
1414
Selector = DefaultSelector;
1515
}
1616

17-
public static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
17+
public static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)
1818
{
19-
return Selector(arguments);
19+
return Selector(authentication);
2020
}
2121

22-
static IEnumerable<IBuildServer> DefaultSelector(Arguments arguments)
22+
static IEnumerable<IBuildServer> DefaultSelector(Authentication authentication)
2323
{
2424
if (BuildServers == null)
2525
{
2626
BuildServers = new List<IBuildServer>
2727
{
28-
new ContinuaCi(arguments),
29-
new TeamCity(arguments),
30-
new AppVeyor(arguments)
28+
new ContinuaCi(authentication),
29+
new TeamCity(authentication),
30+
new AppVeyor(authentication)
3131
};
3232
}
3333

GitVersionCore/BuildServers/ContinuaCi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
public class ContinuaCi : BuildServerBase
66
{
7-
Arguments arguments;
7+
Authentication authentication;
88

9-
public ContinuaCi(Arguments arguments)
9+
public ContinuaCi(Authentication authentication)
1010
{
11-
this.arguments = arguments;
11+
this.authentication = authentication;
1212
}
1313

1414
public override bool CanApplyToCurrentContext()
@@ -35,7 +35,7 @@ public override void PerformPreProcessingSteps(string gitDirectory)
3535
throw new WarningException("Failed to find .git directory on agent");
3636
}
3737

38-
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
38+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
3939
}
4040

4141
public override string[] GenerateSetParameterMessage(string name, string value)

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion
66

77
public static class GitHelper
88
{
9-
public static void NormalizeGitDirectory(string gitDirectory, Arguments arguments, string branch = null)
9+
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, string branch = null)
1010
{
1111
using (var repo = new Repository(gitDirectory))
1212
{
@@ -17,7 +17,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Arguments argument
1717
Logger.WriteInfo(string.Format("Fetching from remote '{0}' using the following refspecs: {1}.",
1818
remote.Name, string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))));
1919

20-
var fetchOptions = BuildFetchOptions(arguments.Username, arguments.Password);
20+
var fetchOptions = BuildFetchOptions(authentication.Username, authentication.Password);
2121
repo.Network.Fetch(remote, fetchOptions);
2222

2323
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
@@ -37,7 +37,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Arguments argument
3737
}
3838
else
3939
{
40-
CreateFakeBranchPointingAtThePullRequestTip(repo, arguments);
40+
CreateFakeBranchPointingAtThePullRequestTip(repo, authentication);
4141
}
4242
}
4343
}
@@ -71,13 +71,13 @@ static FetchOptions BuildFetchOptions(string username, string password)
7171
return fetchOptions;
7272
}
7373

74-
static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Arguments arguments)
74+
static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Authentication authentication)
7575
{
7676
var remote = repo.Network.Remotes.Single();
7777

78-
var remoteTips = string.IsNullOrEmpty(arguments.Username) ?
78+
var remoteTips = string.IsNullOrEmpty(authentication.Username) ?
7979
GetRemoteTipsForAnonymousUser(repo, remote) :
80-
GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, arguments.Username, arguments.Password);
80+
GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, authentication.Username, authentication.Password);
8181

8282
var headTipSha = repo.Head.Tip.Sha;
8383

GitVersionCore/BuildServers/TeamCity.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
public class TeamCity : BuildServerBase
66
{
7-
Arguments arguments;
7+
Authentication authentication;
88

9-
public TeamCity(Arguments arguments)
9+
public TeamCity(Authentication authentication)
1010
{
11-
this.arguments = arguments;
11+
this.authentication = authentication;
1212
}
1313

1414
public override bool CanApplyToCurrentContext()
@@ -23,7 +23,7 @@ public override void PerformPreProcessingSteps(string gitDirectory)
2323
throw new WarningException("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");
2424
}
2525

26-
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
26+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
2727
}
2828

2929
public override string[] GenerateSetParameterMessage(string name, string value)

GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="AssemblyMetaData.cs" />
5959
<Compile Include="AssemblyVersionsGenerator.cs" />
6060
<Compile Include="AssemblyVersioningScheme.cs" />
61+
<Compile Include="Authentication.cs" />
6162
<Compile Include="BuildServers\AppVeyor.cs" />
6263
<Compile Include="BuildServers\BuildServerBase.cs" />
6364
<Compile Include="BuildServers\BuildServerList.cs" />

GitVersionExe/GitPreparer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ string GetGitInfoFromUrl()
4242
}
4343

4444
Credentials credentials = null;
45-
if (!string.IsNullOrWhiteSpace(arguments.Username) && !string.IsNullOrWhiteSpace(arguments.Password))
45+
var authentication = arguments.Authentication;
46+
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
4647
{
47-
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", arguments.Username));
48+
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", authentication.Username));
4849

4950
credentials = new UsernamePasswordCredentials
5051
{
51-
Username = arguments.Username,
52-
Password = arguments.Password
52+
Username = authentication.Username,
53+
Password = authentication.Password
5354
};
5455
}
5556

@@ -61,7 +62,7 @@ string GetGitInfoFromUrl()
6162
if (!string.IsNullOrWhiteSpace(arguments.TargetBranch))
6263
{
6364
// Normalize (download branches) before using the branch
64-
GitHelper.NormalizeGitDirectory(gitDirectory, arguments);
65+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication);
6566

6667
using (var repository = new Repository(gitDirectory))
6768
{

0 commit comments

Comments
 (0)