diff --git a/gradle.properties b/gradle.properties index 6dda49b..c455ffe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ kotlin.code.style=official -ziro.cli.version=v6.2.2 +ziro.cli.version=v6.5.0 diff --git a/src/main/graphql/github/fragments/PullRequest.graphql b/src/main/graphql/github/fragments/PullRequest.graphql index da9c5fe..63bfc5d 100644 --- a/src/main/graphql/github/fragments/PullRequest.graphql +++ b/src/main/graphql/github/fragments/PullRequest.graphql @@ -12,6 +12,11 @@ fragment PullRequestFragment on PullRequest { repository { name } + reviews(last: 1) { + nodes { + state + } + } state title url diff --git a/src/main/graphql/github/mutations/MergePullRequest.graphql b/src/main/graphql/github/mutations/MergePullRequest.graphql new file mode 100644 index 0000000..dccfeb5 --- /dev/null +++ b/src/main/graphql/github/mutations/MergePullRequest.graphql @@ -0,0 +1,7 @@ +mutation MergePullRequest($input: MergePullRequestInput!) { + mergePullRequest(input: $input) { + pullRequest { + ...PullRequestFragment + } + } +} diff --git a/src/main/graphql/github/queries/GetChecks.graphql b/src/main/graphql/github/queries/GetChecks.graphql new file mode 100644 index 0000000..02bb9ec --- /dev/null +++ b/src/main/graphql/github/queries/GetChecks.graphql @@ -0,0 +1,18 @@ +query GetChecks($owner: String!, $repo: String!, $gitReference: String!) { + repository(owner: $owner, name: $repo) { + object(expression: $gitReference) { + ... on Commit { + checkSuites(first: 5) { + nodes { + checkRuns(first: 10) { + nodes { + name + conclusion + } + } + } + } + } + } + } +} diff --git a/src/main/graphql/github/queries/GetStatuses.graphql b/src/main/graphql/github/queries/GetStatuses.graphql index 7759cd8..94c82da 100644 --- a/src/main/graphql/github/queries/GetStatuses.graphql +++ b/src/main/graphql/github/queries/GetStatuses.graphql @@ -7,6 +7,8 @@ query GetStatuses($owner: String!, $repo: String!, $gitReference: String!) { creator { login } + context + description state targetUrl } @@ -14,4 +16,4 @@ query GetStatuses($owner: String!, $repo: String!, $gitReference: String!) { } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/github/GitHubClient.kt b/src/main/kotlin/github/GitHubClient.kt index be69739..157a813 100644 --- a/src/main/kotlin/github/GitHubClient.kt +++ b/src/main/kotlin/github/GitHubClient.kt @@ -5,6 +5,8 @@ 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.MergePullRequestInput +import com.ziro.engineering.github.graphql.sdk.type.PullRequestMergeMethod import com.ziro.engineering.github.graphql.sdk.type.PullRequestUpdateState import com.ziro.engineering.github.graphql.sdk.type.UpdatePullRequestInput import java.net.URI @@ -96,6 +98,28 @@ class GitHubClient : AutoCloseable { ?.contexts ?: emptyList() } + fun getChecks( + repoOwner: String = DEFAULT_REPOSITORY_OWNER, + repoName: String = DEFAULT_REPOSITORY_NAME, + gitReference: String + ): List = runBlocking { + val query = GetChecksQuery(repoOwner, repoName, gitReference) + + apolloClient + .query(query) + .toFlow() + .single() + .data + ?.repository + ?.`object` + ?.onCommit + ?.checkSuites + ?.nodes + ?.flatMap { checkSuite -> checkSuite?.checkRuns?.nodes.orEmpty() } + ?.filterNotNull() + .orEmpty() + } + fun createPullRequest( repoId: String, baseBranch: String, @@ -195,6 +219,27 @@ class GitHubClient : AutoCloseable { pullRequestFragment } + fun mergePullRequest( + id: String, + commitTitle: String?, + expectedHeadOid: String?, + mergeMethod: PullRequestMergeMethod + ) = runBlocking { + val input = + MergePullRequestInput( + commitHeadline = Optional.presentIfNotNull(commitTitle), + expectedHeadOid = Optional.presentIfNotNull(expectedHeadOid), + mergeMethod = Optional.present(mergeMethod), + pullRequestId = id) + + val mutation = MergePullRequestMutation(input) + val response = apolloClient.mutation(mutation).execute() + + if (response.hasErrors()) { + throw RuntimeException(response.errors?.joinToString(separator = "\n") { it.message }) + } + } + override fun close() { apolloClient.closeQuietly() }