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

Disable Autobuilder for Writer's Staging #1068

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 4 additions & 67 deletions api/controllers/v1/github.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as c from 'config';

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

View workflow job for this annotation

GitHub Actions / test

'c' is defined but never used
import * as mongodb from 'mongodb';

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

View workflow job for this annotation

GitHub Actions / test

'mongodb' is defined but never used
import { JobRepository } from '../../../src/repositories/jobRepository';

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

View workflow job for this annotation

GitHub Actions / test

'JobRepository' is defined but never used
import { ConsoleLogger } from '../../../src/services/logger';

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

View workflow job for this annotation

GitHub Actions / test

'ConsoleLogger' is defined but never used
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
import { markBuildArtifactsForDeletion, validateJsonWebhook } from '../../handlers/github';

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

View workflow job for this annotation

GitHub Actions / test

'validateJsonWebhook' is defined but never used
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,
repoBranchesRepository: RepoBranchesRepository,
prefix: string,
Expand Down Expand Up @@ -61,75 +62,11 @@
};
}

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
Loading