-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code for retrieving project branches to separate class
- Loading branch information
1 parent
d6d4ad9
commit 2db8ef5
Showing
4 changed files
with
112 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/com/dabsquared/gitlabjenkins/trigger/branch/ProjectBranchesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.dabsquared.gitlabjenkins.trigger.branch; | ||
|
||
import com.dabsquared.gitlabjenkins.GitLabProjectBranchesService; | ||
import com.dabsquared.gitlabjenkins.Messages; | ||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty; | ||
import hudson.model.Job; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.scm.SCM; | ||
import jenkins.triggers.SCMTriggerItem; | ||
import org.eclipse.jgit.transport.RemoteConfig; | ||
import org.eclipse.jgit.transport.URIish; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* @author Robin Müller | ||
*/ | ||
public final class ProjectBranchesProvider { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(ProjectBranchesProvider.class.getName()); | ||
private static final ProjectBranchesProvider INSTANCE = new ProjectBranchesProvider(); | ||
|
||
private ProjectBranchesProvider() { } | ||
|
||
public static ProjectBranchesProvider instance() { | ||
return INSTANCE; | ||
} | ||
|
||
public List<String> getProjectBranches(Job<?, ?> project) throws IOException { | ||
final URIish sourceRepository = getSourceRepoURLDefault(project); | ||
GitLabConnectionProperty connectionProperty = project.getProperty(GitLabConnectionProperty.class); | ||
if (connectionProperty != null && connectionProperty.getClient() != null) { | ||
return GitLabProjectBranchesService.instance().getBranches(connectionProperty.getClient(), sourceRepository.toString()); | ||
} else { | ||
LOGGER.log(Level.WARNING, "getProjectBranches: gitlabHostUrl hasn't been configured globally. Job {0}.", project.getFullName()); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
/** | ||
* Get the URL of the first declared repository in the project configuration. | ||
* Use this as default source repository url. | ||
* | ||
* @return URIish the default value of the source repository url | ||
* @throws IllegalStateException Project does not use git scm. | ||
*/ | ||
private URIish getSourceRepoURLDefault(Job<?, ?> job) { | ||
SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job); | ||
GitSCM gitSCM = getGitSCM(item); | ||
if(gitSCM == null) { | ||
LOGGER.log(Level.WARNING, "Could not find GitSCM for project. Project = {1}, next build = {2}", | ||
array(job.getName(), String.valueOf(job.getNextBuildNumber()))); | ||
throw new IllegalStateException("This project does not use git:" + job.getName()); | ||
} | ||
return getFirstRepoURL(gitSCM.getRepositories()); | ||
} | ||
|
||
private URIish getFirstRepoURL(List<RemoteConfig> repositories) { | ||
if (!repositories.isEmpty()) { | ||
List<URIish> uris = repositories.get(repositories.size() - 1).getURIs(); | ||
if (!uris.isEmpty()) { | ||
return uris.get(uris.size() - 1); | ||
} | ||
} | ||
throw new IllegalStateException(Messages.GitLabPushTrigger_NoSourceRepository()); | ||
} | ||
|
||
private GitSCM getGitSCM(SCMTriggerItem item) { | ||
if(item != null) { | ||
for(SCM scm : item.getSCMs()) { | ||
if(scm instanceof GitSCM) { | ||
return (GitSCM) scm; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private Object[] array(Object... objects) { | ||
return objects; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,8 @@ public void setUp() throws IOException { | |
@Test | ||
public void shouldReturnProjectFromGitlabApi() throws Exception { | ||
// when | ||
GitlabProject gitlabProject = branchesService.findGitlabProjectForRepositoryUrl(gitLab, | ||
"[email protected]:groupOne/A.git"); | ||
GitlabProject gitlabProject = branchesService.findGitlabProjectForRepositoryUrl( | ||
gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
|
||
// then | ||
assertThat(gitlabProject, is(gitlabProjectA)); | ||
|
@@ -69,7 +69,7 @@ public void shouldReturnProjectFromGitlabApi() throws Exception { | |
@Test | ||
public void shouldReturnBranchNamesFromGitlabApi() throws Exception { | ||
// when | ||
List<String> actualBranchNames = branchesService.getBranches(gitLab, "[email protected]:groupOne/B.git"); | ||
List<String> actualBranchNames = branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/B.git"); | ||
|
||
// then | ||
assertThat(actualBranchNames, is(branchNamesProjectB)); | ||
|
@@ -78,9 +78,9 @@ public void shouldReturnBranchNamesFromGitlabApi() throws Exception { | |
@Test | ||
public void shouldNotCallGitlabApiGetProjectsWhenElementIsCached() throws Exception { | ||
// when | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
verify(gitlabApi, times(1)).getProjects(); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/B.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/B.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(1)).getProjects(); | ||
|
@@ -89,9 +89,9 @@ public void shouldNotCallGitlabApiGetProjectsWhenElementIsCached() throws Except | |
@Test | ||
public void shouldCallGitlabApiGetProjectsWhenElementIsNotCached() throws Exception { | ||
// when | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
verify(gitlabApi, times(1)).getProjects(); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/DoesNotExist.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/DoesNotExist.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(2)).getProjects(); | ||
|
@@ -100,9 +100,9 @@ public void shouldCallGitlabApiGetProjectsWhenElementIsNotCached() throws Except | |
@Test | ||
public void shoulNotCallGitlabApiGetBranchesWhenElementIsCached() throws Exception { | ||
// when | ||
branchesService.getBranches(gitLab, "[email protected]:groupOne/B.git"); | ||
branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/B.git"); | ||
verify(gitlabApi, times(1)).getBranches(gitlabProjectB); | ||
branchesService.getBranches(gitLab, "[email protected]:groupOne/B.git"); | ||
branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/B.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(1)).getProjects(); | ||
|
@@ -111,7 +111,7 @@ public void shoulNotCallGitlabApiGetBranchesWhenElementIsCached() throws Excepti | |
@Test | ||
public void shoulNotMakeUnnecessaryCallsToGitlabApiGetBranches() throws Exception { | ||
// when | ||
branchesService.getBranches(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(1)).getBranches(gitlabProjectA); | ||
|
@@ -121,12 +121,12 @@ public void shoulNotMakeUnnecessaryCallsToGitlabApiGetBranches() throws Exceptio | |
@Test | ||
public void shouldExpireBranchCacheAtSetTime() throws Exception { | ||
// first call should retrieve branches from gitlabApi | ||
branchesService.getBranches(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
verify(gitlabApi, times(1)).getBranches(gitlabProjectA); | ||
|
||
long timeAfterCacheExpiry = GitLabProjectBranchesService.BRANCH_CACHE_TIME_IN_MILLISECONDS + 2; | ||
when(timeUtility.getCurrentTimeInMillis()).thenReturn(timeAfterCacheExpiry); | ||
branchesService.getBranches(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.getBranches(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(2)).getBranches(gitlabProjectA); | ||
|
@@ -135,12 +135,12 @@ public void shouldExpireBranchCacheAtSetTime() throws Exception { | |
@Test | ||
public void shouldExpireProjectCacheAtSetTime() throws Exception { | ||
// first call should retrieve projects from gitlabApi | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
verify(gitlabApi, times(1)).getProjects(); | ||
|
||
long timeAfterCacheExpiry = GitLabProjectBranchesService.PROJECT_MAP_CACHE_TIME_IN_MILLISECONDS + 2; | ||
when(timeUtility.getCurrentTimeInMillis()).thenReturn(timeAfterCacheExpiry); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab, "[email protected]:groupOne/A.git"); | ||
branchesService.findGitlabProjectForRepositoryUrl(gitLab.instance(), "[email protected]:groupOne/A.git"); | ||
|
||
// then | ||
verify(gitlabApi, times(2)).getProjects(); | ||
|
@@ -203,4 +203,4 @@ private GitlabProject setupGitlabProject(String namespace, String name) { | |
return project; | ||
} | ||
|
||
} | ||
} |
This prints {0} instead of the full project name: