View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.aspects.Immutable;
8   import com.jcabi.aspects.Loggable;
9   import jakarta.json.JsonObject;
10  import java.io.IOException;
11  import java.net.URI;
12  import java.net.URISyntaxException;
13  import java.net.URL;
14  import lombok.EqualsAndHashCode;
15  import lombok.ToString;
16  
17  /**
18   * GitHub repo commit.
19   *
20   * <p>The repo commit exposes all available properties through its
21   * {@code json()} method. However, it is recommended to use its
22   * "smart" decorator, which helps you to get access to all JSON properties,
23   * for example:
24   *
25   * <pre> URL url = new RepoCommit.Smart(commit).url();</pre>
26   *
27   * @see <a href="https://developer.github.com/v3/repos/commits/">Commits API</a>
28   * @since 0.8
29   * @checkstyle MultipleStringLiterals (500 lines)
30   */
31  @Immutable
32  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
33  public interface RepoCommit extends Comparable<RepoCommit>, JsonReadable {
34  
35      /**
36       * The repo we're in.
37       * @return Repo
38       */
39      Repo repo();
40  
41      /**
42       * SHA of it.
43       * @return SHA
44       */
45      String sha();
46  
47      /**
48       * Smart commit.
49       * @since 0.8
50       */
51      @Immutable
52      @ToString
53      @Loggable(Loggable.DEBUG)
54      @EqualsAndHashCode(of = { "commit", "jsn" })
55      final class Smart implements RepoCommit {
56          /**
57           * Encapsulated repo commit.
58           */
59          private final transient RepoCommit commit;
60  
61          /**
62           * SmartJson object for convenient JSON parsing.
63           */
64          private final transient SmartJson jsn;
65  
66          /**
67           * Public ctor.
68           * @param cmt RepoCommit
69           */
70          public Smart(
71              final RepoCommit cmt
72          ) {
73              this.commit = cmt;
74              this.jsn = new SmartJson(cmt);
75          }
76  
77          /**
78           * Get its message.
79           * @return Message of repo commit
80           * @throws IOException If there is any I/O problem
81           */
82          public String message() throws IOException {
83              return this.jsn.json()
84                  .getJsonObject("commit")
85                  .getString("message");
86          }
87  
88          /**
89           * Get its URL.
90           * @return URL of repo commit
91           * @throws IOException If there is any I/O problem
92           */
93          public URL url() throws IOException {
94              try {
95                  return new URI(this.jsn.text("url")).toURL();
96              } catch (final URISyntaxException ex) {
97                  throw new IllegalArgumentException(ex);
98              }
99          }
100 
101         /**
102          * Returns the login of the author.
103          * @return The login
104          * @throws IOException If there is any I/O problem
105          * @since 1.1
106          */
107         public String author() throws IOException {
108             return this.jsn.json()
109                 .getJsonObject("commit")
110                 .getJsonObject("author")
111                 .getString("name");
112         }
113 
114         /**
115          * Returns TRUE if the commit is verified.
116          * @return TRUE if verified
117          * @throws IOException If there is any I/O problem
118          * @since 1.1
119          */
120         public boolean isVerified() throws IOException {
121             return this.jsn.json()
122                 .getJsonObject("commit")
123                 .getJsonObject("verification")
124                 .getBoolean("verified");
125         }
126 
127         @Override
128         public Repo repo() {
129             return this.commit.repo();
130         }
131 
132         @Override
133         public String sha() {
134             return this.commit.sha();
135         }
136 
137         @Override
138         public JsonObject json() throws IOException {
139             return this.commit.json();
140         }
141 
142         @Override
143         public int compareTo(
144             final RepoCommit obj
145         ) {
146             return this.commit.compareTo(obj);
147         }
148     }
149 
150 }