Skip to content

Commit b1b4cf2

Browse files
committed
Move BranchDeserializer to its own file
1 parent 22bce81 commit b1b4cf2

File tree

2 files changed

+76
-51
lines changed

2 files changed

+76
-51
lines changed

src/main/java/com/spotify/github/v3/repos/Branch.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,11 @@
2121
package com.spotify.github.v3.repos;
2222

2323
import com.fasterxml.jackson.annotation.JsonProperty;
24-
import com.fasterxml.jackson.core.JsonParser;
25-
import com.fasterxml.jackson.databind.DeserializationContext;
26-
import com.fasterxml.jackson.databind.JsonDeserializer;
27-
import com.fasterxml.jackson.databind.JsonNode;
2824
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2925
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
3026
import com.spotify.github.GithubStyle;
31-
import com.spotify.github.v3.git.ImmutableShaLink;
3227
import com.spotify.github.v3.git.ShaLink;
33-
import java.io.IOException;
3428
import java.net.URI;
35-
import java.net.URISyntaxException;
36-
import java.net.URLEncoder;
37-
import java.nio.charset.StandardCharsets;
3829
import java.util.Optional;
3930
import javax.annotation.Nullable;
4031
import org.immutables.value.Value;
@@ -62,45 +53,3 @@ public interface Branch {
6253
Optional<URI> protectionUrl();
6354
}
6455

65-
class BranchDeserializer extends JsonDeserializer<ImmutableBranch> {
66-
67-
private URI fixInvalidGithubUrl(final String invalidUrl) throws IOException {
68-
// There's a bug in github where it gives you back non-url-encoded characters
69-
// in the protection_url field. For example if your branch has a single "%" in its name.
70-
// As of this writing, the protection URL looks something like this
71-
// https://github-server.tld/api/v3/repos/owner/repo-name/branches/branch-name/protection
72-
final String[] schemaAndPath = invalidUrl.split("//");
73-
String[] pathParts = schemaAndPath[1].split("/");
74-
for (int i = 0; i < pathParts.length; i++) {
75-
pathParts[i] = URLEncoder.encode(pathParts[i], StandardCharsets.UTF_8);
76-
}
77-
String fixedUrlString = schemaAndPath[0] + "//" + String.join("/", pathParts);
78-
return URI.create(fixedUrlString);
79-
}
80-
81-
@Override
82-
public ImmutableBranch deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext)
83-
throws IOException {
84-
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
85-
URI protectionUrl;
86-
var immutableBranchBuilder = ImmutableBranch.builder();
87-
if (node.has("protected")) {
88-
immutableBranchBuilder.isProtected(node.get("protected").asBoolean());
89-
String protectionUrlString = node.get("protection_url").asText();
90-
try {
91-
protectionUrl = new URI(protectionUrlString);
92-
} catch (URISyntaxException e) {
93-
protectionUrl = fixInvalidGithubUrl(protectionUrlString);
94-
}
95-
immutableBranchBuilder.protectionUrl(protectionUrl);
96-
}
97-
ImmutableShaLink shaLink = ImmutableShaLink.builder()
98-
.sha(node.get("commit").get("sha").asText())
99-
.url(URI.create(node.at("/commit/url").asText()))
100-
.build();
101-
return immutableBranchBuilder
102-
.name(node.get("name").textValue())
103-
.commit(shaLink)
104-
.build();
105-
}
106-
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.repos;
22+
23+
import com.fasterxml.jackson.core.JsonParser;
24+
import com.fasterxml.jackson.databind.DeserializationContext;
25+
import com.fasterxml.jackson.databind.JsonDeserializer;
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.spotify.github.v3.git.ImmutableShaLink;
28+
import java.io.IOException;
29+
import java.net.URI;
30+
import java.net.URISyntaxException;
31+
import java.net.URLEncoder;
32+
import java.nio.charset.StandardCharsets;
33+
34+
public class BranchDeserializer extends JsonDeserializer<ImmutableBranch> {
35+
36+
private URI fixInvalidGithubUrl(final String invalidUrl) throws IOException {
37+
// There's a bug in github where it gives you back non-url-encoded characters
38+
// in the protection_url field. For example if your branch has a single "%" in its name.
39+
// As of this writing, the protection URL looks something like this
40+
// https://github-server.tld/api/v3/repos/owner/repo-name/branches/branch-name/protection
41+
final String[] schemaAndPath = invalidUrl.split("//");
42+
String[] pathParts = schemaAndPath[1].split("/");
43+
for (int i = 0; i < pathParts.length; i++) {
44+
pathParts[i] = URLEncoder.encode(pathParts[i], StandardCharsets.UTF_8);
45+
}
46+
String fixedUrlString = schemaAndPath[0] + "//" + String.join("/", pathParts);
47+
return URI.create(fixedUrlString);
48+
}
49+
50+
@Override
51+
public ImmutableBranch deserialize(final JsonParser jsonParser,
52+
final DeserializationContext deserializationContext)
53+
throws IOException {
54+
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
55+
URI protectionUrl;
56+
var immutableBranchBuilder = ImmutableBranch.builder();
57+
if (node.has("protected")) {
58+
immutableBranchBuilder.isProtected(node.get("protected").asBoolean());
59+
String protectionUrlString = node.get("protection_url").asText();
60+
try {
61+
protectionUrl = new URI(protectionUrlString);
62+
} catch (URISyntaxException e) {
63+
protectionUrl = fixInvalidGithubUrl(protectionUrlString);
64+
}
65+
immutableBranchBuilder.protectionUrl(protectionUrl);
66+
}
67+
ImmutableShaLink shaLink = ImmutableShaLink.builder()
68+
.sha(node.get("commit").get("sha").asText())
69+
.url(URI.create(node.at("/commit/url").asText()))
70+
.build();
71+
return immutableBranchBuilder
72+
.name(node.get("name").textValue())
73+
.commit(shaLink)
74+
.build();
75+
}
76+
}

0 commit comments

Comments
 (0)