From 11daa94adaa3e2bc77543aadc74ea964f8950a81 Mon Sep 17 00:00:00 2001 From: Csaba Kozak Date: Mon, 4 Jan 2016 18:53:57 +0100 Subject: [PATCH] Configure Gitlab URL at job level --- README.md | 2 +- .../com/dabsquared/gitlabjenkins/GitLab.java | 19 ++-- .../gitlabjenkins/GitLabPushTrigger.java | 89 +++++++++++++++---- .../gitlabjenkins/GitLabWebHook.java | 2 +- .../GitLabPushTrigger/config.jelly | 12 +++ ...ractGitLabPushTriggerGitlabServerTest.java | 5 +- 6 files changed, 104 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b6499bb6a..c716e92df 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Configuring access to Gitlab Optionally, the plugin communicates with the Gitlab server in order to fetch additional information. At this moment, this information is limited to fetching the source project of a Merge Request, in order to support merging from forked repositories. -To enable this functionality, a user should be set up on Gitlab, with adequate permissions to access the repository. On the global configuration screen, supply the gitlab host url ``http://your.gitlab.server`` and the API token of the user of choice. +To enable this functionality, a user should be set up on Gitlab, with adequate permissions to access the repository. On the global configuration screen, supply the gitlab host url ``http://your.gitlab.server`` and the API token of the user of choice. This can be overridden in the job configuration. Using it With A Job ===================== diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLab.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLab.java index 8414b3b5f..1b0550291 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLab.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLab.java @@ -10,13 +10,20 @@ public class GitLab { private static final Logger LOGGER = Logger.getLogger(GitLab.class.getName()); private GitlabAPI api; - public GitlabAPI instance() { + private String gitlabApiToken; + private String gitlabHostUrl = ""; + private boolean ignoreCertificateErrors; + + public GitLab(String gitlabApiToken, String gitlabHostUrl, boolean ignoreCertificateErrors) { + this.gitlabApiToken = gitlabApiToken; + this.gitlabHostUrl = gitlabHostUrl; + this.ignoreCertificateErrors = ignoreCertificateErrors; + } + + public GitlabAPI instance() { if (api == null) { - String token = GitLabPushTrigger.getDesc().getGitlabApiToken(); - String url = GitLabPushTrigger.getDesc().getGitlabHostUrl(); - boolean ignoreCertificateErrors = GitLabPushTrigger.getDesc().getIgnoreCertificateErrors(); - LOGGER.log(Level.FINE, "Connecting to Gitlab server ({0})", url); - api = GitlabAPI.connect(url, token); + LOGGER.log(Level.FINE, "Connecting to Gitlab server ({0})", gitlabHostUrl); + api = GitlabAPI.connect(gitlabHostUrl, gitlabApiToken); api.ignoreCertificateErrors(ignoreCertificateErrors); } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java index da690331a..0370f8347 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java @@ -89,12 +89,18 @@ public class GitLabPushTrigger extends Trigger> { private final String excludeBranchesSpec; private final String targetBranchRegex; private boolean acceptMergeRequestOnSuccess = false; + private final String jobGitlabApiToken; + private final String jobGitlabHostUrl; + private final boolean jobIgnoreCertificateErrors; + + private GitLab gitLab; @DataBoundConstructor public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, String triggerOpenMergeRequestOnPush, boolean ciSkip, boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addCiMessage, boolean addVoteOnMergeRequest, boolean acceptMergeRequestOnSuccess, String branchFilterName, - String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex) { + String includeBranchesSpec, String excludeBranchesSpec, String targetBranchRegex, + String jobGitlabApiToken, String jobGitlabHostUrl, boolean jobIgnoreCertificateErrors) { this.triggerOnPush = triggerOnPush; this.triggerOnMergeRequest = triggerOnMergeRequest; this.triggerOpenMergeRequestOnPush = triggerOpenMergeRequestOnPush; @@ -108,6 +114,13 @@ public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, S this.excludeBranchesSpec = excludeBranchesSpec; this.targetBranchRegex = targetBranchRegex; this.acceptMergeRequestOnSuccess = acceptMergeRequestOnSuccess; + this.jobGitlabApiToken = jobGitlabApiToken; + this.jobGitlabHostUrl = jobGitlabHostUrl; + this.jobIgnoreCertificateErrors = jobIgnoreCertificateErrors; + + if (jobGitlabApiToken != null && !jobGitlabApiToken.isEmpty()) { + gitLab = new GitLab(jobGitlabApiToken, jobGitlabHostUrl, jobIgnoreCertificateErrors); + } } public boolean getTriggerOnPush() { @@ -210,6 +223,26 @@ public String getExcludeBranchesSpec() { public String getTargetBranchRegex() { return this.targetBranchRegex == null ? "" : this.targetBranchRegex; } + public String getJobGitlabApiToken() { + if (jobGitlabApiToken != null && !jobGitlabApiToken.isEmpty()) { + return jobGitlabApiToken; + } + + return getDescriptor().getGitlabApiToken(); + } + + public String getJobGitlabHostUrl() { + if (jobGitlabHostUrl != null && !jobGitlabHostUrl.isEmpty()) { + return jobGitlabHostUrl; + } + + return getDescriptor().getGitlabHostUrl(); + } + + public boolean isJobIgnoreCertificateErrors() { + return jobIgnoreCertificateErrors; + } + // executes when the Trigger receives a push request public void onPost(final GitLabPushRequest req) { // TODO 1.621+ use standard method @@ -250,7 +283,7 @@ public void run() { } if(addCiMessage) { - req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); + req.createCommitStatus(getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); } } @@ -290,8 +323,8 @@ private Action[] createActions(GitLabPushRequest req) { if (!getDescriptor().getGitlabHostUrl().isEmpty()) { // Get source repository if communication to Gitlab is possible try { - sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace(); - sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl(); + sourceRepoName = req.getSourceProject(getGitlab()).getPathWithNamespace(); + sourceRepoURL = req.getSourceProject(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()}); } @@ -381,7 +414,7 @@ protected Job asJob() { } if(addCiMessage) { - req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); + req.createCommitStatus(getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); } } @@ -420,8 +453,8 @@ private Action[] createActions(GitLabMergeRequest req) { if (!getDescriptor().getGitlabHostUrl().isEmpty()) { // Get source repository if communication to Gitlab is possible try { - sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace(); - sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl(); + sourceRepoName = req.getSourceProject(getGitlab()).getPathWithNamespace(); + sourceRepoURL = req.getSourceProject(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()}); } @@ -498,7 +531,7 @@ private void onCompletedPushRequest(Run run, GitLabPushCause cause) { }else { status = "failed"; } - cause.getPushRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), status, Jenkins.getInstance().getRootUrl() + run.getUrl()); + cause.getPushRequest().createCommitStatus(getGitlab().instance(), status, Jenkins.getInstance().getRootUrl() + run.getUrl()); } } @@ -507,7 +540,7 @@ private void onCompleteMergeRequest(Run run,GitLabMergeCause cause){ try { GitlabProject proj = new GitlabProject(); proj.setId(cause.getMergeRequest().getObjectAttribute().getTargetProjectId()); - this.getDescriptor().getGitlab().instance().acceptMergeRequest( + getGitlab().instance().acceptMergeRequest( proj, cause.getMergeRequest().getObjectAttribute().getId(), "Merge Request accepted by jenkins build success"); @@ -531,8 +564,8 @@ private void onCompleteMergeRequest(Run run,GitLabMergeCause cause){ try { GitlabProject proj = new GitlabProject(); proj.setId(cause.getMergeRequest().getObjectAttribute().getTargetProjectId()); - org.gitlab.api.models.GitlabMergeRequest mr = this.getDescriptor().getGitlab().instance().getMergeRequest(proj,cause.getMergeRequest().getObjectAttribute().getId()); - this.getDescriptor().getGitlab().instance().createNote(mr,msg.toString()); + org.gitlab.api.models.GitlabMergeRequest mr = getGitlab().instance().getMergeRequest(proj,cause.getMergeRequest().getObjectAttribute().getId()); + getGitlab().instance().createNote(mr,msg.toString()); } catch (IOException e) { e.printStackTrace(); } @@ -547,7 +580,7 @@ private void onCompleteMergeRequest(Run run,GitLabMergeCause cause){ }else { status = "failed"; } - cause.getMergeRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), status, Jenkins.getInstance().getRootUrl() + run.getUrl()); + cause.getMergeRequest().createCommitStatus(getGitlab().instance(), status, Jenkins.getInstance().getRootUrl() + run.getUrl()); } } @@ -567,13 +600,13 @@ public void onStarted(Run run) { private void onStartedPushRequest(Run run, GitLabPushCause cause) { if(addCiMessage) { - cause.getPushRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), "running", Jenkins.getInstance().getRootUrl() + run.getUrl()); + cause.getPushRequest().createCommitStatus(getGitlab().instance(), "running", Jenkins.getInstance().getRootUrl() + run.getUrl()); } } private void onStartedMergeRequest(Run run, GitLabMergeCause cause) { if(addCiMessage) { - cause.getMergeRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), "running", Jenkins.getInstance().getRootUrl() + run.getUrl()); + cause.getMergeRequest().createCommitStatus(getGitlab().instance(), "running", Jenkins.getInstance().getRootUrl() + run.getUrl()); } } @@ -597,6 +630,14 @@ public static DescriptorImpl getDesc() { return DescriptorImpl.get(); } + public GitLab getGitlab() { + if (gitLab != null) { + return gitLab; + } + + return getDescriptor().getGitlab(); + } + public File getLogFile() { return new File(job.getRootDir(), "gitlab-polling.log"); } @@ -717,7 +758,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc gitlabHostUrl = formData.getString("gitlabHostUrl"); ignoreCertificateErrors = formData.getBoolean("ignoreCertificateErrors"); save(); - gitlab = new GitLab(); + gitlab = new GitLab(gitlabApiToken, gitlabHostUrl, ignoreCertificateErrors); return super.configure(req, formData); } @@ -921,6 +962,22 @@ public FormValidation doCheckGitlabApiToken(@QueryParameter String value) { return FormValidation.ok(); } + public FormValidation doCheckJobGitlabHostUrl(@QueryParameter String value) { + if (gitlabHostUrl != null && !gitlabHostUrl.isEmpty()) { + return FormValidation.ok(); + } + + return doCheckGitlabHostUrl(value); + } + + public FormValidation doCheckJobGitlabApiToken(@QueryParameter String value) { + if (gitlabApiToken != null && !gitlabApiToken.isEmpty()) { + return FormValidation.ok(); + } + + return doCheckGitlabApiToken(value); + } + public FormValidation doTestConnection(@QueryParameter("gitlabHostUrl") final String hostUrl, @QueryParameter("gitlabApiToken") final String token, @QueryParameter("ignoreCertificateErrors") final boolean ignoreCertificateErrors) throws IOException { try { @@ -933,7 +990,7 @@ public FormValidation doTestConnection(@QueryParameter("gitlabHostUrl") final St public GitLab getGitlab() { if (gitlab == null) { - gitlab = new GitLab(); + gitlab = new GitLab(gitlabApiToken, gitlabHostUrl, ignoreCertificateErrors); } return gitlab; } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java index e9d1d8a1d..60125a936 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabWebHook.java @@ -388,7 +388,7 @@ public void generatePushBuild(String json, Job project, StaplerRequest req, Stap protected void buildOpenMergeRequests(GitLabPushTrigger trigger, Integer projectId, String projectRef) { try { - GitLab api = new GitLab(); + GitLab api = trigger.getGitlab(); List mergeRequests = api.instance().getOpenMergeRequests(projectId); for (org.gitlab.api.models.GitlabMergeRequest mr : mergeRequests) { diff --git a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly index 27c8a2fb7..9534ab4cf 100644 --- a/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly +++ b/src/main/resources/com/dabsquared/gitlabjenkins/GitLabPushTrigger/config.jelly @@ -1,6 +1,18 @@ + + + + + + + + + + diff --git a/src/test/java/com/dabsquared/gitlabjenkins/AbstractGitLabPushTriggerGitlabServerTest.java b/src/test/java/com/dabsquared/gitlabjenkins/AbstractGitLabPushTriggerGitlabServerTest.java index 959359dc8..10497e695 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/AbstractGitLabPushTriggerGitlabServerTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/AbstractGitLabPushTriggerGitlabServerTest.java @@ -115,10 +115,13 @@ protected GitLabPushTrigger setUpWithPushTrigger() { String includeBranchesSpec = null; String excludeBranchesSpec = null; String targetBranchRegex = null; + String gitlabApiToken = null; + String gitlabHostUrl = ""; + boolean ignoreCertificateErrors = false; GitLabPushTrigger gitLabPushTrigger = new GitLabPushTrigger(triggerOnPush, triggerOnMergeRequest, triggerOpenMergeRequestOnPush, ciSkip, setBuildDescription, addNoteOnMergeRequest, addCiMessage, addVoteOnMergeRequest, acceptMergeRequestOnSuccess, branchFilter, includeBranchesSpec, - excludeBranchesSpec, targetBranchRegex); + excludeBranchesSpec, targetBranchRegex, gitlabApiToken, gitlabHostUrl, ignoreCertificateErrors); return gitLabPushTrigger; }