Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ query GetLinkedPullRequests($ids: [ID!]!) {
nodes(ids: $ids) {
... on Issue {
ghNodeId
id
}
}
}
7 changes: 6 additions & 1 deletion src/main/graphql/zenhub/queries/GetRelease.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ query GetRelease($releaseId: ID!, $endCursor: String) {
issues(first: 100, after: $endCursor) {
nodes {
id
ghNodeId
pullRequest
pullRequestObject {
state
}
}
pageInfo {
hasNextPage
Expand All @@ -17,4 +22,4 @@ query GetRelease($releaseId: ID!, $endCursor: String) {
}
}
}
}
}
26 changes: 23 additions & 3 deletions src/main/kotlin/github/GitHubClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import com.ziro.engineering.github.graphql.sdk.*
import com.ziro.engineering.github.graphql.sdk.fragment.PullRequestFragment
import com.ziro.engineering.github.graphql.sdk.type.CreatePullRequestInput
import com.ziro.engineering.github.graphql.sdk.type.PullRequestUpdateState
import com.ziro.engineering.github.graphql.sdk.type.UpdatePullRequestInput
Expand Down Expand Up @@ -101,7 +102,7 @@ class GitHubClient : AutoCloseable {
currBranch: String,
title: String,
body: String?,
) = runBlocking {
): PullRequestFragment = runBlocking {
val input =
CreatePullRequestInput(
clientMutationId = Optional.absent(),
Expand Down Expand Up @@ -163,16 +164,35 @@ class GitHubClient : AutoCloseable {
?.pullRequestFragment
}

fun updatePullRequest(id: String, body: String?, state: PullRequestUpdateState?) = runBlocking {
fun updatePullRequest(
id: String,
baseBranch: String?,
body: String?,
state: PullRequestUpdateState?
): PullRequestFragment = runBlocking {
val input =
UpdatePullRequestInput(
pullRequestId = id,
baseRefName = Optional.presentIfNotNull(baseBranch),
body = Optional.presentIfNotNull(body),
state = Optional.presentIfNotNull(state))

val mutation = UpdatePullRequestMutation(input)
val response = apolloClient.mutation(mutation).execute()
return@runBlocking response.data?.updatePullRequest?.pullRequest?.pullRequestFragment

if (response.hasErrors()) {
val exception = Exception(response.errors?.joinToString { it.message })
throw IllegalStateException(exception)
}

val pullRequestFragment = response.data?.updatePullRequest?.pullRequest?.pullRequestFragment

if (pullRequestFragment == null) {
val exception = Exception("Pull request fragment is null")
throw IllegalStateException(exception)
}

pullRequestFragment
}

override fun close() {
Expand Down
116 changes: 83 additions & 33 deletions src/main/kotlin/zenhub/ZenHubClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,36 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
apolloClient.mutation(mutation).toFlow().single().data?.closeIssues
}

fun getRelease(releaseId: String): Release? {
fun getRelease(releaseId: String, pullRequestsOnly: Boolean): Release? {
var queryResult: GetReleaseQuery.OnRelease?
val releaseIssueIds = mutableSetOf<String>()
var endCursor: String? = null
var hasNextPage: Boolean

do {
queryResult = getRelease(releaseId, endCursor)
val pageIssues = queryResult?.issues?.nodes?.map { issue -> issue.id } ?: emptyList()
releaseIssueIds.addAll(pageIssues)

val pageIssues =
queryResult
?.issues
?.nodes
?.filter { node ->
!pullRequestsOnly ||
(node.pullRequest &&
node.pullRequestObject?.state?.equals(PullRequestState.OPEN) ==
true)
}
?.map { node ->
if (pullRequestsOnly) {
requireNotNull(node.ghNodeId) {
"ghNodeId is null for node id=${node.id}"
}
} else {
node.id
}
} ?: emptyList()

releaseIssueIds.addAll(pageIssues)
hasNextPage = queryResult?.issues?.pageInfo?.hasNextPage ?: false
endCursor = queryResult?.issues?.pageInfo?.endCursor
} while (hasNextPage)
Expand Down Expand Up @@ -290,45 +309,57 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC

for (release in releases) {
if (release.title == title) {
return@runBlocking getRelease(release.id)
return@runBlocking getRelease(release.id, false)
}
}

throw IllegalArgumentException("Release with title $title not found")
}

fun addIssuesToRelease(
issueIds: Set<String>,
releaseId: String
): AddIssuesToReleasesMutation.Release? = runBlocking {
fun addIssuesToRelease(issueIds: Set<String>, releaseId: String): Release = runBlocking {
val input =
AddIssuesToReleasesInput(Optional.absent(), issueIds.toList(), listOf(releaseId))
val mutation = AddIssuesToReleasesMutation(input)
apolloClient
.mutation(mutation)
.toFlow()
.single()
.data
?.addIssuesToReleases
?.releases
?.get(0)
val response = apolloClient.mutation(mutation).execute()

if (response.hasErrors()) {
val exception = Exception(response.errors?.joinToString { it.message })
throw IllegalStateException(exception)
}

val releaseId =
response.data?.addIssuesToReleases?.releases?.get(0)?.id
?: throw IllegalStateException("Mutation response has null release ID")

val release =
getRelease(releaseId, false)
?: throw IllegalStateException(
"Unable to retrieve release with ID $releaseId. This should never happen because the mutation was successful!")

release
}

fun removeIssuesFromRelease(
issueIds: Set<String>,
releaseId: String
): RemoveIssuesFromReleasesMutation.Release? = runBlocking {
fun removeIssuesFromRelease(issueIds: Set<String>, releaseId: String): Release = runBlocking {
val input =
RemoveIssuesFromReleasesInput(Optional.absent(), issueIds.toList(), listOf(releaseId))
val mutation = RemoveIssuesFromReleasesMutation(input)
apolloClient
.mutation(mutation)
.toFlow()
.single()
.data
?.removeIssuesFromReleases
?.releases
?.get(0)
val response = apolloClient.mutation(mutation).execute()

if (response.hasErrors()) {
val exception = Exception(response.errors?.joinToString { it.message })
throw IllegalStateException(exception)
}

val releaseId =
response.data?.removeIssuesFromReleases?.releases?.get(0)?.id
?: throw IllegalStateException("Mutation response has null release ID")

val release =
getRelease(releaseId, false)
?: throw IllegalStateException(
"Unable to retrieve release with ID $releaseId. This should never happen because the mutation was successful!")

release
}

fun getIssueEvents(githubRepoId: Int, issueNumber: Int): ArrayList<GetIssueEventsQuery.Node> {
Expand Down Expand Up @@ -595,7 +626,7 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
return@runBlocking null
}

getRelease(issue.issueFragment.releases.nodes[0].id)
getRelease(issue.issueFragment.releases.nodes[0].id, false)
}

override fun close() {
Expand Down Expand Up @@ -682,11 +713,30 @@ class ZenHubClient(val zenhubWorkspaceId: String = DEFAULT_WORKSPACE_ID) : AutoC
apolloClient.query(query).toFlow().single().data?.viewer?.githubUser
}

fun getLinkedPullRequests(linkIds: Set<String>): List<String>? = runBlocking {
fun getPullRequestGitHubIdsFromLinkIds(linkIds: Set<String>) = runBlocking {
val query = GetLinkedPullRequestsQuery(linkIds.toList())
apolloClient.query(query).toFlow().single().data?.nodes?.mapNotNull { node ->
node?.onIssue?.ghNodeId
}

apolloClient
.query(query)
.toFlow()
.single()
.data
?.nodes
?.mapNotNull { node -> node?.onIssue?.ghNodeId }
?.toSet()
}

fun getPullRequestZenHubIdsFromLinkIds(linkIds: Set<String>) = runBlocking {
val query = GetLinkedPullRequestsQuery(linkIds.toList())

apolloClient
.query(query)
.toFlow()
.single()
.data
?.nodes
?.mapNotNull { node -> node?.onIssue?.id }
?.toSet()
}

fun createIssue(
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.3.0
9.0.0