Skip to content

Commit e9f2740

Browse files
cschoellChristof SchöllMarkEWaite
authored
Notify Commit: Ignore ssh subdomain to allow matching azure devops git ssh urls for notify (#1758)
* Loosely match host, ignoring ssh. subdomain to make notifiy commit hook work with azure devops * Remove import reformatting https://github.com/jenkinsci/git-plugin/blob/master/CONTRIBUTING.adoc says: > Code formatting in the git plugin varies between files. Try to maintain > reasonable consistency with the existing files where feasible. Please > don’t perform wholesale reformatting of a file without discussing > with the current maintainers. New code should follow the SCM API code > style guidelines. The SCM API code style guidelines say: https://github.com/jenkinsci/scm-api-plugin/blob/master/CONTRIBUTING.md#imports For code in src/test: > * imports of anything other than JUnit classes and Hamcrest matchers are banned. > * static imports of anything other than JUnit classes and Hamcrest > matchers are strongly discouraged. > * import static org.hamcrest.Matchers.*, import static org.junit.Assert.* > are expressly permitted. Any other static * imports are discouraged > unless code readability is significantly enhanced and the import is > restricted to a single class. * Ignore /_git/ meta path of https url when matching * Ignore versioning (as used in azure devops) in ssh path --------- Co-authored-by: Christof Schöll <[email protected]> Co-authored-by: Mark Waite <[email protected]>
1 parent 3ab523c commit e9f2740

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/hudson/plugins/git/GitStatus.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,28 @@ public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object n
208208
* @return true if left-hand side loosely matches right-hand side
209209
*/
210210
public static boolean looselyMatches(URIish lhs, URIish rhs) {
211-
return Objects.equals(lhs.getHost(),rhs.getHost())
211+
return looselyMatchHost(lhs, rhs)
212212
&& Objects.equals(normalizePath(lhs.getPath()), normalizePath(rhs.getPath()));
213213
}
214214

215+
/**
216+
* Match hosts removing any "ssh." at the start of the subdomain.
217+
* Some cloud providers prepend "ssh." in the host for ssh urls - while only allowing to send the https url (without ssh.) to the notify commit endpoint.
218+
*
219+
* Ignoring the "ssh" subdomain allows keeping loosely matching the url.
220+
*/
221+
private static boolean looselyMatchHost(URIish lhs, URIish rhs) {
222+
String lhsHost = StringUtils.removeStart(lhs.getHost(), "ssh.");
223+
String rhsHost = StringUtils.removeStart(rhs.getHost(), "ssh.");
224+
return Objects.equals(lhsHost, rhsHost);
225+
}
226+
215227
private static String normalizePath(String path) {
216228
if (path.startsWith("/")) path=path.substring(1);
217229
if (path.endsWith("/")) path=path.substring(0,path.length()-1);
218230
if (path.endsWith(".git")) path=path.substring(0,path.length()-4);
231+
if (path.matches("v\\d/.*")) path=path.substring(3); //remove leading versioning used in azure devops (e.g. v3/...)
232+
path = path.replace("/_git/", "/"); //ignore _git meta path in http urls as they are usually not in ssh urls
219233
return path;
220234
}
221235

src/test/java/hudson/plugins/git/GitStatusTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,20 @@ void testDoNotifyCommitWithAllowModeSha1() throws Exception {
596596
assertNotNull(lastBuild);
597597
assertEquals(1, lastBuild.getNumber());
598598
}
599+
600+
@Test
601+
public void testDoNotifyCommitWithSshAzureDevopsPath() throws Exception { /* No parameters */
602+
this.repoURL = "[email protected]:v3/myorg/PROJECT/reponame";
603+
FreeStyleProject project = setupNotifyProject();
604+
final String differingUrl = "https://[email protected]/myorg/PROJECT/_git/reponame";
605+
this.gitStatus.doNotifyCommit(requestWithNoParameter, differingUrl, branch, sha1, notifyCommitApiToken);
606+
assertEquals("URL: " + differingUrl
607+
+ " SHA1: " + sha1
608+
+ " Branches: " + branch, this.gitStatus.toString());
609+
610+
r.waitUntilNoActivity();
611+
FreeStyleBuild lastBuild = project.getLastBuild();
612+
assertNotNull(lastBuild);
613+
}
614+
599615
}

0 commit comments

Comments
 (0)