@@ -229,17 +229,36 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
229229 apolloClient.mutation(mutation).toFlow().single().data?.closeIssues
230230 }
231231
232- fun getRelease (releaseId : String ): Release ? {
232+ fun getRelease (releaseId : String , pullRequestsOnly : Boolean ): Release ? {
233233 var queryResult: GetReleaseQuery .OnRelease ?
234234 val releaseIssueIds = mutableSetOf<String >()
235235 var endCursor: String? = null
236236 var hasNextPage: Boolean
237237
238238 do {
239239 queryResult = getRelease(releaseId, endCursor)
240- val pageIssues = queryResult?.issues?.nodes?.map { issue -> issue.id } ? : emptyList()
241- releaseIssueIds.addAll(pageIssues)
242240
241+ val pageIssues =
242+ queryResult
243+ ?.issues
244+ ?.nodes
245+ ?.filter { node ->
246+ ! pullRequestsOnly ||
247+ (node.pullRequest &&
248+ node.pullRequestObject?.state?.equals(PullRequestState .OPEN ) ==
249+ true )
250+ }
251+ ?.map { node ->
252+ if (pullRequestsOnly) {
253+ requireNotNull(node.ghNodeId) {
254+ " ghNodeId is null for node id=${node.id} "
255+ }
256+ } else {
257+ node.id
258+ }
259+ } ? : emptyList()
260+
261+ releaseIssueIds.addAll(pageIssues)
243262 hasNextPage = queryResult?.issues?.pageInfo?.hasNextPage ? : false
244263 endCursor = queryResult?.issues?.pageInfo?.endCursor
245264 } while (hasNextPage)
@@ -290,45 +309,57 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
290309
291310 for (release in releases) {
292311 if (release.title == title) {
293- return @runBlocking getRelease(release.id)
312+ return @runBlocking getRelease(release.id, false )
294313 }
295314 }
296315
297316 throw IllegalArgumentException (" Release with title $title not found" )
298317 }
299318
300- fun addIssuesToRelease (
301- issueIds : Set <String >,
302- releaseId : String
303- ): AddIssuesToReleasesMutation .Release ? = runBlocking {
319+ fun addIssuesToRelease (issueIds : Set <String >, releaseId : String ): Release = runBlocking {
304320 val input =
305321 AddIssuesToReleasesInput (Optional .absent(), issueIds.toList(), listOf (releaseId))
306322 val mutation = AddIssuesToReleasesMutation (input)
307- apolloClient
308- .mutation(mutation)
309- .toFlow()
310- .single()
311- .data
312- ?.addIssuesToReleases
313- ?.releases
314- ?.get(0 )
323+ val response = apolloClient.mutation(mutation).execute()
324+
325+ if (response.hasErrors()) {
326+ val exception = Exception (response.errors?.joinToString { it.message })
327+ throw IllegalStateException (exception)
328+ }
329+
330+ val releaseId =
331+ response.data?.addIssuesToReleases?.releases?.get(0 )?.id
332+ ? : throw IllegalStateException (" Mutation response has null release ID" )
333+
334+ val release =
335+ getRelease(releaseId, false )
336+ ? : throw IllegalStateException (
337+ " Unable to retrieve release with ID $releaseId . This should never happen because the mutation was successful!" )
338+
339+ release
315340 }
316341
317- fun removeIssuesFromRelease (
318- issueIds : Set <String >,
319- releaseId : String
320- ): RemoveIssuesFromReleasesMutation .Release ? = runBlocking {
342+ fun removeIssuesFromRelease (issueIds : Set <String >, releaseId : String ): Release = runBlocking {
321343 val input =
322344 RemoveIssuesFromReleasesInput (Optional .absent(), issueIds.toList(), listOf (releaseId))
323345 val mutation = RemoveIssuesFromReleasesMutation (input)
324- apolloClient
325- .mutation(mutation)
326- .toFlow()
327- .single()
328- .data
329- ?.removeIssuesFromReleases
330- ?.releases
331- ?.get(0 )
346+ val response = apolloClient.mutation(mutation).execute()
347+
348+ if (response.hasErrors()) {
349+ val exception = Exception (response.errors?.joinToString { it.message })
350+ throw IllegalStateException (exception)
351+ }
352+
353+ val releaseId =
354+ response.data?.removeIssuesFromReleases?.releases?.get(0 )?.id
355+ ? : throw IllegalStateException (" Mutation response has null release ID" )
356+
357+ val release =
358+ getRelease(releaseId, false )
359+ ? : throw IllegalStateException (
360+ " Unable to retrieve release with ID $releaseId . This should never happen because the mutation was successful!" )
361+
362+ release
332363 }
333364
334365 fun getIssueEvents (githubRepoId : Int , issueNumber : Int ): ArrayList <GetIssueEventsQuery .Node > {
@@ -595,7 +626,7 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
595626 return @runBlocking null
596627 }
597628
598- getRelease(issue.issueFragment.releases.nodes[0 ].id)
629+ getRelease(issue.issueFragment.releases.nodes[0 ].id, false )
599630 }
600631
601632 override fun close () {
@@ -682,11 +713,30 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
682713 apolloClient.query(query).toFlow().single().data?.viewer?.githubUser
683714 }
684715
685- fun getLinkedPullRequests (linkIds : Set <String >): List < String > ? = runBlocking {
716+ fun getPullRequestGitHubIdsFromLinkIds (linkIds : Set <String >) = runBlocking {
686717 val query = GetLinkedPullRequestsQuery (linkIds.toList())
687- apolloClient.query(query).toFlow().single().data?.nodes?.mapNotNull { node ->
688- node?.onIssue?.ghNodeId
689- }
718+
719+ apolloClient
720+ .query(query)
721+ .toFlow()
722+ .single()
723+ .data
724+ ?.nodes
725+ ?.mapNotNull { node -> node?.onIssue?.ghNodeId }
726+ ?.toSet()
727+ }
728+
729+ fun getPullRequestZenHubIdsFromLinkIds (linkIds : Set <String >) = runBlocking {
730+ val query = GetLinkedPullRequestsQuery (linkIds.toList())
731+
732+ apolloClient
733+ .query(query)
734+ .toFlow()
735+ .single()
736+ .data
737+ ?.nodes
738+ ?.mapNotNull { node -> node?.onIssue?.id }
739+ ?.toSet()
690740 }
691741
692742 fun createIssue (
0 commit comments