Skip to content

Commit

Permalink
feat: add CLI to skip gdrive files (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanierh authored Nov 3, 2023
1 parent c415c6f commit 82f3d35
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
59 changes: 56 additions & 3 deletions connectors/src/admin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import {
STOP_CONNECTOR_BY_TYPE,
SYNC_CONNECTOR_BY_TYPE,
} from "@connectors/connectors";
import { getDocumentId } from "@connectors/connectors/google_drive/temporal/activities";
import { launchGoogleDriveRenewWebhooksWorkflow } from "@connectors/connectors/google_drive/temporal/client";
import { toggleSlackbot } from "@connectors/connectors/slack/bot";
import { launchSlackSyncOneThreadWorkflow } from "@connectors/connectors/slack/temporal/client";
import { Connector, NotionDatabase, NotionPage } from "@connectors/lib/models";
import {
Connector,
GoogleDriveFiles,
NotionDatabase,
NotionPage,
} from "@connectors/lib/models";
import { Result } from "@connectors/lib/result";

const connectors = async (command: string, args: parseArgs.ParsedArgs) => {
Expand Down Expand Up @@ -249,10 +255,57 @@ const notion = async (command: string, args: parseArgs.ParsedArgs) => {
}
};

const google = async (command: string) => {
const google = async (command: string, args: parseArgs.ParsedArgs) => {
switch (command) {
case "restart-google-webhooks": {
await throwOnError(launchGoogleDriveRenewWebhooksWorkflow());
return;
}
case "skip-file": {
if (!args.wId) {
throw new Error("Missing --wId argument");
}
if (!args.dataSourceName) {
throw new Error("Missing --dataSourceName argument");
}
if (!args.fileId) {
throw new Error("Missing --fileId argument");
}

const connector = await Connector.findOne({
where: {
workspaceId: args.wId,
dataSourceName: args.dataSourceName,
},
});
if (!connector) {
throw new Error(
`Could not find connector for workspace ${args.wId} and data source ${args.dataSourceName}`
);
}

const existingFile = await GoogleDriveFiles.findOne({
where: {
driveFileId: args.fileId,
connectorId: connector.id,
},
});
if (existingFile) {
await existingFile.update({
skipReason: args.reason || "blacklisted",
});
} else {
await GoogleDriveFiles.create({
driveFileId: args.fileId,
dustFileId: getDocumentId(args.fileId),
name: "unknown",
mimeType: "unknown",
connectorId: connector.id,
skipReason: args.reason || "blacklisted",
});
}

return;
}
}
};
Expand Down Expand Up @@ -367,7 +420,7 @@ const main = async () => {
await notion(command, argv);
return;
case "google":
await google(command);
await google(command, argv);
return;
case "slack":
await slack(command, argv);
Expand Down
20 changes: 20 additions & 0 deletions connectors/src/connectors/google_drive/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,26 @@ async function syncOneFile(
const documentId = getDocumentId(file.id);
let documentContent: string | undefined = undefined;

const fileInDb = await GoogleDriveFiles.findOne({
where: {
connectorId: connectorId,
driveFileId: file.id,
},
});

if (fileInDb?.skipReason) {
logger.info(
{
documentId,
dataSourceConfig,
fileId: file.id,
title: file.name,
},
`Google Drive document skipped with skip reason ${fileInDb.skipReason}`
);
return false;
}

if (FILES_IGNORE_LIST.includes(file.id)) {
logger.info(
{
Expand Down
5 changes: 5 additions & 0 deletions connectors/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ export class GoogleDriveFiles extends Model<
declare updatedAt: CreationOptional<Date>;
declare lastSeenTs: Date | null;
declare lastUpsertedTs: Date | null;
declare skipReason: string | null;
declare connectorId: ForeignKey<Connector["id"]>;
declare dustFileId: string;
declare driveFileId: string;
Expand Down Expand Up @@ -1135,6 +1136,10 @@ GoogleDriveFiles.init(
type: DataTypes.DATE,
allowNull: true,
},
skipReason: {
type: DataTypes.STRING,
allowNull: true,
},
connectorId: {
type: DataTypes.INTEGER,
allowNull: false,
Expand Down

0 comments on commit 82f3d35

Please sign in to comment.