summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeEditApi.java
blob: 0a7fd084a26da0df4af66a495bafd8825e6c8d38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.changes;

import com.google.gerrit.client.VoidResult;
import com.google.gerrit.client.editor.EditFileInfo;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.HttpCallback;
import com.google.gerrit.client.rpc.NativeString;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;

/** REST API helpers to remotely edit a change. */
public class ChangeEditApi {
  /** Get file (or commit message) contents. */
  public static void get(PatchSet.Id id, String path, boolean base, HttpCallback<NativeString> cb) {
    RestApi api;
    if (id.get() != 0) {
      // Read from a published revision, when change edit doesn't
      // exist for the caller, or is not currently active.
      api = ChangeApi.revision(id).view("files").id(path).view("content");
    } else if (Patch.COMMIT_MSG.equals(path)) {
      api = editMessage(id.getParentKey().get()).addParameter("base", base);
    } else {
      api = editFile(id.getParentKey().get(), path).addParameter("base", base);
    }
    api.get(cb);
  }

  /** Get file (or commit message) contents of the edit. */
  public static void get(PatchSet.Id id, String path, HttpCallback<NativeString> cb) {
    get(id, path, false, cb);
  }

  /** Get meta info for change edit. */
  public static void getMeta(PatchSet.Id id, String path, AsyncCallback<EditFileInfo> cb) {
    if (id.get() != 0) {
      throw new IllegalStateException("only supported for edits");
    }
    editFile(id.getParentKey().get(), path).view("meta").get(cb);
  }

  /** Put message into a change edit. */
  public static void putMessage(int id, String m, GerritCallback<VoidResult> cb) {
    editMessage(id).put(m, cb);
  }

  /** Put contents into a file or commit message in a change edit. */
  public static void put(int id, String path, String content, GerritCallback<VoidResult> cb) {
    if (Patch.COMMIT_MSG.equals(path)) {
      putMessage(id, content, cb);
    } else {
      editFile(id, path).put(content, cb);
    }
  }

  /** Delete a file in the pending edit. */
  public static void delete(int id, String path, AsyncCallback<VoidResult> cb) {
    editFile(id, path).delete(cb);
  }

  /** Rename a file in the pending edit. */
  public static void rename(int id, String path, String newPath, AsyncCallback<VoidResult> cb) {
    Input in = Input.create();
    in.oldPath(path);
    in.newPath(newPath);
    ChangeApi.edit(id).post(in, cb);
  }

  /** Restore (undo delete/modify) a file in the pending edit. */
  public static void restore(int id, String path, AsyncCallback<VoidResult> cb) {
    Input in = Input.create();
    in.restorePath(path);
    ChangeApi.edit(id).post(in, cb);
  }

  private static RestApi editMessage(int id) {
    return ChangeApi.change(id).view("edit:message");
  }

  private static RestApi editFile(int id, String path) {
    return ChangeApi.edit(id).id(path);
  }

  private static class Input extends JavaScriptObject {
    static Input create() {
      return createObject().cast();
    }

    final native void restorePath(String p) /*-{ this.restore_path=p }-*/;

    final native void oldPath(String p) /*-{ this.old_path=p }-*/;

    final native void newPath(String p) /*-{ this.new_path=p }-*/;

    protected Input() {}
  }
}