Skip to content

Commit 8030fc1

Browse files
committed
Fixed Git authentication issues.
1 parent 779318f commit 8030fc1

File tree

9 files changed

+55
-117
lines changed

9 files changed

+55
-117
lines changed

src/PostSharp.Engineering.BuildTools/Build/Helpers/GitIntegrationHelper.cs

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,12 @@ public static bool TryAddTagToLastCommit( BuildContext context, PublishSettings
6666
return false;
6767
}
6868

69-
// Returns the remote origin.
70-
if ( !ToolInvocationHelper.InvokeTool(
71-
context.Console,
72-
"git",
73-
$"remote get-url origin",
74-
context.RepoDirectory,
75-
out _,
76-
out var gitOrigin ) )
69+
// Gets the remote origin.
70+
if ( !GitHelper.TryGetRemoteUrl( context, out var gitOrigin ) )
7771
{
7872
return false;
7973
}
8074

81-
gitOrigin = gitOrigin.Trim();
82-
var isHttps = gitOrigin.StartsWith( "https", StringComparison.InvariantCulture );
83-
84-
// When on TeamCity, if the repository is of HTTPS origin, the origin will be updated to form including Git authentication credentials.
85-
if ( context.IsContinuousIntegrationBuild )
86-
{
87-
if ( isHttps )
88-
{
89-
if ( !TeamCityHelper.TryGetTeamCitySourceWriteToken(
90-
out var teamcitySourceWriteTokenEnvironmentVariableName,
91-
out var teamcitySourceCodeWritingToken ) )
92-
{
93-
context.Console.WriteImportantMessage(
94-
$"{teamcitySourceWriteTokenEnvironmentVariableName} environment variable is not set. Using default credentials." );
95-
}
96-
else
97-
{
98-
gitOrigin = gitOrigin.Insert( 8, $"teamcity%40postsharp.net:{teamcitySourceCodeWritingToken}@" );
99-
}
100-
}
101-
}
102-
10375
// Pushes tag to origin.
10476
if ( !ToolInvocationHelper.InvokeTool(
10577
context.Console,
@@ -129,49 +101,12 @@ public static bool TryCommitVersionBump( BuildContext context, Version? currentV
129101
return false;
130102
}
131103

132-
// Returns the remote origin.
133-
ToolInvocationHelper.InvokeTool(
134-
context.Console,
135-
"git",
136-
$"remote get-url origin",
137-
context.RepoDirectory,
138-
out var gitExitCode,
139-
out var gitOrigin );
140-
141-
if ( gitExitCode != 0 )
104+
// Gets the remote origin.
105+
if ( !GitHelper.TryGetRemoteUrl( context, out var gitOrigin ) )
142106
{
143-
context.Console.WriteError( gitOrigin );
144-
145107
return false;
146108
}
147109

148-
gitOrigin = gitOrigin.Trim();
149-
var isHttps = gitOrigin.StartsWith( "https", StringComparison.InvariantCulture );
150-
151-
// When on TeamCity, Git user credentials are set to TeamCity and if the repository is of HTTPS origin, the origin will be updated to form including Git authentication credentials.
152-
if ( context.IsContinuousIntegrationBuild )
153-
{
154-
if ( !TeamCityHelper.TrySetGitIdentityCredentials( context ) )
155-
{
156-
return false;
157-
}
158-
159-
if ( isHttps )
160-
{
161-
if ( !TeamCityHelper.TryGetTeamCitySourceWriteToken(
162-
out var teamcitySourceWriteTokenEnvironmentVariableName,
163-
out var teamcitySourceCodeWritingToken ) )
164-
{
165-
context.Console.WriteImportantMessage(
166-
$"{teamcitySourceWriteTokenEnvironmentVariableName} environment variable is not set. Using default credentials." );
167-
}
168-
else
169-
{
170-
gitOrigin = gitOrigin.Insert( 8, $"teamcity%40postsharp.net:{teamcitySourceCodeWritingToken}@" );
171-
}
172-
}
173-
}
174-
175110
if ( !ToolInvocationHelper.InvokeTool(
176111
context.Console,
177112
"git",

src/PostSharp.Engineering.BuildTools/Build/Publishing/PostPublishCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal class PostPublishCommand : BaseCommand<PublishSettings>
1919
public static bool Execute( BuildContext context, PublishSettings settings )
2020
{
2121
context.Console.WriteHeading( "Finishing publishing." );
22+
23+
GitHelper.ConfigureCredentials( context );
2224

2325
if ( !MasterGenerator.TryWriteFiles( context, settings ) )
2426
{

src/PostSharp.Engineering.BuildTools/Build/Publishing/PrePublishCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static bool Execute( BuildContext context, PublishSettings settings )
2121
{
2222
var product = context.Product;
2323

24+
GitHelper.ConfigureCredentials( context );
25+
2426
if ( !MasterGenerator.TryWriteFiles( context, settings ) )
2527
{
2628
return false;

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/Model/TeamCityProject.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public TeamCityProject( TeamCityBuildConfiguration[] configurations, string[] ad
2020
this._subProjects = subProjects ?? [];
2121
}
2222

23-
public TeamCityProject( string objectName, string projectName, TeamCityBuildConfiguration[] configurations, string[] additionalBuildTypes, TeamCityProject[]? subProjects = null )
23+
public TeamCityProject(
24+
string objectName,
25+
string projectName,
26+
TeamCityBuildConfiguration[] configurations,
27+
string[] additionalBuildTypes,
28+
TeamCityProject[]? subProjects = null )
2429
: this( configurations, additionalBuildTypes, subProjects )
2530
{
2631
this._objectName = objectName;
@@ -52,14 +57,14 @@ void WriteProjectBody( TeamCityProject project )
5257
{
5358
writer.WriteLine( $" buildType({configuration.ObjectName})" );
5459
}
55-
60+
5661
foreach ( var configuration in project._additionalBuildTypes )
5762
{
5863
writer.WriteLine( $" buildType({configuration})" );
5964
}
6065

6166
writer.WriteLine();
62-
var configurationsOrder = string.Join( ',', project._configurations.Select( c => c.ObjectName ) );
67+
var configurationsOrder = string.Join( ',', project._configurations.Select( c => c.ObjectName ).Concat( this._additionalBuildTypes ) );
6368
writer.WriteLine( $" buildTypesOrder = arrayListOf({configurationsOrder})" );
6469

6570
if ( project._subProjects.Length > 0 )

src/PostSharp.Engineering.BuildTools/ContinuousIntegration/VcsRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using PostSharp.Engineering.BuildTools.Build;
55
using PostSharp.Engineering.BuildTools.ContinuousIntegration.Model;
66
using PostSharp.Engineering.BuildTools.Utilities;
7+
using System;
78
using System.Diagnostics.CodeAnalysis;
89
using System.Threading.Tasks;
910

src/PostSharp.Engineering.BuildTools/Dependencies/AutoUpdatedDependenciesHelper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,8 @@ public static bool TryUpdateAutoUpdatedDependencies( BuildContext context, Publi
164164
return false;
165165
}
166166

167-
// Returns the remote origin.
168-
if ( !ToolInvocationHelper.InvokeTool(
169-
context.Console,
170-
"git",
171-
"remote get-url origin",
172-
context.RepoDirectory,
173-
out _,
174-
out var gitOrigin ) )
167+
// Gets the remote origin.
168+
if ( !GitHelper.TryGetRemoteUrl( context, out var gitOrigin ) )
175169
{
176170
return false;
177171
}

src/PostSharp.Engineering.BuildTools/Dependencies/Model/VersionFile.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Build.Definition;
44
using Microsoft.Build.Evaluation;
55
using PostSharp.Engineering.BuildTools.Build;
6+
using System;
67
using System.Collections.Generic;
78
using System.Collections.Immutable;
89
using System.Diagnostics.CodeAnalysis;
@@ -33,57 +34,71 @@ public static bool TryRead(
3334
{
3435
versionFile = null;
3536
var dependenciesBuilder = ImmutableDictionary.CreateBuilder<string, DependencySource>();
36-
var versionsPath = Path.Combine( context.RepoDirectory, context.Product.VersionsFilePath );
37-
var centralPackageManagementVersionsPath = Path.Combine( context.RepoDirectory, "Directory.Packages.props" );
37+
var versionsPropsPath = Path.Combine( context.RepoDirectory, context.Product.VersionsFilePath );
38+
var directoryPackagesPropsPath = Path.Combine( context.RepoDirectory, "Directory.Packages.props" );
3839

39-
if ( !File.Exists( versionsPath ) )
40+
if ( !File.Exists( versionsPropsPath ) )
4041
{
41-
context.Console.WriteError( $"The file '{versionsPath}' does not exist." );
42+
context.Console.WriteError( $"The file '{versionsPropsPath}' does not exist." );
4243

4344
return false;
4445
}
4546

4647
var projectOptions = new ProjectOptions { GlobalProperties = new Dictionary<string, string>() { ["DoNotLoadGeneratedVersionFiles"] = "True" } };
4748

48-
var versionsProject = Project.FromFile( versionsPath, projectOptions );
49+
var versionsProject = Project.FromFile( versionsPropsPath, projectOptions );
4950
Project? centralPackageManagementVersionsProject = null;
5051

51-
if ( File.Exists( centralPackageManagementVersionsPath ) )
52+
if ( File.Exists( directoryPackagesPropsPath ) )
5253
{
53-
centralPackageManagementVersionsProject = Project.FromFile( centralPackageManagementVersionsPath, projectOptions );
54+
centralPackageManagementVersionsProject = Project.FromFile( directoryPackagesPropsPath, projectOptions );
5455
}
5556

5657
var defaultDependencyProperties = context.Product.ParametrizedDependencies
57-
.ToDictionary(
58+
.ToDictionary<ParametrizedDependency, string, (string Version, string File)>(
5859
d => d.Name,
59-
d => versionsProject.Properties.SingleOrDefault( p => p.Name == d.NameWithoutDot + "Version" )?.EvaluatedValue
60-
?? centralPackageManagementVersionsProject?.Properties.SingleOrDefault( p => p.Name == d.NameWithoutDot + "Version" )?.EvaluatedValue );
60+
d =>
61+
{
62+
var property = versionsProject.Properties.SingleOrDefault( p => p.Name == d.NameWithoutDot + "Version" );
63+
64+
if ( property == null )
65+
{
66+
property = centralPackageManagementVersionsProject?.Properties.SingleOrDefault( p => p.Name == d.NameWithoutDot + "Version" );
67+
}
68+
69+
if ( property == null )
70+
{
71+
return default;
72+
}
73+
74+
var s = property.EvaluatedValue.Trim();
75+
76+
return (s, property.Xml.Location.File);
77+
} );
6178

6279
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
6380

6481
foreach ( var dependencyDefinition in context.Product.ParametrizedDependencies )
6582
{
6683
var dependencyVersion = defaultDependencyProperties[dependencyDefinition.Name];
6784

68-
if ( dependencyVersion == null )
85+
if ( dependencyVersion == default )
6986
{
7087
// A property is required because we update it during the release process.
7188

7289
context.Console.WriteError(
73-
$"{versionsPath}: a property named '{dependencyDefinition.NameWithoutDot}Version' must exist, even with empty value." );
90+
$"A property named '{dependencyDefinition.NameWithoutDot}Version' must be defined, typically in 'eng/AutoUpdatedVersions.props', even with empty value." );
7491

7592
continue;
7693
}
7794

78-
dependencyVersion = dependencyVersion.Trim();
79-
8095
// The property value can be either empty or a semantic version, but empty values are not allowed on guest devices,
8196
// i.e. for build outside our VPN.
8297

83-
if ( dependencyVersion != "" && !Regex.IsMatch( dependencyVersion, @"^\d+.*$" ) )
98+
if ( dependencyVersion.Version != "" && !Regex.IsMatch( dependencyVersion.Version, @"^\d+.*$" ) )
8499
{
85100
context.Console.WriteError(
86-
$"{versionsPath}: invalid value '{dependencyVersion}' for property '{dependencyDefinition.Name}Version': the value is neither empty nor a valid version number." );
101+
$"{dependencyVersion.File}: invalid value '{dependencyVersion}' for property '{dependencyDefinition.Name}Version': the value is neither empty nor a valid version number." );
87102

88103
versionFile = null;
89104

@@ -95,16 +110,16 @@ public static bool TryRead(
95110

96111
if ( BuildContext.IsGuestDevice || !dependencyDefinition.Definition.GenerateSnapshotDependency )
97112
{
98-
if ( dependencyVersion == "" )
113+
if ( dependencyVersion.Version == "" )
99114
{
100-
context.Console.WriteError( $"{versionsPath}: missing value for property '{dependencyDefinition.NameWithoutDot}Version'." );
115+
context.Console.WriteError( $"{dependencyVersion.File}: missing value for property '{dependencyDefinition.NameWithoutDot}Version'." );
101116

102117
versionFile = null;
103118

104119
return false;
105120
}
106121

107-
dependencySource = DependencySource.CreateFeed( dependencyVersion, DependencyConfigurationOrigin.Default );
122+
dependencySource = DependencySource.CreateFeed( dependencyVersion.Version, DependencyConfigurationOrigin.Default );
108123
}
109124
else if ( settings.UseLocalDependencies && dependencyDefinition.Definition.ProductFamily == context.Product.ProductFamily )
110125
{

src/PostSharp.Engineering.BuildTools/Tools/Git/DownstreamMerge.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ private static bool TryMerge(
364364

365365
context.Console.WriteImportantMessage( $"Merging '{sourceBranch}' branch to '{targetBranch}' branch" );
366366

367+
GitHelper.ConfigureCredentials( context );
368+
367369
if ( !GitHelper.TryMerge( context, sourceBranch, targetBranch, "--no-commit --no-ff", true ) )
368370
{
369371
return false;

src/PostSharp.Engineering.BuildTools/Utilities/GitHelper.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -452,24 +452,6 @@ private static bool TryGetOriginUrl( BuildContext context, [NotNullWhen( true )]
452452

453453
url = gitOutput.Trim();
454454

455-
var isHttps = url.StartsWith( "https", StringComparison.InvariantCulture );
456-
457-
// When on TeamCity, origin will be updated to form including Git authentication credentials.
458-
if ( isHttps && context.IsContinuousIntegrationBuild )
459-
{
460-
if ( !TeamCityHelper.TryGetTeamCitySourceWriteToken(
461-
out var teamcitySourceWriteTokenEnvironmentVariableName,
462-
out var teamcitySourceCodeWritingToken ) )
463-
{
464-
context.Console.WriteImportantMessage(
465-
$"{teamcitySourceWriteTokenEnvironmentVariableName} environment variable is not set. Using default credentials." );
466-
}
467-
else
468-
{
469-
url = url.Insert( 8, $"teamcity%40postsharp.net:{teamcitySourceCodeWritingToken}@" );
470-
}
471-
}
472-
473455
return true;
474456
}
475457

0 commit comments

Comments
 (0)