diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java index ee6af04..fdcb3b3 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java @@ -40,150 +40,164 @@ import org.kohsuke.stapler.StaplerRequest; * @author Daniel Brooks */ public class GitLabPushTrigger extends Trigger> { + private boolean triggerOnPush = true; + private boolean triggerOnMergeRequest = true; + @DataBoundConstructor + public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest) { + this.triggerOnPush = triggerOnPush; + this.triggerOnMergeRequest = triggerOnMergeRequest; + } - @DataBoundConstructor - public GitLabPushTrigger() { + public boolean getTriggerOnPush() { + return triggerOnPush; + } + public boolean getTriggerOnMergeRequest() { + return triggerOnMergeRequest; } public void onPost(final GitLabPushRequest req) { - getDescriptor().queue.execute(new Runnable() { + if (triggerOnPush) { + getDescriptor().queue.execute(new Runnable() { - public void run() { - LOGGER.log(Level.INFO, "{0} triggered.", job.getName()); - String name = " #" + job.getNextBuildNumber(); - GitLabPushCause cause = createGitLabPushCause(req); - Action[] actions = createActions(req); + public void run() { + LOGGER.log(Level.INFO, "{0} triggered.", job.getName()); + String name = " #" + job.getNextBuildNumber(); + GitLabPushCause cause = createGitLabPushCause(req); + Action[] actions = createActions(req); - if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) { - LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Triggering {1}", new String[]{job.getName(), name}); - } else { - LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.", job.getName()); + if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) { + LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Triggering {1}", new String[]{job.getName(), name}); + } else { + LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.", job.getName()); + } } - } - private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) { - GitLabPushCause cause; - String triggeredByUser = req.getCommits().get(0).getAuthor().getName(); - try { - cause = new GitLabPushCause(triggeredByUser, getLogFile()); - } catch (IOException ex) { - cause = new GitLabPushCause(triggeredByUser); + private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) { + GitLabPushCause cause; + String triggeredByUser = req.getCommits().get(0).getAuthor().getName(); + try { + cause = new GitLabPushCause(triggeredByUser, getLogFile()); + } catch (IOException ex) { + cause = new GitLabPushCause(triggeredByUser); + } + return cause; } - return cause; - } - private Action[] createActions(GitLabPushRequest req) { - ArrayList actions = new ArrayList(); + private Action[] createActions(GitLabPushRequest req) { + ArrayList actions = new ArrayList(); - String branch = req.getRef().replaceAll("refs/heads/", ""); + String branch = req.getRef().replaceAll("refs/heads/", ""); - LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch); + LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch); - Map values = new HashMap(); - values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch)); - values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch)); - values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch)); + Map values = new HashMap(); + values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch)); + values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch)); + values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch)); - List listValues = new ArrayList(values.values()); + List listValues = new ArrayList(values.values()); - ParametersAction parametersAction = new ParametersAction(listValues); - actions.add(parametersAction); + ParametersAction parametersAction = new ParametersAction(listValues); + actions.add(parametersAction); - RevisionParameterAction revision = new RevisionParameterAction(req.getLastCommit().getId()); - actions.add(revision); + RevisionParameterAction revision = new RevisionParameterAction(req.getLastCommit().getId()); + actions.add(revision); - Action[] actionsArray = actions.toArray(new Action[0]); + Action[] actionsArray = actions.toArray(new Action[0]); - return actionsArray; - } + return actionsArray; + } - }); + }); + } } public void onPost(final GitLabMergeRequest req) { - getDescriptor().queue.execute(new Runnable() { - public void run() { - LOGGER.log(Level.INFO, "{0} triggered.", job.getName()); - String name = " #" + job.getNextBuildNumber(); - GitLabMergeCause cause = createGitLabMergeCause(req); - Action[] actions = createActions(req); + if (triggerOnMergeRequest) { + getDescriptor().queue.execute(new Runnable() { + public void run() { + LOGGER.log(Level.INFO, "{0} triggered.", job.getName()); + String name = " #" + job.getNextBuildNumber(); + GitLabMergeCause cause = createGitLabMergeCause(req); + Action[] actions = createActions(req); - if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) { - LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Triggering {1}", new String[]{job.getName(), name}); - } else { - LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getName()); + if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) { + LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Triggering {1}", new String[]{job.getName(), name}); + } else { + LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getName()); + } } - } - private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) { - GitLabMergeCause cause; - try { - cause = new GitLabMergeCause(req, getLogFile()); - } catch (IOException ex) { - cause = new GitLabMergeCause(req); + private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) { + GitLabMergeCause cause; + try { + cause = new GitLabMergeCause(req, getLogFile()); + } catch (IOException ex) { + cause = new GitLabMergeCause(req); + } + return cause; } - return cause; - } - private Action[] createActions(GitLabMergeRequest req) { - List actions = new ArrayList(); + private Action[] createActions(GitLabMergeRequest req) { + List actions = new ArrayList(); - Map values = new HashMap(); - values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", String.valueOf(req.getObjectAttribute().getSourceBranch()))); - values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", String.valueOf(req.getObjectAttribute().getTargetBranch()))); + Map values = new HashMap(); + values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", String.valueOf(req.getObjectAttribute().getSourceBranch()))); + values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", String.valueOf(req.getObjectAttribute().getTargetBranch()))); - // Get source repository if communication to Gitlab is possible - String sourceRepoName = "origin"; - String sourceRepoURL = null; - - try { - sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace(); - sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl(); - } catch (IOException ex) { - LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()}); - sourceRepoURL = getSourceRepoURLDefault(); - } finally { - values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName)); - values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL)); + // Get source repository if communication to Gitlab is possible + String sourceRepoName = "origin"; + String sourceRepoURL = null; + + try { + sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace(); + sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl(); + } catch (IOException ex) { + LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()}); + sourceRepoURL = getSourceRepoURLDefault(); + } finally { + values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName)); + values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL)); + } + + List listValues = new ArrayList(values.values()); + + ParametersAction parametersAction = new ParametersAction(listValues); + actions.add(parametersAction); + + Action[] actionsArray = actions.toArray(new Action[0]); + + return actionsArray; } - List listValues = new ArrayList(values.values()); - - ParametersAction parametersAction = new ParametersAction(listValues); - actions.add(parametersAction); - - Action[] actionsArray = actions.toArray(new Action[0]); - - return actionsArray; + /** + * Get the URL of the first declared repository in the project configuration. + * Use this as default source repository url. + * + * @return String the default value of the source repository url + */ + private String getSourceRepoURLDefault() { + String url = null; + SCM scm = job.getScm(); + if (scm instanceof GitSCM) { + List repositories = ((GitSCM) scm).getRepositories(); + if (!repositories.isEmpty()){ + RemoteConfig defaultRepository = repositories.get(repositories.size()-1); + List uris = defaultRepository.getURIs(); + if (!uris.isEmpty()) { + URIish defaultUri = uris.get(uris.size()); + url = defaultUri.toString(); + } + } + } + return url; + } + }); + + } } - - /** - * Get the URL of the first declared repository in the project configuration. - * Use this as default source repository url. - * - * @return String the default value of the source repository url - */ - private String getSourceRepoURLDefault() { - String url = null; - SCM scm = job.getScm(); - if (scm instanceof GitSCM) { - List repositories = ((GitSCM) scm).getRepositories(); - if (!repositories.isEmpty()){ - RemoteConfig defaultRepository = repositories.get(repositories.size()-1); - List uris = defaultRepository.getURIs(); - if (!uris.isEmpty()) { - URIish defaultUri = uris.get(uris.size()); - url = defaultUri.toString(); - } - } - } - return url; - } - - }); - } @Override public DescriptorImpl getDescriptor() {