22 * -\-\-
33 * github-api
44 * --
5- * Copyright (C) 2016 - 2020 Spotify AB
5+ * Copyright (C) 2016 - 2021 Spotify AB
66 * --
77 * Licensed under the Apache License, Version 2.0 (the "License");
88 * you may not use this file except in compliance with the License.
2121package com .spotify .github .v3 .repos ;
2222
2323import com .fasterxml .jackson .core .JsonParser ;
24+ import com .fasterxml .jackson .core .type .TypeReference ;
2425import com .fasterxml .jackson .databind .DeserializationContext ;
2526import com .fasterxml .jackson .databind .JsonDeserializer ;
26- import com .fasterxml .jackson .databind .JsonNode ;
27- import com .spotify .github .v3 .git .ImmutableShaLink ;
2827import java .io .IOException ;
2928import java .net .URI ;
3029import java .net .URISyntaxException ;
3130import java .net .URLEncoder ;
3231import java .nio .charset .StandardCharsets ;
32+ import java .util .Optional ;
3333
34- public class BranchDeserializer extends JsonDeserializer <ImmutableBranch > {
34+ public class BranchProtectionUrlDeserializer extends JsonDeserializer <Optional < URI > > {
3535
36- private URI fixInvalidGithubUrl (final String invalidUrl ) throws IOException {
36+ private URI fixInvalidGithubUrl (final String invalidUrl ) {
3737 // There's a bug in github where it gives you back non-url-encoded characters
3838 // in the protection_url field. For example if your branch has a single "%" in its name.
3939 // As of this writing, the protection URL looks something like this
@@ -48,29 +48,20 @@ private URI fixInvalidGithubUrl(final String invalidUrl) throws IOException {
4848 }
4949
5050 @ Override
51- public ImmutableBranch deserialize (final JsonParser jsonParser ,
52- final DeserializationContext deserializationContext )
51+ public Optional < URI > deserialize (
52+ final JsonParser jsonParser , final DeserializationContext deserializationContext )
5353 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 ();
54+
55+ TypeReference <Optional <String >> ref = new TypeReference <>() {};
56+ Optional <String > protectionUrlStringOpt = jsonParser .readValueAs (ref );
57+
58+ return protectionUrlStringOpt .map (
59+ protectionUrlString -> {
60+ try {
61+ return new URI (protectionUrlString );
62+ } catch (URISyntaxException e ) {
63+ return fixInvalidGithubUrl (protectionUrlString );
64+ }
65+ });
7566 }
7667}
0 commit comments