Skip to content

Commit 2f848c5

Browse files
authored
Add event delay to BitbucketSCMSource (Default: 5s)
Co-authored-by: Gleb Samsonov <[email protected]>
1 parent 6cabb4f commit 2f848c5

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ public class BitbucketSCMSource extends SCMSource {
133133
private static final String CLOUD_REPO_TEMPLATE = "{/owner,repo}";
134134
private static final String SERVER_REPO_TEMPLATE = "/projects{/owner}/repos{/repo}";
135135

136+
/** How long to delay events received from Bitbucket in order to allow the API caches to sync. */
137+
private static /*mostly final*/ int eventDelaySeconds =
138+
Math.min(
139+
300,
140+
Math.max(
141+
0, Integer.getInteger(BitbucketSCMSource.class.getName() + ".eventDelaySeconds", 5)));
142+
136143
/**
137144
* Bitbucket URL.
138145
*/
@@ -1113,6 +1120,27 @@ public SCMHeadOrigin originOf(@NonNull String repoOwner, @NonNull String reposit
11131120
return new SCMHeadOrigin.Fork(repoOwner + "/" + repository);
11141121
}
11151122

1123+
1124+
/**
1125+
* Returns how long to delay events received from Bitbucket in order to allow the API caches to sync.
1126+
*
1127+
* @return how long to delay events received from Bitbucket in order to allow the API caches to sync.
1128+
*/
1129+
public static int getEventDelaySeconds() {
1130+
return eventDelaySeconds;
1131+
}
1132+
1133+
/**
1134+
* Sets how long to delay events received from Bitbucket in order to allow the API caches to sync.
1135+
*
1136+
* @param eventDelaySeconds number of seconds to delay, will be restricted into a value within the
1137+
* range {@code [0,300]} inclusive
1138+
*/
1139+
@Restricted(NoExternalUse.class) // to allow configuration from system groovy console
1140+
public static void setEventDelaySeconds(int eventDelaySeconds) {
1141+
BitbucketSCMSource.eventDelaySeconds = Math.min(300, Math.max(0, eventDelaySeconds));
1142+
}
1143+
11161144
@Symbol("bitbucket")
11171145
@Extension
11181146
public static class DescriptorImpl extends SCMSourceDescriptor {

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPullRequestHookProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.HashMap;
4040
import java.util.Map;
4141
import java.util.Set;
42+
import java.util.concurrent.TimeUnit;
4243
import java.util.logging.Level;
4344
import java.util.logging.Logger;
4445
import jenkins.plugins.git.AbstractGitSCMSource;
@@ -89,7 +90,7 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta
8990
return;
9091
}
9192

92-
SCMHeadEvent.fireNow(new HeadEvent(serverUrl, eventType, pullRequestEvent, origin));
93+
SCMHeadEvent.fireLater(new HeadEvent(serverUrl, eventType, pullRequestEvent, origin), BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
9394
}
9495

9596
private static final class HeadEvent extends NativeServerHeadEvent<NativeServerPullRequestEvent> implements HasPullRequests {

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Map;
5050
import java.util.Objects;
5151
import java.util.Set;
52+
import java.util.concurrent.TimeUnit;
5253
import java.util.logging.Level;
5354
import java.util.logging.Logger;
5455
import jenkins.plugins.git.AbstractGitSCMSource;
@@ -109,7 +110,7 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta
109110
}
110111

111112
for (final SCMEvent.Type type : events.keySet()) {
112-
SCMHeadEvent.fireNow(new HeadEvent(serverUrl, type, events.get(type), origin, refsChangedEvent));
113+
SCMHeadEvent.fireLater(new HeadEvent(serverUrl, type, events.get(type), origin, refsChangedEvent), BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
113114
}
114115
}
115116

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/PullRequestHookProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Locale;
4747
import java.util.Map;
4848
import java.util.Set;
49+
import java.util.concurrent.TimeUnit;
4950
import java.util.logging.Logger;
5051
import jenkins.plugins.git.AbstractGitSCMSource;
5152
import jenkins.scm.api.SCMEvent;
@@ -90,7 +91,7 @@ public void process(final HookEventType hookEvent, String payload, final Bitbuck
9091
break;
9192
}
9293
// assume updated as a catch-all type
93-
SCMHeadEvent.fireNow(new HeadEvent(eventType, pull, origin, hookEvent, instanceType));
94+
SCMHeadEvent.fireLater(new HeadEvent(eventType, pull, origin, hookEvent, instanceType), BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
9495
}
9596
}
9697
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/PushHookProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.HashMap;
4646
import java.util.List;
4747
import java.util.Map;
48+
import java.util.concurrent.TimeUnit;
4849
import java.util.logging.Level;
4950
import java.util.logging.Logger;
5051
import jenkins.plugins.git.AbstractGitSCMSource;
@@ -86,7 +87,7 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta
8687
type = SCMEvent.Type.UPDATED;
8788
}
8889
}
89-
SCMHeadEvent.fireNow(new SCMHeadEvent<BitbucketPushEvent>(type, push, origin) {
90+
SCMHeadEvent.fireLater(new SCMHeadEvent<BitbucketPushEvent>(type, push, origin) {
9091
@Override
9192
public boolean isMatch(@NonNull SCMNavigator navigator) {
9293
if (!(navigator instanceof BitbucketSCMNavigator)) {
@@ -187,7 +188,7 @@ public boolean isMatch(@NonNull SCM scm) {
187188
// TODO
188189
return false;
189190
}
190-
});
191+
}, BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
191192
}
192193
}
193194
}

0 commit comments

Comments
 (0)