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

DOP-4020: repos_branches to docsets #909

Merged
merged 27 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3f27242
find todos in main RepoBranchesRepository, consolidate slack helpers
mmeigs Sep 14, 2023
8d53507
delete unused RepoBranchesRepository method
mmeigs Sep 14, 2023
8216fd2
replace getRepo everywhere with new DocsetsRepository getRepo
mmeigs Sep 14, 2023
2dfc6bd
add docsetsRepository to each JobHandler and test
mmeigs Sep 14, 2023
42b70f0
add docsetsRepo to JobHandlers
mmeigs Sep 14, 2023
eeed174
more test finds, missing references
mmeigs Sep 14, 2023
763f2c6
added env vars for docsetCollection
mmeigs Sep 14, 2023
776e30f
modify tests
mmeigs Sep 15, 2023
22b977c
set to stage ecs
mmeigs Sep 15, 2023
5317f13
add docsets to parameter store and serverless
mmeigs Sep 15, 2023
f96335d
more env vars
mmeigs Sep 15, 2023
3909513
persistence module repos_branches migrate to docsets
mmeigs Sep 18, 2023
5dbe09f
work on tests in persistence module
mmeigs Sep 18, 2023
ea80698
Merge branch 'master' into DOP-4020-docsets
mmeigs Sep 18, 2023
4cf61c0
dont use preprd
mmeigs Sep 18, 2023
8269fe0
fixed persistence tests
mmeigs Sep 19, 2023
0cff1a1
merge two separate docsetsRepos
mmeigs Sep 19, 2023
01200c2
deploy preprd
mmeigs Sep 19, 2023
8352898
use .env
mmeigs Sep 19, 2023
e6f8bf4
debug failing test
mmeigs Sep 20, 2023
b002f2e
all tests passing
mmeigs Sep 21, 2023
2b3a87a
Merge branch 'master' into DOP-4020-docsets
mmeigs Sep 21, 2023
fc45dff
PR feedback
mmeigs Sep 21, 2023
e2ca1d4
fix test
mmeigs Sep 21, 2023
b59e573
fix v2 to match v1, double query
mmeigs Sep 21, 2023
6d1e6d6
Merge branch 'master' into DOP-4020-docsets
mmeigs Sep 22, 2023
4727b68
add warning to checkSnootyTomlPath
mmeigs Sep 22, 2023
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
1 change: 1 addition & 0 deletions api/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"entitlementCollection": "USER_ENTITLEMENT_COL_NAME",
"dashboardUrl": "DASHBOARD_URL",
"repoBranchesCollection": "REPO_BRANCHES_COL_NAME",
"docsetsCollection": "DOCSETS_COL_NAME",
"taskDefinitionFamily": "TASK_DEFINITION_FAMILY",
"jobsQueueUrl": "JOBS_QUEUE_URL",
"jobUpdatesQueueUrl": "JOB_UPDATES_QUEUE_URL",
Expand Down
1 change: 1 addition & 0 deletions api/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"jobQueueCollection": "JOB_QUEUE_COL_NAME",
"entitlementCollection": "USER_ENTITLEMENT_COL_NAME",
"repoBranchesCollection": "REPO_BRANCHES_COL_NAME",
"docsetsCollection": "DOCSETS_COL_NAME",
"MONGO_TIMEOUT_S": 15,
"JOB_TIMEOUT_S": 900,
"RETRY_TIMEOUT_MS": 5000,
Expand Down
17 changes: 12 additions & 5 deletions api/controllers/v1/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import { ConsoleLogger } from '../../../src/services/logger';
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
import { markBuildArtifactsForDeletion, validateJsonWebhook } from '../../handlers/github';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';
import { ReposBranchesDocument } from '../../../modules/persistence/src/services/metadata/repos_branches';

async function prepGithubPushPayload(githubEvent: any, repoBranchesRepository: RepoBranchesRepository, prefix: string) {
async function prepGithubPushPayload(
githubEvent: any,

Check warning on line 11 in api/controllers/v1/github.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
repoBranchesRepository: RepoBranchesRepository,
prefix: string,
repoInfo: ReposBranchesDocument
) {
const branch_name = githubEvent.ref.split('/')[2];
const branch_info = await repoBranchesRepository.getRepoBranchAliases(githubEvent.repository.name, branch_name);
const urlSlug = branch_info.aliasObject?.urlSlug ?? branch_name;
const repo_info = await repoBranchesRepository.getRepo(githubEvent.repository.name);
const project = repo_info?.project ?? githubEvent.repository.name;
const project = repoInfo?.project ?? githubEvent.repository.name;

return {
title: githubEvent.repository.full_name,
Expand Down Expand Up @@ -50,13 +56,14 @@
};
}

export const TriggerBuild = async (event: any = {}, context: any = {}): Promise<any> => {

Check warning on line 59 in api/controllers/v1/github.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 59 in api/controllers/v1/github.ts

View workflow job for this annotation

GitHub Actions / test

'context' is assigned a value but never used

Check warning on line 59 in api/controllers/v1/github.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 59 in api/controllers/v1/github.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const client = new mongodb.MongoClient(c.get('dbUrl'));
await client.connect();
const db = client.db(c.get('dbName'));
const consoleLogger = new ConsoleLogger();
const jobRepository = new JobRepository(db, c, consoleLogger);
const repoBranchesRepository = new RepoBranchesRepository(db, c, consoleLogger);
const docsetsRepository = new DocsetsRepository(db, c, consoleLogger);

if (!validateJsonWebhook(event, c.get<string>('githubSecret'))) {
const errMsg = "X-Hub-Signature incorrect. Github webhook token doesn't match";
Expand All @@ -77,10 +84,10 @@
}

const env = c.get<string>('env');
const repoInfo = await repoBranchesRepository.getRepo(body.repository.name);
const repoInfo = await docsetsRepository.getRepo(body.repository.name);
const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : '';
// TODO: Make job be of type Job
const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix);
const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix, repoInfo);
try {
consoleLogger.info(job.title, 'Creating Job');
const jobId = await jobRepository.insertJob(job, c.get('jobsQueueUrl'));
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/v1/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import * as mongodb from 'mongodb';
import { IConfig } from 'config';
import { RepoEntitlementsRepository } from '../../../src/repositories/repoEntitlementsRepository';
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
import { ConsoleLogger } from '../../../src/services/logger';
import { SlackConnector } from '../../../src/services/slack';
import { JobRepository } from '../../../src/repositories/jobRepository';
Expand All @@ -12,8 +11,9 @@
import { SQSConnector } from '../../../src/services/queue';
import { Batch } from '../../../src/services/batch';
import { notifyBuildSummary, snootyBuildComplete } from '../../handlers/jobs';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';

export const TriggerLocalBuild = async (event: any = {}, context: any = {}): Promise<any> => {

Check warning on line 16 in api/controllers/v1/jobs.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 16 in api/controllers/v1/jobs.ts

View workflow job for this annotation

GitHub Actions / test

'context' is assigned a value but never used

Check warning on line 16 in api/controllers/v1/jobs.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const client = new mongodb.MongoClient(c.get('dbUrl'));
await client.connect();
const db = client.db(c.get('dbName'));
Expand Down Expand Up @@ -258,7 +258,7 @@
const db = client.db(c.get('dbName'));
const models = {
jobs: new JobRepository(db, c, consoleLogger),
repoBranches: new RepoBranchesRepository(db, c, consoleLogger),
repoBranches: new DocsetsRepository(db, c, consoleLogger),
};
const job = await models.jobs.getJobById(jobId);
const repo = await models.repoBranches.getRepo(job.payload.repoName);
Expand Down
67 changes: 17 additions & 50 deletions api/controllers/v1/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,14 @@ import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRe
import { ConsoleLogger, ILogger } from '../../../src/services/logger';
import { SlackConnector } from '../../../src/services/slack';
import { JobRepository } from '../../../src/repositories/jobRepository';

function isUserEntitled(entitlementsObject: any): boolean {
return (entitlementsObject?.repos?.length ?? 0) > 0;
}

function isRestrictedToDeploy(userId: string): boolean {
const { restrictedProdDeploy, entitledSlackUsers } = c.get<any>('prodDeploy');
return restrictedProdDeploy && !entitledSlackUsers.includes(userId);
}

function prepResponse(statusCode, contentType, body) {
return {
statusCode: statusCode,
headers: { 'Content-Type': contentType },
body: body,
};
}

async function buildEntitledBranchList(entitlement: any, repoBranchesRepository: RepoBranchesRepository) {
const entitledBranches: string[] = [];
for (const repo of entitlement.repos) {
const [repoOwner, repoName] = repo.split('/');
const branches = await repoBranchesRepository.getRepoBranches(repoName);
for (const branch of branches) {
let buildWithSnooty = true;
if ('buildsWithSnooty' in branch) {
buildWithSnooty = branch['buildsWithSnooty'];
}
if (buildWithSnooty) {
entitledBranches.push(`${repoOwner}/${repoName}/${branch['gitBranchName']}`);
}
}
}
return entitledBranches.sort();
}

function getQSString(qs: string) {
const key_val = {};
const arr = qs.split('&');
if (arr) {
arr.forEach((keyval) => {
const kvpair = keyval.split('=');
key_val[kvpair[0]] = kvpair[1];
});
}
return key_val;
}
Comment on lines -8 to -54
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized these functions were identical and duplicated between v1 and v2. Rolled them out to a handlers file same as others we've done in the past month.

import {
buildEntitledBranchList,
getQSString,
isRestrictedToDeploy,
isUserEntitled,
prepResponse,
} from '../../handlers/slack';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';

export const DisplayRepoOptions = async (event: any = {}, context: any = {}): Promise<any> => {
const consoleLogger = new ConsoleLogger();
Expand Down Expand Up @@ -102,7 +63,12 @@ const deployHelper = (deployable, payload, jobTitle, jobUserName, jobUserEmail)

// For every repo/branch selected to be deployed, return an array of jobs with the payload data
// needed for a successful build.
export const getDeployableJobs = async (values, entitlement, repoBranchesRepository: RepoBranchesRepository) => {
export const getDeployableJobs = async (
values,
entitlement,
repoBranchesRepository: RepoBranchesRepository,
docsetsRepository: DocsetsRepository
) => {
const deployable = [];

for (let i = 0; i < values.repo_option.length; i++) {
Expand All @@ -113,7 +79,7 @@ export const getDeployableJobs = async (values, entitlement, repoBranchesReposit
const jobUserName = entitlement.github_username;
const jobUserEmail = entitlement?.email ?? '';

const repoInfo = await repoBranchesRepository.getRepo(repoName);
const repoInfo = await docsetsRepository.getRepo(repoName);
const non_versioned = repoInfo.branches.length === 1;

const branchObject = await repoBranchesRepository.getRepoBranchAliases(repoName, branchName);
Expand Down Expand Up @@ -196,6 +162,7 @@ export const DeployRepo = async (event: any = {}, context: any = {}): Promise<an
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);
const jobRepository = new JobRepository(db, c, consoleLogger);

// This is coming in as urlencoded string, need to decode before parsing
Expand All @@ -210,7 +177,7 @@ export const DeployRepo = async (event: any = {}, context: any = {}): Promise<an

const values = slackConnector.parseSelection(stateValues);

const deployable = await getDeployableJobs(values, entitlement, repoBranchesRepository);
const deployable = await getDeployableJobs(values, entitlement, repoBranchesRepository, docsetsRepository);
if (deployable.length > 0) {
await deployRepo(deployable, consoleLogger, jobRepository, c.get('jobsQueueUrl'));
}
Expand Down
13 changes: 8 additions & 5 deletions api/controllers/v2/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import { ConsoleLogger } from '../../../src/services/logger';
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
import { EnhancedJob, JobStatus } from '../../../src/entities/job';
import { markBuildArtifactsForDeletion, validateJsonWebhook } from '../../handlers/github';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';
import { getMonorepoPaths } from '../../../src/monorepo';
import { getUpdatedFilePaths } from '../../../src/monorepo/utils/path-utils';
import { ReposBranchesDocument } from '../../../modules/persistence/src/services/metadata/associated_products';

async function prepGithubPushPayload(
githubEvent: PushEvent,
repoBranchesRepository: RepoBranchesRepository,
prefix: string
prefix: string,
repoInfo: ReposBranchesDocument
): Promise<Omit<EnhancedJob, '_id'>> {
const branch_name = githubEvent.ref.split('/')[2];
const branch_info = await repoBranchesRepository.getRepoBranchAliases(githubEvent.repository.name, branch_name);
const urlSlug = branch_info.aliasObject?.urlSlug ?? branch_name;
const repo_info = await repoBranchesRepository.getRepo(githubEvent.repository.name);
const project = repo_info?.project ?? githubEvent.repository.name;
const project = repoInfo?.project ?? githubEvent.repository.name;

return {
title: githubEvent.repository.full_name,
Expand Down Expand Up @@ -58,6 +60,7 @@ export const TriggerBuild = async (event: APIGatewayEvent): Promise<APIGatewayPr
const consoleLogger = new ConsoleLogger();
const jobRepository = new JobRepository(db, c, consoleLogger);
const repoBranchesRepository = new RepoBranchesRepository(db, c, consoleLogger);
const docsetsRepository = new DocsetsRepository(db, c, consoleLogger);

if (!event.body) {
const err = 'Trigger build does not have a body in event payload';
Expand Down Expand Up @@ -98,10 +101,10 @@ export const TriggerBuild = async (event: APIGatewayEvent): Promise<APIGatewayPr
}

const env = c.get<string>('env');
const repoInfo = await repoBranchesRepository.getRepo(body.repository.name);
const repoInfo = await docsetsRepository.getRepo(body.repository.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more time here for double fetching getRepo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed! tysm

const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : '';

const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix);
const job = await prepGithubPushPayload(body, repoBranchesRepository, jobPrefix, repoInfo);

if (process.env.MONOREPO_PATH_FEATURE === 'true') {
try {
Expand Down
67 changes: 17 additions & 50 deletions api/controllers/v2/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,14 @@ import { SlackConnector } from '../../../src/services/slack';
import { JobRepository } from '../../../src/repositories/jobRepository';
import { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import { JobStatus } from '../../../src/entities/job';

function isUserEntitled(entitlementsObject: any): boolean {
return (entitlementsObject?.repos?.length ?? 0) > 0;
}

function isRestrictedToDeploy(userId: string): boolean {
const { restrictedProdDeploy, entitledSlackUsers } = c.get<any>('prodDeploy');
return restrictedProdDeploy && !entitledSlackUsers.includes(userId);
}

function prepResponse(statusCode, contentType, body) {
return {
statusCode: statusCode,
headers: { 'Content-Type': contentType },
body: body,
};
}

async function buildEntitledBranchList(entitlement: any, repoBranchesRepository: RepoBranchesRepository) {
const entitledBranches: string[] = [];
for (const repo of entitlement.repos) {
const [repoOwner, repoName] = repo.split('/');
const branches = await repoBranchesRepository.getRepoBranches(repoName);
for (const branch of branches) {
let buildWithSnooty = true;
if ('buildsWithSnooty' in branch) {
buildWithSnooty = branch['buildsWithSnooty'];
}
if (buildWithSnooty) {
entitledBranches.push(`${repoOwner}/${repoName}/${branch['gitBranchName']}`);
}
}
}
return entitledBranches.sort();
}

function getQSString(qs: string) {
const key_val = {};
const arr = qs.split('&');
if (arr) {
arr.forEach((keyval) => {
const kvpair = keyval.split('=');
key_val[kvpair[0]] = kvpair[1];
});
}
return key_val;
}
import {
buildEntitledBranchList,
getQSString,
isRestrictedToDeploy,
isUserEntitled,
prepResponse,
} from '../../handlers/slack';
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';

export const DisplayRepoOptions = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
const consoleLogger = new ConsoleLogger();
Expand Down Expand Up @@ -119,7 +80,12 @@ const deployHelper = (deployable, payload, jobTitle, jobUserName, jobUserEmail)

// For every repo/branch selected to be deployed, return an array of jobs with the payload data
// needed for a successful build.
export const getDeployableJobs = async (values, entitlement, repoBranchesRepository: RepoBranchesRepository) => {
export const getDeployableJobs = async (
values,
entitlement,
repoBranchesRepository: RepoBranchesRepository,
docsetsRepository: DocsetsRepository
) => {
const deployable = [];

for (let i = 0; i < values.repo_option.length; i++) {
Expand All @@ -130,7 +96,7 @@ export const getDeployableJobs = async (values, entitlement, repoBranchesReposit
const jobUserName = entitlement.github_username;
const jobUserEmail = entitlement?.email ?? '';

const repoInfo = await repoBranchesRepository.getRepo(repoName);
const repoInfo = await docsetsRepository.getRepo(repoName);
const non_versioned = repoInfo.branches.length === 1;

const branchObject = await repoBranchesRepository.getRepoBranchAliases(repoName, branchName);
Expand Down Expand Up @@ -220,6 +186,7 @@ export const DeployRepo = async (event: APIGatewayEvent): Promise<APIGatewayProx
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);
const jobRepository = new JobRepository(db, c, consoleLogger);

// This is coming in as urlencoded string, need to decode before parsing
Expand All @@ -234,7 +201,7 @@ export const DeployRepo = async (event: APIGatewayEvent): Promise<APIGatewayProx

const values = slackConnector.parseSelection(stateValues);

const deployable = await getDeployableJobs(values, entitlement, repoBranchesRepository);
const deployable = await getDeployableJobs(values, entitlement, repoBranchesRepository, docsetsRepository);
if (deployable.length > 0) {
await deployRepo(deployable, consoleLogger, jobRepository, c.get('jobsQueueUrl'));
}
Expand Down
6 changes: 3 additions & 3 deletions api/handlers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as mongodb from 'mongodb';
import { APIGatewayEvent } from 'aws-lambda';
import { PullRequestEvent } from '@octokit/webhooks-types';
import { ConsoleLogger } from '../../src/services/logger';
import { RepoBranchesRepository } from '../../src/repositories/repoBranchesRepository';
import { DocsetsRepository } from '../../src/repositories/docsetsRepository';
import { UpdatedDocsRepository } from '../../src/repositories/updatedDocsRepository';
import { MetadataRepository } from '../../src/repositories/metadataRepository';

Expand Down Expand Up @@ -94,8 +94,8 @@ export const markBuildArtifactsForDeletion = async (event: APIGatewayEvent) => {
try {
await client.connect();
const poolDb = client.db(c.get('dbName'));
const repoBranchesRepository = new RepoBranchesRepository(poolDb, c, consoleLogger);
const project = (await repoBranchesRepository.getProjectByRepoName(repository.name)) as string;
const docsetsRepository = new DocsetsRepository(poolDb, c, consoleLogger);
const project = (await docsetsRepository.getProjectByRepoName(repository.name)) as string;

// Start marking build artifacts for deletion
const snootyDb = client.db(c.get('snootyDbName'));
Expand Down
Loading
Loading