From 77034edeb9034e2dcc3d63b4aeea06a4795ef28a Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 26 Aug 2024 12:58:34 +0530 Subject: [PATCH 01/12] code_changes_For regular expression --- src/main/java/hudson/plugins/git/GitSCM.java | 24 +++- .../hudson/plugins/git/util/BuildData.java | 65 +++++++++++ .../hudson/plugins/git/GitSCM/global.jelly | 3 + .../plugins/git/util/BuildData/index.jelly | 2 + .../plugins/git/util/BuildData/summary.jelly | 2 + .../plugins/git/util/BuildDataTest.java | 108 +++++++++++++++++- .../git/GitSCMJCasCCompatibilityTest.java | 3 + .../jenkins/plugins/git/gitscm-casc.yaml | 1 + 8 files changed, 205 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index 6c06095ad5..9099aa21cf 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -1656,7 +1656,7 @@ public static final class DescriptorImpl extends SCMDescriptor { private boolean allowSecondFetch; private boolean disableGitToolChooser; private boolean addGitTagAction; - + private String globalUrlRegEx; public DescriptorImpl() { super(GitSCM.class, GitRepositoryBrowser.class); load(); @@ -1764,7 +1764,29 @@ public void setGlobalConfigName(String globalConfigName) { public String getGlobalConfigEmail() { 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()) { + // If the user doesn't provide a value, use the default one + globalUrlRegEx = getDefaultGlobalUrlRegEx(); + } + this.globalUrlRegEx = globalUrlRegEx; + + } + public String getDefaultGlobalUrlRegEx() { + return "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*gitlab.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*?//(?\\w+).*visualstudio.*?/(?[^/]+?)(?:\\.git)?/?$)"+"&&&"+ + "(.*bitbucket.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*assembla.com[:/](?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(git@git.*?[:/](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"; + } /** * Global setting to be used to set GIT_COMMITTER_EMAIL and GIT_AUTHOR_EMAIL. * @param globalConfigEmail user.email value to be assigned diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index ba4cb6d783..4bb6875b87 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -30,6 +30,12 @@ import java.net.URISyntaxException; import java.util.logging.Level; import java.util.logging.Logger; +import java.net.MalformedURLException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import hudson.plugins.git.GitSCM; + /** * Captures the Git related information for a build. * @@ -293,6 +299,65 @@ public boolean hasBeenReferenced(String remoteUrl) { return remoteUrls.contains(remoteUrl); } + protected GitSCM.DescriptorImpl getDescriptorImpl(){ + return new GitSCM.DescriptorImpl(); + } + + public String getRepoName(String remoteUrl) throws MalformedURLException { + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(remoteUrl); + + if (matcher.matches()) { + if (regex.contains("(?")) { // Check if regex contains the 'repo' named group + try { + return matcher.group("repo"); + } catch (IllegalArgumentException | IllegalStateException e) { + return "Failed to extract 'repo' group"; + } + } else { + return "Regex must contain a named group 'repo'"; + } + } + } + return "No matching repository name found in the URL"; + } + + public String getOrganizationName(String remoteUrl) { + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(remoteUrl); + + if (matcher.matches()) { + // Check if the pattern includes the 'org' group + if (regex.contains("(?")) { + try { + return matcher.group("org"); + } catch (IllegalArgumentException | IllegalStateException e) { + return "Regex must contain a named group 'org'"; + } + } else { + return "Organization name not found in the URL"; + } + } + } + return "No matching organization name found in the URL"; + +} + @Override public BuildData clone() { BuildData clone; diff --git a/src/main/resources/hudson/plugins/git/GitSCM/global.jelly b/src/main/resources/hudson/plugins/git/GitSCM/global.jelly index 2536b5ca71..f34054e1c8 100644 --- a/src/main/resources/hudson/plugins/git/GitSCM/global.jelly +++ b/src/main/resources/hudson/plugins/git/GitSCM/global.jelly @@ -8,6 +8,9 @@ + + + diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 7684a80e6f..24e399e2d9 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -18,6 +18,8 @@
${%Repository}: ${remoteUrl}
+
${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
${%Repo Name}: ${it.getRepoName(remoteUrl)}
    diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly index 6b49d2f7c2..4c6c8a612b 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly @@ -14,6 +14,8 @@
    ${%Repository}: ${remoteUrl}
    +
    ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
    ${%Repo Name}: ${it.getRepoName(remoteUrl)}
      diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index fb47137281..f3c6810dc2 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -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 @@ -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(); @@ -441,6 +452,7 @@ public void testSimilarToContainsNullURL() { simple3.addRemoteUrl(null); simple3.addRemoteUrl(SIMPLE_URL); assertTrue(simple.similarTo(simple3)); + System.out.println("Test Case " + ":............................................................................................"); } @Test @@ -534,7 +546,7 @@ public void testSimilarTo() { assertFalse("Distinct objects shouldn't be similar", dataClone.similarTo(data)); data2.addRemoteUrl(noSlash); - assertTrue("Objects with same remote URL dissimilar", data2.similarTo(dataClone)); + assertTrue("Objects with same remote URL dissimilar", data2.similarTo (dataClone)); assertTrue("Objects with same remote URL dissimilar", dataClone.similarTo(data2)); // Another saved build still keeps objects similar @@ -578,4 +590,96 @@ public void testHashCodeEmptyData() { emptyData.remoteUrls = null; assertEquals(emptyData.hashCode(), emptyData.hashCode()); } + + // Helper method for GitOrgRepoName-related setup + private void setupGitOrgRepoNameMock() throws MalformedURLException { + + MockedStatic 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.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*gitlab.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*?//(?\\w+).*visualstudio.*?/(?[^/]+?)(?:\\.git)?/?$)" + + "&&&" + + "(.*bitbucket.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*assembla.com[:/](?[^/]+?)(?:\\.git)?$)"+ + "&&&" + + "(git@git.*?[:/](?[^/]+)/(?[^/]+?)(?:\\.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 testUrls = new ArrayList<>(); + testUrls.add(new TestUrl( "https://github.com/mohdishaq786/Backend_challenge_stage2.git","mohdishaq786","Backend_challenge_stage2")); + testUrls.add(new TestUrl("git@bitbucket.org:markewaite/tasks.git", "markewaite", "tasks")); + testUrls.add(new TestUrl("git@bitbucket.org:markewaite/bin.git", "markewaite", "bin")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/tasks.git", "markewaite", "tasks")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/git-client-plugin.git", "markewaite", "git-client-plugin")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/bin.git", "markewaite", "bin")); + testUrls.add(new TestUrl("https://MarkEWaite:also-a-password@gitlab.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("https://MarkEWaite:another-password@github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("https://MarkEWaite:yes-this-is-a-password@github.com/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("git@github.com:MarkEWaite/bin.git", "MarkEWaite", "bin")); + testUrls.add(new TestUrl("git@gitlab.com:MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("git@github.com: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", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("git@git.assembla.com:git-plugin.bin.git", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("ssh://markwaite@vs-ssh.visualstudio.com:22/DefaultCollection/_ssh/elisp", "markwaite", "elisp")); + testUrls.add(new TestUrl("ssh://git@github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("ssh://git.assembla.com/git-plugin.bin.git", "Organization name not found in the URL", "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; + } + } + + } diff --git a/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java b/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java index 1b84d2f82a..441e688416 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java @@ -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("me@mail.com", gitSCM.getGlobalConfigEmail()); + assertEquals("Global URL RegEx not configured correctly", + "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.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()); diff --git a/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml b/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml index 7120c21f9a..f01320ce63 100644 --- a/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml +++ b/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml @@ -8,3 +8,4 @@ unclassified: addGitTagAction: true showEntireCommitSummaryInChanges: true useExistingAccountWithSameEmail: false + globalUrlRegEx: "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" From 1f5584cd25d14497dcfd5cd441b82821f5b11882 Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 26 Aug 2024 14:24:05 +0530 Subject: [PATCH 02/12] refactor repo name to Repository Name --- .../resources/hudson/plugins/git/util/BuildData/index.jelly | 2 +- .../resources/hudson/plugins/git/util/BuildData/summary.jelly | 2 +- src/test/java/hudson/plugins/git/util/BuildDataTest.java | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 24e399e2d9..438e0c1aea 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -19,7 +19,7 @@
      ${%Repository}: ${remoteUrl}
      ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
      ${%Repo Name}: ${it.getRepoName(remoteUrl)} +
      ${%Repository Name}: ${it.getRepoName(remoteUrl)}
        diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly index 4c6c8a612b..eb654f247a 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly @@ -15,7 +15,7 @@
        ${%Repository}: ${remoteUrl}
        ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
        ${%Repo Name}: ${it.getRepoName(remoteUrl)} +
        ${%Repository Name}: ${it.getRepoName(remoteUrl)}
          diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index f3c6810dc2..9da02b9a0a 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -452,7 +452,6 @@ public void testSimilarToContainsNullURL() { simple3.addRemoteUrl(null); simple3.addRemoteUrl(SIMPLE_URL); assertTrue(simple.similarTo(simple3)); - System.out.println("Test Case " + ":............................................................................................"); } @Test From ef74ca4acc1ccf473f5071d01cdd1ae53ebad41b Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 26 Aug 2024 12:58:34 +0530 Subject: [PATCH 03/12] code_changes_For regular expression --- src/main/java/hudson/plugins/git/GitSCM.java | 24 +++- .../hudson/plugins/git/util/BuildData.java | 65 +++++++++++ .../hudson/plugins/git/GitSCM/global.jelly | 3 + .../plugins/git/util/BuildData/index.jelly | 2 + .../plugins/git/util/BuildData/summary.jelly | 2 + .../plugins/git/util/BuildDataTest.java | 108 +++++++++++++++++- .../git/GitSCMJCasCCompatibilityTest.java | 3 + .../jenkins/plugins/git/gitscm-casc.yaml | 1 + 8 files changed, 205 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index ceab12095a..f733e948f7 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -1663,7 +1663,7 @@ public static final class DescriptorImpl extends SCMDescriptor { private boolean allowSecondFetch; private boolean disableGitToolChooser; private boolean addGitTagAction; - + private String globalUrlRegEx; public DescriptorImpl() { super(GitSCM.class, GitRepositoryBrowser.class); load(); @@ -1771,7 +1771,29 @@ public void setGlobalConfigName(String globalConfigName) { public String getGlobalConfigEmail() { 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()) { + // If the user doesn't provide a value, use the default one + globalUrlRegEx = getDefaultGlobalUrlRegEx(); + } + this.globalUrlRegEx = globalUrlRegEx; + + } + public String getDefaultGlobalUrlRegEx() { + return "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*gitlab.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*?//(?\\w+).*visualstudio.*?/(?[^/]+?)(?:\\.git)?/?$)"+"&&&"+ + "(.*bitbucket.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(.*assembla.com[:/](?[^/]+?)(?:\\.git)?$)"+"&&&"+ + "(git@git.*?[:/](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"; + } /** * Global setting to be used to set GIT_COMMITTER_EMAIL and GIT_AUTHOR_EMAIL. * @param globalConfigEmail user.email value to be assigned diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index ba4cb6d783..4bb6875b87 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -30,6 +30,12 @@ import java.net.URISyntaxException; import java.util.logging.Level; import java.util.logging.Logger; +import java.net.MalformedURLException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import hudson.plugins.git.GitSCM; + /** * Captures the Git related information for a build. * @@ -293,6 +299,65 @@ public boolean hasBeenReferenced(String remoteUrl) { return remoteUrls.contains(remoteUrl); } + protected GitSCM.DescriptorImpl getDescriptorImpl(){ + return new GitSCM.DescriptorImpl(); + } + + public String getRepoName(String remoteUrl) throws MalformedURLException { + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(remoteUrl); + + if (matcher.matches()) { + if (regex.contains("(?")) { // Check if regex contains the 'repo' named group + try { + return matcher.group("repo"); + } catch (IllegalArgumentException | IllegalStateException e) { + return "Failed to extract 'repo' group"; + } + } else { + return "Regex must contain a named group 'repo'"; + } + } + } + return "No matching repository name found in the URL"; + } + + public String getOrganizationName(String remoteUrl) { + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(remoteUrl); + + if (matcher.matches()) { + // Check if the pattern includes the 'org' group + if (regex.contains("(?")) { + try { + return matcher.group("org"); + } catch (IllegalArgumentException | IllegalStateException e) { + return "Regex must contain a named group 'org'"; + } + } else { + return "Organization name not found in the URL"; + } + } + } + return "No matching organization name found in the URL"; + +} + @Override public BuildData clone() { BuildData clone; diff --git a/src/main/resources/hudson/plugins/git/GitSCM/global.jelly b/src/main/resources/hudson/plugins/git/GitSCM/global.jelly index 2536b5ca71..f34054e1c8 100644 --- a/src/main/resources/hudson/plugins/git/GitSCM/global.jelly +++ b/src/main/resources/hudson/plugins/git/GitSCM/global.jelly @@ -8,6 +8,9 @@ + + + diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 7684a80e6f..24e399e2d9 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -18,6 +18,8 @@
          ${%Repository}: ${remoteUrl}
          +
          ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
          ${%Repo Name}: ${it.getRepoName(remoteUrl)}
            diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly index 6b49d2f7c2..4c6c8a612b 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly @@ -14,6 +14,8 @@
            ${%Repository}: ${remoteUrl}
            +
            ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
            ${%Repo Name}: ${it.getRepoName(remoteUrl)}
              diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index fb47137281..f3c6810dc2 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -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 @@ -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(); @@ -441,6 +452,7 @@ public void testSimilarToContainsNullURL() { simple3.addRemoteUrl(null); simple3.addRemoteUrl(SIMPLE_URL); assertTrue(simple.similarTo(simple3)); + System.out.println("Test Case " + ":............................................................................................"); } @Test @@ -534,7 +546,7 @@ public void testSimilarTo() { assertFalse("Distinct objects shouldn't be similar", dataClone.similarTo(data)); data2.addRemoteUrl(noSlash); - assertTrue("Objects with same remote URL dissimilar", data2.similarTo(dataClone)); + assertTrue("Objects with same remote URL dissimilar", data2.similarTo (dataClone)); assertTrue("Objects with same remote URL dissimilar", dataClone.similarTo(data2)); // Another saved build still keeps objects similar @@ -578,4 +590,96 @@ public void testHashCodeEmptyData() { emptyData.remoteUrls = null; assertEquals(emptyData.hashCode(), emptyData.hashCode()); } + + // Helper method for GitOrgRepoName-related setup + private void setupGitOrgRepoNameMock() throws MalformedURLException { + + MockedStatic 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.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*gitlab.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*?//(?\\w+).*visualstudio.*?/(?[^/]+?)(?:\\.git)?/?$)" + + "&&&" + + "(.*bitbucket.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" + + "&&&" + + "(.*assembla.com[:/](?[^/]+?)(?:\\.git)?$)"+ + "&&&" + + "(git@git.*?[:/](?[^/]+)/(?[^/]+?)(?:\\.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 testUrls = new ArrayList<>(); + testUrls.add(new TestUrl( "https://github.com/mohdishaq786/Backend_challenge_stage2.git","mohdishaq786","Backend_challenge_stage2")); + testUrls.add(new TestUrl("git@bitbucket.org:markewaite/tasks.git", "markewaite", "tasks")); + testUrls.add(new TestUrl("git@bitbucket.org:markewaite/bin.git", "markewaite", "bin")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/tasks.git", "markewaite", "tasks")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/git-client-plugin.git", "markewaite", "git-client-plugin")); + testUrls.add(new TestUrl("https://markewaite@bitbucket.org/markewaite/bin.git", "markewaite", "bin")); + testUrls.add(new TestUrl("https://MarkEWaite:also-a-password@gitlab.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("https://MarkEWaite:another-password@github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("https://MarkEWaite:yes-this-is-a-password@github.com/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("git@github.com:MarkEWaite/bin.git", "MarkEWaite", "bin")); + testUrls.add(new TestUrl("git@gitlab.com:MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("git@github.com: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", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("git@git.assembla.com:git-plugin.bin.git", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("ssh://markwaite@vs-ssh.visualstudio.com:22/DefaultCollection/_ssh/elisp", "markwaite", "elisp")); + testUrls.add(new TestUrl("ssh://git@github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); + testUrls.add(new TestUrl("ssh://git.assembla.com/git-plugin.bin.git", "Organization name not found in the URL", "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; + } + } + + } diff --git a/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java b/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java index 1b84d2f82a..441e688416 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMJCasCCompatibilityTest.java @@ -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("me@mail.com", gitSCM.getGlobalConfigEmail()); + assertEquals("Global URL RegEx not configured correctly", + "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.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()); diff --git a/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml b/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml index 7120c21f9a..f01320ce63 100644 --- a/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml +++ b/src/test/resources/jenkins/plugins/git/gitscm-casc.yaml @@ -8,3 +8,4 @@ unclassified: addGitTagAction: true showEntireCommitSummaryInChanges: true useExistingAccountWithSameEmail: false + globalUrlRegEx: "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)" From 6f85d331bb0070edb47d3040ea964de6018c57d1 Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 26 Aug 2024 14:24:05 +0530 Subject: [PATCH 04/12] refactor repo name to Repository Name --- .../resources/hudson/plugins/git/util/BuildData/index.jelly | 2 +- .../resources/hudson/plugins/git/util/BuildData/summary.jelly | 2 +- src/test/java/hudson/plugins/git/util/BuildDataTest.java | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 24e399e2d9..438e0c1aea 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -19,7 +19,7 @@
              ${%Repository}: ${remoteUrl}
              ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
              ${%Repo Name}: ${it.getRepoName(remoteUrl)} +
              ${%Repository Name}: ${it.getRepoName(remoteUrl)}
                diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly index 4c6c8a612b..eb654f247a 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly @@ -15,7 +15,7 @@
                ${%Repository}: ${remoteUrl}
                ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
                ${%Repo Name}: ${it.getRepoName(remoteUrl)} +
                ${%Repository Name}: ${it.getRepoName(remoteUrl)}
                  diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index f3c6810dc2..9da02b9a0a 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -452,7 +452,6 @@ public void testSimilarToContainsNullURL() { simple3.addRemoteUrl(null); simple3.addRemoteUrl(SIMPLE_URL); assertTrue(simple.similarTo(simple3)); - System.out.println("Test Case " + ":............................................................................................"); } @Test From b1b8b01e65ef9653aad6d016f8f82987807c4d0d Mon Sep 17 00:00:00 2001 From: ishaq <49558047+mohdishaq786@users.noreply.github.com> Date: Tue, 27 Aug 2024 03:53:02 +0530 Subject: [PATCH 05/12] remove unwanted space --- src/test/java/hudson/plugins/git/util/BuildDataTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index 9da02b9a0a..32a504752a 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -545,7 +545,7 @@ public void testSimilarTo() { assertFalse("Distinct objects shouldn't be similar", dataClone.similarTo(data)); data2.addRemoteUrl(noSlash); - assertTrue("Objects with same remote URL dissimilar", data2.similarTo (dataClone)); + assertTrue("Objects with same remote URL dissimilar", data2.similarTo(dataClone)); assertTrue("Objects with same remote URL dissimilar", dataClone.similarTo(data2)); // Another saved build still keeps objects similar From f82aae6a35b93ec4f8b86eeee5d33e1767c2e2d0 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 26 Aug 2024 20:04:59 -0600 Subject: [PATCH 06/12] Use consistent indentation --- .../resources/hudson/plugins/git/util/BuildData/index.jelly | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 438e0c1aea..515d63dde3 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -18,8 +18,8 @@
                  ${%Repository}: ${remoteUrl}
                  -
                  ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
                  ${%Repository Name}: ${it.getRepoName(remoteUrl)} +
                  ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
                  ${%Repository Name}: ${it.getRepoName(remoteUrl)}
                    From 539ad45114d41997efcde6966ebb5628c6f3f290 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 26 Aug 2024 20:06:02 -0600 Subject: [PATCH 07/12] Consistent formatting --- src/main/java/hudson/plugins/git/GitSCM.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index f733e948f7..903f08607b 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -1664,6 +1664,7 @@ public static final class DescriptorImpl extends SCMDescriptor { private boolean disableGitToolChooser; private boolean addGitTagAction; private String globalUrlRegEx; + public DescriptorImpl() { super(GitSCM.class, GitRepositoryBrowser.class); load(); From f169f3d9140c7a615f04b08ba921f31482b92adc Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 26 Aug 2024 20:08:54 -0600 Subject: [PATCH 08/12] More consistent formatting --- src/main/java/hudson/plugins/git/GitSCM.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index 903f08607b..50a012995f 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -1772,6 +1772,7 @@ public void setGlobalConfigName(String globalConfigName) { public String getGlobalConfigEmail() { return Util.fixEmptyAndTrim(globalConfigEmail); } + /** * Global setting for Regular Expression * @return url regex @@ -1784,9 +1785,8 @@ public void setGlobalUrlRegEx(String globalUrlRegEx){ globalUrlRegEx = getDefaultGlobalUrlRegEx(); } this.globalUrlRegEx = globalUrlRegEx; - - } + public String getDefaultGlobalUrlRegEx() { return "(.*github.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ "(.*gitlab.*?[/:](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"+"&&&"+ @@ -1795,6 +1795,7 @@ public String getDefaultGlobalUrlRegEx() { "(.*assembla.com[:/](?[^/]+?)(?:\\.git)?$)"+"&&&"+ "(git@git.*?[:/](?[^/]+)/(?[^/]+?)(?:\\.git)?$)"; } + /** * Global setting to be used to set GIT_COMMITTER_EMAIL and GIT_AUTHOR_EMAIL. * @param globalConfigEmail user.email value to be assigned From 43c99f5be52182da7f910689e72726b9de605b3c Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 26 Aug 2024 20:23:38 -0600 Subject: [PATCH 09/12] Use consistent indentation --- .../hudson/plugins/git/util/BuildData.java | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index 4bb6875b87..8e9d1d26e1 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -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; @@ -26,16 +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.net.MalformedURLException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import hudson.plugins.git.GitSCM; - /** * Captures the Git related information for a build. * @@ -304,15 +303,15 @@ protected GitSCM.DescriptorImpl getDescriptorImpl(){ } public String getRepoName(String remoteUrl) throws MalformedURLException { - GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); - String globalRegex = descriptor.getGlobalUrlRegEx(); - - if (globalRegex == null || globalRegex.isEmpty()) { - return "Global Regex is not set up"; - } - String[] regexps = globalRegex.split("&&&"); - for (String regex : regexps) { - Pattern pattern = Pattern.compile(regex); + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(remoteUrl); if (matcher.matches()) { @@ -326,37 +325,36 @@ public String getRepoName(String remoteUrl) throws MalformedURLException { return "Regex must contain a named group 'repo'"; } } - } - return "No matching repository name found in the URL"; - } + } + return "No matching repository name found in the URL"; + } public String getOrganizationName(String remoteUrl) { - GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); - String globalRegex = descriptor.getGlobalUrlRegEx(); - if (globalRegex == null || globalRegex.isEmpty()) { - return "Global Regex is not set up"; - } - String[] regexps = globalRegex.split("&&&"); - for (String regex : regexps) { - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(remoteUrl); - - if (matcher.matches()) { - // Check if the pattern includes the 'org' group - if (regex.contains("(?")) { - try { - return matcher.group("org"); - } catch (IllegalArgumentException | IllegalStateException e) { - return "Regex must contain a named group 'org'"; + GitSCM.DescriptorImpl descriptor = getDescriptorImpl(); + String globalRegex = descriptor.getGlobalUrlRegEx(); + if (globalRegex == null || globalRegex.isEmpty()) { + return "Global Regex is not set up"; + } + String[] regexps = globalRegex.split("&&&"); + for (String regex : regexps) { + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(remoteUrl); + + if (matcher.matches()) { + // Check if the pattern includes the 'org' group + if (regex.contains("(?")) { + try { + return matcher.group("org"); + } catch (IllegalArgumentException | IllegalStateException e) { + return "Regex must contain a named group 'org'"; + } + } else { + return "Organization name not found in the URL"; } - } else { - return "Organization name not found in the URL"; } } + return "No matching organization name found in the URL"; } - return "No matching organization name found in the URL"; - -} @Override public BuildData clone() { From 9fa2bf4011c58502ff54affbac61ef7deff8029d Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Sun, 8 Sep 2024 23:23:13 +0530 Subject: [PATCH 10/12] add_changes_according_to_new_requirement --- .../hudson/plugins/git/util/BuildData.java | 48 ++++++++++++++----- .../plugins/git/util/BuildData/index.jelly | 8 +++- .../plugins/git/util/BuildData/summary.jelly | 8 +++- .../plugins/git/util/BuildDataTest.java | 6 +-- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index 8e9d1d26e1..257673ef6e 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -301,13 +301,22 @@ public boolean hasBeenReferenced(String remoteUrl) { protected GitSCM.DescriptorImpl getDescriptorImpl(){ return new GitSCM.DescriptorImpl(); } - - public String getRepoName(String remoteUrl) throws MalformedURLException { + /** + * 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()) { - return "Global Regex is not set up"; + globalRegex = descriptor.getDefaultGlobalUrlRegEx(); } String[] regexps = globalRegex.split("&&&"); for (String regex : regexps) { @@ -319,21 +328,33 @@ public String getRepoName(String remoteUrl) throws MalformedURLException { try { return matcher.group("repo"); } catch (IllegalArgumentException | IllegalStateException e) { - return "Failed to extract 'repo' group"; + // Return null if there is an error extracting the 'repo' group + return null; } } else { - return "Regex must contain a named group 'repo'"; + // Return null if regex does not contain the 'repo' named group + return null; } } } - return "No matching repository name found in the URL"; - } - + // Return null if no matching repository name is found in the URL + return null; + } + /** + * 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()) { - return "Global Regex is not set up"; + globalRegex = descriptor.getDefaultGlobalUrlRegEx(); } String[] regexps = globalRegex.split("&&&"); for (String regex : regexps) { @@ -346,14 +367,17 @@ public String getOrganizationName(String remoteUrl) { try { return matcher.group("org"); } catch (IllegalArgumentException | IllegalStateException e) { - return "Regex must contain a named group 'org'"; + // Return null if there is an error extracting the 'org' group + return null; } } else { - return "Organization name not found in the URL"; + // Return null if regex does not contain the 'org' named group + return null; } } } - return "No matching organization name found in the URL"; + // Return null if no matching org name is found in the URL + return null; } @Override diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly index 515d63dde3..03167e1e3b 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/index.jelly @@ -18,8 +18,12 @@
                    ${%Repository}: ${remoteUrl}
                    -
                    ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
                    ${%Repository Name}: ${it.getRepoName(remoteUrl)} + +
                    ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
                    + +
                    ${%Repository Name}: ${it.getRepoName(remoteUrl)} +
                      diff --git a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly index eb654f247a..553bb09a6c 100644 --- a/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly +++ b/src/main/resources/hudson/plugins/git/util/BuildData/summary.jelly @@ -14,8 +14,12 @@
                      ${%Repository}: ${remoteUrl}
                      -
                      ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} -
                      ${%Repository Name}: ${it.getRepoName(remoteUrl)} + +
                      ${%Organization Name}: ${it.getOrganizationName(remoteUrl)} +
                      + +
                      ${%Repository Name}: ${it.getRepoName(remoteUrl)} +
                        diff --git a/src/test/java/hudson/plugins/git/util/BuildDataTest.java b/src/test/java/hudson/plugins/git/util/BuildDataTest.java index 32a504752a..c1527f0066 100644 --- a/src/test/java/hudson/plugins/git/util/BuildDataTest.java +++ b/src/test/java/hudson/plugins/git/util/BuildDataTest.java @@ -649,11 +649,11 @@ public void testOrganizationAndRepoNameExtraction() throws MalformedURLException 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", "Organization name not found in the URL", "git-plugin.bin")); - testUrls.add(new TestUrl("git@git.assembla.com:git-plugin.bin.git", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("https://git.assembla.com/git-plugin.bin.git", null, "git-plugin.bin")); + testUrls.add(new TestUrl("git@git.assembla.com:git-plugin.bin.git", null, "git-plugin.bin")); testUrls.add(new TestUrl("ssh://markwaite@vs-ssh.visualstudio.com:22/DefaultCollection/_ssh/elisp", "markwaite", "elisp")); testUrls.add(new TestUrl("ssh://git@github.com/MarkEWaite/tasks.git", "MarkEWaite", "tasks")); - testUrls.add(new TestUrl("ssh://git.assembla.com/git-plugin.bin.git", "Organization name not found in the URL", "git-plugin.bin")); + testUrls.add(new TestUrl("ssh://git.assembla.com/git-plugin.bin.git", null, "git-plugin.bin")); From 77ae34105324a0fa937092fbb0836f013d0ddba1 Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 9 Sep 2024 01:39:33 +0530 Subject: [PATCH 11/12] add_changes_according_to_new_requirement --- .../hudson/plugins/git/util/BuildData.java | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index 257673ef6e..3f4cc76408 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -301,16 +301,7 @@ public boolean hasBeenReferenced(String remoteUrl) { protected GitSCM.DescriptorImpl getDescriptorImpl(){ return new GitSCM.DescriptorImpl(); } - /** - * 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(); @@ -340,16 +331,7 @@ public String getRepoName(String remoteUrl) throws MalformedURLException { // Return null if no matching repository name is found in the URL return null; } - /** - * 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(); From 41c3afdcca6e47c35fdf5a5cf10601807561b9c0 Mon Sep 17 00:00:00 2001 From: mohdishaq786 Date: Mon, 9 Sep 2024 01:47:18 +0530 Subject: [PATCH 12/12] Add JavaDoc to `getRepoName` and `getOrganizationName` --- .../hudson/plugins/git/util/BuildData.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/plugins/git/util/BuildData.java b/src/main/java/hudson/plugins/git/util/BuildData.java index 3f4cc76408..257673ef6e 100644 --- a/src/main/java/hudson/plugins/git/util/BuildData.java +++ b/src/main/java/hudson/plugins/git/util/BuildData.java @@ -301,7 +301,16 @@ public boolean hasBeenReferenced(String remoteUrl) { protected GitSCM.DescriptorImpl getDescriptorImpl(){ return new GitSCM.DescriptorImpl(); } - + /** + * 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(); @@ -331,7 +340,16 @@ public String getRepoName(String remoteUrl) throws MalformedURLException { // Return null if no matching repository name is found in the URL return null; } - + /** + * 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();