Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2dc736a

Browse filesBrowse files
authored
Merge pull request hub4j#1241 from bitwiseman/malformed-url
Move Malformed URL catching to core
2 parents c0d8059 + 58099fc commit 2dc736a
Copy full SHA for 2dc736a
Expand file treeCollapse file tree

13 files changed

+95
-108
lines changed

‎src/main/java/org/kohsuke/github/GHAppInstallation.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHAppInstallation.java
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.kohsuke.github.internal.EnumUtils;
66

77
import java.io.IOException;
8-
import java.net.MalformedURLException;
98
import java.net.URL;
109
import java.util.Collections;
1110
import java.util.List;
@@ -123,11 +122,7 @@ public String getRepositoriesUrl() {
123122
public PagedSearchIterable<GHRepository> listRepositories() {
124123
GitHubRequest request;
125124

126-
try {
127-
request = root().createRequest().withPreview(MACHINE_MAN).withUrlPath("/installation/repositories").build();
128-
} catch (MalformedURLException e) {
129-
throw new GHException("", e);
130-
}
125+
request = root().createRequest().withPreview(MACHINE_MAN).withUrlPath("/installation/repositories").build();
131126

132127
return new PagedSearchIterable<>(root(), request, GHAppInstallationRepositoryResult.class);
133128
}

‎src/main/java/org/kohsuke/github/GHAppInstallationsIterable.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHAppInstallationsIterable.java
+4-9Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import java.net.MalformedURLException;
43
import java.util.Iterator;
54

65
import javax.annotation.Nonnull;
@@ -20,14 +19,10 @@ public GHAppInstallationsIterable(GitHub root) {
2019
@Nonnull
2120
@Override
2221
public PagedIterator<GHAppInstallation> _iterator(int pageSize) {
23-
try {
24-
final GitHubRequest request = root.createRequest().withUrlPath(APP_INSTALLATIONS_URL).build();
25-
return new PagedIterator<>(
26-
adapt(GitHubPageIterator.create(root.getClient(), GHAppInstallationsPage.class, request, pageSize)),
27-
null);
28-
} catch (MalformedURLException e) {
29-
throw new GHException("Malformed URL", e);
30-
}
22+
final GitHubRequest request = root.createRequest().withUrlPath(APP_INSTALLATIONS_URL).build();
23+
return new PagedIterator<>(
24+
adapt(GitHubPageIterator.create(root.getClient(), GHAppInstallationsPage.class, request, pageSize)),
25+
null);
3126
}
3227

3328
protected Iterator<GHAppInstallation[]> adapt(final Iterator<GHAppInstallationsPage> base) {

‎src/main/java/org/kohsuke/github/GHArtifactsIterable.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHArtifactsIterable.java
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import java.net.MalformedURLException;
43
import java.util.Iterator;
54

65
import javax.annotation.Nonnull;
@@ -16,11 +15,7 @@ class GHArtifactsIterable extends PagedIterable<GHArtifact> {
1615

1716
public GHArtifactsIterable(GHRepository owner, GitHubRequest.Builder<?> requestBuilder) {
1817
this.owner = owner;
19-
try {
20-
this.request = requestBuilder.build();
21-
} catch (MalformedURLException e) {
22-
throw new GHException("Malformed URL", e);
23-
}
18+
this.request = requestBuilder.build();
2419
}
2520

2621
@Nonnull

‎src/main/java/org/kohsuke/github/GHCompare.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHCompare.java
+12-17Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.jetbrains.annotations.NotNull;
77

88
import java.io.IOException;
9-
import java.net.MalformedURLException;
109
import java.net.URL;
1110
import java.util.Collections;
1211
import java.util.Iterator;
@@ -362,23 +361,19 @@ public GHCompareCommitsIterable() {
362361
@Nonnull
363362
@Override
364363
public PagedIterator<Commit> _iterator(int pageSize) {
365-
try {
366-
GitHubRequest request = owner.root()
367-
.createRequest()
368-
.injectMappingValue("GHCompare_usePaginatedCommits", usePaginatedCommits)
369-
.withUrlPath(owner.getApiTailUrl(url.substring(url.lastIndexOf("/compare/"))))
370-
.build();
371-
372-
// page_size must be set for GHCompare commit pagination
373-
if (pageSize == 0) {
374-
pageSize = 10;
375-
}
376-
return new PagedIterator<>(
377-
adapt(GitHubPageIterator.create(owner.root().getClient(), GHCompare.class, request, pageSize)),
378-
item -> item.wrapUp(owner));
379-
} catch (MalformedURLException e) {
380-
throw new GHException("Malformed URL", e);
364+
GitHubRequest request = owner.root()
365+
.createRequest()
366+
.injectMappingValue("GHCompare_usePaginatedCommits", usePaginatedCommits)
367+
.withUrlPath(owner.getApiTailUrl(url.substring(url.lastIndexOf("/compare/"))))
368+
.build();
369+
370+
// page_size must be set for GHCompare commit pagination
371+
if (pageSize == 0) {
372+
pageSize = 10;
381373
}
374+
return new PagedIterator<>(
375+
adapt(GitHubPageIterator.create(owner.root().getClient(), GHCompare.class, request, pageSize)),
376+
item -> item.wrapUp(owner));
382377
}
383378

384379
protected Iterator<Commit[]> adapt(final Iterator<GHCompare> base) {

‎src/main/java/org/kohsuke/github/GHPerson.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHPerson.java
+5-10Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.FileNotFoundException;
44
import java.io.IOException;
5-
import java.net.MalformedURLException;
65
import java.net.URL;
76
import java.util.Collections;
87
import java.util.Date;
@@ -115,15 +114,11 @@ public PagedIterable<GHRepository> listRepositories(final int pageSize) {
115114
public synchronized Iterable<List<GHRepository>> iterateRepositories(final int pageSize) {
116115
return () -> {
117116
final PagedIterator<GHRepository> pager;
118-
try {
119-
GitHubPageIterator<GHRepository[]> iterator = GitHubPageIterator.create(root().getClient(),
120-
GHRepository[].class,
121-
root().createRequest().withUrlPath("users", login, "repos").build(),
122-
pageSize);
123-
pager = new PagedIterator<>(iterator, null);
124-
} catch (MalformedURLException e) {
125-
throw new GHException("Unable to build GitHub API URL", e);
126-
}
117+
GitHubPageIterator<GHRepository[]> iterator = GitHubPageIterator.create(root().getClient(),
118+
GHRepository[].class,
119+
root().createRequest().withUrlPath("users", login, "repos").build(),
120+
pageSize);
121+
pager = new PagedIterator<>(iterator, null);
127122

128123
return new Iterator<List<GHRepository>>() {
129124
public boolean hasNext() {

‎src/main/java/org/kohsuke/github/GHSearchBuilder.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHSearchBuilder.java
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.apache.commons.lang3.StringUtils;
44

5-
import java.net.MalformedURLException;
65
import java.util.ArrayList;
76
import java.util.List;
87

@@ -47,11 +46,7 @@ public GHQueryBuilder<T> q(String term) {
4746
public PagedSearchIterable<T> list() {
4847

4948
req.set("q", StringUtils.join(terms, " "));
50-
try {
51-
return new PagedSearchIterable<>(root(), req.build(), receiverType);
52-
} catch (MalformedURLException e) {
53-
throw new GHException("", e);
54-
}
49+
return new PagedSearchIterable<>(root(), req.build(), receiverType);
5550
}
5651

5752
/**

‎src/main/java/org/kohsuke/github/GHWorkflowJobQueryBuilder.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHWorkflowJobQueryBuilder.java
+1-7Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.kohsuke.github;
22

3-
import java.net.MalformedURLException;
4-
53
/**
64
* Lists up jobs of a workflow run with some filtering.
75
*
@@ -38,10 +36,6 @@ public GHWorkflowJobQueryBuilder all() {
3836

3937
@Override
4038
public PagedIterable<GHWorkflowJob> list() {
41-
try {
42-
return new GHWorkflowJobsIterable(repo, req.build());
43-
} catch (MalformedURLException e) {
44-
throw new GHException(e.getMessage(), e);
45-
}
39+
return new GHWorkflowJobsIterable(repo, req.build());
4640
}
4741
}

‎src/main/java/org/kohsuke/github/GHWorkflowRunsIterable.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHWorkflowRunsIterable.java
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import java.net.MalformedURLException;
43
import java.util.Iterator;
54

65
import javax.annotation.Nonnull;
@@ -16,11 +15,7 @@ class GHWorkflowRunsIterable extends PagedIterable<GHWorkflowRun> {
1615

1716
public GHWorkflowRunsIterable(GHRepository owner, GitHubRequest.Builder<?> requestBuilder) {
1817
this.owner = owner;
19-
try {
20-
this.request = requestBuilder.build();
21-
} catch (MalformedURLException e) {
22-
throw new GHException("Malformed URL", e);
23-
}
18+
this.request = requestBuilder.build();
2419
}
2520

2621
@Nonnull

‎src/main/java/org/kohsuke/github/GHWorkflowsIterable.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHWorkflowsIterable.java
+8-14Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import java.net.MalformedURLException;
43
import java.util.Iterator;
54

65
import javax.annotation.Nonnull;
@@ -20,19 +19,14 @@ public GHWorkflowsIterable(GHRepository owner) {
2019
@Nonnull
2120
@Override
2221
public PagedIterator<GHWorkflow> _iterator(int pageSize) {
23-
try {
24-
GitHubRequest request = owner.root()
25-
.createRequest()
26-
.withUrlPath(owner.getApiTailUrl("actions/workflows"))
27-
.build();
28-
29-
return new PagedIterator<>(
30-
adapt(GitHubPageIterator
31-
.create(owner.root().getClient(), GHWorkflowsPage.class, request, pageSize)),
32-
null);
33-
} catch (MalformedURLException e) {
34-
throw new GHException("Malformed URL", e);
35-
}
22+
GitHubRequest request = owner.root()
23+
.createRequest()
24+
.withUrlPath(owner.getApiTailUrl("actions/workflows"))
25+
.build();
26+
27+
return new PagedIterator<>(
28+
adapt(GitHubPageIterator.create(owner.root().getClient(), GHWorkflowsPage.class, request, pageSize)),
29+
null);
3630
}
3731

3832
protected Iterator<GHWorkflow[]> adapt(final Iterator<GHWorkflowsPage> base) {

‎src/main/java/org/kohsuke/github/GitHubPageIterator.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubPageIterator.java
+5-9Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,12 @@ private GitHubPageIterator(GitHubClient client, Class<T> type, GitHubRequest req
7272
*/
7373
static <T> GitHubPageIterator<T> create(GitHubClient client, Class<T> type, GitHubRequest request, int pageSize) {
7474

75-
try {
76-
if (pageSize > 0) {
77-
GitHubRequest.Builder<?> builder = request.toBuilder().with("per_page", pageSize);
78-
request = builder.build();
79-
}
80-
81-
return new GitHubPageIterator<>(client, type, request);
82-
} catch (MalformedURLException e) {
83-
throw new GHException("Unable to build GitHub API URL", e);
75+
if (pageSize > 0) {
76+
GitHubRequest.Builder<?> builder = request.toBuilder().with("per_page", pageSize);
77+
request = builder.build();
8478
}
79+
80+
return new GitHubPageIterator<>(client, type, request);
8581
}
8682

8783
/**

‎src/main/java/org/kohsuke/github/GitHubRequest.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubRequest.java
+21-12Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private GitHubRequest(@Nonnull List<Entry> args,
6060
@Nonnull String method,
6161
@Nonnull RateLimitTarget rateLimitTarget,
6262
@CheckForNull InputStream body,
63-
boolean forceBody) throws MalformedURLException {
63+
boolean forceBody) {
6464
this.args = Collections.unmodifiableList(new ArrayList<>(args));
6565
this.headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers));
6666
this.injectedMappingValues = Collections.unmodifiableMap(new LinkedHashMap<>(injectedMappingValues));
@@ -85,17 +85,26 @@ public static Builder<?> newBuilder() {
8585

8686
/**
8787
* Gets the final GitHub API URL.
88+
*
89+
* @throws GHException
90+
* wrapping a {@link MalformedURLException} if the GitHub API URL cannot be constructed
8891
*/
8992
@Nonnull
90-
static URL getApiURL(String apiUrl, String tailApiUrl) throws MalformedURLException {
91-
if (tailApiUrl.startsWith("/")) {
92-
if ("github.com".equals(apiUrl)) {// backward compatibility
93-
return new URL(GitHubClient.GITHUB_URL + tailApiUrl);
94-
} else {
95-
return new URL(apiUrl + tailApiUrl);
93+
static URL getApiURL(String apiUrl, String tailApiUrl) {
94+
try {
95+
if (!tailApiUrl.startsWith("/")) {
96+
apiUrl = "";
97+
} else if ("github.com".equals(apiUrl)) {
98+
// backward compatibility
99+
apiUrl = GitHubClient.GITHUB_URL;
96100
}
97-
} else {
98-
return new URL(tailApiUrl);
101+
return new URL(apiUrl + tailApiUrl);
102+
} catch (Exception e) {
103+
// The data going into constructing this URL should be controlled by the GitHub API framework,
104+
// so a malformed URL here is a framework runtime error.
105+
// All callers of this method ended up wrapping and throwing GHException,
106+
// indicating the functionality should be moved to the common code path.
107+
throw new GHException("Unable to build GitHub API URL", e);
99108
}
100109
}
101110

@@ -349,10 +358,10 @@ private Builder(@Nonnull List<Entry> args,
349358
* Builds a {@link GitHubRequest} from this builder.
350359
*
351360
* @return a {@link GitHubRequest}
352-
* @throws MalformedURLException
353-
* if the GitHub API URL cannot be constructed
361+
* @throws GHException
362+
* wrapping a {@link MalformedURLException} if the GitHub API URL cannot be constructed
354363
*/
355-
public GitHubRequest build() throws MalformedURLException {
364+
public GitHubRequest build() {
356365
return new GitHubRequest(args,
357366
headers,
358367
injectedMappingValues,

‎src/main/java/org/kohsuke/github/Requester.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/Requester.java
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.ByteArrayInputStream;
3131
import java.io.IOException;
3232
import java.io.InputStream;
33-
import java.net.MalformedURLException;
3433
import java.util.Iterator;
3534
import java.util.function.Consumer;
3635

@@ -151,11 +150,7 @@ public static InputStream copyInputStream(InputStream inputStream) throws IOExce
151150
* @return the {@link PagedIterable} for this builder.
152151
*/
153152
public <R> PagedIterable<R> toIterable(Class<R[]> type, Consumer<R> itemInitializer) {
154-
try {
155-
return new GitHubPageContentsIterable<>(client, build(), type, itemInitializer);
156-
} catch (MalformedURLException e) {
157-
throw new GHException(e.getMessage(), e);
158-
}
153+
return new GitHubPageContentsIterable<>(client, build(), type, itemInitializer);
159154

160155
}
161156
}

‎src/test/java/org/kohsuke/github/GitHubStaticTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GitHubStaticTest.java
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.kohsuke.github;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
45

6+
import java.net.MalformedURLException;
57
import java.net.URL;
68
import java.text.SimpleDateFormat;
79
import java.time.Duration;
@@ -344,6 +346,38 @@ public void testMappingReaderWriter() throws Exception {
344346

345347
}
346348

349+
@Test
350+
public void testGitHubRequest_getApiURL() throws Exception {
351+
assertThat(GitHubRequest.getApiURL("github.com", "/endpoint").toString(),
352+
equalTo("https://api.github.com/endpoint"));
353+
354+
// This URL is completely invalid but doesn't throw
355+
assertThat(GitHubRequest.getApiURL("github.com", "//endpoint&?").toString(),
356+
equalTo("https://api.github.com//endpoint&?"));
357+
358+
assertThat(GitHubRequest.getApiURL("ftp://whoa.github.com", "/endpoint").toString(),
359+
equalTo("ftp://whoa.github.com/endpoint"));
360+
assertThat(GitHubRequest.getApiURL(null, "ftp://api.test.github.com/endpoint").toString(),
361+
equalTo("ftp://api.test.github.com/endpoint"));
362+
363+
GHException e;
364+
e = Assert.assertThrows(GHException.class,
365+
() -> GitHubRequest.getApiURL("gopher://whoa.github.com", "/endpoint"));
366+
assertThat(e.getMessage(), equalTo("Unable to build GitHub API URL"));
367+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
368+
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
369+
370+
e = Assert.assertThrows(GHException.class, () -> GitHubRequest.getApiURL("bogus", "/endpoint"));
371+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
372+
assertThat(e.getCause().getMessage(), equalTo("no protocol: bogus/endpoint"));
373+
374+
e = Assert.assertThrows(GHException.class,
375+
() -> GitHubRequest.getApiURL(null, "gopher://api.test.github.com/endpoint"));
376+
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
377+
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
378+
379+
}
380+
347381
static String formatDate(Date dt, String format) {
348382
return formatZonedDate(dt, format, "GMT");
349383
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.