Hi Gerrit Experts
I am new to writing REST endpoints in JAVA and I am trying to write a Gerrit plugin for 2.12 which basically adds a comment to the change when a new button is clicked. I am referring to cookbook-plugin and modified below snippets so that a comment "Test Comment" is posted on a change when button "Library-compliance+1" is clicked. But, I see that the comment is getting posted on many other events like clicking "Code-review+1" or refreshing the page. etc.. What am I doing wrong?
In cookbook-plugin, here are the files I changed:
Greetings.java:
30 class Greetings implements RestReadView<RevisionResource> {
31
32 @Inject
33 GerritApi gApi;
34 @Override
35 public Response<GreetInfo> apply(RevisionResource rev) {
36 int id = rev.getChangeResource().getChange().getChangeId();
37 ReviewInput in = new ReviewInput();
38 in.message("Test Comment");
39 try {
40 gApi.changes().id(id).current().review(in);
41 } catch (Exception error) {
42 }
43 GreetInfo l = new GreetInfo("test");
44 return Response.ok(l);
45 }
46 }
CookBookPlugin.java
54 Plugin.get().panel(
55 GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_BUTTONS,
56 new Panel.EntryPoint() {
57 @Override
58 public void onLoad(Panel panel) {
59
60 Button b = new HighlightButton("Library-Compliance+1");
61 b.addClickHandler(new ClickHandler() {
62 @Override
63 public void onClick(ClickEvent event) {
64 new RestApi("changes").id("3").view("cookbook", "Greetings")
65 .get(new AsyncCallback<AccountPreferencesInfo>() {
66 @Override
67 public void onSuccess(AccountPreferencesInfo result) {
68 Window.alert("PASSING");
69 }
70 @Override
71 public void onFailure(Throwable caught) {
72 Window.alert("FAILING");
73 // never invoked
74 }
75 });
76 }
77 });
78 panel.setWidget(b);
Appreciate any help on this.
Thanks
Sharath