Skip to content

Commit b363d64

Browse files
committed
Merge pull request hudson2-plugins#19 from paulsowden/commit-notify
Add an action extension to remotely trigger a poll for a given git repo
2 parents f26bebb + 2951c05 commit b363d64

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package hudson.plugins.git;
2+
3+
import hudson.Extension;
4+
import hudson.model.AbstractModelObject;
5+
import hudson.model.AbstractProject;
6+
import hudson.model.Hudson;
7+
import hudson.model.RootAction;
8+
9+
import hudson.scm.SCM;
10+
import hudson.triggers.SCMTrigger;
11+
12+
import org.spearce.jgit.transport.RemoteConfig;
13+
import org.spearce.jgit.transport.URIish;
14+
15+
import org.kohsuke.stapler.StaplerRequest;
16+
import org.kohsuke.stapler.StaplerResponse;
17+
18+
import javax.servlet.ServletException;
19+
import static javax.servlet.http.HttpServletResponse.SC_OK;
20+
import java.io.IOException;
21+
22+
import java.util.logging.Logger;
23+
import static java.util.logging.Level.INFO;
24+
25+
/**
26+
* Information screen for the use of Git in Hudson.
27+
*/
28+
@Extension
29+
public class GitStatus extends AbstractModelObject implements RootAction {
30+
public String getDisplayName() {
31+
return "Git";
32+
}
33+
34+
public String getSearchUrl() {
35+
return getUrlName();
36+
}
37+
38+
public String getIconFileName() {
39+
// TODO
40+
return null;
41+
}
42+
43+
public String getUrlName() {
44+
return "git";
45+
}
46+
47+
public void doNotifyCommit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
48+
String urlString = req.getParameter("url");
49+
URIish url = null;
50+
try {
51+
url = new URIish(urlString);
52+
} catch (java.net.URISyntaxException e) { }
53+
54+
boolean scmFound = false,
55+
triggerFound = false,
56+
urlFound = false;
57+
for (AbstractProject<?,?> project : Hudson.getInstance().getItems(AbstractProject.class)) {
58+
SCM scm = project.getScm();
59+
if (scm instanceof GitSCM) scmFound = true; else continue;
60+
61+
SCMTrigger trigger = project.getTrigger(SCMTrigger.class);
62+
if (trigger!=null) triggerFound = true; else continue;
63+
64+
GitSCM git = (GitSCM) scm;
65+
for (RemoteConfig repository : git.getRepositories()) {
66+
boolean repositoryMatches = false;
67+
for (URIish remoteURL : repository.getURIs()) {
68+
if (url.equals(remoteURL)) { repositoryMatches = true; break; }
69+
}
70+
if (repositoryMatches) urlFound = true; else continue;
71+
72+
trigger.run();
73+
}
74+
}
75+
76+
if (url == null) LOGGER.warning("Couldn't read url: " + urlString);
77+
else if (!scmFound) LOGGER.warning("No git jobs found");
78+
else if (!triggerFound) LOGGER.warning("No git jobs using SCM polling");
79+
else if (!urlFound) LOGGER.warning("No git jobs using repository: " + url.toString());
80+
81+
rsp.setStatus(SC_OK);
82+
}
83+
84+
private static final Logger LOGGER = Logger.getLogger(GitStatus.class.getName());
85+
}
86+

0 commit comments

Comments
 (0)