Skip to content
safran

Type to search. Enter opens the first result.

    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_TOKEN
    safran repos
    safran sync
    safran links refresh
    list-projects.ts
    import { 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-TOKEN header.
    • Git gets HTTP Basic with the user oauth2 and the token as password, passed only through the environment of the git process (an http.<origin>.extraHeader setting), 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_TOKEN

    url 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_TOKEN

    At least one of group and projects must be set. projects are added to what the group lists.

    Configuration reference

    FieldTypeDefaultRequiredMeaning
    idstringyesName of this entry; the host of its repositories.
    includestring[][]noGlob patterns on repository names. Empty means all.
    excludestring[][]noGlob patterns on repository names. Exclude wins over include.
    type"gitlab"yesSelects this plugin.
    urlURLhttps://gitlab.comnoRoot of the GitLab instance.
    token_envvariable namenoVariable holding a token with read_api and read_repository. Omit for public projects.
    groupstringone of the twoFull path of a group, like acme/platform. Its projects and those of every subgroup are listed.
    projectsstring[]one of the twoExplicit projects by full path, in addition to the group. At least one when present.
    token_expiresdate (YYYY-MM-DD)noThe 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-api is payments-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.
    • fullName is 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 Link header, moved onto the configured origin before it is requested, so the token only goes to the host in the profile. When a proxy drops Link, the x-next-page header 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 fieldSource
    idThe merge request's iid (the number in its URL)
    title, descriptionAs in GitLab
    mergedAtmerged_at
    mergeCommitThe merge commit; otherwise the squash commit; otherwise the head of the diff (a fast-forward merge creates neither)
    sourceBranch, targetBranchSource and target branch
    linkedTicketKeysThe issues GitLab says the merge request closes
    urlThe 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):

    IssueKey
    In the same projectpayments-api#12 (the repository's short name)
    In another projectIts full reference, e.g. acme/backend/ledger#12
    From an external tracker integrated with GitLab, such as JiraThe 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 explicit projects.
    • 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 to rejected; an unknown state is none. Rows of a listing carry no decisions, so reviewers is absent there: read the merge request for votes.
    • Mergeability. From detailed_merge_status: mergeable is clean, conflict is conflicts, the checking states are unknown, and every other value (approval, pipeline, rebase, draft, …) is blocked.
    • Thread status. A discussion is resolved when its first resolvable note is, otherwise active. GitLab's API has no "outdated" flag, so that status is never used. GitLab's own notes ("added 2 commits") are marked system. 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 truncated with 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, both INVALID_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-url.ts
    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-58

    The 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

    ConstantValueMeaning
    DEFAULT_PULL_REQUEST_LIMIT / MAX_PULL_REQUEST_LIMIT50 / 500Merge requests returned per call.
    GITLAB_MERGE_REQUEST_PAGE_SIZE50Merge requests asked from GitLab per request.
    DEFAULT_MAX_PULL_REQUEST_PAGES20Upstream pages one call reads before returning what it has, with a cursor to continue.
    GITLAB_PAGE_SIZE100Projects, diffs and closing issues per request.
    DEFAULT_MAX_REPO_PAGES100Pages of the project listing (10,000 projects).
    GITLAB_MAX_DIFF_PAGES50Pages of diffs per merge request (5000 changed paths); a larger one is cut, with a warning in the log.
    DEFAULT_ENRICH_CONCURRENCY5Parallel 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_after includes the instant itself; the one-second margin above exists for that reason.

    • Only the first GITLAB_PAGE_SIZE closing 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 unreviewed and reviewed; approved and requested_changes are 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 binary nor truncated.

    • Comments on a whole file use position_type: "file", whose minimum GitLab version is not documented.

    See also