Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
backported changes from master to ensure ListenerInitializer has syst…
Browse files Browse the repository at this point in the history
…em permissions and runs after jobs are loaded
  • Loading branch information
Karsten Kraus authored and Karsten Kraus committed Aug 24, 2017
1 parent 9ebdd9f commit 7450cc4
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMNavigator;
import argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMSource;
import hudson.Extension;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Item;
import hudson.model.RootAction;
import hudson.model.UnprotectedRootAction;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMNavigatorOwner;
Expand Down Expand Up @@ -44,14 +47,22 @@ public static GitLabSCMWebHook get() {
return Jenkins.getInstance().getExtensionList(RootAction.class).get(GitLabSCMWebHook.class);
}

@Initializer(after = InitMilestone.JOB_LOADED)
public static void initialize() {
new Thread(new Runnable() {
@Override
public void run() {
ACL.impersonate(ACL.SYSTEM, new ListenerInitializerTask(GitLabSCMWebHook.get()));
}
}).start();
}

private final HookManager manager;
private final HookHandler handler;

public GitLabSCMWebHook() {
manager = new HookManager();
handler = new HookHandler();
new Thread(new ListenerInitializerTask()).start();
}

public void addListener(GitLabSCMNavigator navigator, Item owner) {
Expand Down Expand Up @@ -127,7 +138,13 @@ private String getListenerId(String id) {

}

private class ListenerInitializerTask implements Runnable {
private static class ListenerInitializerTask implements Runnable {
private final GitLabSCMWebHook hook;

private ListenerInitializerTask(GitLabSCMWebHook hook) {
this.hook = hook;
}

@Override
public void run() {
for (SCMNavigatorOwner owner : Jenkins.getInstance().getAllItems(SCMNavigatorOwner.class)) {
Expand All @@ -149,13 +166,13 @@ public void run() {

private void handle(GitLabSCMSource source, SCMSourceOwner owner) {
if (source.getListenToWebHooks()) {
addListener(source, owner);
hook.addListener(source, owner);
}
}

private void handle(GitLabSCMNavigator navigator, SCMNavigatorOwner owner) {
if (navigator.getListenToWebHooks() && !StringUtils.isEmpty(navigator.getConnectionName())) {
addListener(navigator, owner);
hook.addListener(navigator, owner);
}
}
}
Expand Down

0 comments on commit 7450cc4

Please sign in to comment.