1 // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package org.jetbrains.plugins.github.api
4 import org.jetbrains.plugins.github.api.GithubApiRequest.Post.GQLQuery
5 import org.jetbrains.plugins.github.api.data.*
6 import org.jetbrains.plugins.github.api.data.graphql.GHGQLPageInfo
7 import org.jetbrains.plugins.github.api.data.graphql.GHGQLPagedRequestResponse
8 import org.jetbrains.plugins.github.api.data.graphql.GHGQLRequestPagination
9 import org.jetbrains.plugins.github.api.data.graphql.query.GHGQLSearchQueryResponse
10 import org.jetbrains.plugins.github.api.data.pullrequest.*
11 import org.jetbrains.plugins.github.api.data.pullrequest.timeline.GHPRTimelineItem
12 import org.jetbrains.plugins.github.api.data.request.GHPullRequestDraftReviewComment
13 import org.jetbrains.plugins.github.api.util.GHSchemaPreview
15 object GHGQLRequests {
19 fun findAll(server: GithubServerPath, organization: String,
20 pagination: GHGQLRequestPagination? = null): GQLQuery<GHGQLPagedRequestResponse<GHTeam>> {
22 return GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.findOrganizationTeams,
23 mapOf("organization" to organization,
24 "pageSize" to pagination?.pageSize,
25 "cursor" to pagination?.afterCursor),
26 TeamsConnection::class.java,
27 "organization", "teams")
30 fun findByUserLogins(server: GithubServerPath, organization: String, logins: List<String>,
31 pagination: GHGQLRequestPagination? = null): GQLQuery<GHGQLPagedRequestResponse<GHTeam>> =
32 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.findOrganizationTeams,
33 mapOf("organization" to organization,
35 "pageSize" to pagination?.pageSize,
36 "cursor" to pagination?.afterCursor),
37 TeamsConnection::class.java,
38 "organization", "teams")
40 private class TeamsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHTeam>)
41 : GHConnection<GHTeam>(pageInfo, nodes)
46 fun findPermission(repository: GHRepositoryCoordinates): GQLQuery<GHRepositoryPermission?> {
47 return GQLQuery.OptionalTraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.findRepositoryPermission,
48 mapOf("repoOwner" to repository.repositoryPath.owner,
49 "repoName" to repository.repositoryPath.repository),
50 GHRepositoryPermission::class.java,
56 fun getCommentBody(server: GithubServerPath, commentId: String): GQLQuery<String> =
57 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.commentBody,
58 mapOf("id" to commentId),
62 fun updateComment(server: GithubServerPath, commentId: String, newText: String): GQLQuery<GHComment> =
63 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateIssueComment,
64 mapOf("id" to commentId,
66 GHComment::class.java,
67 "updateIssueComment", "issueComment")
69 fun deleteComment(server: GithubServerPath, commentId: String): GQLQuery<Any?> =
70 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.deleteIssueComment,
71 mapOf("id" to commentId),
76 fun findOne(repository: GHRepositoryCoordinates, number: Long): GQLQuery<GHPullRequest?> {
77 return GQLQuery.OptionalTraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.findPullRequest,
78 mapOf("repoOwner" to repository.repositoryPath.owner,
79 "repoName" to repository.repositoryPath.repository,
81 GHPullRequest::class.java,
82 "repository", "pullRequest").apply {
83 acceptMimeType = GHSchemaPreview.PR_DRAFT.mimeType
88 fun update(repository: GHRepositoryCoordinates, pullRequestId: String, title: String?, description: String?): GQLQuery<GHPullRequest> {
89 val parameters = mutableMapOf<String, Any>("pullRequestId" to pullRequestId)
90 if (title != null) parameters["title"] = title
91 if (description != null) parameters["body"] = description
92 return GQLQuery.TraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.updatePullRequest, parameters,
93 GHPullRequest::class.java,
94 "updatePullRequest", "pullRequest").apply {
95 acceptMimeType = GHSchemaPreview.PR_DRAFT.mimeType
99 fun markReadyForReview(repository: GHRepositoryCoordinates, pullRequestId: String): GQLQuery<Any?> =
100 GQLQuery.Parsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.markPullRequestReadyForReview,
101 mutableMapOf<String, Any>("pullRequestId" to pullRequestId),
102 Any::class.java).apply {
103 acceptMimeType = GHSchemaPreview.PR_DRAFT.mimeType
106 fun mergeabilityData(repository: GHRepositoryCoordinates, number: Long): GQLQuery<GHPullRequestMergeabilityData?> =
107 GQLQuery.OptionalTraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.pullRequestMergeabilityData,
108 mapOf("repoOwner" to repository.repositoryPath.owner,
109 "repoName" to repository.repositoryPath.repository,
111 GHPullRequestMergeabilityData::class.java,
112 "repository", "pullRequest").apply {
113 acceptMimeType = "${GHSchemaPreview.CHECKS.mimeType},${GHSchemaPreview.PR_MERGE_INFO.mimeType}"
116 fun search(server: GithubServerPath, query: String, pagination: GHGQLRequestPagination? = null)
117 : GQLQuery<GHGQLSearchQueryResponse<GHPullRequestShort>> {
119 return GQLQuery.Parsed(server.toGraphQLUrl(), GHGQLQueries.issueSearch,
120 mapOf("query" to query,
121 "pageSize" to pagination?.pageSize,
122 "cursor" to pagination?.afterCursor),
123 PRSearch::class.java).apply {
124 acceptMimeType = GHSchemaPreview.PR_DRAFT.mimeType
128 private class PRSearch(search: SearchConnection<GHPullRequestShort>)
129 : GHGQLSearchQueryResponse<GHPullRequestShort>(search)
131 fun reviewThreads(repository: GHRepositoryCoordinates, number: Long,
132 pagination: GHGQLRequestPagination? = null): GQLQuery<GHGQLPagedRequestResponse<GHPullRequestReviewThread>> {
133 return GQLQuery.TraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.pullRequestReviewThreads,
134 mapOf("repoOwner" to repository.repositoryPath.owner,
135 "repoName" to repository.repositoryPath.repository,
137 "pageSize" to pagination?.pageSize,
138 "cursor" to pagination?.afterCursor),
139 ThreadsConnection::class.java,
140 "repository", "pullRequest", "reviewThreads")
143 private class ThreadsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPullRequestReviewThread>)
144 : GHConnection<GHPullRequestReviewThread>(pageInfo, nodes)
146 fun commits(repository: GHRepositoryCoordinates, number: Long,
147 pagination: GHGQLRequestPagination? = null): GQLQuery<GHGQLPagedRequestResponse<GHPullRequestCommit>> {
148 return GQLQuery.TraversedParsed(repository.serverPath.toGraphQLUrl(), GHGQLQueries.pullRequestCommits,
149 mapOf("repoOwner" to repository.repositoryPath.owner,
150 "repoName" to repository.repositoryPath.repository,
152 "pageSize" to pagination?.pageSize,
153 "cursor" to pagination?.afterCursor),
154 CommitsConnection::class.java,
155 "repository", "pullRequest", "commits")
158 private class CommitsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPullRequestCommit>)
159 : GHConnection<GHPullRequestCommit>(pageInfo, nodes)
162 fun items(server: GithubServerPath, repoOwner: String, repoName: String, number: Long,
163 pagination: GHGQLRequestPagination? = null)
164 : GQLQuery<GHGQLPagedRequestResponse<GHPRTimelineItem>> {
166 return GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.pullRequestTimeline,
167 mapOf("repoOwner" to repoOwner,
168 "repoName" to repoName,
170 "pageSize" to pagination?.pageSize,
171 "cursor" to pagination?.afterCursor,
172 "since" to pagination?.since),
173 TimelineConnection::class.java,
174 "repository", "pullRequest", "timelineItems")
177 private class TimelineConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPRTimelineItem>)
178 : GHConnection<GHPRTimelineItem>(pageInfo, nodes)
183 fun create(server: GithubServerPath, pullRequestId: String,
184 event: GHPullRequestReviewEvent?, body: String?, commitSha: String?,
185 comments: List<GHPullRequestDraftReviewComment>?): GQLQuery<GHPullRequestPendingReview> =
186 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.createReview,
187 mapOf("pullRequestId" to pullRequestId,
189 "commitOid" to commitSha,
190 "comments" to comments,
192 GHPullRequestPendingReview::class.java,
193 "addPullRequestReview", "pullRequestReview")
195 fun submit(server: GithubServerPath, reviewId: String, event: GHPullRequestReviewEvent, body: String?): GQLQuery<Any> =
196 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.submitReview,
197 mapOf("reviewId" to reviewId,
202 fun updateBody(server: GithubServerPath, reviewId: String, newText: String): GQLQuery<GHPullRequestReview> =
203 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateReview,
204 mapOf("reviewId" to reviewId,
206 GHPullRequestReview::class.java,
207 "updatePullRequestReview", "pullRequestReview")
209 fun delete(server: GithubServerPath, reviewId: String): GQLQuery<Any> =
210 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.deleteReview,
211 mapOf("reviewId" to reviewId),
214 fun pendingReviews(server: GithubServerPath, pullRequestId: String): GQLQuery<GHNodes<GHPullRequestPendingReview>> {
215 return GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.pendingReview,
216 mapOf("pullRequestId" to pullRequestId),
217 PendingReviewNodes::class.java,
221 private class PendingReviewNodes(nodes: List<GHPullRequestPendingReview>) :
222 GHNodes<GHPullRequestPendingReview>(nodes)
224 fun addComment(server: GithubServerPath,
226 body: String, commitSha: String, fileName: String, diffLine: Int)
227 : GQLQuery<GHPullRequestReviewCommentWithPendingReview> =
228 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.addReviewComment,
229 mapOf("reviewId" to reviewId,
231 "commit" to commitSha,
233 "position" to diffLine),
234 GHPullRequestReviewCommentWithPendingReview::class.java,
235 "addPullRequestReviewComment", "comment")
237 fun addComment(server: GithubServerPath,
241 : GQLQuery<GHPullRequestReviewCommentWithPendingReview> =
242 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.addReviewComment,
243 mapOf("reviewId" to reviewId,
244 "inReplyTo" to inReplyTo,
246 GHPullRequestReviewCommentWithPendingReview::class.java,
247 "addPullRequestReviewComment", "comment")
249 fun deleteComment(server: GithubServerPath, commentId: String): GQLQuery<GHPullRequestPendingReview> =
250 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.deleteReviewComment,
251 mapOf("id" to commentId),
252 GHPullRequestPendingReview::class.java,
253 "deletePullRequestReviewComment", "pullRequestReview")
255 fun updateComment(server: GithubServerPath, commentId: String, newText: String): GQLQuery<GHPullRequestReviewComment> =
256 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateReviewComment,
257 mapOf("id" to commentId,
259 GHPullRequestReviewComment::class.java,
260 "updatePullRequestReviewComment", "pullRequestReviewComment")
262 fun resolveThread(server: GithubServerPath, threadId: String): GQLQuery<GHPullRequestReviewThread> =
263 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.resolveReviewThread,
264 mapOf("threadId" to threadId),
265 GHPullRequestReviewThread::class.java,
266 "resolveReviewThread", "thread")
268 fun unresolveThread(server: GithubServerPath, threadId: String): GQLQuery<GHPullRequestReviewThread> =
269 GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.unresolveReviewThread,
270 mapOf("threadId" to threadId),
271 GHPullRequestReviewThread::class.java,
272 "unresolveReviewThread", "thread")