Skip to content

Commit 149b26f

Browse files
committed
Add hashtags-changed event
With the arrival of NoteDB[1], gerrit now supports hashtags on changes which might open interesting use-cases, like go's use of hashtags for managing CL[2]. This patch adds support for the hashtag changed event. [1] https://gerrit-review.googlesource.com/Documentation/note-db.html [2] golang/go#24836
1 parent 45d1057 commit 149b26f

File tree

6 files changed

+198
-1
lines changed

6 files changed

+198
-1
lines changed

src/main/java/com/sonymobile/tools/gerrit/gerritevents/dto/GerritEventKeys.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ public abstract class GerritEventKeys {
265265
* reason.
266266
*/
267267
public static final String REASON = "reason";
268+
/**
269+
* hashtags.
270+
*/
271+
public static final String HASHTAGS = "hashtags";
268272

269273
/**
270274
* Empty default constructor to hinder instantiation.

src/main/java/com/sonymobile/tools/gerrit/gerritevents/dto/GerritEventType.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetNotified;
4040
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PrivateStateChanged;
4141
import com.sonymobile.tools.gerrit.gerritevents.dto.events.WipStateChanged;
42+
import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
4243

4344
import java.util.LinkedList;
4445
import java.util.List;
@@ -114,7 +115,12 @@ public enum GerritEventType {
114115
/***
115116
* A work in progress state changed event.
116117
*/
117-
WIP_STATE_CHANGED("wip-state-changed", true, WipStateChanged.class);
118+
WIP_STATE_CHANGED("wip-state-changed", true, WipStateChanged.class),
119+
120+
/**
121+
* A hashtags changed event.
122+
*/
123+
HASHTAGS_CHANGED("hashtags-changed", true, HashtagsChanged.class);
118124

119125
private String typeValue;
120126
private boolean interesting;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2018 Diogo Ferreira <[email protected]> All rights reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.sonymobile.tools.gerrit.gerritevents.dto.events;
26+
27+
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventType;
28+
import net.sf.json.JSONArray;
29+
import net.sf.json.JSONObject;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.HASHTAGS;
35+
36+
/**
37+
* A DTO representation of the hashtags-changed Gerrit Event.
38+
* @author Diogo Ferreira &lt;[email protected]&gt;
39+
*/
40+
public class HashtagsChanged extends ChangeBasedEvent {
41+
private List<String> hashtags;
42+
43+
@Override
44+
public GerritEventType getEventType() {
45+
return GerritEventType.HASHTAGS_CHANGED;
46+
}
47+
48+
@Override
49+
public boolean isScorable() {
50+
return false;
51+
}
52+
53+
public List<String> getHashtags() {
54+
return hashtags;
55+
}
56+
57+
@Override
58+
public void fromJson(JSONObject json) {
59+
super.fromJson(json);
60+
61+
if (json.containsKey(HASHTAGS)) {
62+
JSONArray hashtagsArray = json.getJSONArray("hashtags");
63+
List<String> hashtags = new ArrayList<String>(hashtagsArray.size());
64+
for (Object tag : hashtagsArray) {
65+
hashtags.add(tag.toString());
66+
}
67+
this.hashtags = hashtags;
68+
}
69+
}
70+
}

src/test/java/com/sonymobile/tools/gerrit/gerritevents/GerritHandlerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ProjectCreated;
4040
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PrivateStateChanged;
4141
import com.sonymobile.tools.gerrit.gerritevents.dto.events.WipStateChanged;
42+
import com.sonymobile.tools.gerrit.gerritevents.dto.events.HashtagsChanged;
4243

4344
import org.junit.After;
4445
import org.junit.Before;
@@ -438,6 +439,10 @@ public void testEventNotificationWithDefaultListenerImplemention() {
438439
WipStateChanged wipStateChanged = new WipStateChanged();
439440
handler.notifyListeners(wipStateChanged);
440441
verify(listenerMock, times(1)).gerritEvent(wipStateChanged);
442+
443+
HashtagsChanged hashtagsChanged = new HashtagsChanged();
444+
handler.notifyListeners(hashtagsChanged);
445+
verify(listenerMock, times(1)).gerritEvent(hashtagsChanged);
441446
}
442447

443448
/**
@@ -648,6 +653,22 @@ public void gerritEvent(WipStateChanged event) {
648653
testListenerWithSpecificSignature(stateChangedListener, new WipStateChanged());
649654
}
650655

656+
/**
657+
* Tests that HashtagsChanged events are going in the method with
658+
* that type as parameter and that other type of events are going
659+
* in the default method.
660+
*/
661+
@Test
662+
public void testEventNotificationWithListenerHashtagsChangedMethodSignature() {
663+
SpecificEventListener stateChangedListener = new SpecificEventListener() {
664+
@SuppressWarnings("unused") //method is called by reflection
665+
public void gerritEvent(HashtagsChanged event) {
666+
specificMethodCalled = true;
667+
}
668+
};
669+
testListenerWithSpecificSignature(stateChangedListener, new HashtagsChanged());
670+
}
671+
651672
/**
652673
* Base test listener implementation.
653674
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2018 Diogo Ferreira
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.sonymobile.tools.gerrit.gerritevents.dto.events;
26+
27+
import com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory;
28+
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEvent;
29+
import net.sf.json.JSONObject;
30+
import org.apache.commons.io.IOUtils;
31+
import org.junit.Test;
32+
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.util.Arrays;
36+
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertTrue;
39+
40+
/**
41+
* @author Diogo Ferreira ([email protected])
42+
*/
43+
public class HashtagsChangedTest {
44+
45+
/**
46+
* Given an hashtag changed event in JSON format, it can be converted correctly.
47+
* @throws IOException if the json file cannot be loaded.
48+
*/
49+
@Test
50+
public void fromJsonShouldDeserializeHashtagsCorrectly() throws IOException {
51+
InputStream stream = getClass().getResourceAsStream("DeserializeHashtagsChangedTest.json");
52+
String json = IOUtils.toString(stream);
53+
JSONObject jsonObject = JSONObject.fromObject(json);
54+
GerritEvent evt = GerritJsonEventFactory.getEvent(jsonObject);
55+
56+
assertTrue("is an HashtagsChanged", evt instanceof HashtagsChanged);
57+
assertEquals("Hashtags match", Arrays.asList("works","yolo"), ((HashtagsChanged)evt).getHashtags());
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"editor": {
3+
"name": "Diogo",
4+
"email": "[email protected]",
5+
"username": "defer"
6+
},
7+
"removed": [
8+
"tag"
9+
],
10+
"hashtags": [
11+
"works",
12+
"yolo"
13+
],
14+
"change": {
15+
"project": "platform/build",
16+
"branch": "master",
17+
"id": "Icd27d85f4c4bc6e95abe8a514a32a68e6f97f181",
18+
"number": 1000,
19+
"subject": "Implement things",
20+
"owner": {
21+
"name": "Diogo",
22+
"email": "[email protected]",
23+
"username": "defer"
24+
},
25+
"url": "http://gerrit.local/1000",
26+
"commitMessage": "Implement things\n\nChange-Id: Icd27d85f4c4bc6e95abe8a514a32a68e6f97f181\n",
27+
"createdOn": 1539093819,
28+
"status": "NEW"
29+
},
30+
"project": "platform/build",
31+
"refName": "refs/heads/master",
32+
"changeKey": {
33+
"id": "Icd27d85f4c4bc6e95abe8a514a32a68e6f97f181"
34+
},
35+
"type": "hashtags-changed",
36+
"eventCreatedOn": 1539112429
37+
}

0 commit comments

Comments
 (0)