allowed branches filter only for Push events

Refactor status.png functionality to support Jenkins instances using
authentication
This commit is contained in:
Dimitris Stafylarakis 2014-09-03 15:58:42 +02:00
parent 48d67cdf99
commit a92c1bf251
3 changed files with 33 additions and 27 deletions

View File

@ -128,7 +128,7 @@ public class GitLabPushTrigger extends Trigger<AbstractProject<?, ?>> {
} }
public void onPost(final GitLabMergeRequest req) { public void onPost(final GitLabMergeRequest req) {
if (triggerOnMergeRequest && (allowedBranches.contains("*") || allowedBranches.contains(getSourceBranch(req)))) { if (triggerOnMergeRequest) {
getDescriptor().queue.execute(new Runnable() { getDescriptor().queue.execute(new Runnable() {
public void run() { public void run() {
LOGGER.log(Level.INFO, "{0} triggered.", job.getName()); LOGGER.log(Level.INFO, "{0} triggered.", job.getName());
@ -319,6 +319,9 @@ public class GitLabPushTrigger extends Trigger<AbstractProject<?, ?>> {
protected URIish getSourceRepoURLDefault() { protected URIish getSourceRepoURLDefault() {
URIish url = null; URIish url = null;
SCM scm = project.getScm(); SCM scm = project.getScm();
if(!(scm instanceof GitSCM)) {
throw new IllegalArgumentException("This repo does not use git.");
}
if (scm instanceof GitSCM) { if (scm instanceof GitSCM) {
List<RemoteConfig> repositories = ((GitSCM) scm).getRepositories(); List<RemoteConfig> repositories = ((GitSCM) scm).getRepositories();
if (!repositories.isEmpty()){ if (!repositories.isEmpty()){

View File

@ -7,6 +7,7 @@ import hudson.model.ItemGroup;
import hudson.model.UnprotectedRootAction; import hudson.model.UnprotectedRootAction;
import hudson.model.AbstractBuild; import hudson.model.AbstractBuild;
import hudson.model.AbstractProject; import hudson.model.AbstractProject;
import hudson.model.BuildAuthorizationToken;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.util.BuildData; import hudson.plugins.git.util.BuildData;
import hudson.scm.SCM; import hudson.scm.SCM;
@ -16,7 +17,9 @@ import hudson.util.HttpResponses;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -90,7 +93,7 @@ public class GitLabWebHook implements UnprotectedRootAction {
while (restOfPathParts.hasNext()) { while (restOfPathParts.hasNext()) {
paths.add(restOfPathParts.next()); paths.add(restOfPathParts.next());
} }
String token = req.getParameter("token"); String token = req.getParameter("token");
//TODO: Check token authentication with project id. For now we are not using this. //TODO: Check token authentication with project id. For now we are not using this.
@ -203,7 +206,7 @@ public class GitLabWebHook implements UnprotectedRootAction {
} }
private void generateStatusPNG(String branch, String commitSHA1, AbstractProject project, StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException { private void generateStatusPNG(String branch, String commitSHA1, AbstractProject project, final StaplerRequest req, final StaplerResponse rsp) throws ServletException, IOException {
SCM scm = project.getScm(); SCM scm = project.getScm();
if(!(scm instanceof GitSCM)) { if(!(scm instanceof GitSCM)) {
throw new IllegalArgumentException("This repo does not use git."); throw new IllegalArgumentException("This repo does not use git.");
@ -222,33 +225,33 @@ public class GitLabWebHook implements UnprotectedRootAction {
if (baseUrl.endsWith("/")) { if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1); baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
} }
String imageUrl = "/plugin/gitlab-plugin/images/unknown.png";
if(null != mainBuild) {
BallColor currentBallColor = mainBuild.getIconColor().noAnime();
if(mainBuild == null) { if(mainBuild.isBuilding()) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png"); imageUrl = "/plugin/gitlab-plugin/images/running.png";
return; }else if(currentBallColor == BallColor.BLUE) {
imageUrl = "/plugin/gitlab-plugin/images/success.png";
}else if(currentBallColor == BallColor.RED) {
imageUrl = "/plugin/gitlab-plugin/images/failed.png";
}else {
imageUrl = "/plugin/gitlab-plugin/images/unknown.png";
}
} }
final URL url = new URL(baseUrl + imageUrl);
ACL.impersonate(ACL.SYSTEM, new Runnable() {
BallColor currentBallColor = mainBuild.getIconColor().noAnime(); public void run() {
try {
rsp.sendRedirect2(url.toString());
} catch (IOException e) {
throw HttpResponses.error(500,"Could not generate response.");
}
}
if(mainBuild.isBuilding()) { });
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/running.png");
}else if(currentBallColor == BallColor.BLUE) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/success.png");
}else if(currentBallColor == BallColor.ABORTED) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
}else if(currentBallColor == BallColor.DISABLED) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
}else if(currentBallColor == BallColor.GREY) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
}else if(currentBallColor == BallColor.NOTBUILT) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
}else if(currentBallColor == BallColor.RED) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/failed.png");
}else if(currentBallColor == BallColor.YELLOW) {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
} else {
rsp.sendRedirect2(baseUrl + "/plugin/gitlab-plugin/images/unknown.png");
}
} }

View File

@ -1,3 +1,3 @@
<div> <div>
Source branches allowed to trigger a build (either as Push or Merge Request). Multiple selections possible with Ctrl-click. Source branches allowed to trigger a build from a <b>Push event</b>. Multiple selections possible with Ctrl-click.
</div> </div>