Skip to content

Commit

Permalink
Configure Gitlab URL at job level
Browse files Browse the repository at this point in the history
  • Loading branch information
WonderCsabo committed Feb 17, 2016
1 parent 75ebe51 commit 11daa94
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/dabsquared/gitlabjenkins/GitLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
89 changes: 73 additions & 16 deletions src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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()});
}
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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()});
}
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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");
Expand All @@ -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();
}
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GitlabMergeRequest> mergeRequests = api.instance().getOpenMergeRequests(projectId);

for (org.gitlab.api.models.GitlabMergeRequest mr : mergeRequests) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:entry title="Gitlab host URL" field="jobGitlabHostUrl"
description="The complete URL to the Gitlab server (i.e. http://gitlab">
<f:textbox/>
</f:entry>
<f:entry title="API Token" field="jobGitlabApiToken"
description="API Token for accessing Gitlab">
<f:textbox/>
</f:entry>
<f:entry title="${%Ignore SSL Certificate Errors}" field="jobIgnoreCertificateErrors">
<f:checkbox />
</f:entry>

<f:entry title="Build on Merge Request Events" field="triggerOnMergeRequest">
<f:checkbox default="true"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 11daa94

Please sign in to comment.