From 4b8f63d650225b3ff8efa2eda77cce7623260b7c Mon Sep 17 00:00:00 2001 From: Steve Riesenberg Date: Wed, 11 Oct 2023 16:56:27 -0500 Subject: [PATCH] Fix NPE when milestone does not exist --- .../java/io/spring/release/SpringReleases.java | 8 ++++++-- .../io/spring/release/SpringReleasesTests.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/spring/release/SpringReleases.java b/core/src/main/java/io/spring/release/SpringReleases.java index 506e549..7a40334 100644 --- a/core/src/main/java/io/spring/release/SpringReleases.java +++ b/core/src/main/java/io/spring/release/SpringReleases.java @@ -161,9 +161,13 @@ public boolean hasNoOpenIssues(String owner, String repo, String version) { public boolean isDueToday(String owner, String repo, String version) { var repository = new Repository(owner, repo); var milestone = this.gitHubApi.getMilestone(repository, version); + if (milestone == null || milestone.dueOn() == null) { + return false; + } + var today = LocalDate.now(); - var dueOn = (milestone.dueOn() != null) ? milestone.dueOn().atZone(ZoneOffset.UTC).toLocalDate() : null; - return (dueOn != null && today.compareTo(dueOn) >= 0); + var dueOn = milestone.dueOn().atZone(ZoneOffset.UTC).toLocalDate(); + return !today.isBefore(dueOn); } /** diff --git a/core/src/test/java/io/spring/release/SpringReleasesTests.java b/core/src/test/java/io/spring/release/SpringReleasesTests.java index d259085..d2762f1 100644 --- a/core/src/test/java/io/spring/release/SpringReleasesTests.java +++ b/core/src/test/java/io/spring/release/SpringReleasesTests.java @@ -243,6 +243,23 @@ public void isDueTodayWhenDueTomorrowThenFalse() { assertThat(repository.name()).isEqualTo(REPO); } + @Test + public void isDueTodayWhenMilestoneDoesNotExistThenFalse() { + var version = "6.1.0"; + when(this.gitHubApi.getMilestone(any(Repository.class), anyString())).thenReturn(null); + + var isDueToday = this.springReleases.isDueToday(OWNER, REPO, version); + assertThat(isDueToday).isFalse(); + + var repositoryCaptor = forClass(Repository.class); + verify(this.gitHubApi).getMilestone(repositoryCaptor.capture(), eq(version)); + verifyNoMoreInteractions(this.gitHubApi); + + var repository = repositoryCaptor.getValue(); + assertThat(repository.owner()).isEqualTo(OWNER); + assertThat(repository.name()).isEqualTo(REPO); + } + @Test public void hasNoOpenIssuesWhenNoOpenIssuesThenTrue() { var version = "6.1.0";