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", "@typescript-eslint/no-unused-vars": "off",
"unused-imports/no-unused-imports": "error", "unused-imports/no-unused-imports": "error",
"@typescript-eslint/no-empty-function": "off",
"unused-imports/no-unused-vars": [ "unused-imports/no-unused-vars": [
"warn", "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 req
* @param res * @param res
* @returns * @returns

View File

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

View File

@@ -150,7 +150,7 @@ router.get(
requireIntegrationAuthorizationAuth({ requireIntegrationAuthorizationAuth({
acceptedRoles: [ADMIN, MEMBER], acceptedRoles: [ADMIN, MEMBER],
}), }),
param("integrationAuthId"), param("integrationAuthId").exists().isString(),
validateRequest, validateRequest,
integrationAuthController.getIntegrationAuthBitBucketWorkspaces 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_LARAVELFORGE_API_URL = "https://forge.laravel.com";
export const INTEGRATION_CHECKLY_API_URL = "https://api.checklyhq.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_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 () => { export const getIntegrationOptions = async () => {
const INTEGRATION_OPTIONS = [ const INTEGRATION_OPTIONS = [

View File

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