Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into DOP-4452
  • Loading branch information
mayaraman19 committed Apr 12, 2024
2 parents c9aa695 + b467418 commit b20b7a7
Show file tree
Hide file tree
Showing 23 changed files with 470 additions and 106 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/deploy-stg-enhanced-webhooks.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
on:
push:
paths: ["api/v2/**", "cdk-infra/lib/constructs/api/**", "cdk-infra/utils/**"]
paths: ['api/**', 'cdk-infra/lib/constructs/api/**', 'cdk-infra/utils/**']
branches:
- "main"
- "integration"
- 'main'
- 'integration'

concurrency:
group: environment-stg-enhanced-webhooks-${{ github.ref }}
Expand All @@ -30,4 +30,3 @@ jobs:
npm ci
npm run deploy:feature:stack -- -c env=dotcomstg -c customFeatureName=enhancedApp-dotcomstg auto-builder-stack-enhancedApp-dotcomstg-webhooks
npm run deploy:feature:stack -- -c env=stg -c customFeatureName=enhancedApp-stg auto-builder-stack-enhancedApp-stg-webhooks
1 change: 0 additions & 1 deletion .github/workflows/deploy-stg-enhanced-worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
branches:
- 'main'
- 'integration'

concurrency:
group: environment-stg-enhanced-worker-${{ github.ref }}
cancel-in-progress: true
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/update-feature-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ jobs:
node_modules
cdk-infra/node_modules
key: ${{ github.head_ref }}
- name: Install Dependencies
if: steps.cache-restore.outputs.cache-hit != 'true'
run: |
npm ci
cd cdk-infra/
npm ci
- uses: dorny/paths-filter@v2
id: filter
with:
Expand Down Expand Up @@ -96,6 +102,12 @@ jobs:
node_modules
cdk-infra/node_modules
key: ${{ github.head_ref }}
- name: Install Dependencies
if: steps.cache-restore.outputs.cache-hit != 'true'
run: |
npm ci
cd cdk-infra/
npm ci
- uses: dorny/paths-filter@v2
id: filter
with:
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ RUN cd ./modules/oas-page-builder \
# where repo work will happen
FROM ubuntu:20.04
ARG WORK_DIRECTORY=/home/docsworker-xlarge
ARG SNOOTY_PARSER_VERSION=0.16.4
ARG SNOOTY_FRONTEND_VERSION=0.16.8
ARG MUT_VERSION=0.11.1
ARG SNOOTY_PARSER_VERSION=0.16.5
ARG SNOOTY_FRONTEND_VERSION=0.16.9
ARG MUT_VERSION=0.11.2
ARG REDOC_CLI_VERSION=1.2.3
ARG NPM_BASE_64_AUTH
ARG NPM_EMAIL
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile.local
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM arm64v8/ubuntu:20.04 as initial
ARG NPM_BASE_64_AUTH
ARG NPM_EMAIL
ARG SNOOTY_PARSER_VERSION=0.16.4
ARG SNOOTY_FRONTEND_VERSION=0.16.8
ARG MUT_VERSION=0.10.7
ARG SNOOTY_PARSER_VERSION=0.16.5
ARG SNOOTY_FRONTEND_VERSION=0.16.9
ARG MUT_VERSION=0.11.2
ARG REDOC_CLI_VERSION=1.2.3
ARG NPM_BASE_64_AUTH
ARG NPM_EMAIL
Expand Down
85 changes: 58 additions & 27 deletions api/controllers/v1/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RepoEntitlementsRepository } from '../../../src/repositories/repoEntitl
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
import { ConsoleLogger, ILogger } from '../../../src/services/logger';
import { SlackConnector } from '../../../src/services/slack';
import { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import { JobRepository } from '../../../src/repositories/jobRepository';
import {
buildEntitledBranchList,
Expand All @@ -13,13 +14,23 @@ import {
prepResponse,
} from '../../handlers/slack';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';
import { Payload } from '../../../src/entities/job';

export const DisplayRepoOptions = async (event: any = {}, context: any = {}): Promise<any> => {
export const DisplayRepoOptions = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
const consoleLogger = new ConsoleLogger();
const slackConnector = new SlackConnector(consoleLogger, c);

if (!slackConnector.validateSlackRequest(event)) {
return prepResponse(401, 'text/plain', 'Signature Mismatch, Authentication Failed!');
}

if (!event.body) {
return {
statusCode: 400,
body: 'Event body is undefined',
};
}

const client = new mongodb.MongoClient(c.get('dbUrl'));
await client.connect();
const db = client.db(process.env.DB_NAME);
Expand All @@ -34,8 +45,11 @@ export const DisplayRepoOptions = async (event: any = {}, context: any = {}): Pr
: 'User is not entitled!';
return prepResponse(401, 'text/plain', response);
}

const isAdmin = await repoEntitlementRepository.getIsAdmin(key_val['user_id']);

const entitledBranches = await buildEntitledBranchList(entitlement, repoBranchesRepository);
const resp = await slackConnector.displayRepoOptions(entitledBranches, key_val['trigger_id']);
const resp = await slackConnector.displayRepoOptions(entitledBranches, key_val['trigger_id'], isAdmin);
if (resp?.status == 200 && resp?.data) {
return {
statusCode: 200,
Expand Down Expand Up @@ -71,22 +85,29 @@ export const getDeployableJobs = async (
) => {
const deployable = [];

for (let i = 0; i < values.repo_option.length; i++) {
let repoOwner: string, repoName: string, branchName: string, directory: string | undefined;
const splitValues = values.repo_option[i].value.split('/');

if (splitValues.length === 3) {
// e.g. mongodb/docs-realm/master => (owner/repo/branch)
[repoOwner, repoName, branchName] = splitValues;
} else if (splitValues.length === 4 && process.env.FEATURE_FLAG_MONOREPO_PATH === 'true') {
// e.g. 10gen/docs-monorepo/cloud-docs/master => (owner/monorepo/repoDirectory/branch)
[repoOwner, repoName, directory, branchName] = splitValues;
for (let i = 0; i < values?.repo_option?.length; i++) {
let jobTitle: string, repoOwner: string, repoName: string, branchName: string, directory: string | undefined;
if (values.deploy_option == 'deploy_all') {
repoOwner = 'mongodb';
branchName = 'master';
repoName = values.repo_option[i].repoName;
jobTitle = `Slack deploy: ${repoOwner}/${repoName}/${branchName}, by ${entitlement.github_username}`;
} else {
throw Error('Selected entitlement value is configured incorrectly. Check user entitlements!');
const splitValues = values.repo_option[i].value.split('/');
jobTitle = `Slack deploy: ${values.repo_option[i].value}, by ${entitlement.github_username}`;

if (splitValues.length === 3) {
// e.g. mongodb/docs-realm/master => (owner/repo/branch)
[repoOwner, repoName, branchName] = splitValues;
} else if (splitValues.length === 4 && process.env.FEATURE_FLAG_MONOREPO_PATH === 'true') {
// e.g. 10gen/docs-monorepo/cloud-docs/master => (owner/monorepo/repoDirectory/branch)
[repoOwner, repoName, directory, branchName] = splitValues;
} else {
throw Error('Selected entitlement value is configured incorrectly. Check user entitlements!');
}
}

const hashOption = values?.hash_option ?? null;
const jobTitle = `Slack deploy: ${values.repo_option[i].value}, by ${entitlement.github_username}`;
const jobUserName = entitlement.github_username;
const jobUserEmail = entitlement?.email ?? '';

Expand All @@ -96,12 +117,11 @@ export const getDeployableJobs = async (
const branchObject = await repoBranchesRepository.getRepoBranchAliases(repoName, branchName, repoInfo.project);
if (!branchObject?.aliasObject) continue;

// TODO: Create strong typing for these rather than comments
const publishOriginalBranchName = branchObject.aliasObject.publishOriginalBranchName; // bool
let aliases = branchObject.aliasObject.urlAliases; // array or null
let urlSlug = branchObject.aliasObject.urlSlug; // string or null, string must match value in urlAliases or gitBranchName
const isStableBranch = branchObject.aliasObject.isStableBranch; // bool or Falsey
aliases = aliases?.filter((a) => a);
const publishOriginalBranchName: boolean = branchObject.aliasObject.publishOriginalBranchName;
const aliases: string[] | null = branchObject.aliasObject.urlAliases;
let urlSlug: string = branchObject.aliasObject.urlSlug; // string or null, string must match value in urlAliases or gitBranchName
const isStableBranch = !!branchObject.aliasObject.isStableBranch; // bool or Falsey, add strong typing

if (!urlSlug || !urlSlug.trim()) {
urlSlug = branchName;
}
Expand All @@ -118,12 +138,10 @@ export const getDeployableJobs = async (
urlSlug,
false,
false,
false,
isStableBranch,
directory
);

newPayload.stable = !!isStableBranch;

if (!aliases || aliases.length === 0) {
if (non_versioned) {
newPayload.urlSlug = '';
Expand Down Expand Up @@ -164,7 +182,7 @@ export const getDeployableJobs = async (
return deployable;
};

export const DeployRepo = async (event: any = {}, context: any = {}): Promise<any> => {
export const DeployRepo = async (event: any = {}): Promise<any> => {
const consoleLogger = new ConsoleLogger();
const slackConnector = new SlackConnector(consoleLogger, c);
if (!slackConnector.validateSlackRequest(event)) {
Expand All @@ -173,6 +191,7 @@ export const DeployRepo = async (event: any = {}, context: any = {}): Promise<an
const client = new mongodb.MongoClient(c.get('dbUrl'));
await client.connect();
const db = client.db(c.get('dbName'));

const repoEntitlementRepository = new RepoEntitlementsRepository(db, c, consoleLogger);
const repoBranchesRepository = new RepoBranchesRepository(db, c, consoleLogger);
const docsetsRepository = new DocsetsRepository(db, c, consoleLogger);
Expand All @@ -183,14 +202,26 @@ export const DeployRepo = async (event: any = {}, context: any = {}): Promise<an
const parsed = JSON.parse(decoded);
const stateValues = parsed.view.state.values;

//TODO: create an interface for slack view_submission payloads
if (parsed.type !== 'view_submission') {
return prepResponse(200, 'text/plain', 'Form not submitted, will not process request');
}

const entitlement = await repoEntitlementRepository.getRepoEntitlementsBySlackUserId(parsed.user.id);
if (!isUserEntitled(entitlement)) {
return prepResponse(401, 'text/plain', 'User is not entitled!');
}

const values = slackConnector.parseSelection(stateValues);

let values = [];
const isAdmin = await repoEntitlementRepository.getIsAdmin(parsed.user.id);
try {
values = await slackConnector.parseSelection(stateValues, isAdmin, repoBranchesRepository);
} catch (e) {
console.log(`Error parsing selection: ${e}`);
return prepResponse(401, 'text/plain', e);
}
const deployable = await getDeployableJobs(values, entitlement, repoBranchesRepository, docsetsRepository);

if (deployable.length > 0) {
await deployRepo(deployable, consoleLogger, jobRepository, c.get('jobsQueueUrl'));
}
Expand Down Expand Up @@ -235,7 +266,7 @@ function createPayload(
};
}

function createJob(payload: any, jobTitle: string, jobUserName: string, jobUserEmail: string) {
function createJob(payload: Payload, jobTitle: string, jobUserName: string, jobUserEmail: string) {
return {
title: jobTitle,
user: jobUserName,
Expand Down
Loading

0 comments on commit b20b7a7

Please sign in to comment.