Skip to content

Notify Commit: Ignore ssh subdomain to allow matching azure devops git ssh urls for notify #1758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/main/java/hudson/plugins/git/GitStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 16 additions & 2 deletions src/test/java/hudson/plugins/git/GitStatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "[email protected]:v3/myorg/PROJECT/reponame";
FreeStyleProject project = setupNotifyProject();
final String differingUrl = "https://[email protected]/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);
}

}
Loading