Skip to content

Commit

Permalink
Move code for retrieving project branches to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hugo committed Mar 15, 2016
1 parent d6d4ad9 commit 2db8ef5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabProject;

Expand Down Expand Up @@ -60,7 +61,7 @@ protected GitLabProjectBranchesService(TimeUtility timeUtility) {
this.timeUtility = timeUtility;
}

public List<String> getBranches(GitLab gitLab, String sourceRepositoryString) throws IOException {
public List<String> getBranches(GitlabAPI client, String sourceRepositoryString) throws IOException {

synchronized (projectBranchCache) {
BranchListEntry branchListEntry = projectBranchCache.get(sourceRepositoryString);
Expand All @@ -74,9 +75,9 @@ public List<String> getBranches(GitLab gitLab, String sourceRepositoryString) th
final List<String> branchNames = new ArrayList<String>();

try {
GitlabProject gitlabProject = findGitlabProjectForRepositoryUrl(gitLab, sourceRepositoryString);
GitlabProject gitlabProject = findGitlabProjectForRepositoryUrl(client, sourceRepositoryString);
if (gitlabProject != null) {
final List<GitlabBranch> branches = gitLab.instance().getBranches(gitlabProject);
final List<GitlabBranch> branches = client.getBranches(gitlabProject);
for (final GitlabBranch branch : branches) {
branchNames.add(branch.getName());
}
Expand All @@ -100,7 +101,7 @@ public List<String> getBranches(GitLab gitLab, String sourceRepositoryString) th
}
}

public GitlabProject findGitlabProjectForRepositoryUrl(GitLab gitLab, String sourceRepositoryString)
public GitlabProject findGitlabProjectForRepositoryUrl(GitlabAPI client, String sourceRepositoryString)
throws IOException {
synchronized (projectMapCache) {
String repositoryUrl = sourceRepositoryString.toLowerCase();
Expand All @@ -115,17 +116,17 @@ public GitlabProject findGitlabProjectForRepositoryUrl(GitLab gitLab, String sou
(Boolean) projectMapCache.containsKey(repositoryUrl), projectCacheExpiry,
timeUtility.getCurrentTimeInMillis() });
}
refreshGitLabProjectMap(gitLab);
refreshGitLabProjectMap(client);
}
return projectMapCache.get(repositoryUrl);
}
}

public Map<String, GitlabProject> refreshGitLabProjectMap(GitLab gitLab) throws IOException {
public Map<String, GitlabProject> refreshGitLabProjectMap(GitlabAPI client) throws IOException {
synchronized (projectMapCache) {
try {
projectMapCache.clear();
List<GitlabProject> projects = gitLab.instance().getProjects();
List<GitlabProject> projects = client.getProjects();
for (GitlabProject gitlabProject : projects) {
projectMapCache.put(gitlabProject.getSshUrl().toLowerCase(), gitlabProject);
projectMapCache.put(gitlabProject.getHttpUrl().toLowerCase(), gitlabProject);
Expand Down
69 changes: 3 additions & 66 deletions src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dabsquared.gitlabjenkins.model.MergeRequestHook;
import com.dabsquared.gitlabjenkins.model.PushHook;
import com.dabsquared.gitlabjenkins.trigger.branch.ProjectBranchesProvider;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterType;
Expand Down Expand Up @@ -263,37 +264,6 @@ public ListBoxModel doFillTriggerOpenMergeRequestOnPushItems(@QueryParameter Str
new Option("On push to source or target branch", "both", triggerOpenMergeRequestOnPush.matches("both") ));
}

private List<String> getProjectBranches(final Job<?, ?> job) throws IOException, IllegalStateException {
if (!(job instanceof AbstractProject<?, ?>)) {
return Lists.newArrayList();
}

final URIish sourceRepository = getSourceRepoURLDefault(job);

if (sourceRepository == null) {
throw new IllegalStateException(Messages.GitLabPushTrigger_NoSourceRepository());
}

if (!getGitlabHostUrl().isEmpty()) {
return GitLabProjectBranchesService.instance().getBranches(getGitlab(), sourceRepository.toString());
} else {
LOGGER.log(Level.WARNING, "getProjectBranches: gitlabHostUrl hasn't been configured globally. Job {0}.",
job.getFullName());
return Lists.newArrayList();
}
}

private GitSCM getGitSCM(SCMTriggerItem item) {
if(item != null) {
for(SCM scm : item.getSCMs()) {
if(scm instanceof GitSCM) {
return (GitSCM) scm;
}
}
}
return null;
}

private static List<String> splitBranchSpec(final String spec) {
return Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(spec));
}
Expand All @@ -305,7 +275,7 @@ private AutoCompletionCandidates doAutoCompleteBranchesSpec(final Job<?, ?> job,
List<String> values = ac.getValues();

try {
List<String> branches = this.getProjectBranches(job);
List<String> branches = ProjectBranchesProvider.instance().getProjectBranches(job);
// show all suggestions for short strings
if (query.length() < 2){
values.addAll(branches);
Expand Down Expand Up @@ -345,7 +315,7 @@ private FormValidation doCheckBranchesSpec(@AncestorInPath final Job<?, ?> proje

final List<String> projectBranches;
try {
projectBranches = this.getProjectBranches(project);
projectBranches = ProjectBranchesProvider.instance().getProjectBranches(project);
} catch (final IllegalStateException ex) {
return FormValidation.warning(Messages.GitLabPushTrigger_CannotConnectToGitLab(ex.getMessage()));
} catch (final IOException ex) {
Expand Down Expand Up @@ -381,39 +351,6 @@ public FormValidation doCheckExcludeBranchesSpec(@AncestorInPath final Job<?, ?>
return this.doCheckBranchesSpec(project, value);
}

/**
* 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.
*/
protected URIish getSourceRepoURLDefault(Job job) {
URIish url = null;
SCMTriggerItem item = 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}",
new String[] {
job.getName(),
String.valueOf(job.getNextBuildNumber()) });
throw new IllegalStateException("This project does not use git:" + job.getName());
}

List<RemoteConfig> repositories = gitSCM.getRepositories();
if (!repositories.isEmpty()) {
RemoteConfig defaultRepository = repositories.get(repositories.size() - 1);
List<URIish> uris = defaultRepository.getURIs();
if (!uris.isEmpty()) {
return uris.get(uris.size() - 1);
}
}

return null;
}

public GitLab getGitlab() {
if (gitlab == null) {
gitlab = new GitLab();
Expand Down
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.

Copy link
@0xC4N1

0xC4N1 Nov 8, 2016

This prints {0} instead of the full project name:

Nov 08, 2016 3:21:03 PM com.dabsquared.gitlabjenkins.trigger.branch.ProjectBranchesProvider getProjectBranches WARNING: getProjectBranches: gitlabHostUrl hasnt been configured globally. Job {0}.

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -203,4 +203,4 @@ private GitlabProject setupGitlabProject(String namespace, String name) {
return project;
}

}
}

0 comments on commit 2db8ef5

Please sign in to comment.