Skip to content
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

[JENKINS-54858] Enhance organization & Repository Name Extraction for Multiple URL Formats /feature/org-repo-name-extraction #1646

Open
wants to merge 14 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
24 changes: 24 additions & 0 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@
private boolean allowSecondFetch;
private boolean disableGitToolChooser;
private boolean addGitTagAction;
private String globalUrlRegEx;

public DescriptorImpl() {
super(GitSCM.class, GitRepositoryBrowser.class);
Expand Down Expand Up @@ -1772,6 +1773,29 @@
return Util.fixEmptyAndTrim(globalConfigEmail);
}

/**
* Global setting for Regular Expression
* @return url regex
*/
public String getGlobalUrlRegEx(){ return globalUrlRegEx; }

public void setGlobalUrlRegEx(String globalUrlRegEx){
if (globalUrlRegEx == null || globalUrlRegEx.trim().isEmpty()) {

Check warning on line 1783 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1783 is only partially covered, 2 branches are missing
// If the user doesn't provide a value, use the default one
globalUrlRegEx = getDefaultGlobalUrlRegEx();

Check warning on line 1785 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1785 is not covered by tests
}
this.globalUrlRegEx = globalUrlRegEx;
}

public String getDefaultGlobalUrlRegEx() {
return "(.*github.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)"+"&&&"+

Check warning on line 1791 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1791 is not covered by tests
"(.*gitlab.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)"+"&&&"+
"(.*?//(?<org>\\w+).*visualstudio.*?/(?<repo>[^/]+?)(?:\\.git)?/?$)"+"&&&"+
"(.*bitbucket.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)"+"&&&"+
"(.*assembla.com[:/](?<repo>[^/]+?)(?:\\.git)?$)"+"&&&"+
"(git@git.*?[:/](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)";
}

/**
* Global setting to be used to set GIT_COMMITTER_EMAIL and GIT_AUTHOR_EMAIL.
* @param globalConfigEmail user.email value to be assigned
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/hudson/plugins/git/util/BuildData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.model.Api;
import hudson.model.Run;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.Revision;
import hudson.plugins.git.UserRemoteConfig;
import java.io.Serializable;
Expand All @@ -26,10 +27,14 @@
import org.kohsuke.stapler.export.ExportedBean;

import static hudson.Util.fixNull;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Captures the Git related information for a build.
*
Expand Down Expand Up @@ -293,6 +298,88 @@
return remoteUrls.contains(remoteUrl);
}

protected GitSCM.DescriptorImpl getDescriptorImpl(){
return new GitSCM.DescriptorImpl();

Check warning on line 302 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 302 is not covered by tests
}
/**
* Extracts the repository name from a given Git remote URL.
* This method uses a global regular expression defined in the Jenkins Git plugin
* configuration (GitSCM.DescriptorImpl). If the global regex is not defined or empty,
* it defaults to the plugin's default regular expression. The method checks if the regex
* pattern contains a named capturing group 'repo' and attempts to extract it.
*
* @param remoteUrl The Git remote URL to parse.
* @return The repository name if matched and extracted successfully; otherwise, null.
*/
public String getRepoName(String remoteUrl) throws MalformedURLException {
GitSCM.DescriptorImpl descriptor = getDescriptorImpl();
String globalRegex = descriptor.getGlobalUrlRegEx();

if (globalRegex == null || globalRegex.isEmpty()) {

Check warning on line 318 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 318 is only partially covered, 2 branches are missing
globalRegex = descriptor.getDefaultGlobalUrlRegEx();

Check warning on line 319 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 319 is not covered by tests
}
String[] regexps = globalRegex.split("&&&");
for (String regex : regexps) {

Check warning on line 322 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 322 is only partially covered, one branch is missing
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(remoteUrl);

if (matcher.matches()) {
if (regex.contains("(?<repo>")) { // Check if regex contains the 'repo' named group

Check warning on line 327 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 327 is only partially covered, one branch is missing
try {
return matcher.group("repo");
} catch (IllegalArgumentException | IllegalStateException e) {
// Return null if there is an error extracting the 'repo' group
return null;
}
} else {
// Return null if regex does not contain the 'repo' named group
return null;
}
}
}
// Return null if no matching repository name is found in the URL
return null;

Check warning on line 341 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 330-341 are not covered by tests
}
/**
* Extracts the repository name from a given Git remote URL.
* This method uses a global regular expression defined in the Jenkins Git plugin
* configuration (GitSCM.DescriptorImpl). If the global regex is not defined or empty,
* it defaults to the plugin's default regular expression. The method checks if the regex
* pattern contains a named capturing group 'repo' and attempts to extract it.
*
* @param remoteUrl The Git remote URL to parse.
* @return The repository name if matched and extracted successfully; otherwise, null.
*/
public String getOrganizationName(String remoteUrl) {
GitSCM.DescriptorImpl descriptor = getDescriptorImpl();
String globalRegex = descriptor.getGlobalUrlRegEx();
if (globalRegex == null || globalRegex.isEmpty()) {

Check warning on line 356 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 356 is only partially covered, 2 branches are missing
globalRegex = descriptor.getDefaultGlobalUrlRegEx();

Check warning on line 357 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 357 is not covered by tests
}
String[] regexps = globalRegex.split("&&&");
for (String regex : regexps) {

Check warning on line 360 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 360 is only partially covered, one branch is missing
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(remoteUrl);

if (matcher.matches()) {
// Check if the pattern includes the 'org' group
if (regex.contains("(?<org>")) {
try {
return matcher.group("org");
} catch (IllegalArgumentException | IllegalStateException e) {
// Return null if there is an error extracting the 'org' group
return null;

Check warning on line 371 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 369-371 are not covered by tests
}
} else {
// Return null if regex does not contain the 'org' named group
return null;
}
}
}
// Return null if no matching org name is found in the URL
return null;

Check warning on line 380 in src/main/java/hudson/plugins/git/util/BuildData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 380 is not covered by tests
}

@Override
public BuildData clone() {
BuildData clone;
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/git/GitSCM/global.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<f:entry title="${%Global Config user.email Value}" field="globalConfigEmail">
<f:textbox />
</f:entry>
<f:entry title="${%Global Repository RegEx Url}" field="globalUrlRegEx">
<f:textbox default="${descriptor.defaultGlobalUrlRegEx}"/>
</f:entry>
<f:entry field="createAccountBasedOnEmail">
<f:checkbox title="${%Create new accounts based on author/committer's email}" name="createAccountBasedOnEmail" checked="${descriptor.createAccountBasedOnEmail}"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<j:if test="${!remoteUrl.startsWith('http')}">
<br/><strong>${%Repository}</strong>: ${remoteUrl}
</j:if>
<j:if test="${!empty(it.getOrganizationName(remoteUrl))}">
<br/><strong>${%Organization Name}</strong>: ${it.getOrganizationName(remoteUrl)}
</j:if>
<j:if test="${!empty(it.getRepoName(remoteUrl))}">
<br/><strong>${%Repository Name}</strong>: ${it.getRepoName(remoteUrl)}
</j:if>
</j:forEach>
</j:if>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<j:if test="${!remoteUrl.startsWith('http')}">
<br/><strong>${%Repository}</strong>: ${remoteUrl}
</j:if>
<j:if test="${!empty(it.getOrganizationName(remoteUrl))}">
<br/><strong>${%Organization Name}</strong>: ${it.getOrganizationName(remoteUrl)}
</j:if>
<j:if test="${!empty(it.getRepoName(remoteUrl))}">
<br/><strong>${%Repository Name}</strong>: ${it.getRepoName(remoteUrl)}
</j:if>
</j:forEach>
</j:if>
<ul>
Expand Down
105 changes: 104 additions & 1 deletion src/test/java/hudson/plugins/git/util/BuildDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import java.net.MalformedURLException;
import org.mockito.MockedStatic;
import jenkins.model.Jenkins;
import hudson.plugins.git.GitSCM;
import java.util.List;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;


/**
* @author Mark Waite
Expand All @@ -33,7 +44,7 @@ public class BuildDataTest {
private BuildData data;
private final ObjectId sha1 = ObjectId.fromString("929e92e3adaff2e6e1d752a8168c1598890fe84c");
private final String remoteUrl = "https://github.com/jenkinsci/git-plugin";

private GitSCM.DescriptorImpl descriptor;
@Before
public void setUp() throws Exception {
data = new BuildData();
Expand Down Expand Up @@ -578,4 +589,96 @@ public void testHashCodeEmptyData() {
emptyData.remoteUrls = null;
assertEquals(emptyData.hashCode(), emptyData.hashCode());
}

// Helper method for GitOrgRepoName-related setup
private void setupGitOrgRepoNameMock() throws MalformedURLException {

MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class);
Jenkins mockJenkins = mock(Jenkins.class);
mockedJenkins.when(Jenkins::getInstanceOrNull).thenReturn(mockJenkins);

// Create a mock for the GitSCM.DescriptorImpl class
descriptor = mock(GitSCM.DescriptorImpl.class);
// Setup the behavior for the mock descriptor when Jenkins.getDescriptor is called
when(mockJenkins.getDescriptor(GitSCM.class)).thenReturn(descriptor);
String mockRegexPattern =
"(.*github.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)" +
"&&&" +
"(.*gitlab.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)" +
"&&&" +
"(.*?//(?<org>\\w+).*visualstudio.*?/(?<repo>[^/]+?)(?:\\.git)?/?$)" +
"&&&" +
"(.*bitbucket.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)" +
"&&&" +
"(.*assembla.com[:/](?<repo>[^/]+?)(?:\\.git)?$)"+
"&&&" +
"(git@git.*?[:/](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)";
when(descriptor.getGlobalUrlRegEx()).thenReturn(mockRegexPattern);
data = spy(new BuildData() {
@Override
protected GitSCM.DescriptorImpl getDescriptorImpl() {
return descriptor;
}
});
}

@Test
public void testOrganizationAndRepoNameExtraction() throws MalformedURLException {
setupGitOrgRepoNameMock();
List<TestUrl> testUrls = new ArrayList<>();
testUrls.add(new TestUrl( "https://github.com/mohdishaq786/Backend_challenge_stage2.git","mohdishaq786","Backend_challenge_stage2"));
testUrls.add(new TestUrl("[email protected]:markewaite/tasks.git", "markewaite", "tasks"));
testUrls.add(new TestUrl("[email protected]:markewaite/bin.git", "markewaite", "bin"));
testUrls.add(new TestUrl("https://[email protected]/markewaite/tasks.git", "markewaite", "tasks"));
testUrls.add(new TestUrl("https://[email protected]/markewaite/git-client-plugin.git", "markewaite", "git-client-plugin"));
testUrls.add(new TestUrl("https://[email protected]/markewaite/bin.git", "markewaite", "bin"));
testUrls.add(new TestUrl("https://MarkEWaite:[email protected]/MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("https://MarkEWaite:[email protected]/MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("https://MarkEWaite:[email protected]/MarkEWaite/bin.git", "MarkEWaite", "bin"));
testUrls.add(new TestUrl("https://gitlab.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("https://gitlab.com/MarkEWaite/tasks", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("https://gitlab.com/MarkEWaite/bin", "MarkEWaite", "bin"));
testUrls.add(new TestUrl("https://github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("[email protected]:MarkEWaite/bin.git", "MarkEWaite", "bin"));
testUrls.add(new TestUrl("[email protected]:MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("[email protected]:MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("https://bitbucket.org/markewaite/bin.git", "markewaite", "bin"));
testUrls.add(new TestUrl("https://bitbucket.org/markewaite/git-client-plugin.git", "markewaite", "git-client-plugin"));
testUrls.add(new TestUrl("https://bitbucket.org/markewaite/tasks.git", "markewaite", "tasks"));
testUrls.add(new TestUrl("https://github.com/MarkEWaite/bin.git", "MarkEWaite", "bin"));
testUrls.add(new TestUrl("https://markwaite.visualstudio.com/_git/elisp", "markwaite", "elisp"));
testUrls.add(new TestUrl("https://markwaite.visualstudio.com/DefaultCollection/_git/", "markwaite", "_git"));
testUrls.add(new TestUrl("https://markwaite.visualstudio.com/DefaultCollection/elisp/_git/elisp", "markwaite", "elisp"));
testUrls.add(new TestUrl("https://git.assembla.com/git-plugin.bin.git", null, "git-plugin.bin"));
testUrls.add(new TestUrl("[email protected]:git-plugin.bin.git", null, "git-plugin.bin"));
testUrls.add(new TestUrl("ssh://[email protected]:22/DefaultCollection/_ssh/elisp", "markwaite", "elisp"));
testUrls.add(new TestUrl("ssh://[email protected]/MarkEWaite/tasks.git", "MarkEWaite", "tasks"));
testUrls.add(new TestUrl("ssh://git.assembla.com/git-plugin.bin.git", null, "git-plugin.bin"));



for (TestUrl testUrl : testUrls) {
String repoName = data.getRepoName(testUrl.remoteUrl);
String orgName = data.getOrganizationName(testUrl.remoteUrl);

assertEquals("Repo name mismatch for URL: " + testUrl.remoteUrl, testUrl.expectedRepoName, repoName);
assertEquals("Org name mismatch for URL: " + testUrl.remoteUrl, testUrl.expectedOrgName, orgName);

}
}

// Helper class to hold test URLs and expected results
private static class TestUrl {
String remoteUrl;
String expectedOrgName;
String expectedRepoName;

TestUrl(String remoteUrl, String expectedOrgName, String expectedRepoName) {
this.remoteUrl = remoteUrl;
this.expectedOrgName = expectedOrgName;
this.expectedRepoName = expectedRepoName;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenk
GitSCM.DescriptorImpl gitSCM = (GitSCM.DescriptorImpl) restartableJenkinsRule.j.jenkins.getScm(GitSCM.class.getSimpleName());
assertEquals("user_name", gitSCM.getGlobalConfigName());
assertEquals("[email protected]", gitSCM.getGlobalConfigEmail());
assertEquals("Global URL RegEx not configured correctly",
"(.*github.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)",
gitSCM.getGlobalUrlRegEx());
assertTrue("Allow second fetch setting not honored", gitSCM.isAllowSecondFetch());
assertTrue("Show entire commit summary setting not honored", gitSCM.isShowEntireCommitSummaryInChanges());
assertTrue("Hide credentials setting not honored", gitSCM.isHideCredentials());
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/jenkins/plugins/git/gitscm-casc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ unclassified:
addGitTagAction: true
showEntireCommitSummaryInChanges: true
useExistingAccountWithSameEmail: false
globalUrlRegEx: "(.*github.*?[/:](?<org>[^/]+)/(?<repo>[^/]+?)(?:\\.git)?$)"