Skip to content
Permalink
Browse files

[JENKINS-25127] Add Button for registering all hooks

  • Loading branch information
KostyaSha committed Mar 27, 2015
1 parent c98e36f commit 6154cedfe18f265aea25756284a3472696eceaff
@@ -33,6 +33,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Base64;
@@ -145,6 +146,11 @@ public void start(AbstractProject<?,?> project, boolean newInstance) {
}
}

/**
* Tries to register hook for current associated job.
* Useful for using from groovy scripts.
* @since 1.11.2
*/
public void registerHooks() {
// make sure we have hooks installed. do this lazily to avoid blocking the UI thread.
final Collection<GitHubRepositoryName> names = GitHubRepositoryNameContributor.parseAssociatedNames(job);
@@ -232,6 +238,7 @@ public void writeLogTo(XMLOutput out) throws IOException {

@Extension
public static class DescriptorImpl extends TriggerDescriptor {
private static final Logger LOGGER = Logger.getLogger(DescriptorImpl.class.getName());
private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue(MasterComputer.threadPoolForRemoting);

private boolean manageHook;
@@ -324,6 +331,37 @@ public FormValidation doCheckHookUrl(@QueryParameter String value) {

}

public FormValidation doReRegister() {
if (!manageHook) {
return FormValidation.error("Works only when Jenkins manages hooks");
}

int triggered = 0;
for (AbstractProject<?,?> job : getJenkinsInstance().getAllItems(AbstractProject.class)) {
if (!job.isBuildable()) {
continue;
}

GitHubPushTrigger trigger = job.getTrigger(GitHubPushTrigger.class);
if (trigger!=null) {
LOGGER.log(Level.FINE, "Calling registerHooks() for {0}", job.getFullName());
trigger.registerHooks();
triggered++;
}
}

LOGGER.log(Level.INFO, "Called registerHooks() for {0} jobs", triggered);
return FormValidation.ok("Called re-register hooks for " + triggered + " jobs");
}

public static final Jenkins getJenkinsInstance() throws IllegalStateException {
Jenkins instance = Jenkins.getInstance();
if (instance == null) {
throw new IllegalStateException("Jenkins has not been started, or was already shut down");
}
return instance;
}

public static DescriptorImpl get() {
return Trigger.all().get(DescriptorImpl.class);
}
@@ -31,5 +31,6 @@
</f:radioBlock>
<f:radioBlock name="hookMode" value="none" title="${%Manually manage hook URLs}" checked="${!instance.isManageHook()}"
help="${descriptor.getHelpFile('manual')}"/>
<f:validateButton title="${%Re-register hooks for all jobs}" progress="${%Scanning all items...}" method="reRegister"/>
</f:section>
</j:jelly>

0 comments on commit 6154ced

Please sign in to comment.
You can’t perform that action at this time.