11using System ;
2- using System . Collections . Generic ;
3- using System . Diagnostics ;
42using System . Diagnostics . CodeAnalysis ;
53using System . Globalization ;
64using System . Linq ;
@@ -31,6 +29,7 @@ public class UriString : StringEquivalent<UriString>, IEquatable<UriString>
3129
3230 public UriString ( string uriString ) : base ( NormalizePath ( uriString ) )
3331 {
32+ if ( uriString == null ) throw new ArgumentNullException ( nameof ( uriString ) , "Cannot create a null UriString" ) ;
3433 if ( uriString . Length == 0 ) return ;
3534 if ( Uri . TryCreate ( uriString , UriKind . Absolute , out url ) )
3635 {
@@ -71,43 +70,6 @@ void SetUri(Uri uri)
7170 }
7271
7372 IsHypertextTransferProtocol = uri . IsHypertextTransferProtocol ( ) ;
74-
75- if ( String . IsNullOrEmpty ( uri . Query ) ) return ;
76-
77- try
78- {
79- var query = ParseQueryString ( uri ) ;
80-
81- if ( query . ContainsKey ( "branch" ) && ! String . IsNullOrEmpty ( query [ "branch" ] ) )
82- {
83- Branch = query [ "branch" ] . Replace ( "%2F" , "/" ) ;
84- }
85-
86- if ( query . ContainsKey ( "pr" ) && ! String . IsNullOrEmpty ( query [ "pr" ] ) )
87- {
88- PullRequest = query [ "pr" ] . Replace ( "%2F" , "/" ) ;
89- }
90-
91- if ( query . ContainsKey ( "filepath" ) && ! String . IsNullOrEmpty ( query [ "filepath" ] ) )
92- {
93- RelativePathToOpen = query [ "filepath" ] . Replace ( "%2F" , "/" ) . Replace ( '/' , '\\ ' ) ;
94- }
95- }
96- catch //(Exception ex)
97- {
98- //log.WarnException("Failed to read URI query", ex);
99- }
100- }
101-
102- // This is not a complete query string parsing algorithm, but it's good enough for our needs.
103- static IDictionary < string , string > ParseQueryString ( Uri uri )
104- {
105- Debug . Assert ( uri . Query . StartsWith ( '?' ) ,
106- String . Format ( CultureInfo . InvariantCulture , "Uri.Query doesn't start with '?': '{0}'" , uri . Query ) ) ;
107- return uri . Query . Substring ( 1 ) . Split ( new [ ] { '&' } , StringSplitOptions . RemoveEmptyEntries )
108- . Select ( pair => pair . Split ( '=' ) )
109- . ToDictionary ( pair => pair . First ( ) , pair => pair . Length > 1 ? pair [ 1 ] : null ,
110- StringComparer . OrdinalIgnoreCase ) ;
11173 }
11274
11375 void SetFilePath ( Uri uri )
@@ -144,8 +106,6 @@ bool ParseScpSyntax(string scpString)
144106 return false ;
145107 }
146108
147- public string Branch { get ; private set ; }
148- public string PullRequest { get ; set ; }
149109 public string Host { get ; private set ; }
150110
151111 public string Owner { get ; private set ; }
@@ -154,20 +114,31 @@ bool ParseScpSyntax(string scpString)
154114
155115 public string NameWithOwner { get ; private set ; }
156116
157- public string RelativePathToOpen { get ; private set ; }
158-
159117 public bool IsFileUri { get ; private set ; }
160118
161- public bool IsValidUri
162- {
163- get { return url != null ; }
164- }
119+ public bool IsValidUri => url != null ;
165120
166- public Uri ToUri ( )
121+ /// <summary>
122+ /// Attempts a best-effort to convert the remote origin to a GitHub Repository URL.
123+ /// </summary>
124+ /// <returns></returns>
125+ public Uri ToRepositoryUrl ( )
167126 {
168- if ( url == null )
169- throw new InvalidOperationException ( "This Uri String is not a valid Uri" ) ;
170- return url ;
127+ if ( url != null && IsFileUri ) return url ;
128+
129+ var scheme = url != null && IsHypertextTransferProtocol
130+ ? url . Scheme
131+ : Uri . UriSchemeHttps ;
132+
133+ return new UriBuilder
134+ {
135+ Scheme = scheme ,
136+ Host = Host ,
137+ Path = NameWithOwner ,
138+ Port = url ? . Port == 80
139+ ? - 1
140+ : ( url ? . Port ?? - 1 )
141+ } . Uri ;
171142 }
172143
173144 /// <summary>
@@ -186,9 +157,7 @@ public static implicit operator UriString(string value)
186157 [ SuppressMessage ( "Microsoft.Usage" , "CA2225:OperatorOverloadsHaveNamedAlternates" ) ]
187158 public static implicit operator string ( UriString uriString )
188159 {
189- if ( uriString == null ) return null ;
190-
191- return uriString . Value ;
160+ return uriString ? . Value ;
192161 }
193162
194163 [ SuppressMessage ( "Microsoft.Usage" , "CA2234:PassSystemUriObjectsInsteadOfStrings" , Justification = "No." ) ]
@@ -197,7 +166,7 @@ public override UriString Combine(string addition)
197166 if ( url != null )
198167 {
199168 var urlBuilder = new UriBuilder ( url ) ;
200- if ( ! String . IsNullOrEmpty ( urlBuilder . Query ) )
169+ if ( ! string . IsNullOrEmpty ( urlBuilder . Query ) )
201170 {
202171 var query = urlBuilder . Query ;
203172 if ( query . StartsWith ( "?" , StringComparison . Ordinal ) )
@@ -221,7 +190,7 @@ public override UriString Combine(string addition)
221190 }
222191 return ToUriString ( urlBuilder . Uri ) ;
223192 }
224- return String . Concat ( Value , addition ) ;
193+ return string . Concat ( Value , addition ) ;
225194 }
226195
227196 public override string ToString ( )
@@ -253,12 +222,12 @@ static string GetSerializedValue(SerializationInfo info)
253222
254223 static string NormalizePath ( string path )
255224 {
256- return path == null ? null : path . Replace ( '\\ ' , '/' ) ;
225+ return path ? . Replace ( '\\ ' , '/' ) ;
257226 }
258227
259228 static string GetRepositoryName ( string repositoryNameSegment )
260229 {
261- if ( String . IsNullOrEmpty ( repositoryNameSegment )
230+ if ( string . IsNullOrEmpty ( repositoryNameSegment )
262231 || repositoryNameSegment . Equals ( "/" , StringComparison . Ordinal ) )
263232 {
264233 return null ;
0 commit comments