Skip to content

Commit da5317b

Browse files
svedinchristiansvedinbrendan-kellam
authored
feat(worker): Add sync all option for bitbucket server (#927)
* Update bitbucket json schema * Add generated schema files * Implement getting all repos * Add documentation * Add changelog --------- Co-authored-by: Christian Svedin <christian.svedin@idainfront.se> Co-authored-by: Brendan Kellam <brendan@sourcebot.dev>
1 parent f9421ed commit da5317b

13 files changed

Lines changed: 92 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Added `wa_user_created` PostHog event fired on successful user sign-up. [#933](https://github.com/sourcebot-dev/sourcebot/pull/933)
1515
- Added `wa_askgh_login_wall_prompted` PostHog event fired when an unauthenticated user attempts to ask a question on Ask GitHub. [#933](https://github.com/sourcebot-dev/sourcebot/pull/933)
1616
- Added Bitbucket Server (Data Center) OAuth 2.0 SSO identity provider support (`provider: "bitbucket-server"`). [#934](https://github.com/sourcebot-dev/sourcebot/pull/934)
17+
- Added Bitbucket Server (Data Center) sync all repositories support. [#927](https://github.com/sourcebot-dev/sourcebot/pull/927)
1718

1819
### Changed
1920
- Hide version upgrade toast for askgithub deployment (`EXPERIMENT_ASK_GH_ENABLED`). [#931](https://github.com/sourcebot-dev/sourcebot/pull/931)

docs/docs/connections/bitbucket-data-center.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ If you're not familiar with Sourcebot [connections](/docs/connections/overview),
3939
}
4040
```
4141
</Accordion>
42+
<Accordion title="Sync all repos">
43+
<Note>Requires a `token` to be set in order to access private repositories.</Note>
44+
```json
45+
{
46+
"type": "bitbucket",
47+
"deploymentType": "server",
48+
"url": "https://mybitbucketdeployment.com",
49+
// Index all repos visible to the provided token
50+
"all": true
51+
}
52+
```
53+
</Accordion>
4254
<Accordion title="Exclude repos from syncing">
4355
```json
4456
{

docs/snippets/schemas/v3/bitbucket.schema.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
"default": "cloud",
6868
"description": "The type of Bitbucket deployment"
6969
},
70+
"all": {
71+
"type": "boolean",
72+
"default": false,
73+
"description": "Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`."
74+
},
7075
"workspaces": {
7176
"type": "array",
7277
"items": {

docs/snippets/schemas/v3/connection.schema.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@
744744
"default": "cloud",
745745
"description": "The type of Bitbucket deployment"
746746
},
747+
"all": {
748+
"type": "boolean",
749+
"default": false,
750+
"description": "Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`."
751+
},
747752
"workspaces": {
748753
"type": "array",
749754
"items": {

docs/snippets/schemas/v3/index.schema.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,11 @@
11591159
"default": "cloud",
11601160
"description": "The type of Bitbucket deployment"
11611161
},
1162+
"all": {
1163+
"type": "boolean",
1164+
"default": false,
1165+
"description": "Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`."
1166+
},
11621167
"workspaces": {
11631168
"type": "array",
11641169
"items": {

packages/backend/src/bitbucket.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ export const getBitbucketReposFromConfig = async (config: BitbucketConnectionCon
7676
let allRepos: BitbucketRepository[] = [];
7777
let allWarnings: string[] = [];
7878

79+
if (config.all === true) {
80+
if (client.deploymentType === BITBUCKET_SERVER) {
81+
const { repos, warnings } = await serverGetAllRepos(client);
82+
allRepos = allRepos.concat(repos);
83+
allWarnings = allWarnings.concat(warnings);
84+
} else {
85+
const warning = `Ignoring option all:true in config: not supported for Bitbucket Cloud`;
86+
logger.warn(warning);
87+
allWarnings = allWarnings.concat(warning);
88+
}
89+
}
90+
7991
if (config.workspaces) {
8092
const { repos, warnings } = await client.getReposForWorkspace(client, config.workspaces);
8193
allRepos = allRepos.concat(repos);
@@ -554,6 +566,26 @@ async function serverGetRepos(client: BitbucketClient, repoList: string[]): Prom
554566
};
555567
}
556568

569+
async function serverGetAllRepos(client: BitbucketClient): Promise<{repos: ServerRepository[], warnings: string[]}> {
570+
logger.debug(`Fetching all repos from Bitbucket Server...`);
571+
const path = `/rest/api/1.0/repos` as ServerGetRequestPath;
572+
const { durationMs, data } = await measure(async () => {
573+
const fetchFn = () => getPaginatedServer<ServerRepository>(path, async (url, start) => {
574+
const response = await client.apiClient.GET(url, {
575+
params: { query: { start } }
576+
});
577+
const { data, error } = response;
578+
if (error) {
579+
throw new Error(`Failed to fetch all repos: ${JSON.stringify(error)}`);
580+
}
581+
return data;
582+
});
583+
return fetchWithRetry(fetchFn, `all repos`, logger);
584+
});
585+
logger.debug(`Found ${data.length} total repos in ${durationMs}ms.`);
586+
return { repos: data, warnings: [] };
587+
}
588+
557589
export function serverShouldExcludeRepo(repo: BitbucketRepository, config: BitbucketConnectionConfig): boolean {
558590
const serverRepo = repo as ServerRepository;
559591

packages/schemas/src/v3/bitbucket.schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ const schema = {
6666
"default": "cloud",
6767
"description": "The type of Bitbucket deployment"
6868
},
69+
"all": {
70+
"type": "boolean",
71+
"default": false,
72+
"description": "Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`."
73+
},
6974
"workspaces": {
7075
"type": "array",
7176
"items": {

packages/schemas/src/v3/bitbucket.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface BitbucketConnectionConfig {
3737
* The type of Bitbucket deployment
3838
*/
3939
deploymentType?: "cloud" | "server";
40+
/**
41+
* Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`.
42+
*/
43+
all?: boolean;
4044
/**
4145
* List of workspaces to sync. Ignored if deploymentType is server.
4246
*/

packages/schemas/src/v3/connection.schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,11 @@ const schema = {
743743
"default": "cloud",
744744
"description": "The type of Bitbucket deployment"
745745
},
746+
"all": {
747+
"type": "boolean",
748+
"default": false,
749+
"description": "Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`."
750+
},
746751
"workspaces": {
747752
"type": "array",
748753
"items": {

packages/schemas/src/v3/connection.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ export interface BitbucketConnectionConfig {
288288
* The type of Bitbucket deployment
289289
*/
290290
deploymentType?: "cloud" | "server";
291+
/**
292+
* Sync all repositories visible to the provided `token` (if any) in the Bitbucket Server instance. This option is ignored if `deploymentType` is `cloud`.
293+
*/
294+
all?: boolean;
291295
/**
292296
* List of workspaces to sync. Ignored if deploymentType is server.
293297
*/

0 commit comments

Comments
 (0)