1 | 86 | |
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.google.common.base.Optional; |
33 | |
import com.jcabi.aspects.Immutable; |
34 | |
import com.jcabi.aspects.Loggable; |
35 | |
import com.jcabi.github.Coordinates; |
36 | |
import com.jcabi.github.Event; |
37 | |
import com.jcabi.github.Issue; |
38 | |
import com.jcabi.github.IssueLabels; |
39 | |
import com.jcabi.github.Label; |
40 | |
import com.jcabi.xml.XML; |
41 | |
import java.io.IOException; |
42 | |
import java.util.Collection; |
43 | |
import java.util.HashSet; |
44 | |
import java.util.Set; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
import lombok.ToString; |
47 | |
import org.xembly.Directives; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@Immutable |
57 | |
@Loggable(Loggable.DEBUG) |
58 | 0 | @ToString |
59 | 0 | @EqualsAndHashCode(of = { "storage", "repo", "ticket" }) |
60 | 12 | final class MkIssueLabels implements IssueLabels { |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private final transient MkStorage storage; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private final transient String self; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private final transient Coordinates repo; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
private final transient int ticket; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
MkIssueLabels( |
92 | |
final MkStorage stg, |
93 | |
final String login, |
94 | |
final Coordinates rep, |
95 | |
final int issue |
96 | 42 | ) throws IOException { |
97 | 42 | this.storage = stg; |
98 | 42 | this.self = login; |
99 | 42 | this.repo = rep; |
100 | 42 | this.ticket = issue; |
101 | 84 | this.storage.apply( |
102 | 42 | new Directives().xpath( |
103 | 42 | String.format( |
104 | |
|
105 | |
"/github/repos/repo[@coords='%s']/issues/issue[number='%d']", |
106 | 42 | rep, this.ticket |
107 | |
) |
108 | 42 | ).addIf("labels") |
109 | |
); |
110 | 42 | } |
111 | |
|
112 | |
@Override |
113 | |
public Issue issue() { |
114 | 2 | return new MkIssue(this.storage, this.self, this.repo, this.ticket); |
115 | |
} |
116 | |
|
117 | |
@Override |
118 | |
public void add(final Iterable<String> labels |
119 | |
) throws IOException { |
120 | 12 | final Collection<String> existing = this.labels(); |
121 | 6 | final Set<String> added = new HashSet<String>(); |
122 | 6 | final Directives dirs = new Directives().xpath(this.xpath()); |
123 | 6 | for (final String label : labels) { |
124 | 6 | dirs.add("label").set(label).up(); |
125 | 6 | if (!existing.contains(label)) { |
126 | 6 | added.add(label); |
127 | |
} |
128 | 6 | } |
129 | 6 | this.storage.apply(dirs); |
130 | 6 | if (!added.isEmpty()) { |
131 | 6 | final MkIssueEvents events = new MkIssueEvents( |
132 | |
this.storage, |
133 | |
this.self, |
134 | |
this.repo |
135 | |
); |
136 | 6 | for (final String label : added) { |
137 | 12 | events.create( |
138 | |
Event.LABELED, |
139 | |
this.ticket, |
140 | |
this.self, |
141 | 6 | Optional.of(label) |
142 | |
); |
143 | 6 | } |
144 | |
} |
145 | 6 | } |
146 | |
|
147 | |
@Override |
148 | |
public void replace(final Iterable<String> labels |
149 | |
) throws IOException { |
150 | 0 | this.clear(); |
151 | 0 | this.add(labels); |
152 | 0 | } |
153 | |
|
154 | |
@Override |
155 | |
public Iterable<Label> iterate() { |
156 | 129 | return new MkIterable<Label>( |
157 | |
this.storage, |
158 | 43 | String.format("%s/*", this.xpath()), |
159 | 47 | new MkIterable.Mapping<Label>() { |
160 | |
@Override |
161 | |
public Label map(final XML xml) { |
162 | 8 | return new MkLabel( |
163 | 4 | MkIssueLabels.this.storage, |
164 | 4 | MkIssueLabels.this.self, |
165 | 4 | MkIssueLabels.this.repo, |
166 | 4 | xml.xpath("./text()").get(0) |
167 | |
); |
168 | |
} |
169 | |
} |
170 | |
); |
171 | |
} |
172 | |
|
173 | |
@Override |
174 | |
public void remove(final String name |
175 | |
) throws IOException { |
176 | 2 | if (this.labels().contains(name)) { |
177 | 2 | this.storage.apply( |
178 | 1 | new Directives().xpath( |
179 | 1 | String.format("%s/label[.='%s']", this.xpath(), name) |
180 | 1 | ).remove() |
181 | |
); |
182 | 1 | new MkIssueEvents( |
183 | |
this.storage, |
184 | |
this.self, |
185 | |
this.repo |
186 | 1 | ).create( |
187 | |
Event.UNLABELED, |
188 | |
this.ticket, |
189 | |
this.self, |
190 | 1 | Optional.of(name) |
191 | |
); |
192 | |
} |
193 | 1 | } |
194 | |
|
195 | |
@Override |
196 | |
public void clear() throws IOException { |
197 | 0 | for (final String label : this.labels()) { |
198 | 0 | this.remove(label); |
199 | 0 | } |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private String xpath() { |
207 | 100 | return String.format( |
208 | |
"/github/repos/repo[@coords='%s']/issues/issue[number='%d']/labels", |
209 | 50 | this.repo, this.ticket |
210 | |
); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
private Collection<String> labels() { |
218 | 7 | final Set<String> labels = new HashSet<String>(); |
219 | 7 | for (final Label label : this.iterate()) { |
220 | 1 | labels.add(label.name()); |
221 | 1 | } |
222 | 7 | return labels; |
223 | |
} |
224 | |
} |