Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtHooks |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$1 |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$AjcClosure1 |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$AjcClosure3 |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$AjcClosure5 |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$AjcClosure7 |
|
| 1.1428571428571428;1.143 | ||||
RtHooks$AjcClosure9 |
|
| 1.1428571428571428;1.143 |
1 | 2 | /** |
2 | * Copyright (c) 2013-2017, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
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 java.util.Map; | |
40 | import javax.json.Json; | |
41 | import javax.json.JsonObject; | |
42 | import javax.json.JsonObjectBuilder; | |
43 | import javax.json.JsonStructure; | |
44 | import lombok.EqualsAndHashCode; | |
45 | ||
46 | /** | |
47 | * Github hooks. | |
48 | * @author Paul Polishchuk (ppol@ua.fm) | |
49 | * @version $Id: bbd98df8031c11487a86a0a8c34bd00aefa127fd $ | |
50 | * @since 0.8 | |
51 | */ | |
52 | @Immutable | |
53 | @Loggable(Loggable.DEBUG) | |
54 | 0 | @EqualsAndHashCode(of = { "entry", "owner", "request" }) |
55 | final class RtHooks implements Hooks { | |
56 | /** | |
57 | * API entry point. | |
58 | */ | |
59 | private final transient Request entry; | |
60 | ||
61 | /** | |
62 | * RESTful request. | |
63 | */ | |
64 | private final transient Request request; | |
65 | ||
66 | /** | |
67 | * Repository. | |
68 | */ | |
69 | private final transient Repo owner; | |
70 | ||
71 | /** | |
72 | * Public ctor. | |
73 | * @param req Request | |
74 | * @param repo Repository | |
75 | */ | |
76 | 6 | public RtHooks(final Request req, final Repo repo) { |
77 | 6 | this.entry = req; |
78 | 6 | final Coordinates coords = repo.coordinates(); |
79 | 6 | this.request = this.entry.uri() |
80 | 6 | .path("/repos") |
81 | 6 | .path(coords.user()) |
82 | 6 | .path(coords.repo()) |
83 | 6 | .path("/hooks") |
84 | 6 | .back(); |
85 | 6 | this.owner = repo; |
86 | 6 | } |
87 | ||
88 | @Override | |
89 | public Repo repo() { | |
90 | 0 | return this.owner; |
91 | } | |
92 | ||
93 | @Override | |
94 | public Iterable<Hook> iterate() { | |
95 | 4 | return new RtPagination<Hook>( |
96 | this.request, | |
97 | 4 | new RtValuePagination.Mapping<Hook, JsonObject>() { |
98 | @Override | |
99 | public Hook map(final JsonObject object) { | |
100 | // @checkstyle MultipleStringLiterals (1 line) | |
101 | 2 | return RtHooks.this.get(object.getInt("id")); |
102 | } | |
103 | } | |
104 | ); | |
105 | } | |
106 | ||
107 | @Override | |
108 | public void remove(final int number) throws IOException { | |
109 | 2 | this.request.method(Request.DELETE) |
110 | 1 | .uri().path(Integer.toString(number)).back() |
111 | 1 | .fetch() |
112 | 1 | .as(RestResponse.class) |
113 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
114 | 1 | } |
115 | ||
116 | @Override | |
117 | public Hook get(final int number) { | |
118 | 8 | return new RtHook(this.entry, this.owner, number); |
119 | } | |
120 | ||
121 | @Override | |
122 | public Hook create( | |
123 | final String name, | |
124 | final Map<String, String> config, | |
125 | final boolean active | |
126 | ) throws IOException { | |
127 | 2 | final JsonObjectBuilder builder = Json.createObjectBuilder(); |
128 | 1 | for (final Map.Entry<String, String> entr : config.entrySet()) { |
129 | 2 | builder.add(entr.getKey(), entr.getValue()); |
130 | 2 | } |
131 | 1 | final JsonStructure json = Json.createObjectBuilder() |
132 | 1 | .add("name", name) |
133 | 1 | .add("config", builder) |
134 | 1 | .add("active", active) |
135 | 1 | .build(); |
136 | 2 | return this.get( |
137 | 1 | this.request.method(Request.POST) |
138 | 1 | .body().set(json).back() |
139 | 1 | .fetch().as(RestResponse.class) |
140 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
141 | 1 | .as(JsonResponse.class) |
142 | 1 | .json().readObject().getInt("id") |
143 | ); | |
144 | } | |
145 | } |