Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/api/src/admin/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ function serializeCredential(
credential: SkillSyncCredentialSummary,
): TGitHubSkillSyncCredentialSummary {
return {
provider: credential.provider,
// This admin surface is GitHub-only; see serializeSourceStatus for why the
// literal is asserted instead of passed through.
provider: 'github',
credentialKey: credential.credentialKey,
credentialPresent: credential.credentialPresent,
tokenFingerprint: credential.tokenFingerprint,
Expand Down Expand Up @@ -129,7 +131,10 @@ function serializeSourceStatus(
): TGitHubSkillSyncSourceStatus {
const includePrivateSourceMetadata = includeCredentialMetadata;
return {
provider: status.provider,
// This admin surface is GitHub-only; `status.provider` is asserted rather
// than passed through so a future generic-provider status object cannot
// silently widen this response's wire contract.
provider: 'github',
sourceId: status.sourceId,
tenantId: status.tenantId,
status: status.status,
Expand Down
85 changes: 85 additions & 0 deletions packages/api/src/skills/sync/adapters/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type {
SkillSyncGitHubSourceConfig,
SkillSyncGitLabSourceConfig,
} from 'librechat-data-provider';
import { createRepoAdapter } from './factory';

const githubSource: SkillSyncGitHubSourceConfig = {
id: 'librechat-skills',
owner: 'LibreChat',
repo: 'skills',
ref: 'main',
paths: ['skills'],
credentialKey: 'github-skills-prod',
};

const gitlabSource: SkillSyncGitLabSourceConfig = {
id: 'librechat-skills-gitlab',
projectId: 'group%2Fskills',
ref: 'main',
paths: ['skills'],
credentialKey: 'gitlab-skills-prod',
};

function credentials() {
return { token: 'secret-token', fetchFn: jest.fn() as unknown as typeof fetch };
}

describe('createRepoAdapter', () => {
it('creates a GitHub adapter exposing the shared GitRepoAdapter surface', () => {
const adapter = createRepoAdapter('github', githubSource, credentials());
expect(typeof adapter.resolveCommit).toBe('function');
expect(typeof adapter.fetchTreeEntries).toBe('function');
expect(typeof adapter.fetchFileContent).toBe('function');
});

it('creates a GitLab adapter exposing the shared GitRepoAdapter surface', () => {
const adapter = createRepoAdapter('gitlab', gitlabSource, credentials());
expect(typeof adapter.resolveCommit).toBe('function');
expect(typeof adapter.fetchTreeEntries).toBe('function');
expect(typeof adapter.fetchFileContent).toBe('function');
});

it('routes a GitHub source through the GitHub REST API base', async () => {
const fetchFn = jest.fn(async () => ({
ok: true,
status: 200,
headers: { get: () => null },
json: async () => ({ sha: 'commit-sha', commit: { tree: { sha: 'tree-sha' } } }),
})) as unknown as typeof fetch;
const adapter = createRepoAdapter('github', githubSource, { token: 't', fetchFn });
await adapter.resolveCommit('main');
const [calledUrl] = (fetchFn as unknown as jest.Mock).mock.calls[0];
expect(calledUrl.toString()).toContain('api.github.com');
});

it('routes a GitLab source through the GitLab REST API base', async () => {
const fetchFn = jest.fn(async () => ({
ok: true,
status: 200,
headers: { get: () => null },
json: async () => ({ id: 'commit-sha' }),
})) as unknown as typeof fetch;
const adapter = createRepoAdapter('gitlab', gitlabSource, { token: 't', fetchFn });
await adapter.resolveCommit('main');
const [calledUrl] = (fetchFn as unknown as jest.Mock).mock.calls[0];
expect(calledUrl.toString()).toContain('gitlab.com/api/v4');
});

it('honors a self-hosted GitLab baseUrl when routing', async () => {
const fetchFn = jest.fn(async () => ({
ok: true,
status: 200,
headers: { get: () => null },
json: async () => ({ id: 'commit-sha' }),
})) as unknown as typeof fetch;
const source: SkillSyncGitLabSourceConfig = {
...gitlabSource,
baseUrl: 'https://gitlab.internal.example.com',
};
const adapter = createRepoAdapter('gitlab', source, { token: 't', fetchFn });
await adapter.resolveCommit('main');
const [calledUrl] = (fetchFn as unknown as jest.Mock).mock.calls[0];
expect(calledUrl.toString()).toContain('gitlab.internal.example.com/api/v4');
});
});
47 changes: 47 additions & 0 deletions packages/api/src/skills/sync/adapters/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type {
SkillSyncGitHubSourceConfig,
SkillSyncGitLabSourceConfig,
} from 'librechat-data-provider';
import type { GitRepoAdapter } from './types';
import { createGitHubRepoAdapter } from './github';
import { createGitLabRepoAdapter } from './gitlab';

type FetchFn = typeof fetch;

export type RepoAdapterCredentials = {
token: string;
fetchFn: FetchFn;
};

export function createRepoAdapter(
provider: 'github',
source: SkillSyncGitHubSourceConfig,
credentials: RepoAdapterCredentials,
): GitRepoAdapter;
export function createRepoAdapter(
provider: 'gitlab',
source: SkillSyncGitLabSourceConfig,
credentials: RepoAdapterCredentials,
): GitRepoAdapter;
export function createRepoAdapter(
provider: 'github' | 'gitlab',
source: SkillSyncGitHubSourceConfig | SkillSyncGitLabSourceConfig,
credentials: RepoAdapterCredentials,
): GitRepoAdapter {
if (provider === 'github') {
const githubSource = source as SkillSyncGitHubSourceConfig;
return createGitHubRepoAdapter({
owner: githubSource.owner,
repo: githubSource.repo,
token: credentials.token,
fetchFn: credentials.fetchFn,
});
}
const gitlabSource = source as SkillSyncGitLabSourceConfig;
return createGitLabRepoAdapter({
baseUrl: gitlabSource.baseUrl,
projectId: gitlabSource.projectId,
token: credentials.token,
fetchFn: credentials.fetchFn,
});
}
Loading