Skip to content

Commit

Permalink
option to poll gitlab immediately on a web hook trigger (#193)
Browse files Browse the repository at this point in the history
Co-authored-by: Basil Crow <[email protected]>
  • Loading branch information
dcendents and basil authored Jun 21, 2022
1 parent fc416b0 commit e99e3d4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins.gitlabbranchsource;

import edu.umd.cs.findbugs.annotations.Nullable;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -39,30 +40,45 @@ public void onNoteEvent(NoteEvent noteEvent) {
@Override
public void onMergeRequestEvent(MergeRequestEvent mrEvent) {
LOGGER.log(Level.FINE, mrEvent.toString());
final long triggerDelay = findTriggerDelay(mrEvent.getProject().getWebUrl());
GitLabMergeRequestSCMEvent trigger = new GitLabMergeRequestSCMEvent(mrEvent, origin);
SCMHeadEvent.fireLater(trigger, triggerDelay, TimeUnit.SECONDS);
fireTrigger(trigger, mrEvent.getProject().getWebUrl());
}

@Override
public void onPushEvent(PushEvent pushEvent) {
LOGGER.log(Level.FINE, pushEvent.toString());
final long triggerDelay = findTriggerDelay(pushEvent.getProject().getWebUrl());
GitLabPushSCMEvent trigger = new GitLabPushSCMEvent(pushEvent, origin);
SCMHeadEvent.fireLater(trigger, triggerDelay, TimeUnit.SECONDS);
fireTrigger(trigger, pushEvent.getProject().getWebUrl());
}

@Override
public void onTagPushEvent(TagPushEvent tagPushEvent) {
LOGGER.log(Level.FINE, tagPushEvent.toString());
final long triggerDelay = findTriggerDelay(tagPushEvent.getProject().getWebUrl());
GitLabTagPushSCMEvent trigger = new GitLabTagPushSCMEvent(tagPushEvent, origin);
SCMHeadEvent.fireLater(trigger, triggerDelay, TimeUnit.SECONDS);
fireTrigger(trigger, tagPushEvent.getProject().getWebUrl());
}

private long findTriggerDelay(final String projectUrl) {
private void fireTrigger(final SCMHeadEvent<?> trigger, final String projectUrl) {
final GitLabServer projectServer = findProjectServer(projectUrl);
if (findImmediateHookTrigger(projectServer)) {
SCMHeadEvent.fireNow(trigger);
}
final long triggerDelay = findTriggerDelay(projectServer);
SCMHeadEvent.fireLater(trigger, triggerDelay, TimeUnit.SECONDS);
}

private boolean findImmediateHookTrigger(@Nullable final GitLabServer projectServer) {
if (projectServer == null) {
LOGGER.log(
Level.WARNING,
"Falling back to no immediate trigger");
return false;
}

return projectServer.isImmediateHookTrigger();
}

private long findTriggerDelay(@Nullable final GitLabServer projectServer) {
if (projectServer == null) {
LOGGER.log(
Level.WARNING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public class GitLabServer extends AbstractDescribableImpl<GitLabServer> {
*/
private Secret secretToken;

/**
* {@code true} if and only if Jenkins should trigger a build immediately on a
* GitLab Web Hook trigger.
*/
private boolean immediateHookTrigger;

/**
* Delay to be used for GitLab Web Hook build triggers.
*/
Expand Down Expand Up @@ -312,6 +318,28 @@ public String getSecretTokenAsPlainText() {
return this.secretToken.getPlainText();
}

/**
* Returns {@code true} if Jenkins should trigger a build immediately on a
* GitLab Web Hook trigger.
*
* @return {@code true} if Jenkins should trigger a build immediately on a
* GitLab Web Hook trigger.
*/
public boolean isImmediateHookTrigger() {
return immediateHookTrigger;
}

/**
* Data Bound Setter for immediate build on a GitLab Web Hook trigger.
*
* @param immediateHookTrigger {@code true} if and only if Jenkins should trigger a build immediately on a
* GitLab Web Hook trigger.
*/
@DataBoundSetter
public void setImmediateHookTrigger(boolean immediateHookTrigger) {
this.immediateHookTrigger = immediateHookTrigger;
}

/**
* Data Bound Setter for web hook trigger delay
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ f.entry(title: _("Root URL for hooks"), field: "hooksRootUrl", "description": "J
}

f.advanced() {
f.entry(title: _("Immediate Web Hook trigger"), field: "immediateHookTrigger", "description": "Trigger a build immediately on a GitLab Web Hook trigger") {
f.checkbox(title: _("Immediate Web Hook trigger"))
}
f.entry(title: _("Web Hook trigger delay"), field: "hookTriggerDelay", "description": "Delay in seconds to be used for GitLab Web Hook build triggers (defaults to GitLab cache timeout)") {
f.textbox()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
Select this option if you want to immediately query gitlab for modifications when a
web hook trigger is received.
Using this option lets you start a build a soon as possible and rely on the "Web Hook trigger delay"
value as a fallback in case gitlab's cache did not return the modifications.
</div>

0 comments on commit e99e3d4

Please sign in to comment.