Skip to content

Commit

Permalink
Added --publish flag to Drupal import. Fixed drupal import bugs. Adde…
Browse files Browse the repository at this point in the history
…d link to folder after import completes. Fixed createSmartComponent function call using incorrect endpoint.
  • Loading branch information
kevinstubbs committed Aug 6, 2024
1 parent 436179d commit 68da812
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 70 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-maps-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pantheon-systems/pcc-cli": patch
---

Added --publish flag to Drupal import. Fixed drupal import bugs. Added link to
folder after import completes. Fixed createSmartComponent function call using
incorrect endpoint.
45 changes: 26 additions & 19 deletions packages/cli/src/cli/commands/import/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,38 @@ import AddOnApiHelper from "../../../lib/addonApiHelper";
import { getLocalAuthDetails } from "../../../lib/localStorage";
import { Logger } from "../../../lib/logger";

export async function createFileOnDrive(
requestBody: Partial<drive_v3.Schema$File>,
body: string,
) {
export async function createFileOnDrive({
requestBody,
body,
drive,
}: {
requestBody: Partial<drive_v3.Schema$File>;
body: string;
drive?: drive_v3.Drive;
}) {
const logger = new Logger();

// Check user has required permission to create drive file
await AddOnApiHelper.getIdToken([
"https://www.googleapis.com/auth/drive.file",
]);
const authDetails = await getLocalAuthDetails();
if (!authDetails) {
logger.error(chalk.red(`ERROR: Failed to retrieve login details.`));
exit(1);
if (!drive) {
// Check user has required permission to create drive file
await AddOnApiHelper.getIdToken([
"https://www.googleapis.com/auth/drive.file",
]);
const authDetails = await getLocalAuthDetails();
if (!authDetails) {
logger.error(chalk.red(`ERROR: Failed to retrieve login details.`));
exit(1);
}

const oauth2Client = new OAuth2Client();
oauth2Client.setCredentials(authDetails);
drive = google.drive({
version: "v3",
auth: oauth2Client,
});
}

// Create Google Doc
const spinner = ora("Creating document on the Google Drive...").start();
const oauth2Client = new OAuth2Client();
oauth2Client.setCredentials(authDetails);
const drive = google.drive({
version: "v3",
auth: oauth2Client,
});

const res = (await drive.files.create({
requestBody: {
...requestBody,
Expand Down
34 changes: 22 additions & 12 deletions packages/cli/src/cli/commands/import/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type DrupalImportParams = {
baseUrl: string;
siteId: string;
verbose: boolean;
automaticallyPublish: boolean;
};

interface DrupalPost {
Expand Down Expand Up @@ -76,7 +77,12 @@ async function getDrupalPosts(url: string) {
}

export const importFromDrupal = errorHandler<DrupalImportParams>(
async ({ baseUrl, siteId, verbose }: DrupalImportParams) => {
async ({
baseUrl,
siteId,
verbose,
automaticallyPublish,
}: DrupalImportParams) => {
const logger = new Logger();

if (baseUrl) {
Expand All @@ -100,7 +106,7 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(
}
}

await AddOnApiHelper.getIdToken([
const idToken = await AddOnApiHelper.getIdToken([
"https://www.googleapis.com/auth/drive.file",
]);

Expand All @@ -118,11 +124,12 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(
auth: oauth2Client,
});

const folderName = `PCC Import from Drupal on ${new Date().toLocaleDateString()} unique id: ${randomUUID()}`;
const folderRes = (await drive.files
.create({
fields: "id,name",
requestBody: {
name: `PCC Import from Drupal on ${new Date().toLocaleDateString()} unique id: ${randomUUID()}`,
name: folderName,
mimeType: "application/vnd.google-apps.folder",
},
})
Expand Down Expand Up @@ -188,23 +195,24 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(

// Initially create a blank document, just to get an article id
// that we can work with for further steps, such as adding smart components.
const { fileId, spinner, drive } = await createFileOnDrive(
{
const { fileId, spinner } = await createFileOnDrive({
requestBody: {
name: post.attributes.title,

parents: [folderId],
},
"",
);
body: "",
drive,
});
spinner.succeed();

// Add it to the PCC site.
await AddOnApiHelper.getDocument(fileId, true);
await AddOnApiHelper.getDocument(fileId, true, undefined, idToken);

// Set the document's content.
const res = (await drive.files.update({
(await drive.files.update({
fileId,
requestBody: {
id: fileId,
mimeType: "application/vnd.google-apps.document",
},
media: {
Expand Down Expand Up @@ -235,7 +243,9 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(
verbose,
);

await AddOnApiHelper.publishDocument(fileId);
if (automaticallyPublish) {
await AddOnApiHelper.publishDocument(fileId);
}
} catch (e) {
console.error(e instanceof AxiosError ? e.response?.data : e);
throw e;
Expand All @@ -248,7 +258,7 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(

logger.log(
chalk.green(
`Successfully imported ${allPosts.length} documents into ${folderRes.data.name}`,
`Successfully imported ${allPosts.length} documents into ${folderName} (https://drive.google.com/drive/u/0/folders/${folderRes.data.id})`,
),
);
},
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,20 @@ yargs(hideBin(process.argv))
default: false,
demandOption: false,
})
.option("publish", {
describe: "Automatically publish each imported article.",
type: "boolean",
default: false,
demandOption: false,
})
.demandOption(["baseUrl", "siteId"]);
},
async (args) =>
await importFromDrupal({
baseUrl: args.baseUrl as string,
siteId: args.siteId as string,
verbose: args.verbose as boolean,
automaticallyPublish: args.publish as boolean,
}),
)
.command(
Expand Down
Loading

0 comments on commit 68da812

Please sign in to comment.