|
11 | 11 |
|
12 | 12 | import org.slf4j.Logger;
|
13 | 13 |
|
14 |
| -import se.bjurr.prnfb.http.ClientKeyStore; |
15 |
| -import se.bjurr.prnfb.listener.PrnfbPullRequestAction; |
16 |
| -import se.bjurr.prnfb.settings.PrnfbNotification; |
17 |
| - |
18 | 14 | import com.atlassian.bitbucket.pull.PullRequest;
|
19 | 15 | import com.atlassian.bitbucket.repository.RepositoryService;
|
20 | 16 | import com.atlassian.bitbucket.server.ApplicationPropertiesService;
|
|
23 | 19 | import com.google.common.annotations.VisibleForTesting;
|
24 | 20 | import com.google.common.base.Supplier;
|
25 | 21 |
|
| 22 | +import se.bjurr.prnfb.http.ClientKeyStore; |
| 23 | +import se.bjurr.prnfb.listener.PrnfbPullRequestAction; |
| 24 | +import se.bjurr.prnfb.settings.PrnfbNotification; |
| 25 | + |
26 | 26 | public class PrnfbRenderer {
|
27 | 27 | private static final Logger LOG = getLogger(PrnfbRenderer.class);
|
28 | 28 |
|
@@ -54,60 +54,57 @@ public class PrnfbRenderer {
|
54 | 54 | this.securityService = securityService;
|
55 | 55 | }
|
56 | 56 |
|
57 |
| - public String render(String string, Boolean forUrl, ClientKeyStore clientKeyStore, |
58 |
| - Boolean shouldAcceptAnyCertificate) { |
59 |
| - string = renderVariable(string, false, clientKeyStore, shouldAcceptAnyCertificate, EVERYTHING_URL); |
| 57 | + private boolean containsVariable(String string, final String regExpStr) { |
| 58 | + return string.contains(regExpStr.replaceAll("\\\\", "")); |
| 59 | + } |
60 | 60 |
|
61 |
| - for (final PrnfbVariable variable : PrnfbVariable.values()) { |
62 |
| - try { |
63 |
| - string = renderVariable(string, forUrl, clientKeyStore, shouldAcceptAnyCertificate, variable); |
64 |
| - } catch (Exception e) { |
65 |
| - LOG.error("Error when resolving " + variable, e); |
66 |
| - } |
| 61 | + @VisibleForTesting |
| 62 | + String getRenderedStringResolved(String string, Boolean forUrl, final String regExpStr, String resolved) { |
| 63 | + String replaceWith = null; |
| 64 | + try { |
| 65 | + replaceWith = forUrl ? encode(resolved, UTF_8.name()) : resolved; |
| 66 | + } catch (UnsupportedEncodingException e) { |
| 67 | + propagate(e); |
| 68 | + } |
| 69 | + try { |
| 70 | + string = string.replaceAll(regExpStr, replaceWith); |
| 71 | + } catch (IllegalArgumentException e) { |
| 72 | + throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e); |
67 | 73 | }
|
68 | 74 | return string;
|
69 | 75 | }
|
70 | 76 |
|
71 |
| - private boolean containsVariable(String string, final String regExpStr) { |
72 |
| - return string.contains(regExpStr.replaceAll("\\\\", "")); |
| 77 | + @VisibleForTesting |
| 78 | + String regexp(PrnfbVariable variable) { |
| 79 | + return "\\$\\{" + variable.name() + "\\}"; |
73 | 80 | }
|
74 | 81 |
|
75 |
| - private String getRenderedString(String string, Boolean forUrl, ClientKeyStore clientKeyStore, |
76 |
| - Boolean shouldAcceptAnyCertificate, final PrnfbVariable variable, final String regExpStr) |
77 |
| - throws UnsupportedEncodingException { |
78 |
| - String resolved = variable.resolve(this.pullRequest, this.pullRequestAction, this.applicationUser, |
79 |
| - this.repositoryService, this.propertiesService, this.prnfbNotification, this.variables, clientKeyStore, |
80 |
| - shouldAcceptAnyCertificate, this.securityService); |
81 |
| - return getRenderedStringResolved(string, forUrl, regExpStr, resolved); |
| 82 | + public String render(String string, Boolean forUrl, ClientKeyStore clientKeyStore, |
| 83 | + Boolean shouldAcceptAnyCertificate) { |
| 84 | + string = renderVariable(string, false, clientKeyStore, shouldAcceptAnyCertificate, EVERYTHING_URL); |
| 85 | + |
| 86 | + for (final PrnfbVariable variable : PrnfbVariable.values()) { |
| 87 | + string = renderVariable(string, forUrl, clientKeyStore, shouldAcceptAnyCertificate, variable); |
| 88 | + } |
| 89 | + return string; |
82 | 90 | }
|
83 | 91 |
|
84 | 92 | private String renderVariable(String string, Boolean forUrl, ClientKeyStore clientKeyStore,
|
85 | 93 | Boolean shouldAcceptAnyCertificate, final PrnfbVariable variable) {
|
86 | 94 | final String regExpStr = regexp(variable);
|
87 | 95 | if (containsVariable(string, regExpStr)) {
|
| 96 | + String resolved = ""; |
88 | 97 | try {
|
89 |
| - string = getRenderedString(string, forUrl, clientKeyStore, shouldAcceptAnyCertificate, variable, regExpStr); |
90 |
| - } catch (UnsupportedEncodingException e) { |
91 |
| - propagate(e); |
| 98 | + resolved = variable.resolve(pullRequest, pullRequestAction, applicationUser, repositoryService, propertiesService, |
| 99 | + prnfbNotification, variables, clientKeyStore, shouldAcceptAnyCertificate, securityService); |
| 100 | + if (resolved == null) { |
| 101 | + resolved = ""; |
| 102 | + } |
| 103 | + } catch (Exception e) { |
| 104 | + LOG.error("Error when resolving " + variable, e); |
92 | 105 | }
|
| 106 | + return getRenderedStringResolved(string, forUrl, regExpStr, resolved); |
93 | 107 | }
|
94 | 108 | return string;
|
95 | 109 | }
|
96 |
| - |
97 |
| - @VisibleForTesting |
98 |
| - String getRenderedStringResolved(String string, Boolean forUrl, final String regExpStr, String resolved) |
99 |
| - throws UnsupportedEncodingException { |
100 |
| - String replaceWith = forUrl ? encode(resolved, UTF_8.name()) : resolved; |
101 |
| - try { |
102 |
| - string = string.replaceAll(regExpStr, replaceWith); |
103 |
| - } catch (IllegalArgumentException e) { |
104 |
| - throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e); |
105 |
| - } |
106 |
| - return string; |
107 |
| - } |
108 |
| - |
109 |
| - @VisibleForTesting |
110 |
| - String regexp(PrnfbVariable variable) { |
111 |
| - return "\\$\\{" + variable.name() + "\\}"; |
112 |
| - } |
113 | 110 | }
|
0 commit comments