Skip to content

Commit

Permalink
DOP-5036 check for env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
anabellabuckvar committed Sep 30, 2024
1 parent 2efa9f4 commit a0609b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
22 changes: 22 additions & 0 deletions search-manifest/src/assertEnvVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const assertEnvVars = (vars: any) => {
const missingVars = Object.entries(vars)
.filter(([, value]) => !value)
.map(([key]) => `- ${key}`)
.join("\n");
if (missingVars)
throw new Error(`Missing env var(s) ${JSON.stringify(missingVars)}`);
return true;
};

export const getEnvVars = () => {
const ENV_VARS = {
ATLAS_CLUSTER0_URI: `mongodb+srv://${process.env.MONGO_ATLAS_USERNAME}:${process.env.MONGO_ATLAS_PASSWORD}@${process.env.MONGO_ATLAS_CLUSTER0_HOST}/?retryWrites=true&w=majority`,
SNOOTY_DB_NAME: `${process.env.MONGO_ATLAS_POOL_DB_NAME}`,
ATLAS_SEARCH_URI: `mongodb+srv://${process.env.MONGO_ATLAS_USERNAME}:${process.env.MONGO_ATLAS_PASSWORD}@${process.env.MONGO_ATLAS_SEARCH_HOST}/?retryWrites=true&w=majority`,
SEARCH_DB_NAME: `${process.env.MONGO_ATLAS_SEARCH_DB_NAME}`,
REPOS_BRANCHES_COLLECTION: "repos_branches",
DOCSETS_COLLECTION: "docsets",
DOCUMENTS_COLLECTION: "documents",
};
if (assertEnvVars(ENV_VARS)) return ENV_VARS;
};
16 changes: 12 additions & 4 deletions search-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ export const generateManifest = async () => {
}
return manifest;
};

//Return indexing data from a page's AST for search purposes.
integration.addBuildEventHandler(
"onSuccess",
async ({ utils: { run }, netlifyConfig }) => {
// Get content repo zipfile as AST representation

await run.command("unzip -o bundle.zip");
const branch = netlifyConfig.build?.environment.BRANCH;

const branchName = netlifyConfig.build?.environment.BRANCH;
const repoName =
process.env.REPO_NAME ?? netlifyConfig.build?.environment.SITE_NAME;
//check that an environment variable for repo name was set
if (!repoName || !branchName) {
throw new Error(
"Repo or branch name was not found, manifest cannot be uploaded to Atlas or S3 "
);
}

const manifest = await generateManifest();

Expand All @@ -64,14 +72,14 @@ integration.addBuildEventHandler(
projectName: string;
url: string;
includeInGlobalSearch: boolean;
} = await getProperties(branch);
} = await getProperties({ branchName, repoName });

console.log("=========== Uploading Manifests to S3=================");
const uploadParams: s3UploadParams = {
bucket: "docs-search-indexes-test",
//TODO: change this values based on environments
prefix: "search-indexes/ab-testing",
fileName: `${projectName}-${branch}.json`,
fileName: `${projectName}-${branchName}.json`,
manifest: manifest.export(),
};

Expand Down
21 changes: 9 additions & 12 deletions search-manifest/src/uploadToAtlas/getProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,19 @@ export const getBranch = (branches: Array<BranchEntry>, branchName: string) => {
throw new Error(`Branch ${branchName} not found in branches object`);
};

const getProperties = async (branchName: string) => {
const REPO_NAME = process.env.REPO_NAME;

//check that an environment variable for repo name was set
if (!REPO_NAME) {
throw new Error(
"No repo name supplied as environment variable, manifest cannot be uploaded to Atlas Search.Documents collection "
);
}

const getProperties = async ({
branchName,
repoName,
}: {
branchName: string;
repoName: string;
}) => {
//connect to database and get repos_branches, docsets collections
const repos_branches = await getReposBranchesCollection();
const docsets = await getDocsetsCollection();

const repo: ReposBranchesDocument = await getRepoEntry({
repoName: REPO_NAME,
repoName: repoName,
repos_branches,
});

Expand All @@ -116,7 +113,7 @@ const getProperties = async (branchName: string) => {
if (!active) {
await deleteStaleProperties(searchProperty);
throw new Error(
`Search manifest should not be generated for inactive version ${version} of repo ${REPO_NAME}. Removing all associated manifests`
`Search manifest should not be generated for inactive version ${version} of repo ${repoName}. Removing all associated manifests`
);
}
// await closeSnootyDb();
Expand Down
4 changes: 2 additions & 2 deletions search-manifest/tests/integration/uploadToS3.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import type {
import {
PutObjectCommand,
PutObjectCommandOutput,
type PutObjectCommandOutput,
S3Client,
} from "@aws-sdk/client-s3";
import { mockClient } from "aws-sdk-client-mock";
Expand Down

0 comments on commit a0609b5

Please sign in to comment.