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.JsonResponse; |
36 | |
import com.jcabi.http.response.RestResponse; |
37 | |
import java.io.IOException; |
38 | |
import java.net.HttpURLConnection; |
39 | |
import javax.json.Json; |
40 | |
import javax.json.JsonObject; |
41 | |
import lombok.EqualsAndHashCode; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
@Immutable |
51 | |
@Loggable(Loggable.DEBUG) |
52 | 0 | @EqualsAndHashCode(of = {"entry", "request", "owner" }) |
53 | |
final class RtReferences implements References { |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private static final String REF = "ref"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private final transient Request entry; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private final transient Request request; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private final transient Repo owner; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 6 | RtReferences(final Request req, final Repo repo) { |
81 | 6 | this.entry = req; |
82 | 6 | this.owner = repo; |
83 | 6 | this.request = req.uri().path("/repos").path(repo.coordinates().user()) |
84 | 6 | .path(repo.coordinates().repo()).path("/git").path("/refs").back(); |
85 | 6 | } |
86 | |
|
87 | |
@Override |
88 | |
public Repo repo() { |
89 | 0 | return this.owner; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public Reference create( |
94 | |
final String ref, |
95 | |
final String sha |
96 | |
) throws IOException { |
97 | 4 | final JsonObject json = Json.createObjectBuilder() |
98 | 2 | .add("sha", sha).add(RtReferences.REF, ref).build(); |
99 | 4 | return this.get( |
100 | 2 | this.request.method(Request.POST) |
101 | 2 | .body().set(json).back() |
102 | 2 | .fetch().as(RestResponse.class) |
103 | 2 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
104 | 2 | .as(JsonResponse.class) |
105 | 2 | .json().readObject().getString(RtReferences.REF) |
106 | |
); |
107 | |
} |
108 | |
|
109 | |
@Override |
110 | |
public Reference get( |
111 | |
final String identifier |
112 | |
) { |
113 | 8 | return new RtReference(this.entry, this.owner, identifier); |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public Iterable<Reference> iterate() { |
118 | 2 | return new RtPagination<Reference>( |
119 | |
this.request, |
120 | 1 | new RtValuePagination.Mapping<Reference, JsonObject>() { |
121 | |
@Override |
122 | |
public Reference map(final JsonObject object) { |
123 | 0 | return RtReferences.this.get( |
124 | 0 | object.getString(RtReferences.REF) |
125 | |
); |
126 | |
} |
127 | |
} |
128 | |
); |
129 | |
} |
130 | |
|
131 | |
@Override |
132 | |
public Iterable<Reference> iterate( |
133 | |
final String subnamespace |
134 | |
) { |
135 | 6 | return new RtPagination<Reference>( |
136 | 2 | this.request.uri().path(subnamespace).back(), |
137 | 4 | new RtValuePagination.Mapping<Reference, JsonObject>() { |
138 | |
@Override |
139 | |
public Reference map(final JsonObject object) { |
140 | 4 | return RtReferences.this.get( |
141 | 2 | object.getString(RtReferences.REF) |
142 | |
); |
143 | |
} |
144 | |
} |
145 | |
); |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public Iterable<Reference> tags() { |
150 | 2 | return this.iterate("tags"); |
151 | |
} |
152 | |
|
153 | |
@Override |
154 | |
public Iterable<Reference> heads() { |
155 | 2 | return this.iterate("heads"); |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public void remove( |
160 | |
final String identifier |
161 | |
) throws IOException { |
162 | 2 | this.request.method(Request.DELETE) |
163 | 1 | .uri().path(identifier).back().fetch() |
164 | 1 | .as(RestResponse.class) |
165 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
166 | 1 | } |
167 | |
|
168 | |
} |