1 /**
2 * Copyright (c) 2013-2023, 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.google.common.base.Optional;
33 import com.jcabi.aspects.Immutable;
34 import com.jcabi.aspects.Loggable;
35 import java.io.IOException;
36 import javax.json.Json;
37 import javax.json.JsonObject;
38 import javax.json.JsonObjectBuilder;
39 import lombok.EqualsAndHashCode;
40 import lombok.ToString;
41
42 /**
43 * Github status.
44 *
45 * <p>The status exposes all available properties through its
46 * {@code json()} method. However, it is recommended to use its
47 * "smart" decorator, which helps you to get access to all JSON properties,
48 * for example:
49 *
50 * <pre> URL url = new Status.Smart(status).url();</pre>
51 *
52 * @author Marcin Cylke (marcin.cylke+github@gmail.com)
53 * @version $Id: 046cf932ddea3bc2b2978234bf0b02802d8d28a2 $
54 * @since 0.23
55 * @see <a href="https://developer.github.com/v3/repos/statuses/">Repo statuses</a>
56 */
57 @Immutable
58 @SuppressWarnings("PMD.TooManyMethods")
59 public interface Statuses extends JsonReadable {
60
61 /**
62 * Associated commit.
63 * @return Commit
64 */
65 Commit commit();
66
67 /**
68 * Create new status.
69 * @param status Add this status
70 * @throws java.io.IOException If there is any I/O problem
71 * @return The added status
72 * @see <a href="https://developer.github.com/v3/repos/statuses/#create-a-status">Create a Status</a>
73 */
74 Status create(
75 final StatusCreate status
76 ) throws IOException;
77
78 /**
79 * List all statuses for a given ref.
80 * @param ref It can be a SHA, a branch name, or a tag name.
81 * @return Iterable of statuses
82 * @see <a href="https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref">List Statuses for a specific Ref</a>
83 */
84 Iterable<Status> list(
85 final String ref
86 );
87
88 /**
89 * Data to use when creating a new GitHub commit status.
90 *
91 * @author Chris Rebert (github@rebertia.com)
92 * @version $Id: 046cf932ddea3bc2b2978234bf0b02802d8d28a2 $
93 * @since 0.24
94 * @see <a href="https://developer.github.com/v3/repos/statuses/#create-a-status">Create a Status</a>
95 */
96 @ToString
97 @Loggable(Loggable.DEBUG)
98 @EqualsAndHashCode(of = {
99 "state",
100 "description",
101 "context",
102 "targeturl"
103 })
104 final class StatusCreate implements JsonReadable {
105 /**
106 * State.
107 */
108 private final transient Status.State state;
109 /**
110 * Description.
111 */
112 private final transient String description;
113 /**
114 * Context string.
115 */
116 private final transient Optional<String> context;
117 /**
118 * Target URL.
119 */
120 private final transient Optional<String> targeturl;
121
122 /**
123 * Public ctor.
124 * @param stat State
125 */
126 public StatusCreate(
127 final Status.State stat
128 ) {
129 this(
130 stat,
131 "",
132 Optional.<String>absent(),
133 Optional.<String>absent()
134 );
135 }
136
137 /**
138 * Private ctor.
139 * @param stat State
140 * @param desc Description
141 * @param cntxt Context
142 * @param target Target URL
143 * @checkstyle ParameterNumberCheck (10 lines)
144 */
145 private StatusCreate(
146 final Status.State stat,
147 final String desc,
148 final Optional<String> cntxt,
149 final Optional<String> target
150 ) {
151 this.state = stat;
152 this.description = desc;
153 this.context = cntxt;
154 this.targeturl = target;
155 }
156
157 /**
158 * Returns a StatusCreate with the given state.
159 * @param stat State
160 * @return StatusCreate
161 */
162 public StatusCreate withState(final Status.State stat) {
163 return new StatusCreate(
164 stat,
165 this.description,
166 this.context,
167 this.targeturl
168 );
169 }
170
171 /**
172 * Returns a StatusCreate with the given description.
173 * @param desc Description
174 * @return StatusCreate
175 */
176 public StatusCreate withDescription(final String desc) {
177 return new StatusCreate(
178 this.state,
179 desc,
180 this.context,
181 this.targeturl
182 );
183 }
184
185 /**
186 * Returns a StatusCreate with the given context.
187 * @param cntxt Context
188 * @return StatusCreate
189 */
190 public StatusCreate withContext(final Optional<String> cntxt) {
191 return new StatusCreate(
192 this.state,
193 this.description,
194 cntxt,
195 this.targeturl
196 );
197 }
198
199 /**
200 * Returns a StatusCreate with the given target URL.
201 * @param target Target URL
202 * @return StatusCreate
203 */
204 public StatusCreate withTargetUrl(final Optional<String> target) {
205 return new StatusCreate(
206 this.state,
207 this.description,
208 this.context,
209 target
210 );
211 }
212
213 @Override
214 public JsonObject json() {
215 final JsonObjectBuilder builder = Json.createObjectBuilder()
216 .add("state", this.state.identifier())
217 .add("description", this.description);
218 if (this.context.isPresent()) {
219 builder.add("context", this.context.get());
220 }
221 if (this.targeturl.isPresent()) {
222 builder.add("target_url", this.targeturl.get());
223 }
224 return builder.build();
225 }
226 }
227 }