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, and it is read-only.
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. |
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.
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.
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.