Skip to content

Commit 981cb7c

Browse files
committed
#1145: persist commit fields in MkCommits.create
MkCommits.create(JsonObject) used to delegate straight to get(sha) without applying any Directives, so the commit node was never inserted into the mock storage XML. Any subsequent call to MkCommit.json() (or any other method that reads from the storage) raised NodeNotFoundException because the XPath /github/repos/repo/git/commits/commit[sha=...] selected nothing. The fix walks the input JsonObject and writes every key as an element under the new <commit> node, mirroring how MkBranches.create and MkReferences.create persist their inputs. Arrays are stored with the @array attribute that JsonNode already understands, so MkCommit.json() round-trips them as JsonArray. Nested objects (e.g. author) are stored as nested elements.
1 parent 7c70469 commit 981cb7c

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

src/main/java/com/jcabi/github/mock/MkCommits.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import com.jcabi.github.Repo;
1313
import com.jcabi.github.Statuses;
1414
import jakarta.json.JsonObject;
15+
import jakarta.json.JsonString;
16+
import jakarta.json.JsonValue;
1517
import java.io.IOException;
18+
import java.util.Map;
1619
import lombok.EqualsAndHashCode;
1720
import org.xembly.Directives;
1821

@@ -74,7 +77,28 @@ public Repo repo() {
7477
@Override
7578
public Commit create(
7679
final JsonObject params
77-
) {
80+
) throws IOException {
81+
final Directives dirs = new Directives().xpath(this.xpath()).add("commit");
82+
for (final Map.Entry<String, JsonValue> entry : params.entrySet()) {
83+
final JsonValue value = entry.getValue();
84+
if (value.getValueType() == JsonValue.ValueType.ARRAY) {
85+
dirs.add(entry.getKey()).attr("array", "true");
86+
for (final JsonValue item : value.asJsonArray()) {
87+
dirs.add("item").set(MkCommits.asText(item)).up();
88+
}
89+
dirs.up();
90+
} else if (value.getValueType() == JsonValue.ValueType.OBJECT) {
91+
dirs.add(entry.getKey());
92+
for (final Map.Entry<String, JsonValue> sub
93+
: value.asJsonObject().entrySet()) {
94+
dirs.add(sub.getKey()).set(MkCommits.asText(sub.getValue())).up();
95+
}
96+
dirs.up();
97+
} else {
98+
dirs.add(entry.getKey()).set(MkCommits.asText(value)).up();
99+
}
100+
}
101+
this.storage.apply(dirs);
78102
return this.get(params.getString("sha"));
79103
}
80104

@@ -91,4 +115,30 @@ public Statuses statuses(
91115
) {
92116
return new MkStatuses(this.get(sha));
93117
}
118+
119+
/**
120+
* XPath of the commits element in the storage XML.
121+
* @return XPath
122+
*/
123+
private String xpath() {
124+
return String.format(
125+
"/github/repos/repo[@coords='%s']/git/commits",
126+
this.coords
127+
);
128+
}
129+
130+
/**
131+
* Render JSON value as plain text suitable for XML storage.
132+
* @param value JSON value
133+
* @return Plain string
134+
*/
135+
private static String asText(final JsonValue value) {
136+
final String result;
137+
if (value.getValueType() == JsonValue.ValueType.STRING) {
138+
result = ((JsonString) value).getString();
139+
} else {
140+
result = value.toString();
141+
}
142+
return result;
143+
}
94144
}

0 commit comments

Comments
 (0)