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, 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_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.

    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.

    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.

    See also