| 1 | 18 | |
| 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.Hook; |
| 36 | |
import com.jcabi.github.Hooks; |
| 37 | |
import com.jcabi.github.Repo; |
| 38 | |
import com.jcabi.xml.XML; |
| 39 | |
import java.io.IOException; |
| 40 | |
import java.util.Map; |
| 41 | |
import lombok.EqualsAndHashCode; |
| 42 | |
import lombok.ToString; |
| 43 | |
import org.xembly.Directives; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@Immutable |
| 52 | |
@Loggable(Loggable.DEBUG) |
| 53 | 0 | @ToString |
| 54 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
| 55 | |
final class MkHooks implements Hooks { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private final transient MkStorage storage; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
private final transient String self; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private final transient Coordinates coords; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public MkHooks( |
| 80 | |
final MkStorage stg, |
| 81 | |
final String login, |
| 82 | |
final Coordinates rep |
| 83 | 5 | ) throws IOException { |
| 84 | 5 | this.storage = stg; |
| 85 | 5 | this.self = login; |
| 86 | 5 | this.coords = rep; |
| 87 | 10 | this.storage.apply( |
| 88 | 5 | new Directives().xpath( |
| 89 | 5 | String.format( |
| 90 | |
"/github/repos/repo[@coords='%s']", |
| 91 | |
this.coords |
| 92 | |
) |
| 93 | 5 | ).addIf("hooks") |
| 94 | |
); |
| 95 | 5 | } |
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public Repo repo() { |
| 99 | 0 | return new MkRepo(this.storage, this.self, this.coords); |
| 100 | |
} |
| 101 | |
|
| 102 | |
@Override |
| 103 | |
public Iterable<Hook> iterate() { |
| 104 | 15 | return new MkIterable<Hook>( |
| 105 | |
this.storage, |
| 106 | 5 | String.format("%s/hook", this.xpath()), |
| 107 | 9 | new MkIterable.Mapping<Hook>() { |
| 108 | |
@Override |
| 109 | |
public Hook map(final XML xml) { |
| 110 | 8 | return MkHooks.this.get( |
| 111 | 4 | Integer.parseInt(xml.xpath("id/text()").get(0)) |
| 112 | |
); |
| 113 | |
} |
| 114 | |
} |
| 115 | |
); |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public Hook get(final int number) { |
| 120 | 19 | return new MkHook(this.storage, this.self, this.coords, number); |
| 121 | |
} |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public Hook create( |
| 125 | |
final String name, |
| 126 | |
final Map<String, String> config, |
| 127 | |
final boolean active |
| 128 | |
) throws IOException { |
| 129 | 10 | this.storage.lock(); |
| 130 | |
final int number; |
| 131 | |
try { |
| 132 | 10 | number = 1 + this.storage.xml().xpath( |
| 133 | 5 | String.format("%s/hook/id/text()", this.xpath()) |
| 134 | 5 | ).size(); |
| 135 | 5 | final Directives dirs = new Directives().xpath(this.xpath()) |
| 136 | 5 | .add("hook") |
| 137 | 5 | .add("id").set(String.valueOf(number)).up() |
| 138 | 5 | .add("name").set(name).up() |
| 139 | 5 | .add("active").set(Boolean.toString(active)).up() |
| 140 | 5 | .add("events").up() |
| 141 | 5 | .add("config"); |
| 142 | 5 | for (final Map.Entry<String, String> entr : config.entrySet()) { |
| 143 | 0 | dirs.add(entr.getKey()).set(entr.getValue()).up(); |
| 144 | 0 | } |
| 145 | 5 | this.storage.apply(dirs); |
| 146 | |
} finally { |
| 147 | 5 | this.storage.unlock(); |
| 148 | 5 | } |
| 149 | 5 | return this.get(number); |
| 150 | |
} |
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public void remove(final int number) throws IOException { |
| 154 | 3 | this.storage.apply( |
| 155 | 1 | new Directives().xpath( |
| 156 | 1 | String.format("%s/hook[id='%d']", this.xpath(), number) |
| 157 | 1 | ).remove() |
| 158 | |
); |
| 159 | 1 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
private String xpath() { |
| 166 | 16 | return String.format( |
| 167 | |
"/github/repos/repo[@coords='%s']/hooks", |
| 168 | |
this.coords |
| 169 | |
); |
| 170 | |
} |
| 171 | |
|
| 172 | |
} |