| 1 | 2 | |
| 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 | |
package com.jcabi.github.mock; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.github.Coordinates; |
| 34 | |
import com.jcabi.github.Github; |
| 35 | |
import com.jcabi.github.Pull; |
| 36 | |
import com.jcabi.github.PullComment; |
| 37 | |
import com.jcabi.github.PullComments; |
| 38 | |
import com.jcabi.xml.XML; |
| 39 | |
import java.io.IOException; |
| 40 | |
import java.util.Map; |
| 41 | |
import javax.json.Json; |
| 42 | |
import javax.json.JsonObject; |
| 43 | |
import lombok.EqualsAndHashCode; |
| 44 | |
import lombok.ToString; |
| 45 | |
import org.xembly.Directives; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
@Immutable |
| 56 | 0 | @ToString |
| 57 | 0 | @EqualsAndHashCode(of = { "storage", "self", "repo", "owner" }) |
| 58 | |
final class MkPullComments implements PullComments { |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private final transient MkStorage storage; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient String self; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private final transient Coordinates repo; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final transient Pull owner; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
MkPullComments( |
| 89 | |
final MkStorage stg, |
| 90 | |
final String login, |
| 91 | |
final Coordinates rep, |
| 92 | |
final Pull pull |
| 93 | 12 | ) throws IOException { |
| 94 | 12 | this.storage = stg; |
| 95 | 12 | this.self = login; |
| 96 | 12 | this.repo = rep; |
| 97 | 12 | this.owner = pull; |
| 98 | 24 | this.storage.apply( |
| 99 | 12 | new Directives().xpath( |
| 100 | 12 | String.format( |
| 101 | |
"/github/repos/repo[@coords='%s']/pulls/pull[number='%d']", |
| 102 | 12 | this.repo, this.owner.number() |
| 103 | |
) |
| 104 | 12 | ).addIf("comments") |
| 105 | |
); |
| 106 | 12 | } |
| 107 | |
@Override |
| 108 | |
public Pull pull() { |
| 109 | 3 | return this.owner; |
| 110 | |
} |
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public PullComment get(final int number) { |
| 114 | 21 | return new MkPullComment(this.storage, this.repo, this.owner, number); |
| 115 | |
} |
| 116 | |
|
| 117 | |
@Override |
| 118 | |
public Iterable<PullComment> iterate( |
| 119 | |
final Map<String, String> params |
| 120 | |
) { |
| 121 | 2 | return new MkIterable<PullComment>( |
| 122 | |
this.storage, |
| 123 | 1 | String.format( |
| 124 | |
"/github/repos/repo[@coords='%s']/pulls/pull/comments", |
| 125 | |
this.repo |
| 126 | |
), |
| 127 | 3 | new MkIterable.Mapping<PullComment>() { |
| 128 | |
@Override |
| 129 | |
public PullComment map(final XML xml) { |
| 130 | 4 | return MkPullComments.this.get( |
| 131 | 2 | Integer.parseInt(xml.xpath("comment/id/text()").get(0)) |
| 132 | |
); |
| 133 | |
} |
| 134 | |
} |
| 135 | |
); |
| 136 | |
} |
| 137 | |
|
| 138 | |
@Override |
| 139 | |
public Iterable<PullComment> iterate( |
| 140 | |
final int number, |
| 141 | |
final Map<String, String> params |
| 142 | |
) { |
| 143 | 8 | return new MkIterable<PullComment>( |
| 144 | 4 | this.storage, String.format("%s/comment", this.xpath()), |
| 145 | 8 | new MkIterable.Mapping<PullComment>() { |
| 146 | |
@Override |
| 147 | |
public PullComment map(final XML xml) { |
| 148 | 8 | return MkPullComments.this.get( |
| 149 | 4 | Integer.parseInt(xml.xpath("id/text()").get(0)) |
| 150 | |
); |
| 151 | |
} |
| 152 | |
} |
| 153 | |
); |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
@Override |
| 158 | |
public PullComment post( |
| 159 | |
final String body, |
| 160 | |
final String commit, |
| 161 | |
final String path, |
| 162 | |
final int position |
| 163 | |
) throws IOException { |
| 164 | 13 | this.storage.lock(); |
| 165 | |
final int number; |
| 166 | |
try { |
| 167 | 13 | number = 1 + this.storage.xml() |
| 168 | 13 | .nodes(String.format("%s/comment/id/text()", this.xpath())) |
| 169 | 13 | .size(); |
| 170 | 26 | this.storage.apply( |
| 171 | 13 | new Directives().xpath(this.xpath()).add("comment") |
| 172 | 13 | .add("id").set(Integer.toString(number)).up() |
| 173 | 13 | .add("url").set("http://localhost/1").up() |
| 174 | 13 | .add("diff_hunk").set("@@ -16,33 +16,40 @@ public...").up() |
| 175 | |
|
| 176 | 13 | .add("path").set(path).up() |
| 177 | 13 | .add("position").set(Integer.toString(position)).up() |
| 178 | 13 | .add("original_position").set(Integer.toString(number)).up() |
| 179 | 13 | .add("commit_id").set(commit).up() |
| 180 | 13 | .add("original_commit_id").set(commit).up() |
| 181 | 13 | .add("body").set(body).up() |
| 182 | 13 | .add("created_at").set(new Github.Time().toString()).up() |
| 183 | 13 | .add("published_at").set(new Github.Time().toString()).up() |
| 184 | 13 | .add("user").add("login").set(this.self).up() |
| 185 | 13 | .add("pull_request_url").set("http://localhost/2").up() |
| 186 | |
); |
| 187 | |
} finally { |
| 188 | 13 | this.storage.unlock(); |
| 189 | 13 | } |
| 190 | 13 | return this.get(number); |
| 191 | |
} |
| 192 | |
|
| 193 | |
@Override |
| 194 | |
public PullComment reply( |
| 195 | |
final String body, |
| 196 | |
final int comment |
| 197 | |
) |
| 198 | |
throws IOException { |
| 199 | 1 | this.storage.lock(); |
| 200 | |
try { |
| 201 | 1 | final JsonObject orig = this.get(comment).json(); |
| 202 | 2 | final PullComment reply = this.post( |
| 203 | |
body, |
| 204 | 1 | orig.getString("commit_id"), |
| 205 | 1 | orig.getString("path"), |
| 206 | |
comment |
| 207 | |
); |
| 208 | 2 | reply.patch( |
| 209 | 1 | Json.createObjectBuilder() |
| 210 | 1 | .add("original_position", String.valueOf(comment)).build() |
| 211 | |
); |
| 212 | 2 | return reply; |
| 213 | |
} finally { |
| 214 | 1 | this.storage.unlock(); |
| 215 | 0 | } |
| 216 | |
} |
| 217 | |
|
| 218 | |
@Override |
| 219 | |
public void remove(final int number) throws IOException { |
| 220 | 2 | this.storage.apply( |
| 221 | 1 | new Directives().xpath( |
| 222 | 1 | String.format("%s/comment[id='%d']", this.xpath(), number) |
| 223 | 1 | ).remove() |
| 224 | |
); |
| 225 | 1 | } |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
private String xpath() { |
| 232 | 62 | return String.format( |
| 233 | |
|
| 234 | |
"/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments", |
| 235 | 31 | this.repo, this.owner.number() |
| 236 | |
); |
| 237 | |
} |
| 238 | |
} |