GitLab
The gitlab code host lists the projects of a group (with its subgroups) or an explicit list of projects, supplies the credentials to clone them, and reads merged merge requests with the issues they close and the files they changed. It works with gitlab.com and self-managed GitLab over REST v4. It also reads merge requests as they are now: reviewers and votes, changed files, review threads and diffs. It reads only, unless the profile turns writes on: then it can also comment on a merge request and reply in a review thread.
Safran's neutral word for a merge request is "pull request"; on this page both mean the same thing.
Profile entry
- id: gitlab
type: gitlab
group: acme
token_env: GITLAB_TOKENsafran repos
safran sync
safran links refreshimport { createSafran } from "@safran/core";
await using safran = await createSafran({ profile: "safran.yaml" });
for (const repo of await safran.repos.list()) console.log(repo.name, repo.fullName, repo.webUrl);
const work = await safran.links.query({ ticket: "PAY-1423" });
for (const record of work) console.log(record.repo, record.id, record.title, record.changedFiles.length);Authentication
token_env names a variable holding a token with the read_api and read_repository scopes. It is optional: leave it out for public projects.
- REST requests send the token in the
PRIVATE-TOKENheader. - Git gets HTTP Basic with the user
oauth2and the token as password, passed only through the environment of the git process (anhttp.<origin>.extraHeadersetting), never inside the clone URL.
GitLab answers 404 for a private project the token may not see; the NOT_FOUND hint says so and names the variable.
Self-managed
- id: gitlab
type: gitlab
url: https://gitlab.acme.test
group: acme/platform
token_env: GITLAB_TOKENurl is the root of the instance; the API is reached at <url>/api/v4.
Explicit projects, with or without a group
- id: gitlab
type: gitlab
group: acme/backend
projects: [acme/mobile/acme-ios, acme/mobile/acme-android]
token_env: GITLAB_TOKENAt least one of group and projects must be set. projects are added to what the group lists.
Configuration reference
| Field | Type | Default | Required | Meaning |
|---|---|---|---|---|
id | string | yes | Name of this entry; the host of its repositories. | |
include | string[] | [] | no | Glob patterns on repository names. Empty means all. |
exclude | string[] | [] | no | Glob patterns on repository names. Exclude wins over include. |
type | "gitlab" | yes | Selects this plugin. | |
url | URL | https://gitlab.com | no | Root of the GitLab instance. |
token_env | variable name | no | Variable holding a token with read_api and read_repository. Omit for public projects. | |
group | string | one of the two | Full path of a group, like acme/platform. Its projects and those of every subgroup are listed. | |
projects | string[] | one of the two | Explicit projects by full path, in addition to the group. At least one when present. | |
token_expires | date (YYYY-MM-DD) | no | The day this entry's token stops working, when you know it. Only used to warn two weeks ahead (safran doctor, get_server_info); never for authentication. |
Paths are written without leading or trailing slashes.
How repositories are listed and named
- A group is listed with the projects of all its subgroups. Projects that are merely shared with the group belong to someone else and are left out.
- A project is called by the last segment of its path:
acme/backend/payments-apiispayments-api. - When two projects of the entry share that segment, each becomes its path below the configured group (
backend/tools,mobile/tools). A project outside the group, or one that still collides, uses its full path. fullNameis the full project path.- A project without a default branch is empty and is skipped. Archived projects are kept, with
archived: true. - Pagination follows the
Linkheader, moved onto the configured origin before it is requested, so the token only goes to the host in the profile. When a proxy dropsLink, thex-next-pageheader is used.
If the same short name exists on another code host entry, the host listed first in the profile keeps it and the later one is named <hostId>/<name>.
include and exclude are applied by Safran after the listing, to the short name and to fullName (the full path). * is any run of characters, ? exactly one; matching is case-insensitive and covers the whole name.
- id: gitlab
type: gitlab
group: acme
token_env: GITLAB_TOKEN
include: ["acme/backend/*", "acme/mobile/*"]
exclude: ["*-sandbox"]Merge requests and linked tickets
Merged merge requests are read newest update first.
PullRequest field | Source |
|---|---|
id | The merge request's iid (the number in its URL) |
title, description | As in GitLab |
mergedAt | merged_at |
mergeCommit | The merge commit; otherwise the squash commit; otherwise the head of the diff (a fast-forward merge creates neither) |
sourceBranch, targetBranch | Source and target branch |
linkedTicketKeys | The issues GitLab says the merge request closes |
url | The merge request's web URL |
What counts as a linked ticket. For each merge request the host asks GitLab for the issues that close on merge (one extra request each, DEFAULT_ENRICH_CONCURRENCY at a time):
| Issue | Key |
|---|---|
| In the same project | payments-api#12 (the repository's short name) |
| In another project | Its full reference, e.g. acme/backend/ledger#12 |
| From an external tracker integrated with GitLab, such as Jira | The external id as GitLab reports it, e.g. PAY-1423 |
When that request fails for one merge request with 403 or 404, the merge request is still listed, without linked issues, and a warning is logged. A bad token, a rate limit or a server error fails the call.
Safran then adds the keys it finds in the title, the source branch and the description, for the project keys declared under trackers[].projects.
Changed files are the new paths of the merge request's diffs, without deleted files.
mergedSince is sent as GitLab's updated_after, one second early, and the exact cut is made on the client against merged_at. merged_after is not used because it only exists from GitLab 17.2 on.
Live merge requests
With the capabilities pull-requests-live, pr-diff and pr-threads the connector reads merge requests as they are now: safran pulls, safran.pulls and the pull request tools. A merge request is a pull request everywhere in Safran, and repo!12 is accepted next to repo#12.
- References.
{web root}/{group…}/{project}/-/merge_requests/{iid}and what the UI hangs behind it (/diffs,/commits,/pipelines, a query,#note_…), plus the older address without/-/. The project must lie in the configured group or be one of the explicitprojects. - Votes. Reviewers and decisions come from three places: the merge request's
reviewers, their per-reviewer state, and the approvals. An approval counts whoever gave it, asked to review or not. A requested-changes state maps torejected; an unknown state isnone. Rows of a listing carry no decisions, soreviewersis absent there: read the merge request for votes. - Mergeability. From
detailed_merge_status:mergeableisclean,conflictisconflicts, the checking states areunknown, and every other value (approval, pipeline, rebase, draft, …) isblocked. - Thread status. A discussion is
resolvedwhen its first resolvable note is, otherwiseactive. GitLab's API has no "outdated" flag, so that status is never used. GitLab's own notes ("added 2 commits") are markedsystem. The discussion id is the thread id. - Diffs are the patches GitLab computed; the context cannot be varied. A diff GitLab left out as too large or collapsed comes back
truncatedwith an empty patch. Line counts are not sent; they are counted from a complete diff.
Writes
Off unless the profile enables them; see writes.
pull_request.comment(write:pr-comment): starts one discussion. For a place in the diff GitLab wants both line numbers of an unchanged line, so the file's diff is read first; that also settles two refusals before anything is sent: a path the merge request does not change, and a line outside the diff, bothINVALID_INPUT.pull_request.reply(write:pr-reply): adds a note to an existing discussion. A general comment can be answered too.
The token needs the api scope. read_api, which is enough for everything else on this page, is refused here.
File links
import { createSafran } from "@safran/core";
await using safran = await createSafran({ profile: "safran.yaml" });
await safran.repos.list(); // fileUrl needs the project list once, to know the full path
const url = safran.hosts.get("gitlab")?.fileUrl?.("payments-api", "src/refund/service.ts", "main", { start: 40, end: 58 });
console.log(url); // …/acme/backend/payments-api/-/blob/main/src/refund/service.ts#L40-58The method is synchronous. It returns undefined for a name it has not seen in a listing yet, unless the name is a full path listed under projects.
Limits
| Constant | Value | Meaning |
|---|---|---|
DEFAULT_PULL_REQUEST_LIMIT / MAX_PULL_REQUEST_LIMIT | 50 / 500 | Merge requests returned per call. |
GITLAB_MERGE_REQUEST_PAGE_SIZE | 50 | Merge requests asked from GitLab per request. |
DEFAULT_MAX_PULL_REQUEST_PAGES | 20 | Upstream pages one call reads before returning what it has, with a cursor to continue. |
GITLAB_PAGE_SIZE | 100 | Projects, diffs and closing issues per request. |
DEFAULT_MAX_REPO_PAGES | 100 | Pages of the project listing (10,000 projects). |
GITLAB_MAX_DIFF_PAGES | 50 | Pages of diffs per merge request (5000 changed paths); a larger one is cut, with a warning in the log. |
DEFAULT_ENRICH_CONCURRENCY | 5 | Parallel requests for closing issues. |
Known caveats
This connector is implemented from GitLab's API documentation and tested against recorded-shape fixtures; it has not been run against a live instance. See verification status. One item is marked UNVERIFIED in the source:
- Missing branch or missing project. When asked for the head of a branch, GitLab answers 404 for a branch that does not exist and for a project the token cannot see. The documentation does not describe the two bodies. The connector treats a body that says "project not found" as a hidden project (
NOT_FOUND) and anything else as "no such branch" (undefined).
Also worth knowing:
-
The documentation does not say whether
updated_afterincludes the instant itself; the one-second margin above exists for that reason. -
Only the first
GITLAB_PAGE_SIZEclosing issues of a merge request are read. -
Live merge requests and writes. None of the merge request endpoints was exercised against a live instance.
-
Reviewer states. The documentation shows only
unreviewedandreviewed;approvedandrequested_changesare the names the product uses. -
Binary files. The documentation says nothing about them. git's own "Binary files … differ" notice is recognised; a binary file sent with an empty diff comes back as an empty patch that is neither
binarynortruncated. -
Comments on a whole file use
position_type: "file", whose minimum GitLab version is not documented.
See also
- Repos in the SDK and links.
- Multiple sources for profiles with more than one code host.
- Connectors overview for the rules shared by all connectors.