Skip to content

Commit

Permalink
Disable Autobuilder for Writer's Staging (#1068)
Browse files Browse the repository at this point in the history
* Disable autobuilder for staging

* Remove trigger build from v1 as well
  • Loading branch information
branberry authored Sep 12, 2024
1 parent 708cbfd commit 7d36348
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 191 deletions.
71 changes: 4 additions & 67 deletions api/controllers/v1/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { markBuildArtifactsForDeletion, validateJsonWebhook } from '../../handle
import { DocsetsRepository } from '../../../src/repositories/docsetsRepository';

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

View workflow job for this annotation

GitHub Actions / test

'DocsetsRepository' is defined but never used
import { ReposBranchesDocsetsDocument } from '../../../modules/persistence/src/services/metadata/repos_branches';
import { PushEvent } from '@octokit/webhooks-types';

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

View workflow job for this annotation

GitHub Actions / test

'PushEvent' is defined but never used
import { APIGatewayProxyResult } from 'aws-lambda';

async function prepGithubPushPayload(

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

View workflow job for this annotation

GitHub Actions / test

'prepGithubPushPayload' is defined but never used
githubEvent: any,
Expand Down Expand Up @@ -61,75 +62,11 @@ async function prepGithubPushPayload(
};
}

export const TriggerBuild = async (event: any = {}, context: any = {}): Promise<any> => {
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";
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
};
}
if (!event.body) {
const err = 'Trigger build does not have a body in event payload';
consoleLogger.error('TriggerBuildError', err);
return {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: err,
};
}

let body: PushEvent;
try {
body = JSON.parse(event.body) as PushEvent;
} catch (e) {
consoleLogger.error('[TriggerBuild]', `ERROR! Could not parse event.body ${e}`);
console.log(`event: ${event} and event body: ${event.body}`);
return {
statusCode: 502,
headers: { 'Content-Type': 'text/plain' },
body: ' ERROR! Could not parse event.body',
};
}

if (body.deleted) {
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: 'Job Ignored (Deletion)',
};
}

const env = c.get<string>('env');
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, repoInfo);
try {
consoleLogger.info(job.title, 'Creating Job');
const jobId = await jobRepository.insertJob(job, c.get('jobsQueueUrl'));
consoleLogger.info(job.title, `Created Job ${jobId}`);
} catch (err) {
return {
statusCode: 500,
headers: { 'Content-Type': 'text/plain' },
body: err,
};
}
export const TriggerBuild = async (): Promise<APIGatewayProxyResult> => {
return {
statusCode: 202,
statusCode: 404,
headers: { 'Content-Type': 'text/plain' },
body: 'Job Queued',
body: 'The Autobuilder is currently disabled for staging. Please use Netlify instead.',
};
};

export const MarkBuildArtifactsForDeletion = markBuildArtifactsForDeletion;
127 changes: 3 additions & 124 deletions api/controllers/v2/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { ProjectsRepository } from '../../../src/repositories/projectsRepository
import { EnhancedJob, EnhancedPayload, 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 { ReposBranchesDocsetsDocument } from '../../../modules/persistence/src/services/metadata/repos_branches';
import { MONOREPO_NAME } from '../../../src/monorepo/utils/monorepo-constants';

const SMOKETEST_SITES = [
'docs-landing',
Expand Down Expand Up @@ -241,129 +238,11 @@ export const triggerSmokeTestAutomatedBuild = async (event: APIGatewayEvent): Pr
}
};

export const TriggerBuild = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
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 (!event.body) {
const err = 'Trigger build does not have a body in event payload';
return {
statusCode: 400,
headers: { 'Content-Type': 'text/plain' },
body: err,
};
}

if (!validateJsonWebhook(event, c.get<string>('githubSecret'))) {
const errMsg = "X-Hub-Signature incorrect. Github webhook token doesn't match";
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
};
}
let body: PushEvent;
try {
body = JSON.parse(event.body) as PushEvent;
} catch (e) {
console.log('[TriggerBuild]: ERROR! Could not parse event.body', e);
return {
statusCode: 502,
headers: { 'Content-Type': 'text/plain' },
body: ' ERROR! Could not parse event.body',
};
}

if (body.deleted) {
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: 'Job Ignored (Deletion)',
};
}

const env = c.get<string>('env');

async function createAndInsertJob(path?: string) {
const repo = body.repository;
const repoInfo = await docsetsRepository.getRepo(repo.name, path);
const jobPrefix = repoInfo?.prefix ? repoInfo['prefix'][env] : '';
const jobTitle = repo.full_name;
const payload = await createPayload({
repoName: repo.name,
prefix: jobPrefix,
repoBranchesRepository,
repoInfo,
githubEvent: body,
});

const job = await prepGithubPushPayload(body, payload, jobTitle);

consoleLogger.info(job.title, 'Creating Job');
const jobId = await jobRepository.insertJob(job, c.get('jobsQueueUrl'));
jobRepository.notify(jobId, c.get('jobUpdatesQueueUrl'), JobStatus.inQueue, 0);
consoleLogger.info(job.title, `Created Job ${jobId}`);
}

if (process.env.FEATURE_FLAG_MONOREPO_PATH === 'true' && body.repository.name === MONOREPO_NAME) {
let monorepoPaths: string[] = [];
try {
if (body.head_commit && body.repository.owner.name) {
monorepoPaths = await getMonorepoPaths({
commitSha: body.head_commit.id,
repoName: body.repository.name,
ownerName: body.repository.owner.name,
updatedFilePaths: getUpdatedFilePaths(body.head_commit),
});
consoleLogger.info(body.repository.full_name, `Monorepo Paths with new changes: ${monorepoPaths}`);
}
} catch (error) {
consoleLogger.warn('Warning, attempting to get monorepo paths caused an error', error);
}

/* Create and insert Job for each monorepo project that has changes */
for (const path of monorepoPaths) {
consoleLogger.info(body.repository.full_name, `Create Job for Monorepo directory: /${path}`);
// TODO: Deal with nested monorepo projects
/* For now, we will ignore nested monorepo projects until necessary */
if (path.split('/').length > 1) continue;

try {
await createAndInsertJob(path);
} catch (err) {
return {
statusCode: 500,
headers: { 'Content-Type': 'text/plain' },
body: err,
};
}
}

return {
statusCode: 202,
headers: { 'Content-Type': 'text/plain' },
body: 'Jobs Queued',
};
}

try {
await createAndInsertJob();
} catch (err) {
return {
statusCode: 500,
headers: { 'Content-Type': 'text/plain' },
body: err,
};
}
export const TriggerBuild = async (): Promise<APIGatewayProxyResult> => {
return {
statusCode: 202,
statusCode: 404,
headers: { 'Content-Type': 'text/plain' },
body: 'Job Queued',
body: 'The Autobuilder is currently disabled for staging. Please use Netlify instead.',
};
};

Expand Down

0 comments on commit 7d36348

Please sign in to comment.