Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[connectors] fix(Zendesk) - fix the article garbage collection #9005

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
16 changes: 4 additions & 12 deletions connectors/src/connectors/zendesk/lib/sync_article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,19 @@ const turndownService = new TurndownService();
*/
export async function deleteArticle(
connectorId: ModelId,
article: ZendeskFetchedArticle,
articleId: number,
dataSourceConfig: DataSourceConfig,
loggerArgs: Record<string, string | number | null>
): Promise<void> {
logger.info(
{
...loggerArgs,
connectorId,
articleId: article.id,
name: article.name,
},
{ ...loggerArgs, connectorId, articleId },
"[Zendesk] Deleting article."
);
await deleteFromDataSource(
dataSourceConfig,
getArticleInternalId({ connectorId, articleId: article.id })
getArticleInternalId({ connectorId, articleId })
);
await ZendeskArticleResource.deleteByArticleId({
connectorId,
articleId: article.id,
});
await ZendeskArticleResource.deleteByArticleId({ connectorId, articleId });
}

/**
Expand Down
19 changes: 19 additions & 0 deletions connectors/src/connectors/zendesk/lib/zendesk_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ async function fetchFromZendeskWithRetries({
return response;
}

/**
* Fetches a single article from the Zendesk API.
*/
export async function fetchZendeskArticle({
brandSubdomain,
accessToken,
articleId,
}: {
brandSubdomain: string;
accessToken: string;
articleId: number;
}): Promise<ZendeskFetchedArticle | null> {
const response = await fetchFromZendeskWithRetries({
url: `https://${brandSubdomain}.zendesk.com/api/v2/help_center/articles/${articleId}`,
accessToken,
});
return response.article ?? null;
}

/**
* Fetches a batch of categories from the Zendesk API.
*/
Expand Down
22 changes: 16 additions & 6 deletions connectors/src/connectors/zendesk/temporal/gc_activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getZendeskSubdomainAndAccessToken } from "@connectors/connectors/zendes
import {
changeZendeskClientSubdomain,
createZendeskClient,
fetchZendeskArticle,
} from "@connectors/connectors/zendesk/lib/zendesk_api";
import { ZENDESK_BATCH_SIZE } from "@connectors/connectors/zendesk/temporal/config";
import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config";
Expand Down Expand Up @@ -150,10 +151,11 @@ export async function removeMissingArticleBatchActivity({
dataSourceId: dataSourceConfig.dataSourceId,
};

const zendeskApiClient = createZendeskClient(
await getZendeskSubdomainAndAccessToken(connector.connectionId)
const { subdomain, accessToken } = await getZendeskSubdomainAndAccessToken(
connector.connectionId
);
await changeZendeskClientSubdomain(zendeskApiClient, {
const zendeskApiClient = createZendeskClient({ subdomain, accessToken });
const brandSubdomain = await changeZendeskClientSubdomain(zendeskApiClient, {
connectorId,
brandId,
});
Expand All @@ -162,10 +164,18 @@ export async function removeMissingArticleBatchActivity({
await concurrentExecutor(
articleIds,
async (articleId) => {
const article =
await zendeskApiClient.helpcenter.articles.show(articleId);
const article = await fetchZendeskArticle({
brandSubdomain,
articleId,
accessToken,
});
if (!article) {
await deleteArticle(connectorId, article, dataSourceConfig, loggerArgs);
await deleteArticle(
connectorId,
articleId,
dataSourceConfig,
loggerArgs
);
}
},
{ concurrency: 10 }
Expand Down