diff --git a/platform/collaboration-tools/src/com/intellij/collaboration/api/page/ApiPageUtil.kt b/platform/collaboration-tools/src/com/intellij/collaboration/api/page/ApiPageUtil.kt index ac074a9ac1a3..beba75e5a21e 100644 --- a/platform/collaboration-tools/src/com/intellij/collaboration/api/page/ApiPageUtil.kt +++ b/platform/collaboration-tools/src/com/intellij/collaboration/api/page/ApiPageUtil.kt @@ -8,12 +8,15 @@ import com.intellij.collaboration.util.URIUtil import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.fold +import org.jetbrains.annotations.ApiStatus import java.net.URI import java.net.http.HttpResponse object ApiPageUtil { - fun createGQLPagesFlow(reversed: Boolean = false, - loader: suspend (GraphQLRequestPagination) -> GraphQLPagedResponseDataDTO?): Flow> = + fun createGQLPagesFlow( + reversed: Boolean = false, + loader: suspend (GraphQLRequestPagination) -> GraphQLPagedResponseDataDTO?, + ): Flow> = flow { var pagination: GraphQLRequestPagination? = GraphQLRequestPagination.DEFAULT while (pagination != null) { @@ -57,6 +60,24 @@ object ApiPageUtil { } } } + + /** + * Prefer not to use this method! + * This is a last resort if Link headers are missing and you need paginated info. + * It's recommended to use the Link header in all other cases though. + * + * Careful: starts pagination at page=1 and increments from there. + */ + @ApiStatus.Internal + fun createPagesFlowByPagination(loader: suspend (page: Int) -> HttpResponse>): Flow>> = + flow { + var page = 1 + do { + val data = loader(page++) + if (data.body().isNotEmpty()) emit(data) + } + while (data.body().isNotEmpty()) + } } suspend fun Flow>.foldToList(): List = foldToList { it } diff --git a/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/api/request/GitLabMergeRequestChangesApi.kt b/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/api/request/GitLabMergeRequestChangesApi.kt index aa53671fc839..9799959c1815 100644 --- a/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/api/request/GitLabMergeRequestChangesApi.kt +++ b/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/api/request/GitLabMergeRequestChangesApi.kt @@ -4,6 +4,7 @@ package org.jetbrains.plugins.gitlab.mergerequest.api.request import com.intellij.collaboration.api.json.loadJsonList import com.intellij.collaboration.api.json.loadJsonValue import com.intellij.collaboration.util.resolveRelative +import com.intellij.collaboration.util.withQuery import org.jetbrains.plugins.gitlab.api.* import org.jetbrains.plugins.gitlab.api.dto.GitLabCommitDetailedRestDTO import org.jetbrains.plugins.gitlab.api.dto.GitLabCommitRestDTO @@ -29,9 +30,8 @@ suspend fun GitLabApi.Rest.loadMergeRequestCommits(uri: URI): HttpResponse + val changes: List, ) @SinceGitLab("9.0", deprecatedIn = "15.7", note = "Deprecated in favour of /diffs") @@ -62,14 +62,15 @@ suspend fun GitLabApi.Rest.loadMergeRequestDiffs(uri: URI): HttpResponse { +suspend fun GitLabApi.Rest.loadCommit( + project: GitLabProjectCoordinates, + commitSha: String, +): HttpResponse { val uri = project.restApiUri .resolveRelative("repository") .resolveRelative("commits") diff --git a/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/data/GitLabMergeRequestChanges.kt b/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/data/GitLabMergeRequestChanges.kt index e43f97b1bf12..26f89d2894d5 100644 --- a/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/data/GitLabMergeRequestChanges.kt +++ b/plugins/gitlab/gitlab-core/src/org/jetbrains/plugins/gitlab/mergerequest/data/GitLabMergeRequestChanges.kt @@ -107,8 +107,9 @@ class GitLabMergeRequestChangesImpl( }.map { it.body().changes }.foldToList(GitLabDiffDTO::toPatch) } else { - ApiPageUtil.createPagesFlowByLinkHeader(api.getMergeRequestDiffsURI(glProject, mergeRequestDetails.iid)) { - api.rest.loadMergeRequestDiffs(it) + // doesn't send back Link headers... + ApiPageUtil.createPagesFlowByPagination { page -> + api.rest.loadMergeRequestDiffs(api.getMergeRequestDiffsURI(glProject, mergeRequestDetails.iid, page)) }.map { it.body() }.foldToList(GitLabDiffDTO::toPatch) } } diff --git a/plugins/gitlab/gitlab-core/testApi/org/jetbrains/plugins/gitlab/apitests/GitLabApiTest.kt b/plugins/gitlab/gitlab-core/testApi/org/jetbrains/plugins/gitlab/apitests/GitLabApiTest.kt index 3c6ef9950310..d92550dc5e2a 100644 --- a/plugins/gitlab/gitlab-core/testApi/org/jetbrains/plugins/gitlab/apitests/GitLabApiTest.kt +++ b/plugins/gitlab/gitlab-core/testApi/org/jetbrains/plugins/gitlab/apitests/GitLabApiTest.kt @@ -66,7 +66,7 @@ class GitLabApiTest : GitLabApiTestCase() { checkVersion(after(v(15, 7))) requiresAuthentication { api -> - val diffs = api.rest.loadMergeRequestDiffs(api.getMergeRequestDiffsURI(glTest1Coordinates, "2")).body() + val diffs = api.rest.loadMergeRequestDiffs(api.getMergeRequestDiffsURI(glTest1Coordinates, "2", 1)).body() assertEquals(glTest1Mr2ChangedFiles, diffs.map { it.newPath }) }