Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🦊 feat: Look Up GitLab Merge Requests Behind a Provider Setting (#16986)

Marco Berettab24bfc0dd0ff
diff --git a/api/server/routes/__test-utils__/convos-route-mocks.js b/api/server/routes/__test-utils__/convos-route-mocks.js
index 402c0877dd..4b9e7a6df3 100644
--- a/api/server/routes/__test-utils__/convos-route-mocks.js
+++ b/api/server/routes/__test-utils__/convos-route-mocks.js
@@ -147,7 +147,7 @@ module.exports = {
() => (_req, res) => res.status(200).json({ tasks: [] }),
),
createBackgroundTaskPolicyMiddleware: jest.fn(() => (_req, _res, next) => next()),
- createGitHubPullRequestSource: jest.fn(() => ({ find: jest.fn() })),
+ createPullRequestSource: jest.fn(() => ({ find: jest.fn() })),
createProxyAwareFetch: jest.fn(() => jest.fn()),
createPullRequestLookup: jest.fn(() => jest.fn()),
createConversationPullRequestsHandler: jest.fn(() => jest.fn()),
diff --git a/api/server/routes/convos.js b/api/server/routes/convos.js
index 95ab08f8a1..795d983f59 100644
--- a/api/server/routes/convos.js
+++ b/api/server/routes/convos.js
@@ -20,7 +20,7 @@ const {
createBackgroundTaskCancelHandler,
createBackgroundTaskPolicyMiddleware,
createConversationPullRequestHandler,
- createGitHubPullRequestSource,
+ createPullRequestSource,
createProxyAwareFetch,
createPullRequestLookup,
createConversationPullRequestsHandler,
@@ -174,7 +174,7 @@ const backgroundTaskCancelHandler = createBackgroundTaskCancelHandler({
});
/** One lookup, so the header's single route and the sidebar's batch route share its cache. */
const pullRequestLookup = createPullRequestLookup({
- source: createGitHubPullRequestSource({ fetchFn: createProxyAwareFetch() }),
+ source: createPullRequestSource({ fetchFn: createProxyAwareFetch() }),
});
const conversationPullRequestHandler = createConversationPullRequestHandler({
getConvoLaneGit: db.getConvoLaneGit,
diff --git a/client/src/components/Chat/PullRequest/Card.tsx b/client/src/components/Chat/PullRequest/Card.tsx
index 5d45279a9e..5140e002f0 100644
--- a/client/src/components/Chat/PullRequest/Card.tsx
+++ b/client/src/components/Chat/PullRequest/Card.tsx
@@ -1,10 +1,19 @@
import { memo } from 'react';
+import { ExternalLink, Gitlab } from 'lucide-react';
import { Button, Chip, GithubIcon, TooltipAnchor } from '@librechat/client';
-import type { TConversationPullRequest } from 'librechat-data-provider';
+import type { PullRequestProvider, TConversationPullRequest } from 'librechat-data-provider';
+import type { ComponentType } from 'react';
import { presentPullRequest } from './status';
import { useLocalize } from '~/hooks';
import PullRequestIcon from './Icon';
+/** Bitbucket has no mark in the icon sets the app ships, so its link shows a plain external one. */
+const PROVIDER_ICONS: Record<PullRequestProvider, ComponentType> = {
+ github: GithubIcon,
+ gitlab: Gitlab,
+ bitbucket: ExternalLink,
+};
+
type CardProps = {
pullRequest: TConversationPullRequest;
/** A refresh failed; the last known pull request stays visible with a way to retry. */
@@ -22,14 +31,15 @@ function PullRequestCard({
}: CardProps) {
const localize = useLocalize();
const view = presentPullRequest(pullRequest);
- const openLabel = localize('com_ui_pr_open_in_github');
+ const openLabel = localize(view.openKey);
+ const ProviderIcon = PROVIDER_ICONS[view.provider];
return (
<div className="flex flex-col gap-3 p-3" data-testid="pull-request-card">
<div className="flex items-center gap-2.25">
<PullRequestIcon icon={view.icon} tone={view.iconTone} className="size-4 shrink-0" />
<span className="text-text-primary text-sm font-medium">
- {localize('com_ui_pr_label', { 0: pullRequest.number })}
+ {localize(view.labelKey, { 0: pullRequest.number })}
</span>
<span className="ml-auto flex items-center gap-1.5 text-xs font-medium">
<span
@@ -57,7 +67,7 @@ function PullRequestCard({
data-testid="pull-request-github-link"
className="text-text-secondary hover:text-text-primary focus-visible:ring-text-primary flex size-7 shrink-0 items-center justify-center rounded-md outline-hidden focus-visible:ring-2 [&_svg]:size-4"
>
- <GithubIcon />
+ <ProviderIcon />
</a>
}
/>
diff --git a/client/src/components/Chat/PullRequest/__tests__/Card.test.tsx b/client/src/components/Chat/PullRequest/__tests__/Card.test.tsx
index d8391eca49..ee079494a1 100644
--- a/client/src/components/Chat/PullRequest/__tests__/Card.test.tsx
+++ b/client/src/components/Chat/PullRequest/__tests__/Card.test.tsx
@@ -64,6 +64,35 @@ describe('PullRequestCard', () => {
layer.remove();
});
+ it('names a GitLab merge request the way GitLab does and links to GitLab', () => {
+ const mr = {
+ ...pr,
+ provider: 'gitlab' as const,
+ url: 'https://gitlab.com/g/r/-/merge_requests/7',
+ };
+ render(<PullRequestCard pullRequest={mr} />);
+ expect(screen.getByText('com_ui_pr_label_gitlab:7')).toBeInTheDocument();
+ const link = screen.getByTestId('pull-request-github-link');
+ expect(link).toHaveAccessibleName('com_ui_pr_open_in_gitlab');
+ expect(link).toHaveAttribute('href', mr.url);
+ expect(link.querySelector('svg')).toHaveClass('lucide-gitlab');
+ });
+
+ it('links a Bitbucket pull request to Bitbucket', () => {
+ render(<PullRequestCard pullRequest={{ ...pr, provider: 'bitbucket' }} />);
+ expect(screen.getByText('com_ui_pr_label:7')).toBeInTheDocument();
+ expect(screen.getByTestId('pull-request-github-link')).toHaveAccessibleName(
+ 'com_ui_pr_open_in_bitbucket',
+ );
+ });
+
+ it('treats a pull request without a provider, from an older server, as GitHub', () => {
+ render(<PullRequestCard pullRequest={pr} />);
+ expect(screen.getByTestId('pull-request-github-link')).toHaveAccessibleName(
+ 'com_ui_pr_open_in_github',
+ );
+ });
+
it('colors added lines and removed lines with their own roles and names them for a screen reader', () => {
render(<PullRequestCard pullRequest={pr} />);
expect(screen.getByText('+12')).toHaveClass('text-status-success');
diff --git a/client/src/components/Chat/PullRequest/__tests__/status.test.ts b/client/src/components/Chat/PullRequest/__tests__/status.test.ts
index c037d79f3c..d31c14696c 100644
--- a/client/src/components/Chat/PullRequest/__tests__/status.test.ts
+++ b/client/src/components/Chat/PullRequest/__tests__/status.test.ts
@@ -27,6 +27,21 @@ describe('presentPullRequest', () => {
});
});
+ it("speaks of a GitLab merge request in GitLab's words, and of anything else as GitHub", () => {
+ expect(presentPullRequest(make({ provider: 'gitlab' }))).toMatchObject({
+ provider: 'gitlab',
+ nounKey: 'com_ui_merge_request',
+ labelKey: 'com_ui_pr_label_gitlab',
+ openKey: 'com_ui_pr_open_in_gitlab',
+ });
+ expect(presentPullRequest(base)).toMatchObject({
+ provider: 'github',
+ nounKey: 'com_ui_pull_request',
+ labelKey: 'com_ui_pr_label',
+ openKey: 'com_ui_pr_open_in_github',
+ });
+ });
+
it('shows merge conflicts as an error on the icon and the state badge', () => {
expect(presentPullRequest(make({ mergeable: 'conflicting' }))).toMatchObject({
icon: 'open',
diff --git a/client/src/components/Chat/PullRequest/menu.tsx b/client/src/components/Chat/PullRequest/menu.tsx
index 079b2bf2a2..fde9379e95 100644
--- a/client/src/components/Chat/PullRequest/menu.tsx
+++ b/client/src/components/Chat/PullRequest/menu.tsx
@@ -44,7 +44,7 @@ export default function usePullRequestMenu(conversationId: string): PullRequestM
return {
item: {
id: 'header-pull-request',
- label: localize('com_ui_pr_label', { 0: pullRequest.number }),
+ label: localize(view.labelKey, { 0: pullRequest.number }),
ariaLabel: summarizePullRequest(pullRequest, localize),
icon: <PullRequestIcon icon={view.icon} tone={view.iconTone} />,
onClick: () => setOpenFor(conversationId),
diff --git a/client/src/components/Chat/PullRequest/status.ts b/client/src/components/Chat/PullRequest/status.ts
index 9205b20bf8..5997d3f261 100644
--- a/client/src/components/Chat/PullRequest/status.ts
+++ b/client/src/components/Chat/PullRequest/status.ts
@@ -1,4 +1,4 @@
-import type { TConversationPullRequest } from 'librechat-data-provider';
+import type { PullRequestProvider, TConversationPullRequest } from 'librechat-data-provider';
import type { TranslationKeys } from '~/hooks/useLocalize';
export type PullRequestTone = 'success' | 'error' | 'warning' | 'neutral' | 'merged';
@@ -15,6 +15,33 @@ export type PullRequestPresentation = {
stateTone: PullRequestTone;
checksKey: TranslationKeys;
checksTone: PullRequestTone;
+ provider: PullRequestProvider;
+ /** What the provider calls it ("Merge request" on GitLab), and its short label ("MR !7"). */
+ nounKey: TranslationKeys;
+ labelKey: TranslationKeys;
+ /** Names the provider the card's link opens. */
+ openKey: TranslationKeys;
+};
+
+const PROVIDERS: Record<
+ PullRequestProvider,
+ Pick<PullRequestPresentation, 'nounKey' | 'labelKey' | 'openKey'>
+> = {
+ github: {
+ nounKey: 'com_ui_pull_request',
+ labelKey: 'com_ui_pr_label',
+ openKey: 'com_ui_pr_open_in_github',
+ },
+ gitlab: {
+ nounKey: 'com_ui_merge_request',
+ labelKey: 'com_ui_pr_label_gitlab',
+ openKey: 'com_ui_pr_open_in_gitlab',
+ },
+ bitbucket: {
+ nounKey: 'com_ui_pull_request',
+ labelKey: 'com_ui_pr_label',
+ openKey: 'com_ui_pr_open_in_bitbucket',
+ },
};
const CHECKS: Record<
@@ -73,8 +100,12 @@ function stateOf(
/** The one place a pull request becomes icon, dot and badges, so the chip and card never disagree. */
export function presentPullRequest(pr: TConversationPullRequest): PullRequestPresentation {
const checks = CHECKS[pr.checks];
+ /** A server from before providers sends none, and only knew GitHub. */
+ const provider = pr.provider ?? 'github';
return {
...stateOf(pr),
+ ...PROVIDERS[provider],
+ provider,
dotTone: pr.state === 'open' ? checks.dot : null,
checksKey: checks.key,
checksTone: checks.tone,
diff --git a/client/src/components/Chat/PullRequest/summary.ts b/client/src/components/Chat/PullRequest/summary.ts
index 4fc3769144..3b1ca4cbd7 100644
--- a/client/src/components/Chat/PullRequest/summary.ts
+++ b/client/src/components/Chat/PullRequest/summary.ts
@@ -9,7 +9,7 @@ export function summarizePullRequest(
): string {
const view = presentPullRequest(pr);
return [
- `${localize('com_ui_pull_request')} ${localize('com_ui_pr_label', { 0: pr.number })}: ${pr.title}`,
+ `${localize(view.nounKey)} ${localize(view.labelKey, { 0: pr.number })}: ${pr.title}`,
localize('com_ui_pr_state', { 0: localize(view.stateKey) }),
localize('com_ui_pr_checks', { 0: localize(view.checksKey) }),
].join(', ');
diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json
index b0ae4acdf7..293183153d 100644
--- a/client/src/locales/en/translation.json
+++ b/client/src/locales/en/translation.json
@@ -7,8 +7,11 @@
"com_ui_pr_checks_running": "Running",
"com_ui_pr_deletions": "{{0}} lines removed",
"com_ui_pr_label": "PR #{{0}}",
+ "com_ui_pr_label_gitlab": "MR !{{0}}",
"com_ui_pr_load_failed": "Couldn't load the pull request",
+ "com_ui_pr_open_in_bitbucket": "Open in Bitbucket",
"com_ui_pr_open_in_github": "Open in GitHub",
+ "com_ui_pr_open_in_gitlab": "Open in GitLab",
"com_ui_pr_refresh_failed": "Couldn't refresh the pull request",
"com_ui_pr_state": "State: {{0}}",
"com_ui_pr_state_closed": "Closed",
@@ -2276,6 +2279,7 @@
"com_ui_memory_updated": "Updated saved memory",
"com_ui_memory_would_exceed": "Cannot save - would exceed limit by {{tokens}} tokens. Delete existing memories to make space.",
"com_ui_mention": "Mention an endpoint, assistant, or preset to quickly switch to it",
+ "com_ui_merge_request": "Merge request",
"com_ui_mermaid": "mermaid",
"com_ui_mermaid_diagram": "Mermaid diagram",
"com_ui_mermaid_export_complete": "Diagram download started.",
diff --git a/librechat.example.yaml b/librechat.example.yaml
index 80d64276c5..9c9b7e1d22 100644
--- a/librechat.example.yaml
+++ b/librechat.example.yaml
@@ -938,9 +938,15 @@ endpoints:
# # repositories involved, with read-only "Pull requests", "Checks" and "Contents" permissions.
# # Without "Checks" every matched pull request fails to load, and without "Contents" a chat
# # whose branch has moved on since its last command does.
+ # # On GitLab the fallbacks are GITLAB_PULL_REQUEST_TOKEN and GITLAB_TOKEN, and the token needs
+ # # read_api; on Bitbucket the fallback is BITBUCKET_PULL_REQUEST_TOKEN. Repositories may be
+ # # nested GitLab group paths such as group/sub/name or group/sub/*.
# pullRequests:
# enabled: true # set false to turn the feature off
- # # GitHub Enterprise Server or a *.ghe.com tenant; github.com when omitted.
+ # # github (default), gitlab, or bitbucket (Bitbucket Cloud). Read from this file only.
+ # # provider: "gitlab"
+ # # GitHub Enterprise Server, a *.ghe.com tenant, or a self-managed GitLab host; the
+ # # provider's own host (github.com, gitlab.com, bitbucket.org) when omitted.
# # host: "github.example.com"
# # Environment variable reference only, never the token itself. Optional, see above.
# token: "${GITHUB_PULL_REQUEST_TOKEN}"
diff --git a/packages/api/src/code/lane.spec.ts b/packages/api/src/code/lane.spec.ts
index 6807086225..fbf9786d54 100644
--- a/packages/api/src/code/lane.spec.ts
+++ b/packages/api/src/code/lane.spec.ts
@@ -77,7 +77,15 @@ describe('createLaneGitRecorder', () => {
);
});
- it.each(['../x', 'o/..', 'a b/c', 'owner', 'o/r/extra', 'x'.repeat(300)])(
+ it('stores a nested GitLab group path with the lane', async () => {
+ const { record, setConvoLaneGit } = make({ repo: 'group/sub/project' });
+ await record?.(laneGit);
+ expect(setConvoLaneGit).toHaveBeenCalledWith(
+ expect.objectContaining({ repo: 'group/sub/project' }),
+ );
+ });
+
+ it.each(['../x', 'o/..', 'o/./r', 'o//r', 'a b/c', 'owner', 'x'.repeat(300)])(
'drops the unsafe repository %s but still records the lane',
async (repo) => {
const { record, setConvoLaneGit } = make({ repo });
diff --git a/packages/api/src/code/lane.ts b/packages/api/src/code/lane.ts
index 47c1740ea1..3a2c2bb88c 100644
--- a/packages/api/src/code/lane.ts
+++ b/packages/api/src/code/lane.ts
@@ -42,7 +42,7 @@ export type LaneContextReader = (
codeAttachmentEpoch?: number;
} | null>;
-const REPO_PATTERN = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
+const REPO_PATTERN = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+){1,19}$/;
const MAX_REPO_LENGTH = 256;
const isSafeRepo = (repo: string | undefined): repo is string =>
diff --git a/packages/api/src/pulls/github.spec.ts b/packages/api/src/pulls/github.spec.ts
index a8bd606349..3040ef0768 100644
--- a/packages/api/src/pulls/github.spec.ts
+++ b/packages/api/src/pulls/github.spec.ts
@@ -67,6 +67,7 @@ describe('createGitHubPullRequestSource', () => {
number: 7,
title: 'Simplify single tool',
url: 'https://github.com/o/r/pull/7',
+ provider: 'github',
additions: 12,
deletions: 3,
state: 'open',
diff --git a/packages/api/src/pulls/github.ts b/packages/api/src/pulls/github.ts
index 354ee7e9ef..81c9d3eece 100644
--- a/packages/api/src/pulls/github.ts
+++ b/packages/api/src/pulls/github.ts
@@ -4,27 +4,36 @@ import type {
PullRequestState,
TConversationPullRequest,
} from 'librechat-data-provider';
+import type { PullRequestFetch } from './shared';
import type { PullRequestSource } from './types';
+import {
+ isCount,
+ isRecord,
+ COMMIT_ID,
+ isPageUrl,
+ retryAfterMs,
+ isPathSegment,
+ MAX_TITLE_LENGTH,
+ DEFAULT_LOOKUP_TIMEOUT_MS,
+ DEFAULT_REQUEST_TIMEOUT_MS,
+ DEFAULT_MAX_CANDIDATE_PAGES,
+ DEFAULT_MAX_HEAD_COMPARISONS,
+ DEFAULT_MAX_CANDIDATE_PULL_REQUESTS,
+} from './shared';
import { isAllowedRepository } from './repository';
import { PullRequestSourceError } from './types';
+export type { PullRequestFetch } from './shared';
+
export const DEFAULT_GITHUB_HOST = 'github.com';
-/** Defaults for the operator bounds in `endpoints.agents.pullRequests`. */
-const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
-const DEFAULT_LOOKUP_TIMEOUT_MS = 30_000;
const DEFAULT_MAX_CHECK_RUN_PAGES = 10;
const CHECK_RUN_PAGE_SIZE = 100;
-const DEFAULT_MAX_CANDIDATE_PULL_REQUESTS = 10;
-const DEFAULT_MAX_HEAD_COMPARISONS = 3;
-const DEFAULT_MAX_CANDIDATE_PAGES = 1;
/**
* Comparing `recorded...candidate`: `ahead` means the candidate builds on the recorded commit and
* `identical` that it is the recorded commit. `behind` means the candidate does not contain it
* (the recorded commit is newer), and `diverged` that the histories split, so neither matches.
*/
const CONTAINS_RECORDED_STATUSES = new Set(['ahead', 'identical']);
-const COMMIT_ID = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
-const MAX_TITLE_LENGTH = 256;
const REPO_PATTERN = /^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/;
const FAILING_CONCLUSIONS = new Set([
'failure',
@@ -35,9 +44,6 @@ const FAILING_CONCLUSIONS = new Set([
'stale',
]);
-/** The only part of `fetch` this module calls, so any compatible client can be injected. */
-export type PullRequestFetch = (input: string, init?: RequestInit) => Promise<Response>;
-
type GitHubPull = {
number: number;
title: string;
@@ -58,12 +64,6 @@ type GitHubPull = {
type GitHubCheckRun = { status: string; conclusion: string | null };
-const isRecord = (value: unknown): value is Record<string, unknown> =>
- value != null && typeof value === 'object' && !Array.isArray(value);
-
-const isCount = (value: unknown): value is number =>
- typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
-
/**
* The REST API of a GitHub host: `api.github.com` for github.com, `api.<host>` for a data residency
* tenant on `ghe.com`, and `/api/v3` on the host itself for GitHub Enterprise Server.
@@ -74,23 +74,12 @@ export function gitHubApiBase(host: string): string {
return `https://${host}/api/v3`;
}
-/** The URL ends up in an href, so only a plain https page on the configured host is accepted. */
-function isGitHubPageUrl(value: unknown, host: string): value is string {
- if (typeof value !== 'string') return false;
- try {
- const url = new URL(value);
- return url.protocol === 'https:' && url.host === host;
- } catch {
- return false;
- }
-}
-
function parsePull(value: unknown, host: string): GitHubPull {
if (
!isRecord(value) ||
!isCount(value.number) ||
typeof value.title !== 'string' ||
- !isGitHubPageUrl(value.html_url, host) ||
+ !isPageUrl(value.html_url, host) ||
(value.state !== 'open' && value.state !== 'closed') ||
typeof value.merged !== 'boolean' ||
typeof value.draft !== 'boolean' ||
@@ -192,6 +181,7 @@ export function toConversationPullRequest(
number: pull.number,
title: pull.title.slice(0, MAX_TITLE_LENGTH),
url: pull.url,
+ provider: 'github',
additions: pull.additions,
deletions: pull.deletions,
state: summarizeState(pull),
@@ -229,27 +219,11 @@ async function isSecondaryLimit(response: Response): Promise<boolean> {
}
}
-/** How long GitHub asked callers to wait: `retry-after` seconds, else the quota reset time. */
-function retryAfterMs(response: Response): number | undefined {
- const retryAfter = Number(response.headers.get('retry-after'));
- if (response.headers.get('retry-after') != null && Number.isFinite(retryAfter)) {
- return Math.max(0, retryAfter * 1000);
- }
- const reset = Number(response.headers.get('x-ratelimit-reset'));
- if (response.headers.get('x-ratelimit-reset') != null && Number.isFinite(reset)) {
- return Math.max(0, reset * 1000 - Date.now());
- }
- return undefined;
-}
-
/**
* Reads pull requests through GitHub's REST API with a token the caller supplies. Only the
* decision (absent, rate limited, failed) leaves this module; response bodies and error text
* never do.
*/
-/** `.` and `..` match the character class but would rewrite the API path. */
-const isPathSegment = (value: string): boolean => value !== '.' && value !== '..';
-
export function createGitHubPullRequestSource({
fetchFn,
}: {
@@ -300,7 +274,7 @@ export function createGitHubPullRequestSource({
}
}
if (isRateLimited(response)) {
- const hinted = retryAfterMs(response);
+ const hinted = retryAfterMs(response, 'x-ratelimit-reset');
/** A 429 that names no wait may still be a secondary limit, told apart by its message. */
const wait =
hinted == null && (await isSecondaryLimit(response)) ? SECONDARY_LIMIT_WAIT_MS : hinted;
diff --git a/packages/api/src/pulls/gitlab.spec.ts b/packages/api/src/pulls/gitlab.spec.ts
new file mode 100644
index 0000000000..347425a768
--- /dev/null
+++ b/packages/api/src/pulls/gitlab.spec.ts
@@ -0,0 +1,303 @@
+import { createGitLabPullRequestSource, summarizePipeline } from './gitlab';
+
+const sha = 'a'.repeat(40);
+const newer = 'b'.repeat(40);
+const mergeRequest = (overrides: Record<string, unknown> = {}) => ({
+ iid: 7,
+ title: 'Simplify single tool',
+ web_url: 'https://gitlab.com/g/r/-/merge_requests/7',
+ state: 'opened',
+ draft: false,
+ sha,
+ source_branch: 'feat/x',
+ source_project_id: 1,
+ target_project_id: 1,
+ detailed_merge_status: 'mergeable',
+ has_conflicts: false,
+ head_pipeline: { status: 'success', sha },
+ ...overrides,
+});
+const listed = (overrides: Record<string, unknown> = {}) => [
+ { iid: 7, state: 'opened', sha, source_project_id: 1, target_project_id: 1, ...overrides },
+];
+const stats = (additions = 12, deletions = 3) => ({
+ data: { project: { mergeRequest: { diffStatsSummary: { additions, deletions } } } },
+});
+const json = (body: unknown, init?: ResponseInit) => Response.json(body, init);
+
+function sourceFor(routes: Record<string, () => Response>) {
+ const fetchFn = jest.fn(async (input: string, _init?: RequestInit) => {
+ const url = String(input);
+ const key = Object.keys(routes).find((fragment) => url.includes(fragment));
+ if (key == null) throw new Error(`unexpected ${url}`);
+ return routes[key]();
+ });
+ return { fetchFn, source: createGitLabPullRequestSource({ fetchFn }) };
+}
+
+const routes = (detail = mergeRequest(), overrides: Record<string, () => Response> = {}) => ({
+ 'state=opened': () => json(listed()),
+ 'state=all': () => json([]),
+ '/merge_requests/7': () => json(detail),
+ '/api/graphql': () => json(stats()),
+ ...overrides,
+});
+
+const find = (
+ source: ReturnType<typeof createGitLabPullRequestSource>,
+ input: Record<string, unknown> = {},
+) => source.find({ repo: 'g/r', branch: 'feat/x', token: 't', ...input });
+
+describe('summarizePipeline', () => {
+ it.each([
+ [null, 'none'],
+ ['success', 'passing'],
+ ['skipped', 'passing'],
+ ['failed', 'failing'],
+ ['canceled', 'failing'],
+ ['running', 'running'],
+ ['pending', 'running'],
+ ['manual', 'running'],
+ ])('reports %s as %s', (status, expected) => {
+ expect(summarizePipeline(status)).toBe(expected);
+ });
+});
+
+describe('createGitLabPullRequestSource', () => {
+ it('maps an open, mergeable merge request with a passing pipeline', async () => {
+ const { source } = sourceFor(routes());
+ await expect(find(source)).resolves.toEqual({
+ number: 7,
+ title: 'Simplify single tool',
+ url: 'https://gitlab.com/g/r/-/merge_requests/7',
+ provider: 'gitlab',
+ additions: 12,
+ deletions: 3,
+ state: 'open',
+ isDraft: false,
+ mergeable: 'clean',
+ checks: 'passing',
+ });
+ });
+
+ it('asks the project by its encoded path, with a bearer token, and nowhere else', async () => {
+ const { source, fetchFn } = sourceFor(routes());
+ await find(source);
+ const urls = fetchFn.mock.calls.map(([url]) => String(url));
+ expect(urls[0]).toContain('https://gitlab.com/api/v4/projects/g%2Fr/merge_requests?');
+ expect(urls.every((url) => url.startsWith('https://gitlab.com/api/'))).toBe(true);
+ const init = fetchFn.mock.calls[0][1] ?? {};
+ expect((init.headers as Record<string, string>).Authorization).toBe('Bearer t');
+ });
+
+ it('reads a nested group project on a self-managed host', async () => {
+ const host = 'gitlab.example.com';
+ const { source, fetchFn } = sourceFor(
+ routes(mergeRequest({ web_url: `https://${host}/g/sub/r/-/merge_requests/7` })),
+ );
+ const result = await find(source, { repo: 'g/sub/r', host });
+ expect(result?.url).toBe(`https://${host}/g/sub/r/-/merge_requests/7`);
+ const urls = fetchFn.mock.calls.map(([url]) => String(url));
+ expect(urls[0]).toContain(`https://${host}/api/v4/projects/g%2Fsub%2Fr/`);
+ expect(urls.every((url) => url.startsWith(`https://${host}/api/`))).toBe(true);
+ });
+
+ it.each([
+ ['another host', 'https://evil.example/g/r/-/merge_requests/7'],
+ ['plain http', 'http://gitlab.com/g/r/-/merge_requests/7'],
+ ['a javascript url', 'javascript:alert(1)'],
+ ])('rejects %s as the merge request page', async (_label, web_url) => {
+ const { source } = sourceFor(routes(mergeRequest({ web_url })));
+ await expect(find(source)).rejects.toMatchObject({ code: 'UPSTREAM_ERROR' });
+ });
+
+ it.each([
+ ['merged', 'merged'],
+ ['closed', 'closed'],
+ ['locked', 'closed'],
+ ])('reports a %s merge request as %s, with no conflict state', async (state, expected) => {
+ const { source } = sourceFor({
+ 'state=opened': () => json([]),
+ 'state=all': () => json(listed({ state })),
+ '/merge_requests/7': () => json(mergeRequest({ state, has_conflicts: true })),
+ '/api/graphql': () => json(stats()),
+ });
+ await expect(find(source)).resolves.toMatchObject({ state: expected, mergeable: 'unknown' });
+ });
+
+ it('reports conflicts and drafts while open', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest({ has_conflicts: true, detailed_merge_status: 'conflict', draft: true })),
+ );
+ await expect(find(source)).resolves.toMatchObject({
+ mergeable: 'conflicting',
+ isDraft: true,
+ });
+ });
+
+ it('ignores a pipeline that ran for an earlier head', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest({ head_pipeline: { status: 'failed', sha: newer } })),
+ );
+ await expect(find(source)).resolves.toMatchObject({ checks: 'none' });
+ });
+
+ it('reports zero lines for a merge request without changes', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest(), {
+ '/api/graphql': () =>
+ json({ data: { project: { mergeRequest: { diffStatsSummary: null } } } }),
+ }),
+ );
+ await expect(find(source)).resolves.toMatchObject({ additions: 0, deletions: 0 });
+ });
+
+ it('fails rather than invent line counts when GraphQL reports an error', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest(), { '/api/graphql': () => json({ errors: [{ message: 'secret' }] }) }),
+ );
+ const error = await find(source).catch((caught: Error) => caught);
+ expect(error).toMatchObject({ code: 'UPSTREAM_ERROR' });
+ expect(String((error as Error).message)).not.toContain('secret');
+ });
+
+ it('finds nothing for a project the token cannot see', async () => {
+ const { source } = sourceFor({ 'state=opened': () => new Response('', { status: 404 }) });
+ await expect(find(source)).resolves.toBeNull();
+ });
+
+ it('reports a rate limit with the wait GitLab asked for', async () => {
+ const { source } = sourceFor({
+ 'state=opened': () => new Response('', { status: 429, headers: { 'retry-after': '30' } }),
+ });
+ await expect(find(source)).rejects.toMatchObject({
+ code: 'RATE_LIMITED',
+ retryAfterMs: 30_000,
+ });
+ });
+
+ it('finds nothing for a lane that has a branch but no commit yet', async () => {
+ const { source, fetchFn } = sourceFor(routes());
+ await expect(find(source, { head: null })).resolves.toBeNull();
+ expect(fetchFn).not.toHaveBeenCalled();
+ });
+
+ it.each(['g', 'g/../r', 'g/./r', 'g/r?x', ''])('asks nothing for the path %p', async (repo) => {
+ const { source, fetchFn } = sourceFor(routes());
+ await expect(find(source, { repo })).resolves.toBeNull();
+ expect(fetchFn).not.toHaveBeenCalled();
+ });
+
+ describe('with a recorded commit', () => {
+ it('accepts a head that builds on the recorded commit', async () => {
+ const { source, fetchFn } = sourceFor(
+ routes(mergeRequest({ sha: newer, head_pipeline: null }), {
+ 'state=opened': () => json(listed({ sha: newer })),
+ '/repository/merge_base': () => json({ id: sha }),
+ }),
+ );
+ await expect(find(source, { head: sha })).resolves.toMatchObject({ number: 7 });
+ const compared = fetchFn.mock.calls
+ .map(([url]) => String(url))
+ .find((url) => url.includes('merge_base'));
+ expect(compared).toContain(`refs[]=${sha}&refs[]=${newer}`);
+ });
+
+ it('rejects a head that does not contain the recorded commit', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest({ sha: newer }), {
+ 'state=opened': () => json(listed({ sha: newer })),
+ '/repository/merge_base': () => json({ id: 'c'.repeat(40) }),
+ }),
+ );
+ await expect(find(source, { head: sha })).resolves.toBeNull();
+ });
+
+ it('treats a commit GitLab does not know as no match', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest({ sha: newer }), {
+ 'state=opened': () => json(listed({ sha: newer })),
+ '/repository/merge_base': () => new Response('', { status: 400 }),
+ }),
+ );
+ await expect(find(source, { head: sha })).resolves.toBeNull();
+ });
+
+ it('stops comparing once the configured budget is spent', async () => {
+ const { source, fetchFn } = sourceFor(
+ routes(mergeRequest({ sha: newer }), {
+ 'state=opened': () =>
+ json([1, 2, 3].map((iid) => ({ ...listed({ sha: newer })[0], iid }))),
+ '/repository/merge_base': () => json({ id: 'c'.repeat(40) }),
+ }),
+ );
+ await find(source, { head: sha, limits: { maxHeadComparisons: 2 } });
+ const comparisons = fetchFn.mock.calls.filter(([url]) => String(url).includes('merge_base'));
+ expect(comparisons).toHaveLength(2);
+ });
+ });
+
+ it('finds a closed merge request behind a newer open fork, without a recorded commit', async () => {
+ const fork = { iid: 9, state: 'opened', sha, source_project_id: 2, target_project_id: 1 };
+ const merged = { iid: 7, state: 'merged', sha, source_project_id: 1, target_project_id: 1 };
+ const { source, fetchFn } = sourceFor({
+ 'state=opened': () => json([]),
+ 'state=all': () => json([fork, merged]),
+ '/merge_requests/7': () => json(mergeRequest({ state: 'merged' })),
+ '/api/graphql': () => json(stats()),
+ });
+ await expect(find(source)).resolves.toMatchObject({ number: 7, state: 'merged' });
+ const history = fetchFn.mock.calls
+ .map(([url]) => String(url))
+ .find((url) => url.includes('state=all'));
+ expect(new URL(history ?? '').searchParams.get('per_page')).toBe('10');
+ });
+
+ it('drops a merge request whose source branch changed after it was listed', async () => {
+ const { source } = sourceFor(routes(mergeRequest({ source_branch: 'other' })));
+ await expect(find(source)).resolves.toBeNull();
+ });
+
+ describe('forks', () => {
+ const fork = { source_project_id: 2, target_project_id: 1 };
+
+ it('never takes a fork on its branch name alone', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest(fork), { 'state=opened': () => json(listed(fork)) }),
+ );
+ await expect(find(source)).resolves.toBeNull();
+ });
+
+ it('takes a fork that carries the recorded commit', async () => {
+ const { source } = sourceFor(
+ routes(mergeRequest(fork), { 'state=opened': () => json(listed(fork)) }),
+ );
+ await expect(find(source, { head: sha })).resolves.toMatchObject({ number: 7 });
+ });
+ });
+
+ describe('a linked merge request', () => {
+ it('is read directly when its source branch is the lane branch', async () => {
+ const { source, fetchFn } = sourceFor(routes());
+ await expect(find(source, { pullRequest: 7 })).resolves.toMatchObject({ number: 7 });
+ const urls = fetchFn.mock.calls.map(([url]) => String(url));
+ expect(urls.some((url) => url.includes('/merge_requests?'))).toBe(false);
+ });
+
+ it('is ignored for another branch, and the branch is searched instead', async () => {
+ const { source, fetchFn } = sourceFor(
+ routes(mergeRequest(), {
+ '/merge_requests/9': () => json(mergeRequest({ iid: 9, source_branch: 'other' })),
+ }),
+ );
+ await expect(find(source, { pullRequest: 9 })).resolves.toMatchObject({ number: 7 });
+ expect(fetchFn.mock.calls.some(([url]) => String(url).includes('state=opened'))).toBe(true);
+ });
+
+ it('from a fork counts only with the recorded commit', async () => {
+ const fork = mergeRequest({ source_project_id: 2, target_project_id: 1 });
+ const { source } = sourceFor(routes(fork, { 'state=opened': () => json([]) }));
+ await expect(find(source, { pullRequest: 7 })).resolves.toBeNull();
+ });
+ });
+});
diff --git a/packages/api/src/pulls/gitlab.ts b/packages/api/src/pulls/gitlab.ts
new file mode 100644
index 0000000000..402a1d84cc
--- /dev/null
+++ b/packages/api/src/pulls/gitlab.ts
@@ -0,0 +1,358 @@
+import type {
+ PullRequestChecks,
+ PullRequestMergeable,
+ PullRequestState,
+ TConversationPullRequest,
+} from 'librechat-data-provider';
+import type { PullRequestFetch } from './shared';
+import type { PullRequestSource } from './types';
+import {
+ isCount,
+ isRecord,
+ COMMIT_ID,
+ isPageUrl,
+ retryAfterMs,
+ isPathSegment,
+ MAX_TITLE_LENGTH,
+ DEFAULT_LOOKUP_TIMEOUT_MS,
+ DEFAULT_REQUEST_TIMEOUT_MS,
+ DEFAULT_MAX_CANDIDATE_PAGES,
+ DEFAULT_MAX_HEAD_COMPARISONS,
+ DEFAULT_MAX_CANDIDATE_PULL_REQUESTS,
+} from './shared';
+import { PullRequestSourceError } from './types';
+
+export const DEFAULT_GITLAB_HOST = 'gitlab.com';
+/** A project path: a namespace of one or more groups, then the project. */
+const PROJECT_PATH = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+){1,19}$/;
+const PASSING_PIPELINES = new Set(['success', 'skipped']);
+const FAILING_PIPELINES = new Set(['failed', 'canceled']);
+const DIFF_STATS_QUERY =
+ 'query($path: ID!, $iid: String!) { project(fullPath: $path) { mergeRequest(iid: $iid) { diffStatsSummary { additions deletions } } } }';
+
+type GitLabMergeRequest = {
+ iid: number;
+ title: string;
+ url: string;
+ state: PullRequestState;
+ draft: boolean;
+ /** The head commit; the pipeline is trusted only when it ran for this commit. */
+ sha: string;
+ /** The branch the merge request merges from, in whichever project it lives; null if unsaid. */
+ sourceBranch: string | null;
+ /** Whether the source branch lives in a fork rather than the project itself. */
+ fromFork: boolean;
+ mergeable: PullRequestMergeable;
+ checks: PullRequestChecks;
+};
+
+type ListItem = { iid: number; state: string; sha: string; fromFork: boolean };
+
+/** A failed or canceled pipeline outranks one still running; anything unfinished is running. */
+export function summarizePipeline(status: string | null): PullRequestChecks {
+ if (status == null) return 'none';
+ if (FAILING_PIPELINES.has(status)) return 'failing';
+ if (PASSING_PIPELINES.has(status)) return 'passing';
+ return 'running';
+}
+
+function summarizeState(state: string): PullRequestState {
+ if (state === 'merged') return 'merged';
+ return state === 'opened' ? 'open' : 'closed';
+}
+
+/** Conflict state only means something while the merge request is open. */
+function summarizeMergeable(value: Record<string, unknown>, state: PullRequestState) {
+ if (state !== 'open') return 'unknown';
+ if (value.has_conflicts === true || value.detailed_merge_status === 'conflict') {
+ return 'conflicting';
+ }
+ if (value.detailed_merge_status === 'mergeable') return 'clean';
+ return 'unknown';
+}
+
+const fromFork = (value: Record<string, unknown>): boolean =>
+ value.source_project_id !== value.target_project_id;
+
+function parseMergeRequest(value: unknown, host: string): GitLabMergeRequest {
+ if (
+ !isRecord(value) ||
+ !isCount(value.iid) ||
+ typeof value.title !== 'string' ||
+ !isPageUrl(value.web_url, host) ||
+ typeof value.state !== 'string' ||
+ !['opened', 'closed', 'locked', 'merged'].includes(value.state) ||
+ typeof value.sha !== 'string' ||
+ !COMMIT_ID.test(value.sha) ||
+ !isCount(value.source_project_id) ||
+ !isCount(value.target_project_id)
+ ) {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ const state = summarizeState(value.state);
+ const pipeline = isRecord(value.head_pipeline) ? value.head_pipeline : null;
+ /** A pipeline of an earlier head says nothing about this one. */
+ const status =
+ pipeline != null && pipeline.sha === value.sha && typeof pipeline.status === 'string'
+ ? pipeline.status
+ : null;
+ return {
+ iid: value.iid,
+ title: value.title,
+ url: value.web_url,
+ state,
+ draft: value.draft === true || value.work_in_progress === true,
+ sha: value.sha,
+ sourceBranch: typeof value.source_branch === 'string' ? value.source_branch : null,
+ fromFork: fromFork(value),
+ mergeable: summarizeMergeable(value, state),
+ checks: summarizePipeline(status),
+ };
+}
+
+function parseListItem(value: unknown): ListItem {
+ if (
+ !isRecord(value) ||
+ !isCount(value.iid) ||
+ typeof value.state !== 'string' ||
+ typeof value.sha !== 'string' ||
+ !COMMIT_ID.test(value.sha) ||
+ !isCount(value.source_project_id) ||
+ !isCount(value.target_project_id)
+ ) {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ return { iid: value.iid, state: value.state, sha: value.sha, fromFork: fromFork(value) };
+}
+
+function parseDiffStats(value: unknown): { additions: number; deletions: number } {
+ if (!isRecord(value) || value.errors != null || !isRecord(value.data)) {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ const project = value.data.project;
+ const mergeRequest = isRecord(project) ? project.mergeRequest : null;
+ if (!isRecord(mergeRequest)) throw new PullRequestSourceError('UPSTREAM_ERROR');
+ /** GitLab has no summary for a merge request without changes. */
+ const summary = mergeRequest.diffStatsSummary;
+ if (summary == null) return { additions: 0, deletions: 0 };
+ if (!isRecord(summary) || !isCount(summary.additions) || !isCount(summary.deletions)) {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ return { additions: summary.additions, deletions: summary.deletions };
+}
+
+/**
+ * Reads merge requests through GitLab's REST API (v4) on gitlab.com or a self-managed host, with a
+ * token the caller supplies; line counts come from one GraphQL query, the only API that has them.
+ * Only the decision (absent, rate limited, failed) leaves this module; response bodies and error
+ * text never do.
+ */
+export function createGitLabPullRequestSource({
+ fetchFn,
+}: {
+ /** The deployment's HTTP client, so proxying and instrumentation are decided by the caller. */
+ fetchFn: PullRequestFetch;
+}): PullRequestSource {
+ type Lookup = {
+ token: string;
+ /** `https://{host}`; every request of the lookup goes there and nowhere else. */
+ origin: string;
+ requestTimeoutMs: number;
+ deadline: AbortSignal;
+ maxCandidatePullRequests: number;
+ maxHeadComparisons: number;
+ maxCandidatePages: number;
+ };
+
+ async function request(
+ url: string,
+ lookup: Lookup,
+ init: { method?: string; body?: string } = {},
+ /** Statuses that mean "nothing there" for this request, besides 404. */
+ absent: readonly number[] = [],
+ ): Promise<unknown | null> {
+ let response: Response;
+ try {
+ response = await fetchFn(url, {
+ ...init,
+ headers: {
+ Accept: 'application/json',
+ Authorization: `Bearer ${lookup.token}`,
+ 'User-Agent': 'LibreChat-Pull-Requests',
+ ...(init.body != null ? { 'Content-Type': 'application/json' } : {}),
+ },
+ signal: AbortSignal.any([AbortSignal.timeout(lookup.requestTimeoutMs), lookup.deadline]),
+ });
+ } catch {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ if (response.ok) {
+ try {
+ return await response.json();
+ } catch {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ }
+ if (response.status === 429) {
+ throw new PullRequestSourceError('RATE_LIMITED', retryAfterMs(response, 'ratelimit-reset'));
+ }
+ /** A project the token cannot see is indistinguishable from one without merge requests. */
+ if (response.status === 404 || absent.includes(response.status)) return null;
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+
+ return {
+ async find({
+ repo,
+ branch,
+ head: recordedHead,
+ pullRequest,
+ host = DEFAULT_GITLAB_HOST,
+ token,
+ limits,
+ }) {
+ /** As on GitHub: a lane with a branch and no commit yet has nothing to match against. */
+ if (recordedHead === null) return null;
+ if (!PROJECT_PATH.test(repo) || !repo.split('/').every(isPathSegment)) return null;
+ if (branch.length === 0) return null;
+ const lookup: Lookup = {
+ token,
+ origin: `https://${host}`,
+ requestTimeoutMs: limits?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
+ deadline: AbortSignal.timeout(limits?.lookupTimeoutMs ?? DEFAULT_LOOKUP_TIMEOUT_MS),
+ maxCandidatePullRequests:
+ limits?.maxCandidatePullRequests ?? DEFAULT_MAX_CANDIDATE_PULL_REQUESTS,
+ maxHeadComparisons: limits?.maxHeadComparisons ?? DEFAULT_MAX_HEAD_COMPARISONS,
+ maxCandidatePages: limits?.maxCandidatePages ?? DEFAULT_MAX_CANDIDATE_PAGES,
+ };
+ const project = `${lookup.origin}/api/v4/projects/${encodeURIComponent(repo)}`;
+ const getJson = (pathname: string, absent: readonly number[] = []) =>
+ request(`${project}${pathname}`, lookup, {}, absent);
+
+ const recorded =
+ typeof recordedHead === 'string' && COMMIT_ID.test(recordedHead) ? recordedHead : undefined;
+ let comparisons = 0;
+ /**
+ * With a recorded commit a merge request counts only when its head is that commit or builds
+ * on it: their merge base is the recorded commit. Every comparison spends the configured
+ * budget, and a commit GitLab does not know is no match.
+ */
+ const contains = async (sha: string): Promise<boolean> => {
+ if (recorded == null || sha === recorded) return true;
+ if (comparisons >= lookup.maxHeadComparisons) return false;
+ comparisons += 1;
+ const base = await getJson(
+ `/repository/merge_base?refs[]=${recorded}&refs[]=${encodeURIComponent(sha)}`,
+ [400],
+ );
+ if (base == null) return false;
+ if (!isRecord(base) || typeof base.id !== 'string') {
+ throw new PullRequestSourceError('UPSTREAM_ERROR');
+ }
+ return base.id === recorded;
+ };
+ /**
+ * GitLab lists merge requests from forks under the same source branch name. A branch name
+ * proves nothing for a fork (anyone can open `patch-1`), so a fork counts only when it
+ * carries the recorded commit.
+ */
+ const matches = async (item: { sha: string; fromFork: boolean }): Promise<boolean> =>
+ item.fromFork ? recorded != null && contains(item.sha) : contains(item.sha);
+
+ const report = async (
+ mergeRequest: GitLabMergeRequest,
+ ): Promise<TConversationPullRequest> => {
+ const stats = parseDiffStats(
+ await request(`${lookup.origin}/api/graphql`, lookup, {
+ method: 'POST',
+ body: JSON.stringify({
+ query: DIFF_STATS_QUERY,
+ variables: { path: repo, iid: String(mergeRequest.iid) },
+ }),
+ }),
+ );
+ return {
+ number: mergeRequest.iid,
+ title: mergeRequest.title.slice(0, MAX_TITLE_LENGTH),
+ url: mergeRequest.url,
+ provider: 'gitlab',
+ additions: stats.additions,
+ deletions: stats.deletions,
+ state: mergeRequest.state,
+ isDraft: mergeRequest.draft,
+ mergeable: mergeRequest.mergeable,
+ checks: mergeRequest.checks,
+ };
+ };
+
+ /** A merge request the conversation's own output linked to is read directly. */
+ if (pullRequest != null && Number.isSafeInteger(pullRequest) && pullRequest > 0) {
+ const linked = await getJson(`/merge_requests/${pullRequest}`);
+ if (linked != null) {
+ const mergeRequest = parseMergeRequest(linked, host);
+ if (mergeRequest.sourceBranch === branch && (await matches(mergeRequest))) {
+ return report(mergeRequest);
+ }
+ }
+ }
+
+ /** `undefined`: the project is not visible. An empty list: it is, and nothing matched. */
+ const list = async (state: 'opened' | 'all', perPage: number, page: number) => {
+ const query = new URLSearchParams({
+ state,
+ source_branch: branch,
+ order_by: 'created_at',
+ sort: 'desc',
+ per_page: String(perPage),
+ page: String(page),
+ });
+ const listed = await getJson(`/merge_requests?${query.toString()}`);
+ if (listed == null) return undefined;
+ if (!Array.isArray(listed)) throw new PullRequestSourceError('UPSTREAM_ERROR');
+ return listed.map(parseListItem);
+ };
+ /**
+ * Reads pages of one state up to the configured budget. The history is read a full page at
+ * a time even without a recorded commit: it also lists open merge requests from forks, which
+ * are skipped, so its newest entry is not necessarily the one that matches.
+ */
+ const listAll = async (state: 'opened' | 'all'): Promise<ListItem[] | undefined> => {
+ const perPage = lookup.maxCandidatePullRequests;
+ const pages = lookup.maxCandidatePages;
+ const all: ListItem[] = [];
+ for (let page = 1; page <= pages; page++) {
+ const items = await list(state, perPage, page);
+ if (items === undefined) return page === 1 ? undefined : all;
+ all.push(...items);
+ if (items.length < perPage) break;
+ }
+ return all;
+ };
+ const choose = async (candidates: ListItem[]): Promise<ListItem | null> => {
+ for (const candidate of candidates) {
+ if (await matches(candidate)) return candidate;
+ }
+ return null;
+ };
+
+ /** Open first on its own, so closed history on a reused branch name cannot hide it. */
+ const open = await listAll('opened');
+ if (open === undefined) return null;
+ const chosen =
+ (await choose(open)) ??
+ (await choose(((await listAll('all')) ?? []).filter((item) => item.state !== 'opened')));
+ if (chosen == null) return null;
+
+ const detail = await getJson(`/merge_requests/${chosen.iid}`);
+ if (detail == null) return null;
+ const mergeRequest = parseMergeRequest(detail, host);
+ /** The merge request may have changed between the list and the detail: its source branch,
+ * the project it comes from, or a push that moved the head off the recorded commit. */
+ if (mergeRequest.sourceBranch !== branch || mergeRequest.fromFork !== chosen.fromFork) {
+ return null;
+ }
+ if (mergeRequest.sha !== chosen.sha && !(await matches(mergeRequest))) return null;
+ return report(mergeRequest);
+ },
+ };
+}
diff --git a/packages/api/src/pulls/handler.spec.ts b/packages/api/src/pulls/handler.spec.ts
index 54fe9a4277..9f3602220d 100644
--- a/packages/api/src/pulls/handler.spec.ts
+++ b/packages/api/src/pulls/handler.spec.ts
@@ -90,6 +90,7 @@ describe('createConversationPullRequestHandler', () => {
branch: 'feat/x',
token: 'ghp_secret',
head: null,
+ provider: 'github',
host: 'github.com',
ttlMs: 30_000,
cacheMaxEntries: 500,
@@ -135,6 +136,39 @@ describe('createConversationPullRequestHandler', () => {
return lookup.mock.calls[0]?.[0]?.host;
}
+ it('takes the provider from the operator config, never from a database override', async () => {
+ const lookup = jest.fn().mockResolvedValue({ ok: true, value: pr });
+ const handler = createConversationPullRequestHandler({
+ getConvoLaneGit: jest.fn().mockResolvedValue({ branch: 'feat/x', head: null, repo: 'o/r' }),
+ getAppConfig: jest
+ .fn()
+ .mockImplementation(async (options: { baseOnly?: boolean }) =>
+ configWith(options.baseOnly ? enabled : { ...enabled, provider: 'gitlab' }),
+ ),
+ lookup,
+ env: { GH_TOKEN: 'ghp_secret', GITLAB_TOKEN: 'glpat_other' },
+ });
+ const res = { status: jest.fn().mockReturnThis(), json: jest.fn() };
+ await handler(
+ { user: { id: 'u1' }, params: { conversationId: 'c1' } } as unknown as ServerRequest,
+ res as unknown as Response,
+ );
+ expect(lookup).toHaveBeenCalledWith(
+ expect.objectContaining({ provider: 'github', host: 'github.com', token: 'ghp_secret' }),
+ );
+ });
+
+ it('looks a GitLab lane up with the GitLab token on the GitLab host', async () => {
+ const { run, lookup } = setup({
+ settings: { ...enabled, token: undefined, provider: 'gitlab' },
+ env: { GH_TOKEN: 'ghp_secret', GITLAB_TOKEN: 'glpat_secret' },
+ });
+ await run();
+ expect(lookup).toHaveBeenCalledWith(
+ expect.objectContaining({ provider: 'gitlab', host: 'gitlab.com', token: 'glpat_secret' }),
+ );
+ });
+
it('takes the host from the operator config, never from a database override', async () => {
const override = { ...enabled, host: 'attacker.example' };
expect(await hostFor({ ...enabled, host: 'ghe.example.com' }, override)).toBe(
diff --git a/packages/api/src/pulls/handler.ts b/packages/api/src/pulls/handler.ts
index a1e3e43d36..84fa4ee837 100644
--- a/packages/api/src/pulls/handler.ts
+++ b/packages/api/src/pulls/handler.ts
@@ -18,6 +18,7 @@ import {
resolvePullRequestHost,
resolvePullRequestToken,
isPullRequestFeatureActive,
+ resolvePullRequestProvider,
} from './settings';
import { ALL_REPOSITORIES, isAllowedRepository } from './repository';
import { getAppConfigOptionsFromUser } from '~/app/service';
@@ -187,9 +188,9 @@ const agentsPullRequests = (config: AppConfig): PullRequestSettings | undefined
(config.endpoints?.[EModelEndpoint.agents] as TAgentsEndpoint | undefined)?.pullRequests;
/**
- * The user's settings, with `host` taken from the operator's YAML alone. Role, group and user
- * overrides live in the database and may be edited by delegated administrators; letting one set
- * the host would let it send the deployment's token to a server of its choosing.
+ * The user's settings, with `provider` and `host` taken from the operator's YAML alone. Role, group
+ * and user overrides live in the database and may be edited by delegated administrators; letting
+ * one choose either would let it send the deployment's token to a server of its choosing.
*/
async function readSettings(
getAppConfig: (options: GetAppConfigOptions) => Promise<AppConfig>,
@@ -202,9 +203,13 @@ async function readSettings(
]);
const settings = agentsPullRequests(resolved);
if (settings == null) return undefined;
- const { host: _override, ...rest } = settings;
- const host = agentsPullRequests(base)?.host;
- return host != null ? { ...rest, host } : rest;
+ const { host: _host, provider: _provider, ...rest } = settings;
+ const operator = agentsPullRequests(base);
+ return {
+ ...rest,
+ ...(operator?.provider != null ? { provider: operator.provider } : {}),
+ ...(operator?.host != null ? { host: operator.host } : {}),
+ };
}
/** Everything one lookup needs from the settings, so the single and batch routes cannot differ. */
@@ -214,7 +219,12 @@ function lookupInput(settings: PullRequestSettings, token: string, lane: Eligibl
branch: lane.branch,
head: lane.head,
...(lane.pullRequest != null ? { pullRequest: lane.pullRequest } : {}),
- host: resolvePullRequestHost(settings.host) ?? undefined,
+ provider: resolvePullRequestProvider(settings.provider) ?? undefined,
+ host:
+ resolvePullRequestHost(
+ settings.host,
+ resolvePullRequestProvider(settings.provider) ?? undefined,
+ ) ?? undefined,
token,
ttlMs: (settings.cacheTtlSeconds ?? 30) * 1000,
cacheMaxEntries: settings.cacheMaxEntries ?? 500,
@@ -302,7 +312,11 @@ export function createConversationPullRequestHandler(deps: {
return;
}
- const token = resolvePullRequestToken(settings.token, deps.env);
+ const token = resolvePullRequestToken(
+ settings.token,
+ deps.env,
+ resolvePullRequestProvider(settings.provider) ?? undefined,
+ );
if (token == null) {
logger.warn('[PullRequests] Enabled without a usable token reference');
res.status(503).json({ error: 'Pull requests are not configured', code: 'NOT_CONFIGURED' });
@@ -403,7 +417,11 @@ export function createConversationPullRequestsHandler(deps: {
* A missing token is a fact about the conversations that needed one, not about the request:
* those entries say so, and every other conversation keeps its correct "no pull request".
*/
- const token = resolvePullRequestToken(settings.token, deps.env);
+ const token = resolvePullRequestToken(
+ settings.token,
+ deps.env,
+ resolvePullRequestProvider(settings.provider) ?? undefined,
+ );
if (token == null) {
logger.warn('[PullRequests] Enabled without a usable token reference');
res.status(200).json({
diff --git a/packages/api/src/pulls/index.ts b/packages/api/src/pulls/index.ts
index ae357681c8..122c4d868d 100644
--- a/packages/api/src/pulls/index.ts
+++ b/packages/api/src/pulls/index.ts
@@ -1,6 +1,8 @@
export * from './types';
export * from './links';
export * from './github';
+export * from './gitlab';
+export * from './sources';
export * from './lookup';
export * from './handler';
export * from './settings';
diff --git a/packages/api/src/pulls/links.spec.ts b/packages/api/src/pulls/links.spec.ts
index 550137bf58..93f9e0db7f 100644
--- a/packages/api/src/pulls/links.spec.ts
+++ b/packages/api/src/pulls/links.spec.ts
@@ -21,6 +21,22 @@ describe('findPullRequestLink', () => {
expect(findPullRequestLink('https://ghe.local:8443/o/r/pull/9', 'o/r')).toBe(9);
});
+ it('finds a GitLab merge request, in a nested group too', () => {
+ const output = 'View it on GitLab: https://gitlab.com/g/r/-/merge_requests/14\n';
+ expect(findPullRequestLink(output, 'g/r')).toBe(14);
+ expect(
+ findPullRequestLink('https://gitlab.example.com/g/sub/r/-/merge_requests/3', 'g/sub/r'),
+ ).toBe(3);
+ });
+
+ it('does not take a merge request of a project nested under the lane one', () => {
+ expect(findPullRequestLink('https://gitlab.com/g/r/x/-/merge_requests/3', 'g/r')).toBeNull();
+ });
+
+ it('finds a Bitbucket pull request', () => {
+ expect(findPullRequestLink('https://bitbucket.org/w/r/pull-requests/21', 'w/r')).toBe(21);
+ });
+
it('accepts a link to a page of the pull request', () => {
expect(findPullRequestLink('https://github.com/o/r/pull/12/files', 'o/r')).toBe(12);
});
@@ -33,6 +49,8 @@ describe('findPullRequestLink', () => {
['another repository', 'https://github.com/other/r/pull/4'],
['a repository that only starts the same', 'https://github.com/o/r-fork/pull/4'],
['an issue', 'https://github.com/o/r/issues/4'],
+ ['a GitLab issue', 'https://gitlab.com/o/r/-/issues/4'],
+ ['a new merge request page', 'https://gitlab.com/o/r/-/merge_requests/new'],
['the page git push suggests', 'https://github.com/o/r/pull/new/feat/x'],
['plain http', 'http://github.com/o/r/pull/4'],
['a number glued to more text', 'https://github.com/o/r/pull/4abc'],
diff --git a/packages/api/src/pulls/links.ts b/packages/api/src/pulls/links.ts
index 911a42fd2b..de21889966 100644
--- a/packages/api/src/pulls/links.ts
+++ b/packages/api/src/pulls/links.ts
@@ -1,11 +1,13 @@
/**
- * `https://{host}/{owner}/{name}/pull/{number}`, ending where the number does. A `/pull/new/` page
- * (what `git push` suggests) has no number and never matches. Any host matches, so a GitHub
- * Enterprise link is found too: the number is only a candidate, which the lookup reads from the
- * configured host and accepts only when its head is the lane's branch.
+ * A pull request page on any provider, ending where the number does: GitHub's
+ * `/{owner}/{name}/pull/{n}`, GitLab's `/{group}/.../{name}/-/merge_requests/{n}` and Bitbucket's
+ * `/{workspace}/{name}/pull-requests/{n}`. A `/pull/new/` page (what `git push` suggests) has no
+ * number and never matches. Any host matches: the number is only a candidate, which the lookup
+ * reads from the configured provider and accepts only when its head is the lane's branch.
*/
const PULL_REQUEST_URL =
- /https:\/\/[A-Za-z0-9.-]+(?::\d{1,5})?\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\/pull\/(\d+)(?![\w-])/g;
+ /https:\/\/[A-Za-z0-9.-]+(?::\d{1,5})?\/((?:[A-Za-z0-9_.-]+\/)+?[A-Za-z0-9_.-]+)\/(?:pull|-\/merge_requests|pull-requests)\/(\d+)(?![\w-])/g;
+const MARKERS = ['/pull/', '/merge_requests/', '/pull-requests/'];
/**
* The pull request of `repo` that command output links to, the last one when it links several,
@@ -13,12 +15,12 @@ const PULL_REQUEST_URL =
* are ignored, so output that mentions another project's pull request cannot claim the lane.
*/
export function findPullRequestLink(output: string, repo: string): number | null {
- if (!output.includes('/pull/')) return null;
+ if (!MARKERS.some((marker) => output.includes(marker))) return null;
const wanted = repo.toLowerCase();
let found: number | null = null;
for (const match of output.matchAll(PULL_REQUEST_URL)) {
- const [, owner, name, digits] = match;
- if (`${owner}/${name}`.toLowerCase() !== wanted) continue;
+ const [, path, digits] = match;
+ if (path.toLowerCase() !== wanted) continue;
const number = Number(digits);
if (Number.isSafeInteger(number) && number > 0) found = number;
}
diff --git a/packages/api/src/pulls/lookup.ts b/packages/api/src/pulls/lookup.ts
index bde8af5275..3bf197a308 100644
--- a/packages/api/src/pulls/lookup.ts
+++ b/packages/api/src/pulls/lookup.ts
@@ -111,6 +111,7 @@ export function createPullRequestLookup({
branch,
head,
pullRequest,
+ provider,
host,
token,
ttlMs,
@@ -135,7 +136,7 @@ export function createPullRequestLookup({
.sort()
.join('|'),
].join(',');
- const key = `${scope}\0${host ?? ''}\0${repo}#${branch}\0${head ?? ''}\0${pullRequest ?? ''}\0${policy}`;
+ const key = `${scope}\0${provider ?? ''}\0${host ?? ''}\0${repo}#${branch}\0${head ?? ''}\0${pullRequest ?? ''}\0${policy}`;
const cached = touch(scope)?.get(key);
if (cached != null && cached.expiresAt > now()) return cached.result;
const pending = inflight.get(key);
@@ -156,6 +157,7 @@ export function createPullRequestLookup({
branch,
head,
pullRequest,
+ ...(provider != null ? { provider } : {}),
...(host != null ? { host } : {}),
token,
limits,
diff --git a/packages/api/src/pulls/repository.spec.ts b/packages/api/src/pulls/repository.spec.ts
index b1cacc1ba4..f0bb2b85d6 100644
--- a/packages/api/src/pulls/repository.spec.ts
+++ b/packages/api/src/pulls/repository.spec.ts
@@ -8,6 +8,11 @@ describe('isAllowedRepository', () => {
['o/other', ['o/*'], true],
['x/r', ['o/*'], false],
['o/r', [], false],
+ ['g/sub/r', ['g/sub/r'], true],
+ ['G/Sub/R', ['g/sub/*'], true],
+ ['g/sub/r', ['g/*'], false],
+ ['g/r', ['g/sub/*'], false],
+ ['g/sub/r', ['g/sub'], false],
['o/r', undefined, false],
])('%s against %j is %s', (repo, allowed, expected) => {
expect(isAllowedRepository(repo, allowed)).toBe(expected);
@@ -15,6 +20,7 @@ describe('isAllowedRepository', () => {
it('matches any repository for the opt-in list, and only for it', () => {
expect(isAllowedRepository('any/thing', ALL_REPOSITORIES)).toBe(true);
+ expect(isAllowedRepository('group/sub/thing', ALL_REPOSITORIES)).toBe(true);
expect(isAllowedRepository('o/r', ['*/r'])).toBe(true);
expect(isAllowedRepository('o/r', ['o/r2'])).toBe(false);
});
diff --git a/packages/api/src/pulls/repository.ts b/packages/api/src/pulls/repository.ts
index 85f5114f2b..1238866d10 100644
--- a/packages/api/src/pulls/repository.ts
+++ b/packages/api/src/pulls/repository.ts
@@ -4,14 +4,19 @@
*/
export const ALL_REPOSITORIES: readonly string[] = ['*/*'];
-/** `owner/name` or `owner/*`, compared without case as GitHub does. Nothing else matches. */
+/**
+ * `owner/name`, `owner/*`, or a nested GitLab path such as `group/sub/name` or `group/sub/*`,
+ * compared without case. `*` stands for exactly one segment, so `group/*` does not reach into
+ * `group/sub/name`. Only the opt-in to every repository matches any path.
+ */
export function isAllowedRepository(repo: string, allowed: readonly string[] | undefined): boolean {
- const [owner, name] = repo.toLowerCase().split('/');
+ const segments = repo.toLowerCase().split('/');
return (allowed ?? []).some((entry) => {
- const [allowedOwner, allowedName] = entry.toLowerCase().split('/');
+ if (entry === ALL_REPOSITORIES[0]) return true;
+ const pattern = entry.toLowerCase().split('/');
return (
- (allowedOwner === '*' || allowedOwner === owner) &&
- (allowedName === '*' || allowedName === name)
+ pattern.length === segments.length &&
+ pattern.every((part, index) => part === '*' || part === segments[index])
);
});
}
diff --git a/packages/api/src/pulls/settings.spec.ts b/packages/api/src/pulls/settings.spec.ts
index b17e5563a2..7f9b9cc94f 100644
--- a/packages/api/src/pulls/settings.spec.ts
+++ b/packages/api/src/pulls/settings.spec.ts
@@ -3,8 +3,23 @@ import {
resolvePullRequestHost,
resolvePullRequestToken,
isPullRequestFeatureActive,
+ resolvePullRequestProvider,
} from './settings';
+describe('resolvePullRequestProvider', () => {
+ it('is GitHub when none is configured', () => {
+ expect(resolvePullRequestProvider(undefined)).toBe('github');
+ });
+
+ it.each(['github', 'gitlab', 'bitbucket'])('accepts %s', (provider) => {
+ expect(resolvePullRequestProvider(provider)).toBe(provider);
+ });
+
+ it('refuses a provider this server does not know', () => {
+ expect(resolvePullRequestProvider('gitea')).toBeNull();
+ });
+});
+
describe('resolvePullRequestHost', () => {
it('is github.com when no host is configured', () => {
expect(resolvePullRequestHost(undefined)).toBe('github.com');
@@ -32,6 +47,17 @@ describe('resolvePullRequestHost', () => {
])('refuses %p rather than falling back to github.com', (host) => {
expect(resolvePullRequestHost(host)).toBeNull();
});
+
+ it("defaults to the provider's own host", () => {
+ expect(resolvePullRequestHost(undefined, 'gitlab')).toBe('gitlab.com');
+ expect(resolvePullRequestHost(undefined, 'bitbucket')).toBe('bitbucket.org');
+ expect(resolvePullRequestHost('GitLab.Example.com', 'gitlab')).toBe('gitlab.example.com');
+ });
+
+ it('refuses any Bitbucket host but Cloud, which is all this server can read', () => {
+ expect(resolvePullRequestHost('bitbucket.example.com', 'bitbucket')).toBeNull();
+ expect(resolvePullRequestHost('Bitbucket.org', 'bitbucket')).toBe('bitbucket.org');
+ });
});
describe('resolvePullRequestToken', () => {
@@ -109,6 +135,27 @@ describe('isPullRequestFeatureActive', () => {
);
});
+ it('falls back to the variables of the configured provider only', () => {
+ const gitlab = { provider: 'gitlab' as const, allowAllRepositories: true };
+ expect(isPullRequestFeatureActive(gitlab, { GITHUB_TOKEN: 't' })).toBe(false);
+ expect(isPullRequestFeatureActive(gitlab, { GITLAB_TOKEN: 't' })).toBe(true);
+ expect(resolvePullRequestToken(undefined, { GITLAB_TOKEN: 'g', GH_TOKEN: 'h' }, 'gitlab')).toBe(
+ 'g',
+ );
+ expect(
+ resolvePullRequestToken(undefined, { BITBUCKET_PULL_REQUEST_TOKEN: 'b' }, 'bitbucket'),
+ ).toBe('b');
+ });
+
+ it('stays dormant for a provider this server does not know', () => {
+ expect(
+ isPullRequestFeatureActive(
+ { provider: 'gitea' as never, token: '${T}', allowAllRepositories: true },
+ env,
+ ),
+ ).toBe(false);
+ });
+
it('stays dormant with a host that is not a host name, so the token goes nowhere', () => {
expect(
isPullRequestFeatureActive(
diff --git a/packages/api/src/pulls/settings.ts b/packages/api/src/pulls/settings.ts
index dd0993353c..0f3e6c5e03 100644
--- a/packages/api/src/pulls/settings.ts
+++ b/packages/api/src/pulls/settings.ts
@@ -1,15 +1,36 @@
-import type { TAgentsEndpoint } from 'librechat-data-provider';
+import { PULL_REQUEST_PROVIDERS } from 'librechat-data-provider';
+import type { PullRequestProvider, TAgentsEndpoint } from 'librechat-data-provider';
export type PullRequestSettings = NonNullable<TAgentsEndpoint['pullRequests']>;
const TOKEN_REFERENCE = /^\$\{([A-Za-z_][A-Za-z0-9_]*)\}$/;
/**
- * Tried in order when `token` is not configured, so a deployment that already holds a GitHub token
- * for something else gets pull requests without more setup. A token named in the config always
- * wins, and one that does not resolve is reported instead of silently replaced.
+ * Tried in order when `token` is not configured, so a deployment that already holds a token for
+ * something else gets pull requests without more setup. A token named in the config always wins,
+ * and one that does not resolve is reported instead of silently replaced.
*/
-const TOKEN_ENV_FALLBACKS = ['GITHUB_PULL_REQUEST_TOKEN', 'GITHUB_TOKEN', 'GH_TOKEN'] as const;
+const TOKEN_ENV_FALLBACKS: Record<PullRequestProvider, readonly string[]> = {
+ github: ['GITHUB_PULL_REQUEST_TOKEN', 'GITHUB_TOKEN', 'GH_TOKEN'],
+ gitlab: ['GITLAB_PULL_REQUEST_TOKEN', 'GITLAB_TOKEN'],
+ bitbucket: ['BITBUCKET_PULL_REQUEST_TOKEN'],
+};
+
+const DEFAULT_HOSTS: Record<PullRequestProvider, string> = {
+ github: 'github.com',
+ gitlab: 'gitlab.com',
+ bitbucket: 'bitbucket.org',
+};
+
+/** The configured provider, GitHub when none is set, `null` for a name this server does not know. */
+export function resolvePullRequestProvider(
+ provider: string | undefined,
+): PullRequestProvider | null {
+ if (provider == null) return 'github';
+ return (PULL_REQUEST_PROVIDERS as readonly string[]).includes(provider)
+ ? (provider as PullRequestProvider)
+ : null;
+}
type Env = Readonly<Record<string, string | undefined>>;
@@ -23,25 +44,34 @@ export function resolveTokenReference(reference: string | undefined, env: Env):
const HOST = /^(?=[a-z0-9])[a-z0-9.-]*[a-z0-9](?::[0-9]{1,5})?$/;
/**
- * The configured GitHub host, lowercased, or `github.com` when none is set. `null` for a value that
- * is not a plain host name: the token is then sent nowhere rather than to a host nobody named.
+ * The configured host, lowercased, or the provider's own when none is set. `null` for a value that
+ * is not a plain host name, and for any Bitbucket host but Cloud's: the token is then sent nowhere
+ * rather than to a host nobody named.
*/
-export function resolvePullRequestHost(host: string | undefined): string | null {
- if (host == null) return 'github.com';
+export function resolvePullRequestHost(
+ host: string | undefined,
+ provider: PullRequestProvider = 'github',
+): string | null {
+ if (host == null) return DEFAULT_HOSTS[provider];
const value = host.trim().toLowerCase();
if (value.length > 253 || !HOST.test(value)) return null;
const [name, port] = value.split(':');
- if (port == null) return value;
- const number = Number(port);
- if (number < 1 || number > 65535) return null;
+ const number = port == null ? null : Number(port);
+ if (number != null && (number < 1 || number > 65535)) return null;
/** A URL drops the https default port, so a page on `host:443` compares as `host`. */
- return number === 443 ? name : `${name}:${number}`;
+ const normalized = number == null || number === 443 ? name : `${name}:${number}`;
+ if (provider === 'bitbucket' && normalized !== DEFAULT_HOSTS.bitbucket) return null;
+ return normalized;
}
/** The configured token reference, else the first fallback variable that is set. */
-export function resolvePullRequestToken(reference: string | undefined, env: Env): string | null {
+export function resolvePullRequestToken(
+ reference: string | undefined,
+ env: Env,
+ provider: PullRequestProvider = 'github',
+): string | null {
if (reference != null) return resolveTokenReference(reference, env);
- for (const name of TOKEN_ENV_FALLBACKS) {
+ for (const name of TOKEN_ENV_FALLBACKS[provider]) {
const value = env[name]?.trim();
if (value) return value;
}
@@ -59,8 +89,11 @@ export function isPullRequestFeatureActive(
env: Env,
): boolean {
if (settings?.enabled === false) return false;
- if (resolvePullRequestHost(settings?.host) == null) return false;
- if (settings?.token == null && resolvePullRequestToken(undefined, env) == null) return false;
+ const provider = resolvePullRequestProvider(settings?.provider);
+ if (provider == null || resolvePullRequestHost(settings?.host, provider) == null) return false;
+ if (settings?.token == null && resolvePullRequestToken(undefined, env, provider) == null) {
+ return false;
+ }
return (
settings?.allowAllRepositories === true || (settings?.allowedRepositories?.length ?? 0) > 0
);
diff --git a/packages/api/src/pulls/shared.ts b/packages/api/src/pulls/shared.ts
new file mode 100644
index 0000000000..821fcb72a5
--- /dev/null
+++ b/packages/api/src/pulls/shared.ts
@@ -0,0 +1,46 @@
+/** Defaults for the operator bounds in `endpoints.agents.pullRequests`, shared by every provider. */
+export const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
+export const DEFAULT_LOOKUP_TIMEOUT_MS = 30_000;
+export const DEFAULT_MAX_CANDIDATE_PULL_REQUESTS = 10;
+export const DEFAULT_MAX_HEAD_COMPARISONS = 3;
+export const DEFAULT_MAX_CANDIDATE_PAGES = 1;
+
+export const COMMIT_ID: RegExp = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
+export const MAX_TITLE_LENGTH = 256;
+
+/** The only part of `fetch` the sources call, so any compatible client can be injected. */
+export type PullRequestFetch = (input: string, init?: RequestInit) => Promise<Response>;
+
+export const isRecord = (value: unknown): value is Record<string, unknown> =>
+ value != null && typeof value === 'object' && !Array.isArray(value);
+
+export const isCount = (value: unknown): value is number =>
+ typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
+
+/** `.` and `..` match the path character class but would rewrite an API path. */
+export const isPathSegment = (value: string): boolean => value !== '.' && value !== '..';
+
+/** The URL ends up in an href, so only a plain https page on the configured host is accepted. */
+export function isPageUrl(value: unknown, host: string): value is string {
+ if (typeof value !== 'string') return false;
+ try {
+ const url = new URL(value);
+ return url.protocol === 'https:' && url.host === host;
+ } catch {
+ return false;
+ }
+}
+
+/** How long a provider asked callers to wait: `retry-after` seconds, else a reset time in epoch
+ * seconds from the named header. */
+export function retryAfterMs(response: Response, resetHeader: string): number | undefined {
+ const retryAfter = Number(response.headers.get('retry-after'));
+ if (response.headers.get('retry-after') != null && Number.isFinite(retryAfter)) {
+ return Math.max(0, retryAfter * 1000);
+ }
+ const reset = Number(response.headers.get(resetHeader));
+ if (response.headers.get(resetHeader) != null && Number.isFinite(reset)) {
+ return Math.max(0, reset * 1000 - Date.now());
+ }
+ return undefined;
+}
diff --git a/packages/api/src/pulls/sources.spec.ts b/packages/api/src/pulls/sources.spec.ts
new file mode 100644
index 0000000000..652eb5e2ab
--- /dev/null
+++ b/packages/api/src/pulls/sources.spec.ts
@@ -0,0 +1,30 @@
+import type { PullRequestSource } from './types';
+import { routePullRequestSources } from './sources';
+
+const input = { repo: 'o/r', branch: 'feat/x', token: 't' };
+
+describe('routePullRequestSources', () => {
+ const github: PullRequestSource = { find: jest.fn().mockResolvedValue(null) };
+ const gitlab: PullRequestSource = { find: jest.fn().mockResolvedValue(null) };
+ const source = routePullRequestSources({ github, gitlab });
+
+ beforeEach(() => jest.clearAllMocks());
+
+ it('asks GitHub when no provider is named, as before providers existed', async () => {
+ await source.find(input);
+ expect(github.find).toHaveBeenCalledWith(input);
+ expect(gitlab.find).not.toHaveBeenCalled();
+ });
+
+ it('asks the named provider and no other', async () => {
+ await source.find({ ...input, provider: 'gitlab' });
+ expect(gitlab.find).toHaveBeenCalledTimes(1);
+ expect(github.find).not.toHaveBeenCalled();
+ });
+
+ it('reports a provider without a source as not configured', async () => {
+ await expect(source.find({ ...input, provider: 'bitbucket' })).rejects.toMatchObject({
+ code: 'NOT_CONFIGURED',
+ });
+ });
+});
diff --git a/packages/api/src/pulls/sources.ts b/packages/api/src/pulls/sources.ts
new file mode 100644
index 0000000000..1eb588d9c7
--- /dev/null
+++ b/packages/api/src/pulls/sources.ts
@@ -0,0 +1,35 @@
+import type { PullRequestProvider } from 'librechat-data-provider';
+import type { PullRequestFetch } from './shared';
+import type { PullRequestSource } from './types';
+import { createGitHubPullRequestSource } from './github';
+import { createGitLabPullRequestSource } from './gitlab';
+import { PullRequestSourceError } from './types';
+
+/**
+ * One source per provider behind the single interface the lookup uses, so the lookup and its cache
+ * stay unaware of providers. A provider this server has no source for cannot be served, which is
+ * reported as not configured rather than answered as "no pull request".
+ */
+export function routePullRequestSources(
+ sources: Partial<Record<PullRequestProvider, PullRequestSource>>,
+): PullRequestSource {
+ return {
+ find(input) {
+ const source = sources[input.provider ?? 'github'];
+ if (source == null) return Promise.reject(new PullRequestSourceError('NOT_CONFIGURED'));
+ return source.find(input);
+ },
+ };
+}
+
+/** Every provider this server can look up, all through the deployment's HTTP client. */
+export function createPullRequestSource({
+ fetchFn,
+}: {
+ fetchFn: PullRequestFetch;
+}): PullRequestSource {
+ return routePullRequestSources({
+ github: createGitHubPullRequestSource({ fetchFn }),
+ gitlab: createGitLabPullRequestSource({ fetchFn }),
+ });
+}
diff --git a/packages/api/src/pulls/transport.ts b/packages/api/src/pulls/transport.ts
index 9d849492df..6e9e027c30 100644
--- a/packages/api/src/pulls/transport.ts
+++ b/packages/api/src/pulls/transport.ts
@@ -1,5 +1,5 @@
import type { Dispatcher } from 'undici';
-import type { PullRequestFetch } from './github';
+import type { PullRequestFetch } from './shared';
import { getEnvProxyDispatcher } from '~/utils/proxy';
/**
diff --git a/packages/api/src/pulls/types.ts b/packages/api/src/pulls/types.ts
index 8d143e036f..e0755adcec 100644
--- a/packages/api/src/pulls/types.ts
+++ b/packages/api/src/pulls/types.ts
@@ -1,4 +1,4 @@
-import type { TConversationPullRequest } from 'librechat-data-provider';
+import type { PullRequestProvider, TConversationPullRequest } from 'librechat-data-provider';
/** Stable, user-safe reasons a lookup could not answer. None carries upstream text. */
export const PULL_REQUEST_ERROR_CODES = [
@@ -49,7 +49,9 @@ export type PullRequestFindInput = {
* branch is searched as if no link had been seen.
*/
pullRequest?: number | null;
- /** The GitHub host the token belongs to, `github.com` when omitted. Already normalized. */
+ /** Which service to ask, GitHub when omitted. */
+ provider?: PullRequestProvider;
+ /** The host the token belongs to, the provider's own when omitted. Already normalized. */
host?: string;
token: string;
limits?: PullRequestLookupLimits;
diff --git a/packages/data-provider/src/code/workspace.ts b/packages/data-provider/src/code/workspace.ts
index 417ed87c30..586f1b46d6 100644
--- a/packages/data-provider/src/code/workspace.ts
+++ b/packages/data-provider/src/code/workspace.ts
@@ -133,7 +133,7 @@ export function isCodeWorkspaceEnvironment(
(environment.repo === undefined ||
(typeof environment.repo === 'string' &&
environment.repo.length <= 256 &&
- /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(environment.repo))) &&
+ /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+){1,19}$/.test(environment.repo))) &&
(environment.ref === undefined ||
(typeof environment.ref === 'string' &&
environment.ref.trim().length > 0 &&
diff --git a/packages/data-provider/src/config.spec.ts b/packages/data-provider/src/config.spec.ts
index 73321f6d64..034d362090 100644
--- a/packages/data-provider/src/config.spec.ts
+++ b/packages/data-provider/src/config.spec.ts
@@ -1182,7 +1182,19 @@ describe('agent pull request config', () => {
expect(parse({ ...enabled, allowedRepositories: [repo] }).success).toBe(true);
});
- it.each(['*/*', '*', 'owner', 'o/r/extra', '../x', 'o/..', 'a b/c', 'o/*x', ''])(
+ it.each(['g/sub/r', 'g/sub/*', 'a/b/c/d'])('accepts the nested group path %s', (repo) => {
+ expect(parse({ ...enabled, allowedRepositories: [repo] }).success).toBe(true);
+ });
+
+ it.each(['github', 'gitlab', 'bitbucket'])('accepts the provider %s', (provider) => {
+ expect(parse({ ...enabled, provider }).success).toBe(true);
+ });
+
+ it('rejects a provider it does not know', () => {
+ expect(parse({ ...enabled, provider: 'gitea' }).success).toBe(false);
+ });
+
+ it.each(['*/*', '*', 'owner', 'g/*/r', '../x', 'o/..', 'g/../r', 'a b/c', 'o/*x', 'o//r', ''])(
'rejects %p as an allowed repository',
(repo) => {
expect(parse({ ...enabled, allowedRepositories: [repo] }).success).toBe(false);
diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts
index c09c889595..5ed9928616 100644
--- a/packages/data-provider/src/config.ts
+++ b/packages/data-provider/src/config.ts
@@ -63,6 +63,7 @@ import { STATEFUL_CODE_ENVIRONMENTS } from './stateful-code';
import { specsConfigSchema, TSpecsConfig } from './models';
import { fileConfigSchema } from './file-config';
import {
+ PULL_REQUEST_PROVIDERS,
PULL_REQUEST_BATCH_VERSION,
PULL_REQUEST_BATCH_TIMEOUT_MAX_SECONDS,
} from './types/pullRequest';
@@ -506,8 +507,8 @@ const skillSyncTenantIdSchema = z
const pullRequestRepositorySchema = z
.string()
.trim()
- .regex(/^[A-Za-z0-9_.-]+\/(?:[A-Za-z0-9_.-]+|\*)$/, {
- message: 'must be owner/name or owner/*',
+ .regex(/^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+){0,18}\/(?:[A-Za-z0-9_.-]+|\*)$/, {
+ message: 'must be owner/name or owner/*, or a nested group path such as group/sub/name',
})
.refine((value) => value.split('/').every((segment) => segment !== '.' && segment !== '..'), {
message: 'must not contain dot segments',
@@ -1896,8 +1897,13 @@ export const agentsEndpointSchema = baseEndpointSchema
pullRequests: z
.object({
enabled: z.boolean().optional(),
- /** The GitHub the token belongs to: `github.com` (the default), a GitHub Enterprise Server
- * host, or a GitHub Enterprise Cloud data residency host ending in `.ghe.com`. */
+ /** Where pull requests live: `github` (the default), `gitlab` for merge requests on
+ * gitlab.com or a self-managed GitLab, or `bitbucket` for Bitbucket Cloud. Read from the
+ * YAML only, as `host` is, so an override cannot send the token to another service. */
+ provider: z.enum(PULL_REQUEST_PROVIDERS).optional(),
+ /** The host the token belongs to: by default `github.com`, `gitlab.com` or `bitbucket.org`
+ * for the provider; a GitHub Enterprise Server or `*.ghe.com` host, or a self-managed
+ * GitLab host. Bitbucket is Cloud only. Read from the YAML only, never an override. */
host: pullRequestHostSchema.optional(),
/** Environment variable reference holding a read-only GitHub token, e.g.
* `${GITHUB_PULL_REQUEST_TOKEN}`. Never the token itself. Optional: without it the first
diff --git a/packages/data-provider/src/types/pullRequest.ts b/packages/data-provider/src/types/pullRequest.ts
index 02ef2cd42b..ce8e5240b3 100644
--- a/packages/data-provider/src/types/pullRequest.ts
+++ b/packages/data-provider/src/types/pullRequest.ts
@@ -1,7 +1,10 @@
+/** Where pull requests are looked up; one per deployment, set by the operator. */
+export const PULL_REQUEST_PROVIDERS = ['github', 'gitlab', 'bitbucket'] as const;
export const PULL_REQUEST_STATES = ['open', 'closed', 'merged'] as const;
export const PULL_REQUEST_MERGEABLE = ['clean', 'conflicting', 'unknown'] as const;
export const PULL_REQUEST_CHECKS = ['passing', 'failing', 'running', 'none'] as const;
+export type PullRequestProvider = (typeof PULL_REQUEST_PROVIDERS)[number];
export type PullRequestState = (typeof PULL_REQUEST_STATES)[number];
export type PullRequestMergeable = (typeof PULL_REQUEST_MERGEABLE)[number];
export type PullRequestChecks = (typeof PULL_REQUEST_CHECKS)[number];
@@ -10,8 +13,10 @@ export type PullRequestChecks = (typeof PULL_REQUEST_CHECKS)[number];
export type TConversationPullRequest = {
number: number;
title: string;
- /** GitHub page for the pull request. */
+ /** Page of the pull request (a merge request on GitLab) on its provider. */
url: string;
+ /** Where the pull request lives. Absent from a server that predates providers: GitHub. */
+ provider?: PullRequestProvider;
additions: number;
deletions: number;
state: PullRequestState;