Add GraphQL field for new merge request pipelines
What does this MR do and why?
The Pipelines tab on the new merge request page lists the branch pipelines of every commit in the compare. Only the REST endpoint (CreationsController#pipelines) loads this data. Until GraphQL can load it too, the mr_pipelines_graphql flag cannot be removed.
This MR adds Project.newMergeRequestPipelines, an experiment field on the source project:
project(fullPath: "source/project") {
newMergeRequestPipelines(sourceBranch: "fix", targetBranch: "master", targetProjectPath: "target/project") {
count
nodes { id }
}
}The resolver builds the unsaved merge request with MergeRequests::BuildService, as the controller does. Then it reuses MergeRequestPipelinesResolver. The new merge request page and the existing merge request page then use the same finder, Ci::PipelinesForMergeRequestFinder.
The result is the same as REST:
- The branch pipelines of the source branch, for every commit in the compare. Pipelines of other branches, tags and other SHAs are left out.
- The finder checks
read_pipelineon the source and the target project, so a merge request from a fork works. - The field requires
create_merge_request_fromon the source project, the same check as the controller.
Queries: REST vs. GraphQL
Both paths run the same finder, so the WHERE clause is the same. The GraphQL field adds bounds that the REST endpoint does not have:
REST (CreationsController#pipelines) |
GraphQL (newMergeRequestPipelines) |
|
|---|---|---|
| Commits in the compare | at most 101 (COMPARE_COMMITS_LIMIT) |
the same, from the same BuildService |
| Pipelines loaded | all of them, in one JSON response | at most 101 (LIMIT 101), one more than the badge shows |
| Count | none in the JSON; the badge runs a COUNT limited to 101 |
no SQL COUNT; exact up to 100, and 101 above that. count(limit: 100) gives the badge its 100+ |
| Gitaly compare per request | 1 | 1; the field can be requested for one project per query |
The Ruby behind each path
Shared by both paths. Ci::PipelinesForMergeRequestFinder#execute, on the unsaved merge request path:
# commit_shas comes from the compare, capped at
# MergeRequests::BuildService::COMPARE_COMMITS_LIMIT (101).
source_project.all_pipelines.ci_branch_sources
.for_branch(source_branch)
.for_sha(commit_shas)
.merge_request_event_first
# Scoped further to the projects where the user has read_pipeline.REST, CreationsController#pipelines. The relation is serialized with no limit:
@pipelines = Ci::PipelinesForMergeRequestFinder.new(@merge_request, current_user).execute
PipelineSerializer.new(project: @project, current_user: current_user).represent(@pipelines)GraphQL, MergeRequestPipelinesResolver#query_for, which the new resolver reuses:
finder = ::Ci::PipelinesForMergeRequestFinder.new(mr, current_user)
resolve_pipelines(finder.authorizing_project, args.merge(merge_request_event_first: true))
.unscope(where: :project_id)
.merge(finder.execute)CachingArrayResolver then loads the rows with query.limit(max_page_size). count counts those loaded rows (object.items), so it never runs an SQL COUNT. The cost is that it cannot go above max_page_size.
The new resolver sets max_page_size 101. MergeRequest.pipelines keeps 500. The value comes from the badge:
- The server-rendered badge stops at
100+(PIPELINE_DISPLAY_LIMIT). - The new merge request page asks for
count(limit: 100)in !266580, which returns[rows, 101].min. - With more than 100 pipelines, 101 loaded rows are enough to return
101, so the badge shows100+. A lower cap would make the badge show the cap instead.
The cap also sets how many items the static complexity check prices the connection at. At 500, the page's query scored 848. At 101, it scores 289.
max_page_size |
Static check | At runtime, first: 15 |
|---|---|---|
| 500 | 848 | 135 |
| 101 | 289 | 135 |
| 50 | 218 | 135 |
| 15 | 169 | 135 |
The user-visible effect: on a branch with more than 101 pipelines, the list pages through the newest 101 and no further. The badge is the same.
The new merge request page still renders the tab and the badge on the server, as before. This MR does not change those queries.
The frontend that uses this field is in !266580. It must merge after this MR is deployed: during a rolling deployment, a frontend that asks for a field the backend does not have yet gets an error.
Why a new field, and the choices made
project.pipelinesusesCi::PipelinesFinder. It accepts oneshaand onesource, so it cannot select the branch pipelines for a set of SHAs.- An unsaved
MergeRequestTypewould return nil or fail onid,iidand most other fields. sourceBranchandtargetBranchuse the same names asmergeRequestCreate.targetProjectPathfollows the*_project_pathconvention for full paths.- The field is on the source project.
Project.mergeRequestsis on the target project, but the new merge request page and its permission check use the source project. - The subclass inherits the filter arguments of
ResolvesPipelines, asMergeRequest.pipelinesdoes. They can only narrow the result.
References
- Implements #637319
- Frontend that uses this field, blocked by this MR: !266580
- Unblocks the flag rollout and removal: #419726
- Replaces the head-commit-only attempt in !266261 (closed)
- Flag removal MR, blocked by this work: !266232
- Badge count symptom of the same gap: #637096
- Review suggestion to set
max_page_size: 101: !266573 (comment 4171624235)
Screenshots or screen recordings
Not applicable. This MR adds an API field only. Screenshots come with !266580.
How to set up and validate locally
- Push a branch with two commits, for example
fixin a project with a repository. Let a pipeline run on each commit. - Open
/-/graphql-explorerand run:query { project(fullPath: "<group>/<project>") { newMergeRequestPipelines(sourceBranch: "fix", targetBranch: "master") { count nodes { id sha ref } } } } - Check that both pipelines are listed, newest first, and that
countis2. - Compare with
/<group>/<project>/-/merge_requests/new/pipelines.json?merge_request[source_branch]=fix&merge_request[target_branch]=master.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.