Skip to content

Commit f3d5fa6

Browse files
Add config param all to enable syncing all projects in GitLab instance (#84)
1 parent 83270ff commit f3d5fa6

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Added `DOMAIN_SUB_PATH` environment variable to allow overriding the default domain subpath. ([#74](https://github.com/sourcebot-dev/sourcebot/pull/74))
13+
- Added option `all` to the GitLab index schema, allowing for indexing all projects in a self-hosted GitLab instance. ([#84](https://github.com/sourcebot-dev/sourcebot/pull/84))
1314

1415
## [2.4.3] - 2024-11-18
1516

configs/basic.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
{
6060
"type": "local",
6161
"path": "/path/to/local/repo"
62+
},
63+
// Index all projects in a self-hosted GitLab instance
64+
// that are visible to the provided token.
65+
// Note: this does not work with gitlab.com
66+
{
67+
"type": "gitlab",
68+
"url": "https://gitlab.example.com",
69+
"token": "my-token",
70+
"all": true
6271
}
6372
]
6473
}

packages/backend/src/gitlab.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from 'path';
77
import micromatch from "micromatch";
88

99
const logger = createLogger("GitLab");
10+
const GITLAB_CLOUD_HOSTNAME = "gitlab.com";
1011

1112
export const getGitLabReposFromConfig = async (config: GitLabConfig, ctx: AppContext) => {
1213
const token = config.token ? getTokenFromConfig(config.token, ctx) : undefined;
@@ -18,9 +19,24 @@ export const getGitLabReposFromConfig = async (config: GitLabConfig, ctx: AppCon
1819
host: config.url,
1920
} : {}),
2021
});
22+
const hostname = config.url ? new URL(config.url).hostname : GITLAB_CLOUD_HOSTNAME;
23+
2124

2225
let allProjects: ProjectSchema[] = [];
2326

27+
if (config.all === true) {
28+
if (hostname !== GITLAB_CLOUD_HOSTNAME) {
29+
logger.debug(`Fetching all projects visible in ${config.url}...`);
30+
const { durationMs, data: _projects } = await measure(() => api.Projects.all({
31+
perPage: 100,
32+
}));
33+
logger.debug(`Found ${_projects.length} projects in ${durationMs}ms.`);
34+
allProjects = allProjects.concat(_projects);
35+
} else {
36+
logger.warn(`Ignoring option all:true in ${ctx.configPath} : host is ${GITLAB_CLOUD_HOSTNAME}`);
37+
}
38+
}
39+
2440
if (config.groups) {
2541
const _projects = (await Promise.all(config.groups.map(async (group) => {
2642
logger.debug(`Fetching project info for group ${group}...`);
@@ -62,7 +78,6 @@ export const getGitLabReposFromConfig = async (config: GitLabConfig, ctx: AppCon
6278

6379
let repos: GitRepository[] = allProjects
6480
.map((project) => {
65-
const hostname = config.url ? new URL(config.url).hostname : "gitlab.com";
6681
const repoId = `${hostname}/${project.path_with_namespace}`;
6782
const repoPath = path.resolve(path.join(ctx.reposPath, `${repoId}.git`))
6883
const isFork = project.forked_from_project !== undefined;

packages/backend/src/schemas/v2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export interface GitLabConfig {
9393
* The URL of the GitLab host. Defaults to https://gitlab.com
9494
*/
9595
url?: string;
96+
/**
97+
* Sync all projects visible to the provided `token` (if any) in the GitLab instance. This option is ignored if `url` is either unset or set to https://gitlab.com .
98+
*/
99+
all?: boolean;
96100
/**
97101
* List of users to sync with. All projects owned by the user and visible to the provided `token` (if any) will be synced, unless explicitly defined in the `exclude` property.
98102
*/

schemas/v2/index.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@
189189
],
190190
"pattern": "^https?:\\/\\/[^\\s/$.?#].[^\\s]*$"
191191
},
192+
"all": {
193+
"type": "boolean",
194+
"default": false,
195+
"description": "Sync all projects visible to the provided `token` (if any) in the GitLab instance. This option is ignored if `url` is either unset or set to https://gitlab.com ."
196+
},
192197
"users": {
193198
"type": "array",
194199
"items": {

0 commit comments

Comments
 (0)