Skip to content

Commit

Permalink
Improve webhook processor code coverage for cloud tag events (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfalco79 authored Feb 3, 2025
1 parent e4bcc66 commit bfeba3b
Show file tree
Hide file tree
Showing 11 changed files with 1,182 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import hudson.security.ACL;
import hudson.security.ACLContext;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.scm.api.SCMHeadEvent;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
Expand Down Expand Up @@ -110,4 +112,21 @@ protected void scmSourceReIndex(final String owner, final String repository) {
}
}
}

/**
* Implementations have to call this method when want propagate an
* {@link SCMHeadEvent} to the scm-api.
*
* @param event the to fire
* @param delaySeconds a delay in seconds to wait before propagate the
* event. If the given value is less than 0 than default will be
* used.
*/
protected void notifyEvent(SCMHeadEvent<?> event, int delaySeconds) {
if (delaySeconds == 0) {
SCMHeadEvent.fireNow(event);
} else {
SCMHeadEvent.fireLater(event, delaySeconds > 0 ? delaySeconds : BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
}
}

Check warning on line 131 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/HookProcessor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 126-131 are not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
import com.cloudbees.jenkins.plugins.bitbucket.server.events.NativeServerPullRequestEvent;
import hudson.RestrictedSince;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMHeadEvent;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -78,6 +76,6 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta
return;
}

SCMHeadEvent.fireLater(new ServerHeadEvent(serverUrl, eventType, pullRequestEvent, origin), BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
notifyEvent(new ServerHeadEvent(serverUrl, eventType, pullRequestEvent, origin), BitbucketSCMSource.getEventDelaySeconds());

Check warning on line 79 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPullRequestHookProcessor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 79 is not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@
import hudson.RestrictedSince;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMHeadEvent;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -106,7 +104,7 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta

for (final SCMEvent.Type type : events.keySet()) {
ServerPushEvent headEvent = new ServerPushEvent(serverUrl, type, events.get(type), origin, repository, mirrorId);
SCMHeadEvent.fireLater(headEvent, BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
notifyEvent(headEvent, BitbucketSCMSource.getEventDelaySeconds());

Check warning on line 107 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 107 is not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudWebhookPayload;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerWebhookPayload;
import hudson.RestrictedSince;
import java.util.concurrent.TimeUnit;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMHeadEvent;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -67,9 +65,4 @@ public void process(final HookEventType hookEvent, String payload, final Bitbuck
}
}
}

/* for test purpose */
protected void notifyEvent(SCMHeadEvent<?> event, int delaySeconds) {
SCMHeadEvent.fireLater(event, delaySeconds, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudWebhookPayload;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerWebhookPayload;
import hudson.RestrictedSince;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.scm.api.SCMEvent;
Expand Down Expand Up @@ -70,14 +69,10 @@ public void process(HookEventType hookEvent, String payload, BitbucketType insta
type = SCMEvent.Type.UPDATED;
}
}
SCMHeadEvent.fireLater(new PushEvent(type, push, origin), BitbucketSCMSource.getEventDelaySeconds(), TimeUnit.SECONDS);
notifyEvent(new PushEvent(type, push, origin), BitbucketSCMSource.getEventDelaySeconds());
}
}
}
}

/* for test purpose */
protected void notifyEvent(SCMHeadEvent<?> event, int delaySeconds) {
SCMHeadEvent.fireLater(event, delaySeconds, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class BitbucketServerRepository implements BitbucketRepository {
@JsonProperty("slug")
private String repositoryName;

// JSON mapping added in setter because the field can not be called "public"
private Boolean public_;
@JsonProperty("public")
private boolean isPublic;

private Boolean archived;

Expand Down Expand Up @@ -100,7 +100,7 @@ public void setProject(BitbucketProject project) {

@Override
public boolean isPrivate() {
return !public_;
return !isPublic;

Check warning on line 103 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/repository/BitbucketServerRepository.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 103 is only partially covered, one branch is missing
}

@Override
Expand All @@ -113,9 +113,8 @@ public void setArchived(Boolean archived) {
this.archived = archived;
}

@JsonProperty("public")
public void setPublic(Boolean public_) {
this.public_ = public_;
public void setPublic(Boolean isPublic) {
this.isPublic = isPublic;
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,38 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import jenkins.scm.api.SCMEvent.Type;
import jenkins.scm.api.SCMHeadEvent;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMSource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.ArgumentCaptor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

class PullRequestHookProcessorTest {

private PullRequestHookProcessor sut;
protected SCMHeadEvent<?> scmEvent;

@BeforeEach
void setup() {
sut = spy(new PullRequestHookProcessor());
sut = new PullRequestHookProcessor() {
@Override
protected void notifyEvent(SCMHeadEvent<?> event, int delaySeconds) {
PullRequestHookProcessorTest.this.scmEvent = event;
}
};
}

@Test
void test_pullrequest_created() throws Exception {
sut.process(HookEventType.PULL_REQUEST_CREATED, loadResource("pullrequest_created.json"), BitbucketType.CLOUD, "origin");

ArgumentCaptor<PREvent> eventCaptor = ArgumentCaptor.forClass(PREvent.class);
verify(sut).notifyEvent(eventCaptor.capture(), anyInt());
PREvent event = eventCaptor.getValue();
PREvent event = (PREvent) scmEvent;
assertThat(event).isNotNull();
assertThat(event.getSourceName()).isEqualTo("test-repos");
assertThat(event.getType()).isEqualTo(Type.CREATED);
Expand All @@ -73,9 +74,7 @@ void test_pullrequest_created() throws Exception {
void test_pullrequest_rejected() throws Exception {
sut.process(HookEventType.PULL_REQUEST_DECLINED, loadResource("pullrequest_rejected.json"), BitbucketType.CLOUD, "origin");

ArgumentCaptor<PREvent> eventCaptor = ArgumentCaptor.forClass(PREvent.class);
verify(sut).notifyEvent(eventCaptor.capture(), anyInt());
PREvent event = eventCaptor.getValue();
PREvent event = (PREvent) scmEvent;
assertThat(event).isNotNull();
assertThat(event.getSourceName()).isEqualTo("test-repos");
assertThat(event.getType()).isEqualTo(Type.REMOVED);
Expand All @@ -86,9 +85,7 @@ void test_pullrequest_rejected() throws Exception {
void test_pullrequest_created_when_event_match_SCMNavigator() throws Exception {
sut.process(HookEventType.PULL_REQUEST_CREATED, loadResource("pullrequest_created.json"), BitbucketType.CLOUD, "origin");

ArgumentCaptor<PREvent> eventCaptor = ArgumentCaptor.forClass(PREvent.class);
verify(sut).notifyEvent(eventCaptor.capture(), anyInt());
PREvent event = eventCaptor.getValue();
PREvent event = (PREvent) scmEvent;
// discard any scm navigator than bitbucket
assertThat(event.isMatch(mock(SCMNavigator.class))).isFalse();

Expand All @@ -112,9 +109,7 @@ void test_pullrequest_created_when_event_match_SCMNavigator() throws Exception {
void test_pullrequest_created_when_event_match_SCMSource(JenkinsRule r) throws Exception {
sut.process(HookEventType.PULL_REQUEST_CREATED, loadResource("pullrequest_created.json"), BitbucketType.CLOUD, "origin");

ArgumentCaptor<PREvent> eventCaptor = ArgumentCaptor.forClass(PREvent.class);
verify(sut).notifyEvent(eventCaptor.capture(), anyInt());
PREvent event = eventCaptor.getValue();
PREvent event = (PREvent) scmEvent;
// discard any scm navigator than bitbucket
assertThat(event.isMatch(mock(SCMSource.class))).isFalse();

Expand All @@ -140,11 +135,9 @@ void test_pullrequest_created_when_event_match_SCMSource(JenkinsRule r) throws E
void test_pullrequest_rejected_returns_empty_pullrequests_when_event_match_SCMSource(JenkinsRule r) throws Exception {
sut.process(HookEventType.PULL_REQUEST_DECLINED, loadResource("pullrequest_rejected.json"), BitbucketType.CLOUD, "origin");

ArgumentCaptor<PREvent> eventCaptor = ArgumentCaptor.forClass(PREvent.class);
verify(sut).notifyEvent(eventCaptor.capture(), anyInt());
PREvent event = eventCaptor.getValue();
PREvent event = (PREvent) scmEvent;

BitbucketSCMSource scmSource = new BitbucketSCMSource("amuniz", "test-repos");
BitbucketSCMSource scmSource = new BitbucketSCMSource("aMUNIZ", "test-repos");
scmSource.setTraits(List.of(new OriginPullRequestDiscoveryTrait(2)));
assertThat(event.isMatch(scmSource)).isTrue();
assertThat(event.getPullRequests(scmSource)).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* The MIT License
*
* Copyright (c) 2025, Nikolas Falco
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.hooks;

import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketTagSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead;
import hudson.scm.SCM;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
import jenkins.scm.api.SCMEvent.Type;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadEvent;
import jenkins.scm.api.SCMRevision;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class PushHookProcessorTest {
private PushHookProcessor sut;
private SCMHeadEvent<?> scmEvent;

@BeforeEach
void setup() {
sut = new PushHookProcessor() {
@Override
protected void notifyEvent(SCMHeadEvent<?> event, int delaySeconds) {
PushHookProcessorTest.this.scmEvent = event;
}
};
}

@Test
void test_tag_created() throws Exception {
sut.process(HookEventType.PUSH, loadResource("tag_created.json"), BitbucketType.CLOUD, "origin");

PushEvent event = (PushEvent) scmEvent;
assertThat(event).isNotNull();
assertThat(event.getSourceName()).isEqualTo("test-repos");
assertThat(event.getType()).isEqualTo(Type.CREATED);
assertThat(event.isMatch(mock(SCM.class))).isFalse();

BitbucketSCMSource scmSource = new BitbucketSCMSource("AMUNIZ", "test-repos");
Map<SCMHead, SCMRevision> heads = event.heads(scmSource);
assertThat(heads.keySet())
.first()
.usingRecursiveComparison()
.isEqualTo(new BitbucketTagSCMHead("simple-tag", 1738608795000l)); // verify is using last commit date
}

@Test
void test_annotated_tag_created() throws Exception {
sut.process(HookEventType.PUSH, loadResource("annotated_tag_created.json"), BitbucketType.CLOUD, "origin");

PushEvent event = (PushEvent) scmEvent;
assertThat(event).isNotNull();
assertThat(event.getSourceName()).isEqualTo("test-repos");
assertThat(event.getType()).isEqualTo(Type.CREATED);
assertThat(event.isMatch(mock(SCM.class))).isFalse();

BitbucketSCMSource scmSource = new BitbucketSCMSource("AMUNIz", "test-repos");
Map<SCMHead, SCMRevision> heads = event.heads(scmSource);
assertThat(heads.keySet())
.first()
.usingRecursiveComparison()
.isEqualTo(new BitbucketTagSCMHead("test-tag", 1738608816000l));
}

@Test
void test_commmit_created() throws Exception {
sut.process(HookEventType.PUSH, loadResource("commit_created.json"), BitbucketType.CLOUD, "origin");

PushEvent event = (PushEvent) scmEvent;
assertThat(event).isNotNull();
assertThat(event.getSourceName()).isEqualTo("test-repos");
assertThat(event.getType()).isEqualTo(Type.UPDATED);
assertThat(event.isMatch(mock(SCM.class))).isFalse();

BitbucketSCMSource scmSource = new BitbucketSCMSource("aMUNIZ", "test-repos");
Map<SCMHead, SCMRevision> heads = event.heads(scmSource);
assertThat(heads.keySet())
.first()
.usingRecursiveComparison()
.isEqualTo(new BranchSCMHead("feature/issue-819"));
assertThat(heads.values())
.first()
.usingRecursiveComparison()
.isEqualTo(new SCMRevisionImpl(new BranchSCMHead("feature/issue-819"), "5ecffa3874e96920f24a2b3c0d0038e47d5cd1a4"));
}

private String loadResource(String resource) throws IOException {
try (InputStream stream = this.getClass().getResourceAsStream("cloud/" + resource)) {
return IOUtils.toString(stream, StandardCharsets.UTF_8);
}
}

}
Loading

0 comments on commit bfeba3b

Please sign in to comment.