Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/org/kohsuke/github/GHCommitPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@ public GHCommitPointer() {
* @throws IOException
* the io exception
*/

//original code
// public GHCommit getCommit() throws IOException {
// return getRepository().getCommit(getSha()); //when repo is null, so this will throw NPE
// }

//fixed code
public GHCommit getCommit() throws IOException {
return getRepository().getCommit(getSha());
GHRepository repository = getRepository();
if (repository == null)
throw new IOException("Cannot commit because repository is null.");

return repository.getCommit(getSha());
}


/**
* String that looks like "USERNAME:REF".
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public URL getDiffUrl() {
* @return the head
*/
public GHCommitPointer getHead() {
return head;
return head; //return head directly, no null check
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/kohsuke/github/GHCommitPointerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.kohsuke.github;

import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;

public class GHCommitPointerTest {
//week8 failing test:
//test the null value
// @Test
// public void nullRepoNpe() throws Exception {

// GHCommitPointer pointer = new GHCommitPointer(); //new object value is null
//
// throws NPE because repository is null.
// pointer.getCommit();
// }

//week9 fixed test
//test the IOException, which is more controllable
@Test
public void nullRepoNpe() {
GHCommitPointer pointer = new GHCommitPointer();

//expect throw IOException, test fail otherwise
IOException ex = Assert.assertThrows(IOException.class, pointer::getCommit); //method reference

//continue to test if error message also contains "repository is null", test fail otherwise
Assert.assertTrue(ex.getMessage().contains("repository is null"));
}
}
Loading