| 1 | 12 | |
| 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.Comment; |
| 35 | |
import com.jcabi.github.Comments; |
| 36 | |
import com.jcabi.github.Coordinates; |
| 37 | |
import com.jcabi.github.Github; |
| 38 | |
import com.jcabi.github.Issue; |
| 39 | |
import com.jcabi.log.Logger; |
| 40 | |
import com.jcabi.xml.XML; |
| 41 | |
import java.io.IOException; |
| 42 | |
import java.util.Date; |
| 43 | |
import lombok.EqualsAndHashCode; |
| 44 | |
import lombok.ToString; |
| 45 | |
import org.xembly.Directives; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
@Immutable |
| 55 | |
@Loggable(Loggable.DEBUG) |
| 56 | 0 | @ToString |
| 57 | 0 | @EqualsAndHashCode(of = { "storage", "self", "repo", "ticket" }) |
| 58 | |
final class MkComments implements Comments { |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient MkStorage storage; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private final transient String self; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private final transient Coordinates repo; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private final transient int ticket; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
MkComments( |
| 90 | |
final MkStorage stg, |
| 91 | |
final String login, |
| 92 | |
final Coordinates rep, |
| 93 | |
final int issue |
| 94 | 6 | ) throws IOException { |
| 95 | 6 | this.storage = stg; |
| 96 | 6 | this.self = login; |
| 97 | 6 | this.repo = rep; |
| 98 | 6 | this.ticket = issue; |
| 99 | 12 | this.storage.apply( |
| 100 | 6 | new Directives().xpath( |
| 101 | 6 | String.format( |
| 102 | |
|
| 103 | |
"/github/repos/repo[@coords='%s']/issues/issue[number='%d']", |
| 104 | 6 | this.repo, this.ticket |
| 105 | |
) |
| 106 | 6 | ).addIf("comments") |
| 107 | |
); |
| 108 | 6 | } |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public Issue issue() { |
| 112 | 12 | return new MkIssue(this.storage, this.self, this.repo, this.ticket); |
| 113 | |
} |
| 114 | |
|
| 115 | |
@Override |
| 116 | |
public Comment get(final int number) { |
| 117 | 18 | return new MkComment( |
| 118 | |
this.storage, this.self, this.repo, this.ticket, number |
| 119 | |
); |
| 120 | |
} |
| 121 | |
|
| 122 | |
@Override |
| 123 | |
public Iterable<Comment> iterate(final Date since) { |
| 124 | 6 | return new MkIterable<Comment>( |
| 125 | |
this.storage, |
| 126 | 2 | String.format("%s/comment", this.xpath()), |
| 127 | 5 | new MkIterable.Mapping<Comment>() { |
| 128 | |
@Override |
| 129 | |
public Comment map(final XML xml) { |
| 130 | 6 | return MkComments.this.get( |
| 131 | 3 | Integer.parseInt(xml.xpath("number/text()").get(0)) |
| 132 | |
); |
| 133 | |
} |
| 134 | |
} |
| 135 | |
); |
| 136 | |
} |
| 137 | |
|
| 138 | |
@Override |
| 139 | |
public Comment post( |
| 140 | |
final String text |
| 141 | |
) throws IOException { |
| 142 | 12 | this.storage.lock(); |
| 143 | |
final int number; |
| 144 | |
try { |
| 145 | 6 | final String timestamp = new Github.Time().toString(); |
| 146 | 6 | number = 1 + this.storage.xml() |
| 147 | 6 | .nodes("//comment/number").size(); |
| 148 | 12 | this.storage.apply( |
| 149 | 6 | new Directives().xpath(this.xpath()).add("comment") |
| 150 | 6 | .add("number").set(Integer.toString(number)).up() |
| 151 | 6 | .add("url") |
| 152 | 6 | .set( |
| 153 | 6 | String.format( |
| 154 | |
|
| 155 | |
"https://api.jcabi-github.invalid/repos/%s/%s/issues/comments/%d", |
| 156 | 6 | this.repo.user(), |
| 157 | 6 | this.repo.repo(), |
| 158 | 6 | number |
| 159 | |
) |
| 160 | |
) |
| 161 | 6 | .up() |
| 162 | 6 | .add("body").set(text).up() |
| 163 | 6 | .add("user") |
| 164 | 6 | .add("login").set(this.self).up() |
| 165 | 6 | .up() |
| 166 | 6 | .add("created_at").set(timestamp).up() |
| 167 | 6 | .add("updated_at").set(timestamp) |
| 168 | |
); |
| 169 | |
} finally { |
| 170 | 6 | this.storage.unlock(); |
| 171 | 6 | } |
| 172 | 12 | Logger.info( |
| 173 | |
this, "comment #%d posted to issue #%d by %s: %[text]s", |
| 174 | 6 | number, this.issue().number(), this.self, text |
| 175 | |
); |
| 176 | 6 | return this.get(number); |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
private String xpath() { |
| 184 | 16 | return String.format( |
| 185 | |
|
| 186 | |
"/github/repos/repo[@coords='%s']/issues/issue[number='%d']/comments", |
| 187 | 8 | this.repo, this.ticket |
| 188 | |
); |
| 189 | |
} |
| 190 | |
|
| 191 | |
} |