b2a7d1506ea5bafe38a2845819f9bad0e7cc6e30
[idea/community.git] / plugins / github / src / org / jetbrains / plugins / github / api / GHGQLRequests.kt
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
3
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
14
15 object GHGQLRequests {
16   object Organization {
17
18     object Team {
19       fun findAll(server: GithubServerPath, organization: String,
20                   pagination: GHGQLRequestPagination? = null): GQLQuery<GHGQLPagedRequestResponse<GHTeam>> {
21
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")
28       }
29
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,
34                                        "logins" to logins,
35                                        "pageSize" to pagination?.pageSize,
36                                        "cursor" to pagination?.afterCursor),
37                                  TeamsConnection::class.java,
38                                  "organization", "teams")
39
40       private class TeamsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHTeam>)
41         : GHConnection<GHTeam>(pageInfo, nodes)
42     }
43   }
44
45   object Repo {
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,
51                                               "repository")
52     }
53   }
54
55   object Comment {
56     fun getCommentBody(server: GithubServerPath, commentId: String): GQLQuery<String> =
57       GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.commentBody,
58                                mapOf("id" to commentId),
59                                String::class.java,
60                                "node", "body")
61
62     fun updateComment(server: GithubServerPath, commentId: String, newText: String): GQLQuery<GHComment> =
63       GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateIssueComment,
64                                mapOf("id" to commentId,
65                                      "body" to newText),
66                                GHComment::class.java,
67                                "updateIssueComment", "issueComment")
68
69     fun deleteComment(server: GithubServerPath, commentId: String): GQLQuery<Any?> =
70       GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.deleteIssueComment,
71                                mapOf("id" to commentId),
72                                Any::class.java)
73   }
74
75   object PullRequest {
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,
80                                                     "number" to number),
81                                               GHPullRequest::class.java,
82                                               "repository", "pullRequest").apply {
83         acceptMimeType = GHSchemaPreview.PR_DRAFT.mimeType
84       }
85     }
86
87
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
96       }
97     }
98
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
104       }
105
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,
110                                              "number" to number),
111                                        GHPullRequestMergeabilityData::class.java,
112                                        "repository", "pullRequest").apply {
113         acceptMimeType = "${GHSchemaPreview.CHECKS.mimeType},${GHSchemaPreview.PR_MERGE_INFO.mimeType}"
114       }
115
116     fun search(server: GithubServerPath, query: String, pagination: GHGQLRequestPagination? = null)
117       : GQLQuery<GHGQLSearchQueryResponse<GHPullRequestShort>> {
118
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
125       }
126     }
127
128     private class PRSearch(search: SearchConnection<GHPullRequestShort>)
129       : GHGQLSearchQueryResponse<GHPullRequestShort>(search)
130
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,
136                                             "number" to number,
137                                             "pageSize" to pagination?.pageSize,
138                                             "cursor" to pagination?.afterCursor),
139                                       ThreadsConnection::class.java,
140                                       "repository", "pullRequest", "reviewThreads")
141     }
142
143     private class ThreadsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPullRequestReviewThread>)
144       : GHConnection<GHPullRequestReviewThread>(pageInfo, nodes)
145
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,
151                                             "number" to number,
152                                             "pageSize" to pagination?.pageSize,
153                                             "cursor" to pagination?.afterCursor),
154                                       CommitsConnection::class.java,
155                                       "repository", "pullRequest", "commits")
156     }
157
158     private class CommitsConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPullRequestCommit>)
159       : GHConnection<GHPullRequestCommit>(pageInfo, nodes)
160
161     object Timeline {
162       fun items(server: GithubServerPath, repoOwner: String, repoName: String, number: Long,
163                 pagination: GHGQLRequestPagination? = null)
164         : GQLQuery<GHGQLPagedRequestResponse<GHPRTimelineItem>> {
165
166         return GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.pullRequestTimeline,
167                                         mapOf("repoOwner" to repoOwner,
168                                               "repoName" to repoName,
169                                               "number" to number,
170                                               "pageSize" to pagination?.pageSize,
171                                               "cursor" to pagination?.afterCursor,
172                                               "since" to pagination?.since),
173                                         TimelineConnection::class.java,
174                                         "repository", "pullRequest", "timelineItems")
175       }
176
177       private class TimelineConnection(pageInfo: GHGQLPageInfo, nodes: List<GHPRTimelineItem>)
178         : GHConnection<GHPRTimelineItem>(pageInfo, nodes)
179     }
180
181     object Review {
182
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,
188                                        "event" to event,
189                                        "commitOid" to commitSha,
190                                        "comments" to comments,
191                                        "body" to body),
192                                  GHPullRequestPendingReview::class.java,
193                                  "addPullRequestReview", "pullRequestReview")
194
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,
198                                        "event" to event,
199                                        "body" to body),
200                                  Any::class.java)
201
202       fun updateBody(server: GithubServerPath, reviewId: String, newText: String): GQLQuery<GHPullRequestReview> =
203         GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateReview,
204                                  mapOf("reviewId" to reviewId,
205                                        "body" to newText),
206                                  GHPullRequestReview::class.java,
207                                  "updatePullRequestReview", "pullRequestReview")
208
209       fun delete(server: GithubServerPath, reviewId: String): GQLQuery<Any> =
210         GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.deleteReview,
211                                  mapOf("reviewId" to reviewId),
212                                  Any::class.java)
213
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,
218                                         "node", "reviews")
219       }
220
221       private class PendingReviewNodes(nodes: List<GHPullRequestPendingReview>) :
222         GHNodes<GHPullRequestPendingReview>(nodes)
223
224       fun addComment(server: GithubServerPath,
225                      reviewId: String,
226                      body: String, commitSha: String, fileName: String, diffLine: Int)
227         : GQLQuery<GHPullRequestReviewCommentWithPendingReview> =
228         GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.addReviewComment,
229                                  mapOf("reviewId" to reviewId,
230                                        "body" to body,
231                                        "commit" to commitSha,
232                                        "file" to fileName,
233                                        "position" to diffLine),
234                                  GHPullRequestReviewCommentWithPendingReview::class.java,
235                                  "addPullRequestReviewComment", "comment")
236
237       fun addComment(server: GithubServerPath,
238                      reviewId: String,
239                      inReplyTo: String,
240                      body: String)
241         : GQLQuery<GHPullRequestReviewCommentWithPendingReview> =
242         GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.addReviewComment,
243                                  mapOf("reviewId" to reviewId,
244                                        "inReplyTo" to inReplyTo,
245                                        "body" to body),
246                                  GHPullRequestReviewCommentWithPendingReview::class.java,
247                                  "addPullRequestReviewComment", "comment")
248
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")
254
255       fun updateComment(server: GithubServerPath, commentId: String, newText: String): GQLQuery<GHPullRequestReviewComment> =
256         GQLQuery.TraversedParsed(server.toGraphQLUrl(), GHGQLQueries.updateReviewComment,
257                                  mapOf("id" to commentId,
258                                        "body" to newText),
259                                  GHPullRequestReviewComment::class.java,
260                                  "updatePullRequestReviewComment", "pullRequestReviewComment")
261
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")
267
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")
273     }
274   }
275 }