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.util.ArrayList; 36 import java.util.Collection; 37 import javax.json.JsonArray; 38 import javax.json.JsonObject; 39 import javax.json.JsonValue; 40 import lombok.EqualsAndHashCode; 41 import lombok.ToString; 42 43 /** 44 * Commits comparison. 45 * @author Alexander Sinyagin (sinyagin.alexander@gmail.com) 46 * @version $Id: faf407f9e1f4dfb35b241a391665208f57f652de $ 47 * @see <a href="https://developer.github.com/v3/repos/commits/#compare-two-commits">Compare two commits</a> 48 */ 49 @Immutable 50 public interface CommitsComparison extends JsonReadable { 51 52 /** 53 * Get a parent repository of commits. 54 * @return Repository 55 */ 56 Repo repo(); 57 58 /** 59 * Iterate over the file changes between the two commits being 60 * compared. 61 * @return Iterable of file changes 62 * @throws IOException If there is any I/O problem 63 */ 64 Iterable<FileChange> files() throws IOException; 65 66 /** 67 * Smart commits comparison with extra features. 68 */ 69 @Immutable 70 @ToString 71 @Loggable(Loggable.DEBUG) 72 @EqualsAndHashCode(of = "comparison") 73 final class Smart implements CommitsComparison { 74 75 /** 76 * Encapsulated commits comparison. 77 */ 78 private final transient CommitsComparison comparison; 79 80 /** 81 * Public ctor. 82 * @param cmprsn Commits comparison 83 */ 84 public Smart(final CommitsComparison cmprsn) { 85 this.comparison = cmprsn; 86 } 87 88 /** 89 * Get commits. 90 * @return Commits 91 * @throws IOException If there is any I/O problem 92 */ 93 public Iterable<RepoCommit> commits() throws IOException { 94 final JsonArray array = this.comparison.json() 95 .getJsonArray("commits"); 96 final Collection<RepoCommit> commits = 97 new ArrayList<>(array.size()); 98 final RepoCommits repo = this.comparison.repo().commits(); 99 for (final JsonValue value : array) { 100 commits.add( 101 repo.get(JsonObject.class.cast(value).getString("sha")) 102 ); 103 } 104 return commits; 105 } 106 107 @Override 108 public Iterable<FileChange> files() throws IOException { 109 return this.comparison.files(); 110 } 111 112 @Override 113 public Repo repo() { 114 return this.comparison.repo(); 115 } 116 117 @Override 118 public JsonObject json() throws IOException { 119 return this.comparison.json(); 120 } 121 } 122 }