PageRenderTime 106ms CodeModel.GetById 51ms RepoModel.GetById 1ms app.codeStats 0ms

/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeApi.java

https://gitlab.com/chenfengxu/gerrit
Java | 401 lines | 295 code | 78 blank | 28 comment | 14 complexity | bfcd81f2b5432113742f869c34c04904 MD5 | raw file
  1. // Copyright (C) 2012 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.google.gerrit.client.changes;
  15. import com.google.gerrit.client.info.AccountInfo;
  16. import com.google.gerrit.client.info.ChangeInfo;
  17. import com.google.gerrit.client.info.ChangeInfo.CommitInfo;
  18. import com.google.gerrit.client.info.ChangeInfo.EditInfo;
  19. import com.google.gerrit.client.info.ChangeInfo.IncludedInInfo;
  20. import com.google.gerrit.client.rpc.CallbackGroup.Callback;
  21. import com.google.gerrit.client.rpc.NativeString;
  22. import com.google.gerrit.client.rpc.RestApi;
  23. import com.google.gerrit.common.Nullable;
  24. import com.google.gerrit.reviewdb.client.PatchSet;
  25. import com.google.gwt.core.client.JavaScriptObject;
  26. import com.google.gwt.user.client.rpc.AsyncCallback;
  27. /** A collection of static methods which work on the Gerrit REST API for specific changes. */
  28. public class ChangeApi {
  29. /** Abandon the change, ending its review. */
  30. public static void abandon(
  31. @Nullable String project, int id, String msg, AsyncCallback<ChangeInfo> cb) {
  32. MessageInput input = MessageInput.create();
  33. input.message(emptyToNull(msg));
  34. call(project, id, "abandon").post(input, cb);
  35. }
  36. /** Create a new work-in-progress change. */
  37. public static void createChange(
  38. String project,
  39. String branch,
  40. String topic,
  41. String subject,
  42. String base,
  43. AsyncCallback<ChangeInfo> cb) {
  44. CreateChangeInput input = CreateChangeInput.create();
  45. input.project(emptyToNull(project));
  46. input.branch(emptyToNull(branch));
  47. input.topic(emptyToNull(topic));
  48. input.subject(emptyToNull(subject));
  49. input.baseChange(emptyToNull(base));
  50. input.workInProgress(true);
  51. new RestApi("/changes/").post(input, cb);
  52. }
  53. /** Restore a previously abandoned change to be open again. */
  54. public static void restore(
  55. @Nullable String project, int id, String msg, AsyncCallback<ChangeInfo> cb) {
  56. MessageInput input = MessageInput.create();
  57. input.message(emptyToNull(msg));
  58. call(project, id, "restore").post(input, cb);
  59. }
  60. /** Create a new change that reverts the delta caused by this change. */
  61. public static void revert(
  62. @Nullable String project, int id, String msg, AsyncCallback<ChangeInfo> cb) {
  63. MessageInput input = MessageInput.create();
  64. input.message(emptyToNull(msg));
  65. call(project, id, "revert").post(input, cb);
  66. }
  67. /** Update the topic of a change. */
  68. public static void topic(
  69. @Nullable String project, int id, String topic, AsyncCallback<String> cb) {
  70. RestApi call = call(project, id, "topic");
  71. topic = emptyToNull(topic);
  72. if (topic != null) {
  73. TopicInput input = TopicInput.create();
  74. input.topic(topic);
  75. call.put(input, NativeString.unwrap(cb));
  76. } else {
  77. call.delete(NativeString.unwrap(cb));
  78. }
  79. }
  80. public static void detail(@Nullable String project, int id, AsyncCallback<ChangeInfo> cb) {
  81. detail(project, id).get(cb);
  82. }
  83. public static RestApi detail(@Nullable String project, int id) {
  84. return call(project, id, "detail");
  85. }
  86. public static RestApi blame(@Nullable String project, PatchSet.Id id, String path, boolean base) {
  87. return revision(project, id).view("files").id(path).view("blame").addParameter("base", base);
  88. }
  89. public static RestApi actions(@Nullable String project, int id, String revision) {
  90. if (revision == null || revision.equals("")) {
  91. revision = "current";
  92. }
  93. return call(project, id, revision, "actions");
  94. }
  95. public static void deleteAssignee(
  96. @Nullable String project, int id, AsyncCallback<AccountInfo> cb) {
  97. change(project, id).view("assignee").delete(cb);
  98. }
  99. public static void setAssignee(
  100. @Nullable String project, int id, String user, AsyncCallback<AccountInfo> cb) {
  101. AssigneeInput input = AssigneeInput.create();
  102. input.assignee(user);
  103. change(project, id).view("assignee").put(input, cb);
  104. }
  105. public static void markPrivate(
  106. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  107. change(project, id).view("private").post(PrivateInput.create(), cb);
  108. }
  109. public static void unmarkPrivate(
  110. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  111. change(project, id).view("private.delete").post(PrivateInput.create(), cb);
  112. }
  113. public static RestApi comments(@Nullable String project, int id) {
  114. return call(project, id, "comments");
  115. }
  116. public static RestApi drafts(@Nullable String project, int id) {
  117. return call(project, id, "drafts");
  118. }
  119. public static void edit(@Nullable String project, int id, AsyncCallback<EditInfo> cb) {
  120. edit(project, id).get(cb);
  121. }
  122. public static void editWithFiles(@Nullable String project, int id, AsyncCallback<EditInfo> cb) {
  123. edit(project, id).addParameterTrue("list").get(cb);
  124. }
  125. public static RestApi edit(@Nullable String project, int id) {
  126. return change(project, id).view("edit");
  127. }
  128. public static RestApi editWithCommands(@Nullable String project, int id) {
  129. return edit(project, id).addParameterTrue("download-commands");
  130. }
  131. public static void includedIn(
  132. @Nullable String project, int id, AsyncCallback<IncludedInInfo> cb) {
  133. call(project, id, "in").get(cb);
  134. }
  135. public static RestApi revision(@Nullable String project, int id, String revision) {
  136. return change(project, id).view("revisions").id(revision);
  137. }
  138. public static RestApi revision(@Nullable String project, PatchSet.Id id) {
  139. int cn = id.getParentKey().get();
  140. String revision = RevisionInfoCache.get(id);
  141. if (revision != null) {
  142. return revision(project, cn, revision);
  143. }
  144. return change(project, cn).view("revisions").id(id.get());
  145. }
  146. public static RestApi reviewers(@Nullable String project, int id) {
  147. return change(project, id).view("reviewers");
  148. }
  149. public static RestApi suggestReviewers(
  150. @Nullable String project, int id, String q, int n, boolean e) {
  151. RestApi api =
  152. change(project, id).view("suggest_reviewers").addParameter("n", n).addParameter("e", e);
  153. if (q != null) {
  154. api.addParameter("q", q);
  155. }
  156. return api;
  157. }
  158. public static RestApi vote(@Nullable String project, int id, int reviewer, String vote) {
  159. return reviewer(project, id, reviewer).view("votes").id(vote);
  160. }
  161. public static RestApi reviewer(@Nullable String project, int id, int reviewer) {
  162. return change(project, id).view("reviewers").id(reviewer);
  163. }
  164. public static RestApi reviewer(@Nullable String project, int id, String reviewer) {
  165. return change(project, id).view("reviewers").id(reviewer);
  166. }
  167. public static RestApi hashtags(@Nullable String project, int changeId) {
  168. return change(project, changeId).view("hashtags");
  169. }
  170. public static RestApi hashtag(@Nullable String project, int changeId, String hashtag) {
  171. return change(project, changeId).view("hashtags").id(hashtag);
  172. }
  173. /** Submit a specific revision of a change. */
  174. public static void cherrypick(
  175. String project,
  176. int id,
  177. String commit,
  178. String destination,
  179. String message,
  180. AsyncCallback<ChangeInfo> cb) {
  181. CherryPickInput cherryPickInput = CherryPickInput.create();
  182. cherryPickInput.setMessage(message);
  183. cherryPickInput.setDestination(destination);
  184. call(project, id, commit, "cherrypick").post(cherryPickInput, cb);
  185. }
  186. /** Move change to another branch. */
  187. public static void move(
  188. String project, int id, String destination, String message, AsyncCallback<ChangeInfo> cb) {
  189. MoveInput moveInput = MoveInput.create();
  190. moveInput.setMessage(message);
  191. moveInput.setDestinationBranch(destination);
  192. change(project, id).view("move").post(moveInput, cb);
  193. }
  194. /** Edit commit message for specific revision of a change. */
  195. public static void message(
  196. @Nullable String project,
  197. int id,
  198. String commit,
  199. String message,
  200. AsyncCallback<JavaScriptObject> cb) {
  201. CherryPickInput input = CherryPickInput.create();
  202. input.setMessage(message);
  203. call(project, id, commit, "message").post(input, cb);
  204. }
  205. /** Submit a specific revision of a change. */
  206. public static void submit(
  207. @Nullable String project, int id, String commit, AsyncCallback<SubmitInfo> cb) {
  208. JavaScriptObject in = JavaScriptObject.createObject();
  209. call(project, id, commit, "submit").post(in, cb);
  210. }
  211. /** Delete a specific draft change. */
  212. public static void deleteChange(
  213. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  214. change(project, id).delete(cb);
  215. }
  216. /** Delete change edit. */
  217. public static void deleteEdit(
  218. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  219. edit(project, id).delete(cb);
  220. }
  221. /** Publish change edit. */
  222. public static void publishEdit(
  223. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  224. JavaScriptObject in = JavaScriptObject.createObject();
  225. change(project, id).view("edit:publish").post(in, cb);
  226. }
  227. /** Rebase change edit on latest patch set. */
  228. public static void rebaseEdit(
  229. @Nullable String project, int id, AsyncCallback<JavaScriptObject> cb) {
  230. JavaScriptObject in = JavaScriptObject.createObject();
  231. change(project, id).view("edit:rebase").post(in, cb);
  232. }
  233. /** Rebase a revision onto the branch tip or another change. */
  234. public static void rebase(
  235. @Nullable String project, int id, String commit, String base, AsyncCallback<ChangeInfo> cb) {
  236. RebaseInput rebaseInput = RebaseInput.create();
  237. rebaseInput.setBase(base);
  238. call(project, id, commit, "rebase").post(rebaseInput, cb);
  239. }
  240. private static class MessageInput extends JavaScriptObject {
  241. final native void message(String m) /*-{ if(m)this.message=m; }-*/;
  242. static MessageInput create() {
  243. return (MessageInput) createObject();
  244. }
  245. protected MessageInput() {}
  246. }
  247. private static class AssigneeInput extends JavaScriptObject {
  248. final native void assignee(String a) /*-{ if(a)this.assignee=a; }-*/;
  249. static AssigneeInput create() {
  250. return (AssigneeInput) createObject();
  251. }
  252. protected AssigneeInput() {}
  253. }
  254. private static class TopicInput extends JavaScriptObject {
  255. final native void topic(String t) /*-{ if(t)this.topic=t; }-*/;
  256. static TopicInput create() {
  257. return (TopicInput) createObject();
  258. }
  259. protected TopicInput() {}
  260. }
  261. private static class CreateChangeInput extends JavaScriptObject {
  262. static CreateChangeInput create() {
  263. return (CreateChangeInput) createObject();
  264. }
  265. public final native void branch(String b) /*-{ if(b)this.branch=b; }-*/;
  266. public final native void topic(String t) /*-{ if(t)this.topic=t; }-*/;
  267. public final native void project(String p) /*-{ if(p)this.project=p; }-*/;
  268. public final native void subject(String s) /*-{ if(s)this.subject=s; }-*/;
  269. public final native void status(String s) /*-{ if(s)this.status=s; }-*/;
  270. public final native void baseChange(String b) /*-{ if(b)this.base_change=b; }-*/;
  271. public final native void workInProgress(Boolean b) /*-{ if(b)this.work_in_progress=b; }-*/;
  272. protected CreateChangeInput() {}
  273. }
  274. private static class CherryPickInput extends JavaScriptObject {
  275. static CherryPickInput create() {
  276. return (CherryPickInput) createObject();
  277. }
  278. final native void setDestination(String d) /*-{ this.destination = d; }-*/;
  279. final native void setMessage(String m) /*-{ this.message = m; }-*/;
  280. protected CherryPickInput() {}
  281. }
  282. private static class MoveInput extends JavaScriptObject {
  283. static MoveInput create() {
  284. return (MoveInput) createObject();
  285. }
  286. final native void setDestinationBranch(String d) /*-{ this.destination_branch = d; }-*/;
  287. final native void setMessage(String m) /*-{ this.message = m; }-*/;
  288. protected MoveInput() {}
  289. }
  290. private static class PrivateInput extends JavaScriptObject {
  291. static PrivateInput create() {
  292. return (PrivateInput) createObject();
  293. }
  294. final native void setMessage(String m) /*-{ this.message = m; }-*/;
  295. protected PrivateInput() {}
  296. }
  297. private static class RebaseInput extends JavaScriptObject {
  298. final native void setBase(String b) /*-{ this.base = b; }-*/;
  299. static RebaseInput create() {
  300. return (RebaseInput) createObject();
  301. }
  302. protected RebaseInput() {}
  303. }
  304. private static RestApi call(@Nullable String project, int id, String action) {
  305. return change(project, id).view(action);
  306. }
  307. private static RestApi call(@Nullable String project, int id, String commit, String action) {
  308. return change(project, id).view("revisions").id(commit).view(action);
  309. }
  310. public static RestApi change(@Nullable String project, int id) {
  311. if (project == null) {
  312. return new RestApi("/changes/").id(String.valueOf(id));
  313. }
  314. return new RestApi("/changes/").id(project, id);
  315. }
  316. public static String emptyToNull(String str) {
  317. return str == null || str.isEmpty() ? null : str;
  318. }
  319. public static void commitWithLinks(
  320. @Nullable String project, int changeId, String revision, Callback<CommitInfo> callback) {
  321. revision(project, changeId, revision).view("commit").addParameterTrue("links").get(callback);
  322. }
  323. }