-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for the GitLab 8.1 commit API #127
Changes from all commits
9c6f635
edd0e7f
7b8a85c
51de287
5c57212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package com.dabsquared.gitlabjenkins; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang.builder.ToStringBuilder; | ||
import org.apache.commons.lang.builder.ToStringStyle; | ||
import org.gitlab.api.GitlabAPI; | ||
import org.gitlab.api.models.GitlabCommitStatus; | ||
import org.gitlab.api.models.GitlabProject; | ||
|
||
/** | ||
* Represents for WebHook payload | ||
|
@@ -23,6 +27,25 @@ public static GitLabPushRequest create(String payload) { | |
public GitLabPushRequest() { | ||
} | ||
|
||
private GitlabProject sourceProject = null; | ||
|
||
public GitlabProject getSourceProject (GitLab api) throws IOException { | ||
if (sourceProject == null) { | ||
sourceProject = api.instance().getProject(project_id); | ||
} | ||
return sourceProject; | ||
} | ||
|
||
public GitlabCommitStatus createCommitStatus(GitlabAPI api, String status, String targetUrl) { | ||
try { | ||
if(getLastCommit()!=null) { | ||
return api.createCommitStatus(sourceProject, checkout_sha, status, checkout_sha, "Jenkins", targetUrl, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as in |
||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private String before; | ||
private String after; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ | |
import jenkins.triggers.SCMTriggerItem; | ||
import jenkins.triggers.SCMTriggerItem.SCMTriggerItems; | ||
import net.sf.json.JSONObject; | ||
import javax.annotation.Nullable; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.eclipse.jgit.transport.RemoteConfig; | ||
|
@@ -82,21 +81,23 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> { | |
private boolean ciSkip = true; | ||
private boolean setBuildDescription = true; | ||
private boolean addNoteOnMergeRequest = true; | ||
private boolean addCiMessage = false; | ||
private boolean addVoteOnMergeRequest = true; | ||
private boolean allowAllBranches = false; | ||
private final String includeBranchesSpec; | ||
private final String excludeBranchesSpec; | ||
private boolean acceptMergeRequestOnSuccess = false; | ||
|
||
@DataBoundConstructor | ||
public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, String triggerOpenMergeRequestOnPush, boolean ciSkip, boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addVoteOnMergeRequest, boolean acceptMergeRequestOnSuccess, boolean allowAllBranches, | ||
public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, String triggerOpenMergeRequestOnPush, boolean ciSkip, boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addCiMessage, boolean addVoteOnMergeRequest, boolean acceptMergeRequestOnSuccess, boolean allowAllBranches, | ||
String includeBranchesSpec, String excludeBranchesSpec) { | ||
this.triggerOnPush = triggerOnPush; | ||
this.triggerOnMergeRequest = triggerOnMergeRequest; | ||
this.triggerOpenMergeRequestOnPush = triggerOpenMergeRequestOnPush; | ||
this.ciSkip = ciSkip; | ||
this.setBuildDescription = setBuildDescription; | ||
this.addNoteOnMergeRequest = addNoteOnMergeRequest; | ||
this.addNoteOnMergeRequest = addNoteOnMergeRequest; | ||
this.addCiMessage = addCiMessage; | ||
this.addVoteOnMergeRequest = addVoteOnMergeRequest; | ||
this.allowAllBranches = allowAllBranches; | ||
this.includeBranchesSpec = includeBranchesSpec; | ||
|
@@ -132,6 +133,10 @@ public boolean getAllowAllBranches() { | |
return allowAllBranches; | ||
} | ||
|
||
public boolean getAddCiMessage() { | ||
return addCiMessage; | ||
} | ||
|
||
public boolean getCiSkip() { | ||
return ciSkip; | ||
} | ||
|
@@ -200,20 +205,18 @@ public void run() { | |
} else { | ||
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.", job.getName()); | ||
} | ||
|
||
if(addCiMessage) { | ||
req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); | ||
} | ||
} | ||
|
||
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) { | ||
GitLabPushCause cause; | ||
String triggeredByUser; | ||
if (req.getCommits().size() > 0){ | ||
triggeredByUser = req.getCommits().get(0).getAuthor().getName(); | ||
} else { | ||
triggeredByUser = req.getUser_name(); | ||
} | ||
try { | ||
cause = new GitLabPushCause(triggeredByUser, getLogFile()); | ||
cause = new GitLabPushCause(req, getLogFile()); | ||
} catch (IOException ex) { | ||
cause = new GitLabPushCause(triggeredByUser); | ||
cause = new GitLabPushCause(req); | ||
} | ||
return cause; | ||
} | ||
|
@@ -230,15 +233,25 @@ private Action[] createActions(GitLabPushRequest req) { | |
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch)); | ||
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch)); | ||
|
||
if (job instanceof AbstractProject<?,?>){ | ||
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0} using project {1} (push)", new String[]{job.getName(), ((AbstractProject<?, ?>) job).getRootProject().getName()}); | ||
}else{ | ||
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0} (push)", new String[]{job.getName()}); | ||
} | ||
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", getDesc().getSourceRepoNameDefault(job))); | ||
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", getDesc().getSourceRepoURLDefault(job).toString())); | ||
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "PUSH")); | ||
|
||
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getName()); | ||
String sourceRepoName = getDesc().getSourceRepoNameDefault(job); | ||
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString(); | ||
|
||
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(); | ||
} catch (IOException ex) { | ||
LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()}); | ||
} | ||
} | ||
|
||
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName)); | ||
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL)); | ||
|
||
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values()); | ||
|
||
ParametersAction parametersAction = new ParametersAction(listValues); | ||
|
@@ -296,6 +309,10 @@ protected Job asJob() { | |
} else { | ||
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getName()); | ||
} | ||
|
||
if(addCiMessage) { | ||
req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl()); | ||
} | ||
} | ||
|
||
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) { | ||
|
@@ -379,14 +396,25 @@ private void setBuildCauseInJob(Run run){ | |
} | ||
} | ||
|
||
public void onCompleted(Run build){ | ||
Cause mCause= build.getCause(GitLabMergeCause.class); | ||
public void onCompleted(Run run){ | ||
Cause mCause= run.getCause(GitLabMergeCause.class); | ||
if (mCause != null && mCause instanceof GitLabMergeCause) { | ||
onCompleteMergeRequest(build,(GitLabMergeCause) mCause); | ||
onCompleteMergeRequest(run, (GitLabMergeCause) mCause); | ||
} | ||
|
||
Cause pCause= run.getCause(GitLabPushCause.class); | ||
if (pCause != null && pCause instanceof GitLabPushCause) { | ||
onCompletedPushRequest(run, (GitLabPushCause) pCause); | ||
} | ||
|
||
} | ||
|
||
private void onCompletedPushRequest(Run run, GitLabPushCause cause) { | ||
if(addCiMessage) { | ||
cause.getPushRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), run.getResult()==Result.SUCCESS?"success":"failure", Jenkins.getInstance().getRootUrl() + run.getUrl()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the commit status api documentation the status must be one of the following values:
|
||
} | ||
} | ||
|
||
private void onCompleteMergeRequest(Run run,GitLabMergeCause cause){ | ||
if (acceptMergeRequestOnSuccess && run.getResult() == Result.SUCCESS) { | ||
try { | ||
|
@@ -423,10 +451,35 @@ private void onCompleteMergeRequest(Run run,GitLabMergeCause cause){ | |
} | ||
} | ||
|
||
if(addCiMessage) { | ||
cause.getMergeRequest().createCommitStatus(this.getDescriptor().getGitlab().instance(), run.getResult()==Result.SUCCESS?"success":"failure", Jenkins.getInstance().getRootUrl() + run.getUrl()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll make a pull request to fix this one ;) --EDIT-- |
||
} | ||
} | ||
|
||
public void onStarted(Run run) { | ||
setBuildCauseInJob(run); | ||
|
||
Cause mCause= run.getCause(GitLabMergeCause.class); | ||
if (mCause != null && mCause instanceof GitLabMergeCause) { | ||
onStartedMergeRequest(run, (GitLabMergeCause) mCause); | ||
} | ||
|
||
Cause pCause= run.getCause(GitLabPushCause.class); | ||
if (pCause != null && pCause instanceof GitLabPushCause) { | ||
onStartedPushRequest(run, (GitLabPushCause) pCause); | ||
} | ||
} | ||
|
||
private void onStartedPushRequest(Run run, GitLabPushCause cause) { | ||
if(addCiMessage) { | ||
cause.getPushRequest().createCommitStatus(this.getDescriptor().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()); | ||
} | ||
} | ||
|
||
private String getSourceBranch(GitLabRequest req) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
Enable GitLab 8.1 Continuous Integration feature. <b>DO NOT ENABLE IF YOU'RE USING A VERSION BEFORE GITLAB 8.1</b>. | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the commit status api documentation for the optional
ref
it saysBut here the commit id is assigned to it again.
Shouldn't it be
objectAttributes.getSourceBranch()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thommy101 can you comment on the above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EmteZogaf can you submit a PR for this so it doesn't get lost as just a comment on the source? Thanks!
EDIT: nevermind, you DID open one. Thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EmteZogaf @omehegan , I'm using the commit id as it's the closest I could find to match the default behavior of GitLab CI itself.
I haven't used the official CI myself (only using Java at the moment). I've looked at the blog post (https://about.gitlab.com/2015/09/22/gitlab-8-0-released/) and they are using the commit id.