Repository: hub4j/github-api
Pull request #1353 (2022-01-14 15:57:58)

Add GitHub#getRepository validation
# Description

<!-- Describe your change here -->

As long as I read the message of `IllegalArgumentException`, I thought is is needed to validate a case in which there are more than two slashes in `name`.

# Before submitting a PR:

- [x] Changes must not break binary backwards compatibility. If you are unclear on how to make the change you think is needed while maintaining backward compatibility, [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [x] Add JavaDocs and other comments as appropriate. Consider including links in comments to relevant documentation on https://docs.github.com/en/rest .  
- [x] Add tests that cover any added or changed code. This generally requires capturing snapshot test data. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
- [ ] Run `mvn -D enable-ci clean install site` locally. If this command doesn't succeed, your change will not pass CI.
- [x] Push your changes to a branch other than `main`. You will create your PR from that branch.

# When creating a PR: 

- [x] Fill in the "Description" above with clear summary of the changes. This includes:
  - [x] If this PR fixes one or more issues, include "Fixes #<issue number>" lines for each issue. 
  - [ ] Provide links to relevant documentation on https://docs.github.com/en/rest where possible.
- [x] All lines of new code should be covered by tests as reported by code coverage. Any lines that are not covered must have PR comments explaining why they cannot be covered. For example, "Reaching this particular exception is hard and is not a particular common scenario."
- [x] Enable "Allow edits from maintainers".



Patch:
diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java
index 064767143..acf366c1d 100644
--- a/src/main/java/org/kohsuke/github/GitHub.java
+++ b/src/main/java/org/kohsuke/github/GitHub.java
@@ -667,7 +667,7 @@ public class GitHub {
      */
     public GHRepository getRepository(String name) throws IOException {
         String[] tokens = name.split("/");
-        if (tokens.length < 2) {
+        if (tokens.length != 2) {
             throw new IllegalArgumentException("Repository name must be in format owner/repo");
         }
         return GHRepository.read(this, tokens[0], tokens[1]);


Test patch:
diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java
index 73002f4ae..90d12a4bf 100644
--- a/src/test/java/org/kohsuke/github/GitHubTest.java
+++ b/src/test/java/org/kohsuke/github/GitHubTest.java
@@ -35,6 +35,14 @@ public class GitHubTest extends AbstractGitHubWireMockTest {
 
         try {
             gitHub.getRepository("hub4j_github-api");
+            fail();
+        } catch (IllegalArgumentException e) {
+            assertThat(e.getMessage(), equalTo("Repository name must be in format owner/repo"));
+        }
+
+        try {
+            gitHub.getRepository("hub4j/github/api");
+            fail();
         } catch (IllegalArgumentException e) {
             assertThat(e.getMessage(), equalTo("Repository name must be in format owner/repo"));
         }