Skip to content

Commit ce58b2e

Browse files
committed
Docker: git authentication.
1 parent 6c9365d commit ce58b2e

File tree

7 files changed

+121
-64
lines changed

7 files changed

+121
-64
lines changed

src/PostSharp.Engineering.BuildTools/Build/Bumping/BumpCommand.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,38 @@ internal class BumpCommand : BaseCommand<BumpSettings>
2020
public static bool Execute( BuildContext context, BumpSettings settings )
2121
{
2222
var product = context.Product;
23-
context.Console.WriteHeading( $"Bumping the '{product.ProductName}' version" );
23+
var console = context.Console;
24+
console.WriteHeading( $"Bumping the '{product.ProductName}' version" );
2425

2526
var developmentBranch = product.DependencyDefinition.Branch;
2627

2728
if ( context.Branch != developmentBranch )
2829
{
29-
context.Console.WriteError(
30+
console.WriteError(
3031
$"The version bump can only be executed on the development branch ('{developmentBranch}'). The current branch is '{context.Branch}'." );
3132

3233
return false;
3334
}
3435

36+
if ( !GitHelper.ConfigureAuthentication( context ) )
37+
{
38+
console.WriteError( "Cannot configure git credentials." );
39+
40+
return false;
41+
}
42+
3543
// It is forbidden to push to the release branch, but it occasionally happens.
3644
// We need to make sure that there are no pending changes in the release branch to be merged to the development branch.
3745
// Failing to do so could result in missing published changes, and it could also break the version bump.
3846
var releaseBranch = product.DependencyDefinition.ReleaseBranch;
3947

4048
if ( releaseBranch == null )
4149
{
42-
context.Console.WriteMessage( "Skipping check for pending changes from the release branch, as the release branch is not set for this product." );
50+
console.WriteMessage( "Skipping check for pending changes from the release branch, as the release branch is not set for this product." );
4351
}
4452
else
4553
{
46-
context.Console.WriteMessage( $"Checking for pending changes from the release branch ('{releaseBranch}')." );
54+
console.WriteMessage( $"Checking for pending changes from the release branch ('{releaseBranch}')." );
4755

4856
if ( !GitHelper.TryCheckoutAndPull( context, releaseBranch ) )
4957
{
@@ -62,9 +70,9 @@ public static bool Execute( BuildContext context, BumpSettings settings )
6270

6371
if ( count > 0 )
6472
{
65-
context.Console.WriteError( $"There are pending changes from the '{releaseBranch}' branch." );
66-
context.Console.WriteError( $"Check the relevancy of the changes and merge the '{releaseBranch}' branch to the '{developmentBranch}'." );
67-
context.Console.WriteError( "Failing to do so could result in invalid version number of this product." );
73+
console.WriteError( $"There are pending changes from the '{releaseBranch}' branch." );
74+
console.WriteError( $"Check the relevancy of the changes and merge the '{releaseBranch}' branch to the '{developmentBranch}'." );
75+
console.WriteError( "Failing to do so could result in invalid version number of this product." );
6876

6977
return false;
7078
}
@@ -88,7 +96,7 @@ public static bool Execute( BuildContext context, BumpSettings settings )
8896

8997
if ( hasBumpSinceLastDeployment && !settings.OverridePreviousBump )
9098
{
91-
context.Console.WriteWarning( "Version has already been bumped since the last deployment." );
99+
console.WriteWarning( "Version has already been bumped since the last deployment." );
92100

93101
return true;
94102
}
@@ -112,15 +120,15 @@ public static bool Execute( BuildContext context, BumpSettings settings )
112120

113121
if ( !hasChangesInDependencies && !hasChangesSinceLastDeployment )
114122
{
115-
context.Console.WriteWarning( $"There are no changes since the last deployment." );
123+
console.WriteWarning( $"There are no changes since the last deployment." );
116124

117125
return true;
118126
}
119127

120128
// If there is a change in dependencies versions, we update BumpInfo.txt with changes.
121129
if ( hasChangesInDependencies )
122130
{
123-
context.Console.WriteMessage(
131+
console.WriteMessage(
124132
$"'{bumpInfoFilePath}' contents are outdated. Overwriting its old content '{oldBumpFileContent}' with new content '{newBumpInfoFile}'." );
125133

126134
File.WriteAllText( bumpInfoFilePath, newBumpInfoFile.ToString() );
@@ -145,12 +153,12 @@ public static bool Execute( BuildContext context, BumpSettings settings )
145153

146154
if ( settings.Force )
147155
{
148-
context.Console.WriteImportantMessage( $"{message} This is being ignored using --force." );
156+
console.WriteImportantMessage( $"{message} This is being ignored using --force." );
149157

150158
return true;
151159
}
152160

153-
context.Console.WriteError( $"{message} Do a fake change in a parent repo or use --force." );
161+
console.WriteError( $"{message} Do a fake change in a parent repo or use --force." );
154162

155163
return false;
156164
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public static bool Execute( BuildContext context, PublishSettings settings )
9595
return false;
9696
}
9797

98+
if ( !GitHelper.ConfigureAuthentication( context ) )
99+
{
100+
context.Console.WriteError( "Cannot configure git credentials." );
101+
102+
return false;
103+
}
104+
98105
if ( !MasterGenerator.TryWriteFiles( context, settings, out _ ) )
99106
{
100107
return false;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AzureDevOpsRepository : VcsRepository
1919
// E.g.
2020
// https://[email protected]/postsharp/Caravela/_git/Caravela.Repo
2121
// https://dev.azure.com/postsharp/Caravela/_git/Caravela.Repo
22-
private static readonly Regex _urlRegex = new Regex(
22+
private static readonly Regex _urlRegex = new(
2323
@"^(?<protocol>https)://(?:(?<user>[^/@]+)@)?(?<domain>[^/]+)/(?<organization>[^/]+)?/(?<project>[^/]+)/_git/(?<repo>[^/]+)$" );
2424

2525
public override string Name { get; }
@@ -44,7 +44,14 @@ public class AzureDevOpsRepository : VcsRepository
4444

4545
public override bool IsSshAgentRequired => false;
4646

47-
public AzureDevOpsRepository( string project, string name, string organisation = "postsharp", string domain = "dev.azure.com", string? defaultBranchParameter = null )
47+
public override string TokenEnvironmentVariableName => EnvironmentVariableNames.AzureDevOpsToken;
48+
49+
public AzureDevOpsRepository(
50+
string project,
51+
string name,
52+
string organisation = "postsharp",
53+
string domain = "dev.azure.com",
54+
string? defaultBranchParameter = null )
4855
: base( defaultBranchParameter )
4956
{
5057
this.Name = name;
@@ -83,7 +90,7 @@ public override bool TryDownloadTextFile( ConsoleHelper console, string branch,
8390
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", authString );
8491

8592
var uri = $"{this.BaseUrl}/{this.Project}/_apis/git/repositories/{this.Name}/items?path={path}&versionDescriptor.version={branch}";
86-
93+
8794
console.WriteMessage( $"Downloading {uri}." );
8895
text = httpClient.GetString( uri );
8996

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static class GitHubHelper
2323
private const string _productHeaderName = "PostSharp.Engineering";
2424
private static readonly string _productHeaderVersion = typeof(GitHubHelper).Assembly.GetName().Version!.ToString();
2525

26-
private static bool TryGetToken(
26+
internal static bool TryGetToken(
2727
ConsoleHelper console,
2828
[NotNullWhen( true )] out string? token,
2929
string tokenEnvironmentVariableName = EnvironmentVariableNames.GitHubToken )
@@ -286,26 +286,25 @@ public static async Task<bool> TryPrintBranchPoliciesAsync(
286286
.Repository( gitHubRepository.Name )
287287
.BranchProtectionRules()
288288
.AllPages()
289-
.Select(
290-
r => new
291-
{
292-
// For this code, use the output of the loop above.
293-
r.AllowsDeletions,
294-
r.AllowsForcePushes,
295-
r.BlocksCreations,
296-
r.DismissesStaleReviews,
297-
r.IsAdminEnforced,
298-
r.Pattern,
299-
r.RequiresApprovingReviews,
300-
r.RequiresCodeOwnerReviews,
301-
r.RequiresCommitSignatures,
302-
r.RequiresConversationResolution,
303-
r.RequiresLinearHistory,
304-
r.RequiresStatusChecks,
305-
r.RequiresStrictStatusChecks,
306-
r.RestrictsPushes,
307-
r.RestrictsReviewDismissals
308-
} )
289+
.Select( r => new
290+
{
291+
// For this code, use the output of the loop above.
292+
r.AllowsDeletions,
293+
r.AllowsForcePushes,
294+
r.BlocksCreations,
295+
r.DismissesStaleReviews,
296+
r.IsAdminEnforced,
297+
r.Pattern,
298+
r.RequiresApprovingReviews,
299+
r.RequiresCodeOwnerReviews,
300+
r.RequiresCommitSignatures,
301+
r.RequiresConversationResolution,
302+
r.RequiresLinearHistory,
303+
r.RequiresStatusChecks,
304+
r.RequiresStrictStatusChecks,
305+
r.RestrictsPushes,
306+
r.RestrictsReviewDismissals
307+
} )
309308
.Compile();
310309

311310
var rules = await graphQl.Run( branchProtectionRulesQuery );

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class GitHubRepository : VcsRepository
1414
// E.g.
1515
// [email protected]:postsharp/Metalama.Documentation.git // Used on TeamCity
1616
// https://github.com/postsharp/Metalama.Documentation.git // Used locally
17-
private static readonly Regex _urlRegex = new Regex( "^(?:[email protected]:|https://github.com/)(?<owner>[^/]+)/(?<repo>[^/]+)\\.git$" );
18-
17+
private static readonly Regex _urlRegex = new( "^(?:[email protected]:|https://github.com/)(?<owner>[^/]+)/(?<repo>[^/]+)\\.git$" );
18+
1919
public override string Name { get; }
2020

2121
public override VcsProvider Provider => VcsProvider.GitHub;
@@ -25,31 +25,33 @@ public class GitHubRepository : VcsRepository
2525
public override string SshUrl => $"[email protected]:{this.Owner}/{this.Name}.git";
2626

2727
public override string HttpUrl => $"https://github.com/{this.Owner}/{this.Name}.git";
28-
28+
2929
public override string DeveloperMachineRemoteUrl => this.HttpUrl;
3030

3131
public override string TeamCityRemoteUrl => this.SshUrl;
32-
32+
3333
public override bool IsSshAgentRequired => true;
3434

35+
public override string TokenEnvironmentVariableName => EnvironmentVariableNames.GitHubToken;
36+
3537
public GitHubRepository( string name, string owner, string? defaultBranchParameter = null )
3638
: base( defaultBranchParameter )
3739
{
3840
this.Name = name;
3941
this.Owner = owner;
4042
}
41-
43+
4244
public static bool TryParse( string repoUrl, [NotNullWhen( true )] out GitHubRepository? repository )
4345
{
4446
var match = _urlRegex.Match( repoUrl );
4547

4648
if ( !match.Success )
4749
{
4850
repository = null;
49-
51+
5052
return false;
5153
}
52-
54+
5355
var owner = match.Groups["owner"].Value;
5456
var name = match.Groups["repo"].Value;
5557
repository = new GitHubRepository( name, owner );

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,41 @@ namespace PostSharp.Engineering.BuildTools.ContinuousIntegration;
1313
public abstract class VcsRepository
1414
{
1515
public const string DefaultDefaultBranchParameter = "DefaultBranch";
16-
16+
1717
protected VcsRepository( string? defaultBranchParameter )
1818
{
1919
this.DefaultBranchParameter = defaultBranchParameter ?? DefaultDefaultBranchParameter;
2020
}
2121

2222
public abstract string Name { get; }
23-
23+
2424
/// <summary>
2525
/// Parameter name used to specify the default branch in TeamCity build configurations.
2626
/// </summary>
2727
public string DefaultBranchParameter { get; }
28-
28+
2929
public abstract VcsProvider Provider { get; }
30-
30+
3131
public abstract string SshUrl { get; }
3232

3333
public abstract string HttpUrl { get; }
34-
34+
3535
/// <summary>
3636
/// Gets the repository URL used by developers on their machines to access the repository.
3737
/// </summary>
3838
public abstract string DeveloperMachineRemoteUrl { get; }
39-
39+
4040
/// <summary>
4141
/// Gets the repository URL used by TeamCity server and agents to access the repository.
4242
/// </summary>
4343
public abstract string TeamCityRemoteUrl { get; }
44-
44+
4545
public abstract bool IsSshAgentRequired { get; }
46-
46+
47+
public abstract string TokenEnvironmentVariableName { get; }
48+
4749
public abstract bool TryDownloadTextFile( ConsoleHelper console, string branch, string path, [NotNullWhen( true )] out string? text );
48-
50+
4951
public abstract Task<bool> TrySetBranchPoliciesAsync( BuildContext context, string buildStatusGenre, string? buildStatusName, bool dry );
5052

5153
public abstract Task<string?> TryCreatePullRequestAsync( ConsoleHelper console, string sourceBranch, string targetBranch, string title );

0 commit comments

Comments
 (0)