1 | 88 | |
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.Github; |
36 | |
import com.jcabi.github.Release; |
37 | |
import com.jcabi.github.Releases; |
38 | |
import com.jcabi.github.Repo; |
39 | |
import com.jcabi.xml.XML; |
40 | |
import java.io.IOException; |
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 MkReleases implements Releases { |
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 MkReleases( |
80 | |
final MkStorage stg, |
81 | |
final String login, |
82 | |
final Coordinates rep |
83 | 33 | ) throws IOException { |
84 | 33 | this.storage = stg; |
85 | 33 | this.self = login; |
86 | 33 | this.coords = rep; |
87 | 66 | this.storage.apply( |
88 | 33 | new Directives().xpath( |
89 | 33 | String.format( |
90 | |
"/github/repos/repo[@coords='%s']", |
91 | |
this.coords |
92 | |
) |
93 | 33 | ).addIf("releases") |
94 | |
); |
95 | 33 | } |
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<Release> iterate() { |
104 | 30 | return new MkIterable<Release>( |
105 | |
this.storage, |
106 | 10 | String.format("%s/release", this.xpath()), |
107 | 20 | new MkIterable.Mapping<Release>() { |
108 | |
@Override |
109 | |
public Release map(final XML xml) { |
110 | 20 | return MkReleases.this.get( |
111 | 10 | Integer.parseInt(xml.xpath("id/text()").get(0)) |
112 | |
); |
113 | |
} |
114 | |
} |
115 | |
); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public Release get(final int number) { |
120 | 88 | return new MkRelease(this.storage, this.self, this.coords, number); |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public Release create( |
125 | |
final String tag |
126 | |
) throws IOException { |
127 | 66 | this.storage.lock(); |
128 | |
final int number; |
129 | |
try { |
130 | 66 | number = 1 + this.storage.xml().xpath( |
131 | 33 | String.format("%s/release/id/text()", this.xpath()) |
132 | 33 | ).size(); |
133 | 66 | this.storage.apply( |
134 | 33 | new Directives().xpath(this.xpath()).add("release") |
135 | 33 | .add("id").set(Integer.toString(number)).up() |
136 | 33 | .add("tag_name").set(tag).up() |
137 | 33 | .add("target_commitish").set("master").up() |
138 | 33 | .add("name").set("").up() |
139 | 33 | .add("body").set("").up() |
140 | 33 | .add("draft").set("true").up() |
141 | 33 | .add("prerelease").set("false").up() |
142 | 33 | .add("created_at").set(new Github.Time().toString()).up() |
143 | 33 | .add("published_at").set(new Github.Time().toString()).up() |
144 | 33 | .add("url").set("http://localhost/1").up() |
145 | 33 | .add("html_url").set("http://localhost/2").up() |
146 | 33 | .add("assets_url").set("http://localhost/3").up() |
147 | 33 | .add("upload_url").set("http://localhost/4").up() |
148 | |
); |
149 | |
} finally { |
150 | 33 | this.storage.unlock(); |
151 | 33 | } |
152 | 33 | return this.get(number); |
153 | |
} |
154 | |
|
155 | |
@Override |
156 | |
public void remove(final int number) throws IOException { |
157 | 2 | this.storage.lock(); |
158 | |
try { |
159 | 2 | this.storage.apply( |
160 | 1 | new Directives().xpath( |
161 | 1 | String.format("%s/release[id='%d']", this.xpath(), number) |
162 | 1 | ).remove() |
163 | |
); |
164 | |
} finally { |
165 | 1 | this.storage.unlock(); |
166 | 1 | } |
167 | 1 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
private String xpath() { |
174 | 77 | return String.format( |
175 | |
"/github/repos/repo[@coords='%s']/releases", |
176 | |
this.coords |
177 | |
); |
178 | |
} |
179 | |
} |