1 | 54 | |
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 | |
|
31 | |
package com.jcabi.github.mock; |
32 | |
|
33 | |
import com.jcabi.aspects.Immutable; |
34 | |
import com.jcabi.aspects.Loggable; |
35 | |
import com.jcabi.github.Coordinates; |
36 | |
import com.jcabi.github.Reference; |
37 | |
import com.jcabi.github.References; |
38 | |
import com.jcabi.github.Repo; |
39 | |
import com.jcabi.xml.XML; |
40 | |
import java.io.IOException; |
41 | |
import lombok.EqualsAndHashCode; |
42 | |
import org.xembly.Directives; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
@Immutable |
51 | |
@Loggable(Loggable.DEBUG) |
52 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
53 | |
final class MkReferences implements References { |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private final transient MkStorage storage; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private final transient String self; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private final transient Coordinates coords; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
MkReferences( |
78 | |
final MkStorage stg, |
79 | |
final String login, |
80 | |
final Coordinates rep |
81 | 17 | ) throws IOException { |
82 | 17 | this.storage = stg; |
83 | 17 | this.self = login; |
84 | 17 | this.coords = rep; |
85 | 34 | this.storage.apply( |
86 | 17 | new Directives().xpath( |
87 | 17 | String.format( |
88 | |
"/github/repos/repo[@coords='%s']/git", |
89 | |
this.coords |
90 | |
) |
91 | 17 | ).addIf("refs") |
92 | |
); |
93 | 17 | } |
94 | |
|
95 | |
@Override |
96 | |
public Repo repo() { |
97 | 2 | return new MkRepo(this.storage, this.self, this.coords); |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public Reference create( |
102 | |
final String ref, |
103 | |
final String sha |
104 | |
) throws IOException { |
105 | 54 | this.storage.apply( |
106 | 18 | new Directives().xpath(this.xpath()).add("reference") |
107 | 18 | .add("ref").set(ref).up() |
108 | 18 | .add("sha").set(sha).up() |
109 | |
); |
110 | 18 | return this.get(ref); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public Reference get( |
115 | |
final String identifier |
116 | |
) { |
117 | 54 | return new MkReference( |
118 | |
this.storage, this.self, this.coords, identifier |
119 | |
); |
120 | |
} |
121 | |
|
122 | |
@Override |
123 | |
public Iterable<Reference> iterate() { |
124 | 9 | return new MkIterable<Reference>( |
125 | |
this.storage, |
126 | 3 | String.format("%s/reference", this.xpath()), |
127 | 8 | new MkIterable.Mapping<Reference>() { |
128 | |
@Override |
129 | |
public Reference map(final XML xml) { |
130 | 10 | return MkReferences.this.get( |
131 | 5 | xml.xpath("ref/text()").get(0) |
132 | |
); |
133 | |
} |
134 | |
} |
135 | |
); |
136 | |
} |
137 | |
|
138 | |
@Override |
139 | |
public Iterable<Reference> iterate( |
140 | |
final String subnamespace |
141 | |
) { |
142 | 12 | return new MkIterable<Reference>( |
143 | |
this.storage, |
144 | 4 | String.format( |
145 | 4 | "%s/reference/ref[starts-with(., 'refs/%s')]", this.xpath(), |
146 | |
subnamespace |
147 | |
), |
148 | 8 | new MkIterable.Mapping<Reference>() { |
149 | |
@Override |
150 | |
public Reference map(final XML xml) { |
151 | 8 | return MkReferences.this.get( |
152 | 4 | xml.xpath("text()").get(0) |
153 | |
); |
154 | |
} |
155 | |
} |
156 | |
); |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public Iterable<Reference> tags() { |
161 | 2 | return this.iterate("tags"); |
162 | |
} |
163 | |
|
164 | |
@Override |
165 | |
public Iterable<Reference> heads() { |
166 | 2 | return this.iterate("heads"); |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public void remove( |
171 | |
final String identifier |
172 | |
) throws IOException { |
173 | 3 | this.storage.apply( |
174 | 1 | new Directives().xpath( |
175 | 1 | String.format( |
176 | 1 | "%s/reference[ref='%s']", this.xpath(), identifier |
177 | |
) |
178 | 1 | ).remove() |
179 | |
); |
180 | 1 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private String xpath() { |
187 | 26 | return String.format( |
188 | |
"/github/repos/repo[@coords='%s']/git/refs", |
189 | |
this.coords |
190 | |
); |
191 | |
} |
192 | |
|
193 | |
} |