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 javax.json.JsonObject;
36  import lombok.EqualsAndHashCode;
37  import lombok.ToString;
38  
39  /**
40   * GitHub pull request ref.
41   *
42   * @author Chris Rebert (github@rebertia.com)
43   * @version $Id: d79104fadaf83ec068e5012e6d79394acaa343c1 $
44   * @since 0.24
45   * @see <a href="https://developer.github.com/v3/pulls/#get-a-single-pull-request">Get a single pull request API</a>
46   *
47   */
48  @Immutable
49  public interface PullRef extends JsonReadable {
50      /**
51       * Get the repository which its commit is in.
52       * @return Repo
53       */
54      Repo repo();
55  
56      /**
57       * Get its ref.
58       * @return Ref
59       * @throws IOException If there is any I/O problem
60       */
61      String ref() throws IOException;
62  
63      /**
64       * Get its commit SHA.
65       * @return Commit SHA
66       * @throws IOException If there is any I/O problem
67       */
68      String sha() throws IOException;
69  
70      /**
71       * Smart pull request ref with extra features.
72       */
73      @Immutable
74      @ToString
75      @Loggable(Loggable.DEBUG)
76      @EqualsAndHashCode(of = { "pullref", "jsn" })
77      final class Smart implements PullRef {
78          /**
79           * Encapsulated pull request ref.
80           */
81          private final transient PullRef pullref;
82          /**
83           * SmartJson object for convenient JSON parsing.
84           */
85          private final transient SmartJson jsn;
86  
87          /**
88           * Public ctor.
89           * @param pref Pull request ref
90           */
91          public Smart(
92              final PullRef pref
93          ) {
94              this.pullref = pref;
95              this.jsn = new SmartJson(pref);
96          }
97  
98          @Override
99          public Repo repo() {
100             return this.pullref.repo();
101         }
102 
103         @Override
104         public String ref() throws IOException {
105             return this.pullref.ref();
106         }
107 
108         @Override
109         public String sha() throws IOException {
110             return this.pullref.sha();
111         }
112 
113         /**
114          * Gets the user who owns the repository which its commit is in.
115          * @return User
116          * @throws IOException If there is any I/O problem
117          */
118         public User user() throws IOException {
119             return this.pullref.repo().github().users().get(
120                 this.jsn.value("user", JsonObject.class).getString("login")
121             );
122         }
123 
124         /**
125          * Get its label. Normally of the form "user:branch".
126          * @return Label string
127          * @throws IOException If there is any I/O problem
128          */
129         public String label() throws IOException {
130             return this.jsn.text("label");
131         }
132 
133         /**
134          * Get its commit.
135          * @return Commit
136          * @throws IOException If there is any I/O problem
137          */
138         public Commit commit() throws IOException {
139             return this.repo().git().commits().get(this.sha());
140         }
141 
142         @Override
143         public JsonObject json() throws IOException {
144             return this.pullref.json();
145         }
146     }
147 }