This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding event types RESCOPED_FROM and RESCOPED_TO
* RESCOPED_FROM, when only source branch is changed * RESCOPED_TO, when only target branch is changed
- Loading branch information
1 parent
2537dec
commit fd4e411
Showing
10 changed files
with
206 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package se.bjurr.prnfs.listener; | ||
|
||
import static com.atlassian.stash.pull.PullRequestAction.APPROVED; | ||
import static com.atlassian.stash.pull.PullRequestAction.COMMENTED; | ||
import static com.atlassian.stash.pull.PullRequestAction.DECLINED; | ||
import static com.atlassian.stash.pull.PullRequestAction.MERGED; | ||
import static com.atlassian.stash.pull.PullRequestAction.OPENED; | ||
import static com.atlassian.stash.pull.PullRequestAction.REOPENED; | ||
import static com.atlassian.stash.pull.PullRequestAction.RESCOPED; | ||
import static com.atlassian.stash.pull.PullRequestAction.UNAPPROVED; | ||
import static com.atlassian.stash.pull.PullRequestAction.UPDATED; | ||
import static com.google.common.collect.Lists.newArrayList; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.atlassian.stash.event.pull.PullRequestEvent; | ||
import com.atlassian.stash.event.pull.PullRequestRescopedEvent; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
public class PrnfsPullRequestAction { | ||
public static final String RESCOPED_TO = "RESCOPED_TO"; | ||
|
||
public static final String RESCOPED_FROM = "RESCOPED_FROM"; | ||
|
||
private static final Map<String, PrnfsPullRequestAction> values = new ImmutableMap.Builder<String, PrnfsPullRequestAction>() | ||
.put(APPROVED.name(), new PrnfsPullRequestAction(APPROVED.name())) // | ||
.put(COMMENTED.name(), new PrnfsPullRequestAction(COMMENTED.name())) // | ||
.put(DECLINED.name(), new PrnfsPullRequestAction(DECLINED.name())) // | ||
.put(MERGED.name(), new PrnfsPullRequestAction(MERGED.name())) // | ||
.put(OPENED.name(), new PrnfsPullRequestAction(OPENED.name())) // | ||
.put(REOPENED.name(), new PrnfsPullRequestAction(REOPENED.name())) // | ||
.put(RESCOPED.name(), new PrnfsPullRequestAction(RESCOPED.name())) // | ||
.put(RESCOPED_FROM, new PrnfsPullRequestAction(RESCOPED_FROM)) // | ||
.put(RESCOPED_TO, new PrnfsPullRequestAction(RESCOPED_TO)) // | ||
.put(UNAPPROVED.name(), new PrnfsPullRequestAction(UNAPPROVED.name())) // | ||
.put(UPDATED.name(), new PrnfsPullRequestAction(UPDATED.name())) // | ||
.build(); | ||
|
||
private final String name; | ||
|
||
private PrnfsPullRequestAction() { | ||
name = null; | ||
} | ||
|
||
private PrnfsPullRequestAction(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public static PrnfsPullRequestAction valueOf(String string) { | ||
if (values.containsKey(string)) { | ||
return values.get(string); | ||
} | ||
throw new RuntimeException("\"" + string + "\" not found!"); | ||
} | ||
|
||
public static List<PrnfsPullRequestAction> values() { | ||
return newArrayList(values.values()); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event) { | ||
if (event instanceof PullRequestRescopedEvent) { | ||
PullRequestRescopedEvent rescopedEvent = (PullRequestRescopedEvent) event; | ||
boolean toChanged = !rescopedEvent.getPreviousToHash().equals( | ||
rescopedEvent.getPullRequest().getToRef().getLatestChangeset()); | ||
boolean fromChanged = !rescopedEvent.getPreviousFromHash().equals( | ||
rescopedEvent.getPullRequest().getFromRef().getLatestChangeset()); | ||
if (fromChanged && !toChanged) { | ||
return PrnfsPullRequestAction.valueOf(RESCOPED_FROM); | ||
} else if (toChanged && !fromChanged) { | ||
return PrnfsPullRequestAction.valueOf(RESCOPED_TO); | ||
} | ||
} | ||
return PrnfsPullRequestAction.valueOf(event.getAction().name()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/se/bjurr/prnfs/listener/PrnfsPullRequestActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package se.bjurr.prnfs.listener; | ||
|
||
import static com.atlassian.stash.pull.PullRequestAction.OPENED; | ||
import static com.atlassian.stash.pull.PullRequestAction.RESCOPED; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_FROM; | ||
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_TO; | ||
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.fromPullRequestEvent; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.atlassian.stash.event.pull.PullRequestEvent; | ||
import com.atlassian.stash.event.pull.PullRequestRescopedEvent; | ||
import com.atlassian.stash.pull.PullRequest; | ||
import com.atlassian.stash.pull.PullRequestAction; | ||
import com.atlassian.stash.pull.PullRequestRef; | ||
|
||
public class PrnfsPullRequestActionTest { | ||
private PullRequestRescopedEvent event; | ||
private PullRequest pullRequest; | ||
private PullRequestRef fromRef; | ||
private PullRequestRef toRef; | ||
|
||
@Before | ||
public void before() { | ||
event = mock(PullRequestRescopedEvent.class); | ||
pullRequest = mock(PullRequest.class); | ||
fromRef = mock(PullRequestRef.class); | ||
toRef = mock(PullRequestRef.class); | ||
when(event.getAction()).thenReturn(RESCOPED); | ||
when(event.getPullRequest()).thenReturn(pullRequest); | ||
when(event.getPullRequest().getFromRef()).thenReturn(fromRef); | ||
when(event.getPullRequest().getToRef()).thenReturn(toRef); | ||
} | ||
|
||
@Test | ||
public void testThatAllPullRequestActionEventsAreMapped() { | ||
for (PullRequestAction a : PullRequestAction.values()) { | ||
assertNotNull(a.name(), PrnfsPullRequestAction.valueOf(a.name())); | ||
assertEquals(a.name(), a.name(), PrnfsPullRequestAction.valueOf(a.name()).getName()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
@Test | ||
public void testThatRescopedEventsAreCalculatedCorrectlyWhenFromAndToChanges() { | ||
when(event.getPreviousFromHash()).thenReturn("FROM"); | ||
when(event.getPreviousToHash()).thenReturn("TO"); | ||
when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM2"); | ||
when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO2"); | ||
assertEquals(RESCOPED.name(), fromPullRequestEvent(event).getName()); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
@Test | ||
public void testThatRescopedEventsAreCalculatedCorrectlyWhenOnlyFromChanges() { | ||
when(event.getPreviousFromHash()).thenReturn("FROM"); | ||
when(event.getPreviousToHash()).thenReturn("TO"); | ||
when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM2"); | ||
when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO"); | ||
assertEquals(RESCOPED_FROM, fromPullRequestEvent(event).getName()); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
@Test | ||
public void testThatRescopedEventsAreCalculatedCorrectlyWhenOnlyToChanges() { | ||
when(event.getPreviousFromHash()).thenReturn("FROM"); | ||
when(event.getPreviousToHash()).thenReturn("TO"); | ||
when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM"); | ||
when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO2"); | ||
assertEquals(RESCOPED_TO, fromPullRequestEvent(event).getName()); | ||
} | ||
|
||
@Test | ||
public void testThatNoneRescopedEventsAreCalculatedCorrectly() { | ||
PullRequestEvent superEvent = mock(PullRequestEvent.class); | ||
when(superEvent.getAction()).thenReturn(OPENED); | ||
assertEquals(OPENED.name(), fromPullRequestEvent(superEvent).getName()); | ||
} | ||
} |