1- // Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2-
3- using PostSharp . Engineering . BuildTools . Build ;
4- using PostSharp . Engineering . BuildTools . ContinuousIntegration . Model ;
5- using PostSharp . Engineering . BuildTools . Tools . TeamCity ;
6- using PostSharp . Engineering . BuildTools . Utilities ;
7- using System ;
8- using System . Diagnostics . CodeAnalysis ;
9- using System . Net . Http ;
10- using System . Net . Http . Headers ;
11- using System . Text ;
12- using System . Text . RegularExpressions ;
13- using System . Threading . Tasks ;
14-
15- namespace PostSharp . Engineering . BuildTools . ContinuousIntegration ;
16-
17- public class AzureDevOpsRepository : VcsRepository
18- {
19- // E.g.
20- // https://postsharp@dev.azure.com/postsharp/Caravela/_git/Caravela.Repo
21- // https://dev.azure.com/postsharp/Caravela/_git/Caravela.Repo
22- private static readonly Regex _urlRegex = new (
23- @"^(?<protocol>https)://(?:(?<user>[^/@]+)@)?(?<domain>[^/]+)/(?<organization>[^/]+)?/(?<project>[^/]+)/_git/(?<repo>[^/]+)$" ) ;
24-
25- public override string Name { get ; }
26-
27- public override VcsProvider Provider => VcsProvider . AzureDevOps ;
28-
29- public string Domain { get ; }
30-
31- public string Organisation { get ; }
32-
33- public string Project { get ; }
34-
35- public string BaseUrl => $ "https://{ this . Domain } /{ this . Organisation } ";
36-
37- public override string SshUrl => throw new NotImplementedException ( "We don't use SSH for Azure DevOps at the moment." ) ;
38-
39- public override string HttpUrl => $ "{ this . BaseUrl } /{ this . Project } /_git/{ this . Name } ";
40-
41- public override string DeveloperMachineRemoteUrl => this . HttpUrl ;
42-
43- public override string TeamCityRemoteUrl => this . HttpUrl ;
44-
45- public override bool IsSshAgentRequired => false ;
46-
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 )
55- {
56- this . Name = name ;
57- this . Domain = domain ;
58- this . Organisation = organisation ;
59- this . Project = project ;
60- }
61-
62- public static bool TryParse ( string repoUrl , [ NotNullWhen ( true ) ] out AzureDevOpsRepository ? repository )
63- {
64- var match = _urlRegex . Match ( repoUrl ) ;
65-
66- if ( ! match . Success )
67- {
68- repository = null ;
69-
70- return false ;
71- }
72-
73- var name = match . Groups [ "repo" ] . Value ;
74- var domain = match . Groups [ "domain" ] . Value ;
75- var organisation = match . Groups [ "organization" ] . Value ;
76- var project = match . Groups [ "project" ] . Value ;
77- repository = new AzureDevOpsRepository ( project , name , organisation , domain ) ;
78-
79- return true ;
80- }
81-
82- public override bool TryDownloadTextFile ( ConsoleHelper console , string branch , string path , [ NotNullWhen ( true ) ] out string ? text )
83- {
84- var httpClient = new HttpClient ( ) ;
85-
86- var teamCitySourceReadToken = TeamCityHelper . GetTeamCitySourceReadToken ( ) ;
87-
88- var authString = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $@ "{ TeamCityHelper . TeamCityUsername } :{ teamCitySourceReadToken } " ) ) ;
89- httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , authString ) ;
90-
91- var uri = $ "{ this . BaseUrl } /{ this . Project } /_apis/git/repositories/{ this . Name } /items?path={ path } &versionDescriptor.version={ branch } ";
92-
93- console . WriteMessage ( $ "Downloading { uri } ." ) ;
94- text = httpClient . GetString ( uri ) ;
95-
96- return true ;
97- }
98-
99- public override Task < bool > TrySetBranchPoliciesAsync ( BuildContext context , string buildStatusGenre , string ? buildStatusName , bool dry )
100- => AzureDevOpsHelper . TrySetBranchPoliciesAsync ( context , this , buildStatusGenre , buildStatusName , dry ) ;
101-
102- public override Task < ( bool Success , string ? Url , bool RequiresBuild ) > TryCreatePullRequestAsync (
103- ConsoleHelper console ,
104- string sourceBranch ,
105- string targetBranch ,
106- string title )
107- => AzureDevOpsHelper . TryCreatePullRequestAsync ( console , this , sourceBranch , targetBranch , title ) ;
1+ // Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2+
3+ using PostSharp . Engineering . BuildTools . Build ;
4+ using PostSharp . Engineering . BuildTools . ContinuousIntegration . Model ;
5+ using PostSharp . Engineering . BuildTools . Tools . TeamCity ;
6+ using PostSharp . Engineering . BuildTools . Utilities ;
7+ using System ;
8+ using System . Diagnostics . CodeAnalysis ;
9+ using System . Net . Http ;
10+ using System . Net . Http . Headers ;
11+ using System . Text ;
12+ using System . Text . RegularExpressions ;
13+ using System . Threading . Tasks ;
14+
15+ namespace PostSharp . Engineering . BuildTools . ContinuousIntegration ;
16+
17+ public class AzureDevOpsRepository : VcsRepository
18+ {
19+ // E.g.
20+ // https://postsharp@dev.azure.com/postsharp/Caravela/_git/Caravela.Repo
21+ // https://dev.azure.com/postsharp/Caravela/_git/Caravela.Repo
22+ private static readonly Regex _urlRegex = new (
23+ @"^(?<protocol>https)://(?:(?<user>[^/@]+)@)?(?<domain>[^/]+)/(?<organization>[^/]+)?/(?<project>[^/]+)/_git/(?<repo>[^/]+)$" ) ;
24+
25+ public override string Name { get ; }
26+
27+ public override VcsProvider Provider => VcsProvider . AzureDevOps ;
28+
29+ public string Domain { get ; }
30+
31+ public string Organisation { get ; }
32+
33+ public string Project { get ; }
34+
35+ public string BaseUrl => $ "https://{ this . Domain } /{ this . Organisation } ";
36+
37+ public override string SshUrl => throw new NotImplementedException ( "We don't use SSH for Azure DevOps at the moment." ) ;
38+
39+ public override string HttpUrl => $ "{ this . BaseUrl } /{ this . Project } /_git/{ this . Name } ";
40+
41+ public override string DeveloperMachineRemoteUrl => this . HttpUrl ;
42+
43+ public override string TeamCityRemoteUrl => this . HttpUrl ;
44+
45+ public override bool IsSshAgentRequired => false ;
46+
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 )
55+ {
56+ this . Name = name ;
57+ this . Domain = domain ;
58+ this . Organisation = organisation ;
59+ this . Project = project ;
60+ }
61+
62+ public static bool TryParse ( string repoUrl , [ NotNullWhen ( true ) ] out AzureDevOpsRepository ? repository )
63+ {
64+ var match = _urlRegex . Match ( repoUrl ) ;
65+
66+ if ( ! match . Success )
67+ {
68+ repository = null ;
69+
70+ return false ;
71+ }
72+
73+ var name = match . Groups [ "repo" ] . Value ;
74+ var domain = match . Groups [ "domain" ] . Value ;
75+ var organisation = match . Groups [ "organization" ] . Value ;
76+ var project = match . Groups [ "project" ] . Value ;
77+ repository = new AzureDevOpsRepository ( project , name , organisation , domain ) ;
78+
79+ return true ;
80+ }
81+
82+ public override bool TryDownloadTextFile ( ConsoleHelper console , string branch , string path , [ NotNullWhen ( true ) ] out string ? text )
83+ {
84+ var httpClient = new HttpClient ( ) ;
85+
86+ var teamCitySourceReadToken = TeamCityHelper . GetTeamCitySourceReadToken ( ) ;
87+
88+ var authString = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( $@ "{ TeamCityHelper . TeamCityUsername } :{ teamCitySourceReadToken } " ) ) ;
89+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , authString ) ;
90+
91+ var uri = $ "{ this . BaseUrl } /{ this . Project } /_apis/git/repositories/{ this . Name } /items?path={ path } &versionDescriptor.version={ branch } ";
92+
93+ console . WriteMessage ( $ "Downloading { uri } ." ) ;
94+ text = httpClient . GetString ( uri ) ;
95+
96+ return true ;
97+ }
98+
99+ public override Task < bool > TrySetBranchPoliciesAsync ( BuildContext context , string buildStatusGenre , string ? buildStatusName , bool dry )
100+ => AzureDevOpsHelper . TrySetBranchPoliciesAsync ( context , this , buildStatusGenre , buildStatusName , dry ) ;
101+
102+ public override Task < ( bool Success , string ? Url , bool RequiresBuild ) > TryCreatePullRequestAsync (
103+ ConsoleHelper console ,
104+ string sourceBranch ,
105+ string targetBranch ,
106+ string title ,
107+ string ? body = null )
108+ => AzureDevOpsHelper . TryCreatePullRequestAsync ( console , this , sourceBranch , targetBranch , title , body ) ;
108109}
0 commit comments