diff --git a/src/main/java/hudson/plugins/git/GitStatus.java b/src/main/java/hudson/plugins/git/GitStatus.java index e273fc19cc..2d84994da1 100644 --- a/src/main/java/hudson/plugins/git/GitStatus.java +++ b/src/main/java/hudson/plugins/git/GitStatus.java @@ -208,14 +208,28 @@ public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object n * @return true if left-hand side loosely matches right-hand side */ public static boolean looselyMatches(URIish lhs, URIish rhs) { - return Objects.equals(lhs.getHost(),rhs.getHost()) + return looselyMatchHost(lhs, rhs) && Objects.equals(normalizePath(lhs.getPath()), normalizePath(rhs.getPath())); } + /** + * Match hosts removing any "ssh." at the start of the subdomain. + * 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. + * + * Ignoring the "ssh" subdomain allows keeping loosely matching the url. + */ + private static boolean looselyMatchHost(URIish lhs, URIish rhs) { + String lhsHost = StringUtils.removeStart(lhs.getHost(), "ssh."); + String rhsHost = StringUtils.removeStart(rhs.getHost(), "ssh."); + return Objects.equals(lhsHost, rhsHost); + } + private static String normalizePath(String path) { if (path.startsWith("/")) path=path.substring(1); if (path.endsWith("/")) path=path.substring(0,path.length()-1); if (path.endsWith(".git")) path=path.substring(0,path.length()-4); + if (path.matches("v\\d/.*")) path=path.substring(3); //remove leading versioning used in azure devops (e.g. v3/...) + path = path.replace("/_git/", "/"); //ignore _git meta path in http urls as they are usually not in ssh urls return path; } diff --git a/src/test/java/hudson/plugins/git/GitStatusTest.java b/src/test/java/hudson/plugins/git/GitStatusTest.java index 65613cb26a..9daf924ae0 100644 --- a/src/test/java/hudson/plugins/git/GitStatusTest.java +++ b/src/test/java/hudson/plugins/git/GitStatusTest.java @@ -17,9 +17,7 @@ import hudson.util.RunList; import java.io.File; import java.io.PrintWriter; -import java.net.URISyntaxException; import java.util.*; -import org.eclipse.jgit.transport.URIish; import java.util.logging.Level; import java.util.logging.Logger; import org.kohsuke.stapler.HttpResponses; @@ -606,4 +604,20 @@ public void testDoNotifyCommitWithAllowModeSha1() throws Exception { assertNotNull(lastBuild); assertEquals(lastBuild.getNumber(), 1); } + + @Test + public void testDoNotifyCommitWithSshAzureDevopsPath() throws Exception { /* No parameters */ + this.repoURL = "git@ssh.dev.azure.com:v3/myorg/PROJECT/reponame"; + FreeStyleProject project = setupNotifyProject(); + final String differingUrl = "https://myorg@dev.azure.com/myorg/PROJECT/_git/reponame"; + this.gitStatus.doNotifyCommit(requestWithNoParameter, differingUrl, branch, sha1, notifyCommitApiToken); + assertEquals("URL: " + differingUrl + + " SHA1: " + sha1 + + " Branches: " + branch, this.gitStatus.toString()); + + r.waitUntilNoActivity(); + FreeStyleBuild lastBuild = project.getLastBuild(); + assertNotNull(lastBuild); + } + }