Skip to content

Commit

Permalink
entitlement validation clean, monorepoDir => directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeigs committed Oct 31, 2023
1 parent 2e6cb83 commit 6704209
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion api/controllers/v1/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async function SubmitArchiveJob(jobId: string) {
repoBranches: new DocsetsRepository(db, c, consoleLogger),
};
const job = await models.jobs.getJobById(jobId);
const repo = await models.repoBranches.getRepo(job.payload.repoName, job?.payload.monorepoDir);
const repo = await models.repoBranches.getRepo(job.payload.repoName, job?.payload.directory);

/* NOTE
* we don't archive landing for two reasons:
Expand Down
12 changes: 6 additions & 6 deletions api/controllers/v1/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export const getDeployableJobs = async (
const deployable = [];

for (let i = 0; i < values.repo_option.length; i++) {
let repoOwner: string, repoName: string, branchName: string, monorepoDir: string | undefined;
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, monorepoDir, branchName] = splitValues;
[repoOwner, repoName, directory, branchName] = splitValues;
} else {
throw Error('Selected entitlement value is configured incorrectly. Check user entitlements!');
}
Expand All @@ -90,7 +90,7 @@ export const getDeployableJobs = async (
const jobUserName = entitlement.github_username;
const jobUserEmail = entitlement?.email ?? '';

const repoInfo = await docsetsRepository.getRepo(repoName, monorepoDir);
const repoInfo = await docsetsRepository.getRepo(repoName, directory);
const non_versioned = repoInfo.branches.length === 1;

const branchObject = await repoBranchesRepository.getRepoBranchAliases(repoName, branchName, repoInfo.project);
Expand Down Expand Up @@ -118,7 +118,7 @@ export const getDeployableJobs = async (
false,
false,
false,
monorepoDir
directory
);

newPayload.stable = !!isStableBranch;
Expand Down Expand Up @@ -211,7 +211,7 @@ function createPayload(
aliased = false,
primaryAlias = false,
stable = false,
monorepoDir?: string
directory?: string
) {
return {
jobType,
Expand All @@ -230,7 +230,7 @@ function createPayload(
newHead,
primaryAlias,
stable,
monorepoDir,
directory,
};
}

Expand Down
12 changes: 6 additions & 6 deletions api/controllers/v2/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ export const getDeployableJobs = async (
const deployable = [];

for (let i = 0; i < values.repo_option.length; i++) {
let repoOwner: string, repoName: string, branchName: string, monorepoDir: string | undefined;
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) {
// e.g. 10gen/docs-monorepo/cloud-docs/master => (owner/monorepo/repoDirectory/branch)
[repoOwner, repoName, monorepoDir, branchName] = splitValues;
[repoOwner, repoName, directory, branchName] = splitValues;
} else {
throw Error('Selected entitlement value is configured incorrectly. Check user entitlements!');
}
Expand All @@ -107,7 +107,7 @@ export const getDeployableJobs = async (
const jobUserName = entitlement.github_username;
const jobUserEmail = entitlement?.email ?? '';

const repoInfo = await docsetsRepository.getRepo(repoName, monorepoDir);
const repoInfo = await docsetsRepository.getRepo(repoName, directory);
const non_versioned = repoInfo.branches.length === 1;

const branchObject = await repoBranchesRepository.getRepoBranchAliases(repoName, branchName, repoInfo.project);
Expand Down Expand Up @@ -135,7 +135,7 @@ export const getDeployableJobs = async (
false,
false,
false,
monorepoDir
directory
);

newPayload.stable = !!isStableBranch;
Expand Down Expand Up @@ -236,7 +236,7 @@ function createPayload(
aliased = false,
primaryAlias = false,
stable = false,
monorepoDir?: string
directory?: string
) {
return {
jobType,
Expand All @@ -254,7 +254,7 @@ function createPayload(
newHead,
primaryAlias,
stable,
monorepoDir,
directory,
};
}

Expand Down
6 changes: 3 additions & 3 deletions api/handlers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export function prepResponse(statusCode, contentType, body) {
export async function buildEntitledBranchList(entitlement: any, repoBranchesRepository: RepoBranchesRepository) {
const entitledBranches: string[] = [];
for (const repo of entitlement.repos) {
const [repoOwner, repoName, monorepoDirPath] = repo.split('/');
const branches = await repoBranchesRepository.getRepoBranches(repoName, monorepoDirPath);
const [repoOwner, repoName, directoryPath] = repo.split('/');
const branches = await repoBranchesRepository.getRepoBranches(repoName, directoryPath);
for (const branch of branches) {
let buildWithSnooty = true;
if ('buildsWithSnooty' in branch) {
buildWithSnooty = branch['buildsWithSnooty'];
}
if (buildWithSnooty) {
entitledBranches.push(
`${repoOwner}/${repoName}${monorepoDirPath ? '/' + monorepoDirPath : ''}/${branch['gitBranchName']}`
`${repoOwner}/${repoName}${directoryPath ? '/' + directoryPath : ''}/${branch['gitBranchName']}`
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/entities/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type Payload = {
prefix: string;
project: string;
includeInGlobalSearch: boolean;
monorepoDir?: string;
directory?: string;
};

export type EnhancedPayload = {
Expand Down
4 changes: 2 additions & 2 deletions src/job/jobHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export abstract class JobHandler {
try {
const makefileFileName =
this.currJob.payload.repoName === MONOREPO_NAME
? this.currJob.payload.monorepoDir
? this.currJob.payload.directory
: this.currJob.payload.repoName;
await this._fileSystemServices.saveUrlAsFile(
`https://raw.githubusercontent.com/mongodb/docs-worker-pool/monorepo-pub-branches/makefiles/Makefile.${makefileFileName}`,
Expand Down Expand Up @@ -740,6 +740,6 @@ function throwIfJobInterupted() {
export function getDirectory(job: Job) {
const { payload } = job;
let directory = payload.repoName;
if (payload.repoName === MONOREPO_NAME && !!payload.monorepoDir) directory += `/${payload.monorepoDir}`;
if (payload.repoName === MONOREPO_NAME && !!payload.directory) directory += `/${payload.directory}`;
return directory;
}
13 changes: 5 additions & 8 deletions src/job/jobValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ export class JobValidator implements IJobValidator {

async throwIfUserNotEntitled(job: Job): Promise<void> {
const entitlementsObject = await this._repoEntitlementRepository.getRepoEntitlementsByGithubUsername(job.user);
// TODO: Ensure that the user is entitled for this specific monorepo project
if (
job.payload.repoName === MONOREPO_NAME &&
entitlementsObject?.repos?.some((r) => r.match(/10gen\/docs-monorepo\//g))
) {
return;
} else if (!entitlementsObject?.repos?.includes(`${job.payload.repoOwner}/${job.payload.repoName}`)) {
throw new AuthorizationError(`${job.user} is not entitled for repo ${job.payload.repoName}`);
const entitlementToFind = `${job.payload.repoOwner}/${job.payload.repoName}${
job.payload.repoName === MONOREPO_NAME ? `/${job.payload.directory}` : ``
}`;
if (!entitlementsObject?.repos?.includes(entitlementToFind)) {
throw new AuthorizationError(`${job.user} is not entitled for repo ${entitlementToFind}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/repositories/docsetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export class DocsetsRepository extends BaseRepository {
return res[0]?.project;
}

async getRepo(repoName: string, monorepoDir?: string): Promise<any> {
async getRepo(repoName: string, directory?: string): Promise<any> {
const matchConditions = { repoName };
if (monorepoDir) matchConditions['directories.snooty_toml'] = `/${monorepoDir}`;
if (directory) matchConditions['directories.snooty_toml'] = `/${directory}`;

const aggregationPipeline = this.getAggregationPipeline(matchConditions);
const cursor = await this.aggregate(aggregationPipeline, `Error while fetching repo by repo name ${repoName}`);
Expand Down
6 changes: 3 additions & 3 deletions src/repositories/repoBranchesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export class RepoBranchesRepository extends BaseRepository {
super(config, logger, 'RepoBranchesRepository', db.collection(config.get('repoBranchesCollection')));
}

async getRepoBranches(repoName: string, monorepoDirPath?: string): Promise<any> {
async getRepoBranches(repoName: string, directoryPath?: string): Promise<any> {
const query = { repoName: repoName };
if (monorepoDirPath) query['directories.snooty_toml'] = `/${monorepoDirPath}`;
if (directoryPath) query['directories.snooty_toml'] = `/${directoryPath}`;
const repo = await this.findOne(
query,
`Mongo Timeout Error: Timedout while retrieving branches for ${repoName}${
monorepoDirPath ? `/${monorepoDirPath}` : ''
directoryPath ? `/${directoryPath}` : ''
}`
);
// if user has specific entitlements
Expand Down

0 comments on commit 6704209

Please sign in to comment.