| 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; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.http.Request; |
| 35 | |
import com.jcabi.http.response.RestResponse; |
| 36 | |
import java.io.IOException; |
| 37 | |
import java.net.HttpURLConnection; |
| 38 | |
import javax.json.Json; |
| 39 | |
import javax.json.JsonObject; |
| 40 | |
import javax.json.JsonObjectBuilder; |
| 41 | |
import javax.json.JsonStructure; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
import org.hamcrest.Matchers; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 0 | @Immutable |
| 54 | |
@Loggable(Loggable.DEBUG) |
| 55 | 0 | @EqualsAndHashCode(of = { "request", "owner", "num" }) |
| 56 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 57 | 0 | final class RtPull implements Pull { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private final transient Request entry; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient Request request; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private final transient Repo owner; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final transient int num; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 11 | RtPull(final Request req, final Repo repo, final int number) { |
| 86 | 11 | this.entry = req; |
| 87 | 11 | final Coordinates coords = repo.coordinates(); |
| 88 | 11 | this.request = this.entry.uri() |
| 89 | 11 | .path("/repos") |
| 90 | 11 | .path(coords.user()) |
| 91 | 11 | .path(coords.repo()) |
| 92 | 11 | .path("/pulls") |
| 93 | 11 | .path(Integer.toString(number)) |
| 94 | 11 | .back(); |
| 95 | 11 | this.owner = repo; |
| 96 | 11 | this.num = number; |
| 97 | 11 | } |
| 98 | |
|
| 99 | |
@Override |
| 100 | |
public String toString() { |
| 101 | 0 | return this.request.uri().get().toString(); |
| 102 | |
} |
| 103 | |
|
| 104 | |
@Override |
| 105 | |
public Repo repo() { |
| 106 | 0 | return this.owner; |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public int number() { |
| 111 | 8 | return this.num; |
| 112 | |
} |
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public Iterable<Commit> commits() throws IOException { |
| 116 | 3 | return new RtPagination<Commit>( |
| 117 | 1 | this.request.uri().path("/commits").back(), |
| 118 | 1 | new RtValuePagination.Mapping<Commit, JsonObject>() { |
| 119 | |
@Override |
| 120 | |
public Commit map(final JsonObject object) { |
| 121 | 0 | return new RtCommit( |
| 122 | 0 | RtPull.this.entry, |
| 123 | 0 | RtPull.this.owner, |
| 124 | 0 | object.getString("sha") |
| 125 | |
); |
| 126 | |
} |
| 127 | |
} |
| 128 | |
); |
| 129 | |
} |
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public Iterable<JsonObject> files() throws IOException { |
| 133 | 3 | return new RtPagination<JsonObject>( |
| 134 | 1 | this.request.uri().path("/files").back(), |
| 135 | |
RtPagination.COPYING |
| 136 | |
); |
| 137 | |
} |
| 138 | |
|
| 139 | |
@Override |
| 140 | |
public void merge( |
| 141 | |
final String msg) |
| 142 | |
throws IOException { |
| 143 | 2 | final JsonStructure json = Json.createObjectBuilder() |
| 144 | 1 | .add("commit_message", msg) |
| 145 | 1 | .build(); |
| 146 | 1 | this.merge(json).assertStatus(HttpURLConnection.HTTP_OK); |
| 147 | 1 | } |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public MergeState merge( |
| 151 | |
final String msg, |
| 152 | |
final String sha) |
| 153 | |
throws IOException { |
| 154 | 0 | final JsonObjectBuilder builder = Json.createObjectBuilder() |
| 155 | 0 | .add("commit_message", msg).add("sha", sha); |
| 156 | 0 | final RestResponse response = this.merge(builder.build()) |
| 157 | 0 | .assertStatus( |
| 158 | 0 | Matchers.isOneOf( |
| 159 | 0 | HttpURLConnection.HTTP_OK, |
| 160 | 0 | HttpURLConnection.HTTP_BAD_METHOD, |
| 161 | 0 | HttpURLConnection.HTTP_CONFLICT |
| 162 | |
) |
| 163 | |
); |
| 164 | |
final MergeState mergeState; |
| 165 | 0 | switch (response.status()) { |
| 166 | |
case HttpURLConnection.HTTP_OK: |
| 167 | 0 | mergeState = MergeState.SUCCESS; |
| 168 | 0 | break; |
| 169 | |
case HttpURLConnection.HTTP_BAD_METHOD: |
| 170 | 0 | mergeState = MergeState.NOT_MERGEABLE; |
| 171 | 0 | break; |
| 172 | |
default: |
| 173 | 0 | mergeState = MergeState.BAD_HEAD; |
| 174 | |
break; |
| 175 | |
} |
| 176 | 0 | return mergeState; |
| 177 | |
} |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public PullComments comments() throws IOException { |
| 181 | 0 | return new RtPullComments(this.entry, this); |
| 182 | |
} |
| 183 | |
|
| 184 | |
@Override |
| 185 | |
public PullRef base() throws IOException { |
| 186 | 3 | return new RtPullRef( |
| 187 | 1 | this.owner.github(), |
| 188 | 1 | this.json().getJsonObject("base") |
| 189 | |
); |
| 190 | |
} |
| 191 | |
|
| 192 | |
@Override |
| 193 | |
public PullRef head() throws IOException { |
| 194 | 3 | return new RtPullRef( |
| 195 | 1 | this.owner.github(), |
| 196 | 1 | this.json().getJsonObject("head") |
| 197 | |
); |
| 198 | |
} |
| 199 | |
|
| 200 | |
@Override |
| 201 | |
public JsonObject json() throws IOException { |
| 202 | 8 | return new RtJson(this.request).fetch(); |
| 203 | |
} |
| 204 | |
|
| 205 | |
@Override |
| 206 | |
public void patch(final JsonObject json) throws IOException { |
| 207 | 0 | new RtJson(this.request).patch(json); |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
@Override |
| 211 | |
public int compareTo( |
| 212 | |
final Pull pull |
| 213 | |
) { |
| 214 | 4 | return this.number() - pull.number(); |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
private RestResponse merge(final JsonStructure payload) |
| 224 | |
throws IOException { |
| 225 | 2 | return this.request.uri() |
| 226 | 1 | .path("/merge").back() |
| 227 | 1 | .body().set(payload).back() |
| 228 | 1 | .method(Request.PUT) |
| 229 | 1 | .fetch() |
| 230 | 1 | .as(RestResponse.class); |
| 231 | |
} |
| 232 | |
|
| 233 | |
} |