Store deployers for projects after successful deployments
All checks were successful
Lint / lint (20.x) (pull_request) Successful in 4m56s

This commit is contained in:
IshaVenikar 2024-10-22 11:19:02 +05:30
parent 224672382f
commit 09762c811a
7 changed files with 146 additions and 119 deletions

View File

@ -490,6 +490,12 @@ export class Database {
return projectRepository.save(newProject); return projectRepository.save(newProject);
} }
async saveProject (project: Project): Promise<Project> {
const projectRepository = this.dataSource.getRepository(Project);
return projectRepository.save(project);
}
async updateProjectById ( async updateProjectById (
projectId: string, projectId: string,
data: DeepPartial<Project> data: DeepPartial<Project>

View File

@ -1,4 +1,5 @@
import { Entity, PrimaryColumn, Column } from 'typeorm'; import { Entity, PrimaryColumn, Column, ManyToMany } from 'typeorm';
import { Project } from './Project';
@Entity() @Entity()
export class Deployer { export class Deployer {
@ -13,4 +14,7 @@ export class Deployer {
@Column('varchar') @Column('varchar')
baseDomain!: string; baseDomain!: string;
@ManyToMany(() => Project, (project) => project.deployers)
projects!: Project[];
} }

View File

@ -7,7 +7,9 @@ import {
ManyToOne, ManyToOne,
JoinColumn, JoinColumn,
OneToMany, OneToMany,
DeleteDateColumn DeleteDateColumn,
JoinTable,
ManyToMany
} from 'typeorm'; } from 'typeorm';
import { User } from './User'; import { User } from './User';
@ -50,8 +52,9 @@ export class Project {
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
auctionId!: string | null; auctionId!: string | null;
@OneToMany(() => Deployer, (deployer) => deployer.deployerId) @ManyToMany(() => Deployer, (deployer) => (deployer.projects))
deployers!: Deployer[]; @JoinTable()
deployers!: Deployer[]
@Column('boolean', { default: false, nullable: true }) @Column('boolean', { default: false, nullable: true })
fundsReleased!: boolean; fundsReleased!: boolean;

View File

@ -198,8 +198,7 @@ export class Service {
if (!deployment.project) { if (!deployment.project) {
log(`Project ${deployment.projectId} not found`); log(`Project ${deployment.projectId} not found`);
return; return;
} } else {
else {
deployment.applicationDeploymentRecordId = record.id; deployment.applicationDeploymentRecordId = record.id;
deployment.applicationDeploymentRecordData = record.attributes; deployment.applicationDeploymentRecordData = record.attributes;
deployment.url = record.attributes.url; deployment.url = record.attributes.url;
@ -207,11 +206,20 @@ export class Service {
deployment.isCurrent = deployment.environment === Environment.Production; deployment.isCurrent = deployment.environment === Environment.Production;
await this.db.updateDeploymentById(deployment.id, deployment); await this.db.updateDeploymentById(deployment.id, deployment);
// Release deployer funds on successful deployment
if (!deployment.project.fundsReleased) {
const fundsReleased = await this.releaseDeployerFundsByProjectId(deployment.projectId);
await this.db.updateProjectById(deployment.projectId, {
fundsReleased,
});
} }
log( log(
`Updated deployment ${deployment.id} with URL ${record.attributes.url}`, `Updated deployment ${deployment.id} with URL ${record.attributes.url}`,
); );
}
}); });
await Promise.all(deploymentUpdatePromises); await Promise.all(deploymentUpdatePromises);
@ -295,24 +303,12 @@ export class Service {
completedAuctionIds.includes(project.auctionId!) completedAuctionIds.includes(project.auctionId!)
); );
for (const project of projectsToBedeployed) { for (const project of projectsToBedeployed) {
const deployers = await this.laconicRegistry.getAuctionWinningDeployerRecords(project!.auctionId!);
if (!deployers) {
log(`No winning deployer for auction ${project!.auctionId}`);
} else {
// TODO:Update project with deployer LRNs
// await this.db.updateProjectById(project.id!, {
// deployers
// });
const deployerRecords = await this.laconicRegistry.getAuctionWinningDeployerRecords(project!.auctionId!); const deployerRecords = await this.laconicRegistry.getAuctionWinningDeployerRecords(project!.auctionId!);
if (!deployerRecords) { if (!deployerRecords) {
log(`No winning deployer for auction ${project!.auctionId}`); log(`No winning deployer for auction ${project!.auctionId}`);
} else { } else {
const deployerLrns = [];
const deployerIds = []; const deployerIds = [];
for (const record of deployerRecords) { for (const record of deployerRecords) {
@ -320,11 +316,9 @@ export class Service {
const deployerLrn = record.names[0]; const deployerLrn = record.names[0];
deployerIds.push(deployerId); deployerIds.push(deployerId);
deployerLrns.push(deployerLrn);
const deployerApiUrl = record.attributes.apiUrl; const deployerApiUrl = record.attributes.apiUrl;
const apiURL = new URL(deployerApiUrl); const baseDomain = deployerApiUrl.substring(deployerApiUrl.indexOf('.') + 1);
const baseDomain = apiURL.hostname.split('.').slice(-3).join('.');
const deployerData = { const deployerData = {
deployerId, deployerId,
@ -334,8 +328,10 @@ export class Service {
}; };
// Store the deployer in the DB // Store the deployer in the DB
// TODO: Update project with deployer const deployer = await this.db.addDeployer(deployerData);
await this.db.addDeployer(deployerData);
// Update project with deployer
await this.updateProjectWithDeployer(project.id, deployer);
} }
for (const deployer of deployerIds) { for (const deployer of deployerIds) {
@ -344,7 +340,6 @@ export class Service {
} }
} }
} }
}
this.auctionStatusCheckTimeout = setTimeout(() => { this.auctionStatusCheckTimeout = setTimeout(() => {
this.checkAuctionStatus(); this.checkAuctionStatus();
@ -617,7 +612,8 @@ export class Service {
async createDeployment( async createDeployment(
userId: string, userId: string,
octokit: Octokit, octokit: Octokit,
data: DeepPartial<Deployment> data: DeepPartial<Deployment>,
deployerLrn?: string
): Promise<Deployment> { ): Promise<Deployment> {
assert(data.project?.repository, 'Project repository not found'); assert(data.project?.repository, 'Project repository not found');
log( log(
@ -646,7 +642,14 @@ export class Service {
); );
} }
const newDeployment = await this.createDeploymentFromData(userId, data, data.deployer!.deployerId!, applicationRecordId, applicationRecordData); let deployer;
if (deployerLrn) {
deployer = await this.createDeployerFromLRN(deployerLrn);
} else {
deployer = data.deployer;
}
const newDeployment = await this.createDeploymentFromData(userId, data, deployer!.deployerId!, applicationRecordId, applicationRecordData);
const { repo, repoUrl } = await getRepoDetails(octokit, data.project.repository, data.commitHash); const { repo, repoUrl } = await getRepoDetails(octokit, data.project.repository, data.commitHash);
const environmentVariablesObj = await this.getEnvVariables(data.project!.id!); const environmentVariablesObj = await this.getEnvVariables(data.project!.id!);
@ -660,7 +663,7 @@ export class Service {
repository: repoUrl, repository: repoUrl,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: `${newDeployment.project.name}`, dns: `${newDeployment.project.name}`,
lrn: data.deployer!.deployerLrn! lrn: deployer!.deployerLrn!
}); });
} }
@ -669,7 +672,7 @@ export class Service {
deployment: newDeployment, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
lrn: data.deployer!.deployerLrn!, lrn: deployer!.deployerLrn!,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: `${newDeployment.project.name}-${newDeployment.id}`, dns: `${newDeployment.project.name}-${newDeployment.id}`,
}); });
@ -679,19 +682,6 @@ export class Service {
applicationDeploymentRequestData, applicationDeploymentRequestData,
}); });
// const deployerRecord = await this.laconicRegistry.getDeployerRecordsByFilter({
// name: deployerLrn,
// });
// TODO: Store deployer data
// newDeployment.project.deployers.push({
// deployerId: deployerRecord[0].id,
// deployerApiUrl: deployerRecord[0].attributes.apiUrl,
// })
// await this.updateProject(newDeployment.project.id, {
// deployers:
// });
return newDeployment; return newDeployment;
} }
@ -804,6 +794,50 @@ export class Service {
return newDeployment; return newDeployment;
} }
async createDeployerFromLRN(deployerLrn: string): Promise<Deployer | null> {
const records = await this.laconicRegistry.getRecordsByName(deployerLrn);
if (records.length === 0) {
log('No records found for deployer LRN:', deployerLrn);
return null;
}
const deployerId = records[0].id;
const deployerApiUrl = records[0].attributes.apiUrl;
const baseDomain = deployerApiUrl.substring(deployerApiUrl.indexOf('.') + 1);
const deployerData = {
deployerId,
deployerLrn,
deployerApiUrl,
baseDomain
};
const deployer = await this.db.addDeployer(deployerData);
return deployer;
}
async updateProjectWithDeployer(
projectId: string,
deployer: Deployer
): Promise<Deployer> {
const deploymentProject = await this.db.getProjects({
where: { id: projectId },
relations: ['deployers']
});
if (!deploymentProject[0].deployers) {
deploymentProject[0].deployers = [];
}
deploymentProject[0].deployers.push(deployer);
await this.db.saveProject(deploymentProject[0]);
return deployer;
}
async addProjectFromTemplate( async addProjectFromTemplate(
user: User, user: User,
organizationSlug: string, organizationSlug: string,
@ -867,6 +901,7 @@ export class Service {
slug: organizationSlug, slug: organizationSlug,
}, },
}); });
if (!organization) { if (!organization) {
throw new Error('Organization does not exist'); throw new Error('Organization does not exist');
} }
@ -897,15 +932,15 @@ export class Service {
domain: null, domain: null,
commitHash: latestCommit.sha, commitHash: latestCommit.sha,
commitMessage: latestCommit.commit.message, commitMessage: latestCommit.commit.message,
deployerLrn: lrn
}; };
if (auctionParams) { if (auctionParams) {
const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionParams!, deploymentData); const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionParams!, deploymentData);
await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId }); await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId });
} else { } else {
await this.createDeployment(user.id, octokit, deploymentData); const newDeployment = await this.createDeployment(user.id, octokit, deploymentData, lrn);
// await this.updateProject(project.id, { deployerLrns: [lrn!] }) // Update project with deployer
await this.updateProjectWithDeployer(newDeployment.projectId, newDeployment.deployer);
} }
await this.createRepoHook(octokit, project); await this.createRepoHook(octokit, project);
@ -959,6 +994,9 @@ export class Service {
); );
const projects = await this.db.getProjects({ const projects = await this.db.getProjects({
where: { repository: repository.full_name }, where: { repository: repository.full_name },
relations: {
deployers: true,
}
}); });
if (!projects.length) { if (!projects.length) {
@ -975,17 +1013,13 @@ export class Service {
branch, branch,
}); });
// TODO: Store deployer in project
// const deployers = project.deployerLrns;
// if (!deployers) {
// log(`No deployer present for project ${project.id}`)
// return;
// }
const deployers = project.deployers; const deployers = project.deployers;
if (!deployers) return; if (!deployers) {
log(`No deployer present for project ${project.id}`)
return;
}
// for (const deployer of deployers) { for (const deployer of deployers) {
// Create deployment with branch and latest commit in GitHub data // Create deployment with branch and latest commit in GitHub data
await this.createDeployment(project.ownerId, octokit, await this.createDeployment(project.ownerId, octokit,
{ {
@ -998,10 +1032,10 @@ export class Service {
domain, domain,
commitHash: headCommit.id, commitHash: headCommit.id,
commitMessage: headCommit.message, commitMessage: headCommit.message,
// deployer: deployer deployer: deployer
}, },
); );
// } }
} }
} }
@ -1038,6 +1072,7 @@ export class Service {
relations: { relations: {
project: true, project: true,
domain: true, domain: true,
deployer: true,
createdBy: true, createdBy: true,
}, },
where: { where: {
@ -1054,7 +1089,7 @@ export class Service {
let newDeployment: Deployment; let newDeployment: Deployment;
if (oldDeployment.project.auctionId) { if (oldDeployment.project.auctionId) {
newDeployment = await this.createDeploymentFromAuction(oldDeployment.project, oldDeployment.deployer.deployerLrn); newDeployment = await this.createDeploymentFromAuction(oldDeployment.project, oldDeployment.deployer.deployerId);
} else { } else {
newDeployment = await this.createDeployment(user.id, octokit, newDeployment = await this.createDeployment(user.id, octokit,
{ {
@ -1114,6 +1149,7 @@ export class Service {
}, },
relations: { relations: {
project: true, project: true,
deployer: true,
}, },
}); });

View File

@ -139,30 +139,8 @@ const Configure = () => {
const projectId = await createProject(createFormData, environmentVariables); const projectId = await createProject(createFormData, environmentVariables);
const { environmentVariables: isEnvironmentVariablesAdded } =
await client.getEnvironmentVariables(projectId); await client.getEnvironmentVariables(projectId);
if (isEnvironmentVariablesAdded.length > 0) {
toast({
id:
createFormData.variables.length > 1
? 'env_variable_added'
: 'env_variables_added',
title:
createFormData.variables.length > 1
? `${createFormData.variables.length} variables added`
: `Variable added`,
variant: 'success',
onDismiss: dismiss,
});
} else {
toast({
id: 'env_variables_not_added',
title: 'Environment variables not added',
variant: 'error',
onDismiss: dismiss,
});
}
if (templateId) { if (templateId) {
createFormData.option === 'Auction' createFormData.option === 'Auction'
? navigate( ? navigate(