View Javadoc
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.jcabi.aspects.Immutable;
33  import com.jcabi.aspects.Loggable;
34  import java.io.IOException;
35  import java.net.URL;
36  import javax.json.JsonObject;
37  import lombok.EqualsAndHashCode;
38  import lombok.ToString;
39  
40  /**
41   * Github repo commit.
42   *
43   * <p>The repo commit exposes all available properties through its
44   * {@code json()} method. However, it is recommended to use its
45   * "smart" decorator, which helps you to get access to all JSON properties,
46   * for example:
47   *
48   * <pre> URL url = new RepoCommit.Smart(commit).url();</pre>
49   *
50   * @author Paul Polishchuk (ppol@ua.fm)
51   * @version $Id: a9385e4ffeaa6d853a5582bacd10c4e77b85674d $
52   * @since 0.8
53   * @see <a href="https://developer.github.com/v3/repos/commits/">Commits API</a>
54   * @checkstyle MultipleStringLiterals (500 lines)
55   */
56  @Immutable
57  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
58  public interface RepoCommit extends Comparable<RepoCommit>, JsonReadable {
59  
60      /**
61       * The repo we're in.
62       * @return Repo
63       */
64      Repo repo();
65  
66      /**
67       * SHA of it.
68       * @return SHA
69       */
70      String sha();
71  
72      /**
73       * Smart commit.
74       */
75      @Immutable
76      @ToString
77      @Loggable(Loggable.DEBUG)
78      @EqualsAndHashCode(of = { "commit", "jsn" })
79      final class Smart implements RepoCommit {
80          /**
81           * Encapsulated repo commit.
82           */
83          private final transient RepoCommit commit;
84          /**
85           * SmartJson object for convenient JSON parsing.
86           */
87          private final transient SmartJson jsn;
88          /**
89           * Public ctor.
90           * @param cmt RepoCommit
91           */
92          public Smart(
93              final RepoCommit cmt
94          ) {
95              this.commit = cmt;
96              this.jsn = new SmartJson(cmt);
97          }
98          /**
99           * Get its message.
100          * @return Message of repo commit
101          * @throws IOException If there is any I/O problem
102          */
103         public String message() throws IOException {
104             return this.jsn.json()
105                 .getJsonObject("commit")
106                 .getString("message");
107         }
108         /**
109          * Get its URL.
110          * @return URL of repo commit
111          * @throws IOException If there is any I/O problem
112          */
113         public URL url() throws IOException {
114             return new URL(this.jsn.text("url"));
115         }
116         /**
117          * Returns the login of the author.
118          * @return The login
119          * @throws IOException If there is any I/O problem
120          * @since 1.1
121          */
122         public String author() throws IOException {
123             return this.jsn.json()
124                 .getJsonObject("commit")
125                 .getJsonObject("author")
126                 .getString("name");
127         }
128         /**
129          * Returns TRUE if the commit is verified.
130          * @return TRUE if verified
131          * @throws IOException If there is any I/O problem
132          * @since 1.1
133          */
134         public boolean isVerified() throws IOException {
135             return this.jsn.json()
136                 .getJsonObject("commit")
137                 .getJsonObject("verification")
138                 .getBoolean("verified");
139         }
140         @Override
141         public Repo repo() {
142             return this.commit.repo();
143         }
144         @Override
145         public String sha() {
146             return this.commit.sha();
147         }
148         @Override
149         public JsonObject json() throws IOException {
150             return this.commit.json();
151         }
152         @Override
153         public int compareTo(
154             final RepoCommit obj
155         ) {
156             return this.commit.compareTo(obj);
157         }
158     }
159 
160 }