Update Bitbucket sync function

This commit is contained in:
Tuan Dang
2023-07-21 20:16:39 +07:00
parent df7ad9e645
commit d9f6c27e4d
6 changed files with 26 additions and 31 deletions

View File

@@ -25,7 +25,6 @@
],
"@typescript-eslint/no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"@typescript-eslint/no-empty-function": "off",
"unused-imports/no-unused-vars": [
"warn",
{

View File

@@ -386,7 +386,7 @@ export const getIntegrationAuthRailwayServices = async (req: Request, res: Respo
};
/**
* Return list of workspaces allowed for integration with integration authorization id [integrationAuthId]
* Return list of workspaces allowed for Bitbucket integration
* @param req
* @param res
* @returns

View File

@@ -678,8 +678,6 @@ const syncSecretsVercel = async ({
return true;
});
// return secret.target.includes(integration.targetEnvironment);
const res: { [key: string]: VercelSecret } = {};
for await (const vercelSecret of vercelSecrets) {
@@ -1963,16 +1961,17 @@ const syncSecretsBitBucket = async ({
secrets: any;
accessToken: string;
}) => {
interface VariablesResponse {
size: number;
page: number;
pageLen: number;
next: string;
previous: string;
values: Array<Variable>;
values: Array<BitbucketVariable>;
}
interface Variable {
interface BitbucketVariable {
type: string;
uuid: string;
key: string;
@@ -1980,13 +1979,11 @@ const syncSecretsBitBucket = async ({
secured: boolean;
}
const existingSecrets: Variable[] = [];
const workspaceSlug = integration.targetEnvironmentId
const repoSlug = integration.appId
let hasNextPage = true;
let variablesUrl = `${INTEGRATION_BITBUCKET_API_URL}/2.0/repositories/${workspaceSlug}/${repoSlug}/pipelines_config/variables`
const res: { [key: string]: BitbucketVariable } = {};
let hasNextPage = true;
let variablesUrl = `${INTEGRATION_BITBUCKET_API_URL}/2.0/repositories/${integration.targetEnvironmentId}/${integration.appId}/pipelines_config/variables`
// Fetch all repository variables
while (hasNextPage) {
const { data }: { data: VariablesResponse } = await standardRequest.get(
variablesUrl,
@@ -2000,8 +1997,8 @@ const syncSecretsBitBucket = async ({
if (data?.values.length > 0) {
data.values.forEach((variable) => {
existingSecrets.push(variable)
})
res[variable.key] = variable;
});
}
if (data.next) {
@@ -2011,12 +2008,11 @@ const syncSecretsBitBucket = async ({
}
}
Object.keys(secrets).forEach(async (key) => {
const existingSecret = existingSecrets.find((secret) => secret.key.toUpperCase() === key.toUpperCase());
if (existingSecret) {
// Update existing secrets
for await (const key of Object.keys(secrets)) {
if (key in res) {
// update existing secret
await standardRequest.put(
`${variablesUrl}/${existingSecret.uuid}`,
`${variablesUrl}/${res[key].uuid}`,
{
key,
value: secrets[key],
@@ -2030,7 +2026,7 @@ const syncSecretsBitBucket = async ({
}
);
} else {
// Create new secrets
// create new secret
await standardRequest.post(
variablesUrl,
{
@@ -2046,22 +2042,22 @@ const syncSecretsBitBucket = async ({
}
);
}
})
}
// Delete secrets
existingSecrets.forEach(async (existingSecret) => {
if (!(existingSecret.key in secrets) && existingSecret.secured) {
for await (const key of Object.keys(res)) {
if (!(key in secrets)) {
// delete secret
await standardRequest.delete(
`${variablesUrl}/${existingSecret.uuid}`,
`${variablesUrl}/${res[key].uuid}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Accept": "application/json",
},
}
}
);
);
}
})
}
}
export { syncSecrets };

View File

@@ -150,7 +150,7 @@ router.get(
requireIntegrationAuthorizationAuth({
acceptedRoles: [ADMIN, MEMBER],
}),
param("integrationAuthId"),
param("integrationAuthId").exists().isString(),
validateRequest,
integrationAuthController.getIntegrationAuthBitBucketWorkspaces
);

View File

@@ -75,7 +75,7 @@ export const INTEGRATION_SUPABASE_API_URL = "https://api.supabase.com";
export const INTEGRATION_LARAVELFORGE_API_URL = "https://forge.laravel.com";
export const INTEGRATION_CHECKLY_API_URL = "https://api.checklyhq.com";
export const INTEGRATION_CLOUDFLARE_PAGES_API_URL = "https://api.cloudflare.com";
export const INTEGRATION_BITBUCKET_API_URL = "https://api.bitbucket.org/";
export const INTEGRATION_BITBUCKET_API_URL = "https://api.bitbucket.org";
export const getIntegrationOptions = async () => {
const INTEGRATION_OPTIONS = [

View File

@@ -34,7 +34,7 @@ const integrationAuthKeys = {
appId: string;
}) => [{ integrationAuthId, appId }, "integrationAuthRailwayServices"] as const,
getIntegrationAuthBitBucketWorkspaces: (integrationAuthId: string) =>
[{ integrationAuthId }, "integrationAuthTeams"] as const,
[{ integrationAuthId }, "integrationAuthBitbucketWorkspaces"] as const,
};
const fetchIntegrationAuthById = async (integrationAuthId: string) => {