Skip to content

Commit

Permalink
Fix unneeded personal notifications when a PR is not actually edited (#…
Browse files Browse the repository at this point in the history
…425)

Co-authored-by: Yevhen Hrytsai <[email protected]>
  • Loading branch information
yevhenhr and Yevhen Hrytsai authored Oct 22, 2024
1 parent c7cea1f commit 96f27ef
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestRescopedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestReviewersUpdatedEvent;
import com.atlassian.bitbucket.event.pull.PullRequestUpdatedEvent;
import com.atlassian.bitbucket.event.repository.RepositoryForkedEvent;
import com.atlassian.bitbucket.event.repository.RepositoryRefsChangedEvent;
import com.atlassian.bitbucket.plugins.slack.notification.NotificationPublisher;
Expand All @@ -18,6 +19,7 @@
import com.atlassian.bitbucket.plugins.slack.notification.renderer.SlackNotificationRenderer;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestParticipant;
import com.atlassian.bitbucket.repository.Ref;
import com.atlassian.bitbucket.repository.RefChange;
import com.atlassian.bitbucket.repository.RefChangeType;
import com.atlassian.bitbucket.repository.Repository;
Expand All @@ -29,6 +31,7 @@
import com.github.seratch.jslack.api.methods.request.chat.ChatPostMessageRequest.ChatPostMessageRequestBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -83,7 +86,18 @@ public void onEvent(final PullRequestEvent event) {
PullRequestRescopedEvent rescopedEvent = (PullRequestRescopedEvent) event;
// no changes for target PR
if (!Optional.ofNullable(rescopedEvent.getAddedCommits()).filter(commits -> commits.getTotal() > 0).isPresent() &&
!Optional.ofNullable(rescopedEvent.getRemovedCommits()).filter(commits -> commits.getTotal() > 0).isPresent()) {
!Optional.ofNullable(rescopedEvent.getRemovedCommits()).filter(commits -> commits.getTotal() > 0).isPresent()) {
return;
}
}

if (event instanceof PullRequestUpdatedEvent) {
PullRequestUpdatedEvent updatedEvent = (PullRequestUpdatedEvent) event;
PullRequest pullRequest = updatedEvent.getPullRequest();
// no changes for source PR
if (StringUtils.equals(updatedEvent.getPreviousDescription(), pullRequest.getDescription()) &&
StringUtils.equals(updatedEvent.getPreviousTitle(), pullRequest.getTitle()) &&
equalsToBranch(updatedEvent)) {
return;
}
}
Expand Down Expand Up @@ -175,6 +189,14 @@ private boolean hasUserAddedOrRemovedHimself(final PullRequestReviewersUpdatedEv
return result;
}

private boolean equalsToBranch(final PullRequestUpdatedEvent updatedEvent) {
Ref previousToRef = updatedEvent.getPreviousToBranch();
if (previousToRef == null) {
return true;
}
return previousToRef.getId().equals(updatedEvent.getPreviousToBranch().getId());
}

@EventListener
public void onEvent(final RepositoryRefsChangedEvent event) {
RepositoryNotificationTypes.byEventClass(event.getClass()).ifPresent(notificationType ->
Expand All @@ -189,9 +211,9 @@ public void onEvent(final RepositoryRefsChangedEvent event) {
Collections::emptySet,
options -> correctedType == RepositoryNotificationTypes.FILE_EDITED
? ofNullable(slackNotificationRenderer.getFileEditedMessage(
(FileEditedEvent) event, refChange, options.getVerbosity()))
(FileEditedEvent) event, refChange, options.getVerbosity()))
: ofNullable(slackNotificationRenderer.getPushMessage(event, refChange,
options.getVerbosity())));
options.getVerbosity())));
}));
}

Expand Down

0 comments on commit 96f27ef

Please sign in to comment.