| 1 | 36 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.github.mock; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.github.Coordinates; |
| 35 | |
import com.jcabi.github.Issue; |
| 36 | |
import com.jcabi.github.Pull; |
| 37 | |
import com.jcabi.github.Pulls; |
| 38 | |
import com.jcabi.github.Repo; |
| 39 | |
import com.jcabi.xml.XML; |
| 40 | |
import java.io.IOException; |
| 41 | |
import java.util.Map; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
import lombok.ToString; |
| 44 | |
import org.xembly.Directives; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Immutable |
| 54 | |
@Loggable(Loggable.DEBUG) |
| 55 | 0 | @ToString |
| 56 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
| 57 | |
final class MkPulls implements Pulls { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private static final String USER_BRANCH_SEP = ":"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private final transient MkStorage storage; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private final transient String self; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private final transient Coordinates coords; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
MkPulls( |
| 88 | |
final MkStorage stg, |
| 89 | |
final String login, |
| 90 | |
final Coordinates rep |
| 91 | 18 | ) throws IOException { |
| 92 | 18 | this.storage = stg; |
| 93 | 18 | this.self = login; |
| 94 | 18 | this.coords = rep; |
| 95 | 36 | this.storage.apply( |
| 96 | 18 | new Directives().xpath( |
| 97 | 18 | String.format( |
| 98 | |
"/github/repos/repo[@coords='%s']", |
| 99 | |
this.coords |
| 100 | |
) |
| 101 | 18 | ).addIf("pulls") |
| 102 | |
); |
| 103 | 18 | } |
| 104 | |
|
| 105 | |
@Override |
| 106 | |
public Repo repo() { |
| 107 | 36 | return new MkRepo(this.storage, this.self, this.coords); |
| 108 | |
} |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public Pull get(final int number) { |
| 112 | 36 | return new MkPull(this.storage, this.self, this.coords, number); |
| 113 | |
} |
| 114 | |
|
| 115 | |
@Override |
| 116 | |
public Pull create( |
| 117 | |
final String title, |
| 118 | |
final String head, |
| 119 | |
final String base |
| 120 | |
) throws IOException { |
| 121 | 36 | if (head.isEmpty()) { |
| 122 | 0 | throw new IllegalArgumentException("head cannot be empty!"); |
| 123 | |
} |
| 124 | 18 | if (base.isEmpty()) { |
| 125 | 0 | throw new IllegalArgumentException("base cannot be empty!"); |
| 126 | |
} |
| 127 | |
final String canonical; |
| 128 | 18 | if (head.contains(MkPulls.USER_BRANCH_SEP)) { |
| 129 | 0 | canonical = head; |
| 130 | |
} else { |
| 131 | 36 | canonical = String.format( |
| 132 | |
"%s%s%s", |
| 133 | 18 | this.coords.user(), |
| 134 | |
MkPulls.USER_BRANCH_SEP, |
| 135 | |
head |
| 136 | |
); |
| 137 | |
} |
| 138 | 18 | this.storage.lock(); |
| 139 | |
final int number; |
| 140 | |
try { |
| 141 | 18 | final Issue issue = this.repo().issues().create(title, "some body"); |
| 142 | 18 | number = issue.number(); |
| 143 | 36 | this.storage.apply( |
| 144 | 18 | new Directives().xpath(this.xpath()).add("pull") |
| 145 | 18 | .add("number").set(Integer.toString(number)).up() |
| 146 | 18 | .add("head").set(canonical).up() |
| 147 | 18 | .add("base").set(base).up() |
| 148 | 18 | .add("user") |
| 149 | 18 | .add("login").set(this.self) |
| 150 | 18 | .up() |
| 151 | |
); |
| 152 | |
} finally { |
| 153 | 18 | this.storage.unlock(); |
| 154 | 18 | } |
| 155 | 18 | return this.get(number); |
| 156 | |
} |
| 157 | |
|
| 158 | |
@Override |
| 159 | |
public Iterable<Pull> iterate(final Map<String, String> params) { |
| 160 | 0 | return new MkIterable<Pull>( |
| 161 | |
this.storage, |
| 162 | 0 | String.format("%s/pull", this.xpath()), |
| 163 | 0 | new MkIterable.Mapping<Pull>() { |
| 164 | |
@Override |
| 165 | |
public Pull map(final XML xml) { |
| 166 | 0 | return MkPulls.this.get( |
| 167 | 0 | Integer.parseInt(xml.xpath("number/text()").get(0)) |
| 168 | |
); |
| 169 | |
} |
| 170 | |
} |
| 171 | |
); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
private String xpath() { |
| 179 | 18 | return String.format( |
| 180 | |
"/github/repos/repo[@coords='%s']/pulls", |
| 181 | |
this.coords |
| 182 | |
); |
| 183 | |
} |
| 184 | |
} |