Skip to content

Commit 37aeac5

Browse files
fix: issue in oldVersions
1 parent 2603e24 commit 37aeac5

File tree

6 files changed

+66
-47
lines changed

6 files changed

+66
-47
lines changed

app/src/integrations/simple-git/existingProject.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export const cloneExistingProjectFromGitServer = async (existingProjectGitServer
3030
};
3131

3232
export const pushToExistingProjectOnGitServer = async (existingProjectGitServerRequest: ExistingProjectGitServerRequest): Promise<string> => {
33+
const baseDir = existingProjectGitServerRequest.clonedProjectPath + '/' + existingProjectGitServerRequest.projectName;
3334
const options: Partial<SimpleGitOptions> = {
34-
baseDir: existingProjectGitServerRequest.existingProject,
35+
baseDir,
3536
binary: 'git',
3637
maxConcurrentProcesses: 6,
3738
trimmed: false,
@@ -45,8 +46,8 @@ export const pushToExistingProjectOnGitServer = async (existingProjectGitServerR
4546
await git.addConfig('user.name', existingProjectGitServerRequest.gitProviderDetails.platformUserName);
4647

4748
// copy over the new files to this cloned files.
48-
fs.cpSync(existingProjectGitServerRequest.generatedProjectPath, existingProjectGitServerRequest.existingProject, {recursive: true});
49-
Logger.info(`${existingProjectGitServerRequest.generatedProjectPath} files copied to ${existingProjectGitServerRequest.existingProject}`);
49+
fs.cpSync(existingProjectGitServerRequest.generatedProjectPath, baseDir, {recursive: true});
50+
Logger.info(`${existingProjectGitServerRequest.generatedProjectPath} files copied to ${baseDir}`);
5051

5152
// add, commit and push
5253
return await gitOperations(git, existingProjectGitServerRequest.gitProviderDetails.repositoryBranch, existingProjectGitServerRequest.projectVersion);

app/src/integrations/simple-git/models.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ export interface GitProviderDetails {
1111

1212
export interface NewProjectGitServerRequest {
1313
projectVersion: string;
14-
generatedProjectPath: string;
1514
gitProviderDetails: GitProviderDetails;
15+
generatedProjectPath: string;
1616
}
1717

1818
export interface ExistingProjectGitServerRequest {
19-
clonedProjectPath: string;
20-
existingProject: string;
19+
projectName: string;
2120
projectVersion: string;
22-
generatedProjectPath: string;
2321
gitProviderDetails: GitProviderDetails;
22+
clonedProjectPath: string;
23+
generatedProjectPath: string;
2424
}

app/src/models/project.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ export interface ProjectEntity {
1515
repository_branch: string;
1616
is_repository_public: boolean;
1717
repository_url: string;
18-
old_versions: string[];
18+
old_versions: string;
1919
created_at?: string;
2020
updated_at?: string;
2121
}
2222

2323
export interface OldVersion {
24-
version: string;
25-
json: string;
24+
[key: string]: string;
25+
}
26+
27+
export interface Metadata {
28+
[key: string]: string;
2629
}
2730

2831
export interface ProjectDTO {
@@ -37,7 +40,7 @@ export interface ProjectDTO {
3740
isRepositoryPublic: boolean;
3841
repositoryUrl: string;
3942
// TODO temporary made optional.
40-
metadata?: Map<string, string>;
43+
metadata?: Metadata;
4144
ownerEmail: string;
4245
oldVersions?: OldVersion[];
4346
createdAt?: string;
@@ -76,6 +79,8 @@ export const getCreateProjectResponse = (projectEntity: ProjectEntity) => {
7679
repositoryBranch: projectEntity.repository_branch,
7780
isRepositoryPublic: projectEntity.is_repository_public,
7881
repositoryUrl: projectEntity.repository_url,
82+
metadata: JSON.parse(projectEntity.metadata),
83+
oldVersions: JSON.parse(projectEntity.old_versions),
7984
};
8085
return projectDTO;
8186
};
@@ -95,6 +100,8 @@ export const getGetProjectResponse = (projectEntity: ProjectEntity) => {
95100
repositoryBranch: projectEntity.repository_branch,
96101
isRepositoryPublic: projectEntity.is_repository_public,
97102
repositoryUrl: projectEntity.repository_url,
103+
metadata: JSON.parse(projectEntity.metadata),
104+
oldVersions: JSON.parse(projectEntity.old_versions),
98105
};
99106
return projectDTO;
100107
};
@@ -106,7 +113,7 @@ export const getListProjectsResponse = (projectEntities: ProjectEntity[]) => {
106113
id: projectEntity.id,
107114
displayName: projectEntity.display_name,
108115
version: projectEntity.version,
109-
json: JSON.parse(projectEntity.json),
116+
json: JSON.parse(JSON.stringify(projectEntity.json)),
110117
ownerEmail: projectEntity.owner_email,
111118
createdAt: projectEntity.created_at,
112119
updatedAt: projectEntity.updated_at,
@@ -117,9 +124,7 @@ export const getListProjectsResponse = (projectEntities: ProjectEntity[]) => {
117124
isRepositoryPublic: projectEntity.is_repository_public,
118125
repositoryUrl: projectEntity.repository_url,
119126
metadata: JSON.parse(projectEntity.metadata),
120-
oldVersions: projectEntity.old_versions ? projectEntity.old_versions.map((oldVersion) => {
121-
return JSON.parse(oldVersion);
122-
}) : [],
127+
oldVersions: JSON.parse(projectEntity.old_versions),
123128
};
124129
projectDTOs.push(projectDTO);
125130
});
@@ -142,12 +147,10 @@ export const getProjectEntity = (projectDTO: ProjectDTO) => {
142147
is_repository_public: projectDTO.isRepositoryPublic,
143148
repository_url: projectDTO.repositoryUrl,
144149
metadata: JSON.stringify(projectDTO.metadata),
145-
old_versions: projectDTO.oldVersions ? projectDTO.oldVersions.map((oldVersion) => {
146-
return JSON.stringify(oldVersion);
147-
}) : [],
150+
old_versions: projectDTO.oldVersions ? JSON.stringify(projectDTO.oldVersions) : '[]',
148151
};
149152
return projectEntity;
150-
};
153+
};
151154

152155
// errors
153156
interface CreateProjectError {

app/src/routes/codeOperations.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import tar from 'tar';
1313
import {X_EMAIL_HEADER} from '../utils/constants';
1414
import {GenerateCodeRequest, getGenerateCodeError, getGenerateCodeResponse, Project} from '../models/code';
1515
import {GitPlatformEntity} from '../models/gitPlatform';
16-
import {OldVersion, ProjectEntity} from '../models/project';
16+
import {Metadata, OldVersion, ProjectEntity} from '../models/project';
1717
import {ExistingProjectGitServerRequest} from '../integrations/simple-git/models';
1818
import {ProjectService} from '../services/projectService';
1919
import {GitPlatformService} from '../services/gitPlatformService';
@@ -72,8 +72,7 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
7272
const lastVersion = previousVersion(currentVersion);
7373
const oldVersions = projectEntity.old_versions;
7474
// iterate over all oldVersions to find the last version in it.
75-
for (const oldVersion of oldVersions as string[]) {
76-
const oldVersionJson = JSON.parse(oldVersion);
75+
for (const oldVersionJson of JSON.parse(oldVersions) as OldVersion[]) {
7776
// check if the lastVersion matches with current version
7877
if (oldVersionJson.version === lastVersion) {
7978
// remove unwanted keys and then do the equality check. This is needed to avoid keys used to render ui.
@@ -87,23 +86,39 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
8786

8887
// create directory hierarchy here itself as creating it after receiving data will not be proper.
8988
const originalProjectPath = `${os.tmpdir()}/${projectEntity.display_name}`;
89+
// this is path where project will be downloaded
9090
const downloadedProjectPath = `${originalProjectPath}_downloaded`;
91+
// this is path where project will be cloned
92+
const clonedProjectPath = `${originalProjectPath}_cloned`;
93+
// this is path in tar file
94+
const generatedProjectPath = `${downloadedProjectPath}` + `${originalProjectPath}`;
95+
// this is path where tar file will be saved
96+
const downloadedProjectTarFilePath = `${originalProjectPath}_downloaded.tar.gz`;
9197
try {
98+
// create downloadedProjectPath
9299
if (fs.existsSync(downloadedProjectPath)) {
93100
cleanup(downloadedProjectPath);
94101
}
95102
fs.mkdirSync(downloadedProjectPath, {recursive: true});
103+
// create clonedProjectPath
104+
if (fs.existsSync(clonedProjectPath)) {
105+
cleanup(clonedProjectPath);
106+
}
107+
fs.mkdirSync(clonedProjectPath, {recursive: true});
96108
} catch (err: any) {
97109
if (err.code !== 'EEXIST') {
98110
const message = `unable to generate code for ${projectEntity.display_name}[${projectEntity.id}] => ${err}`;
99111
return resource.status(500).json(getGenerateCodeError(message));
100112
} else {
101113
// first clean up and then recreate (it might be a residue of previous run)
114+
// create downloadedProjectPath
102115
cleanup(downloadedProjectPath);
103116
fs.mkdirSync(downloadedProjectPath, {recursive: true});
117+
// create clonedProjectPath
118+
cleanup(clonedProjectPath);
119+
fs.mkdirSync(clonedProjectPath, {recursive: true});
104120
}
105121
}
106-
const projectTarFilePath = `${downloadedProjectPath}_downloaded.tar.gz`;
107122

108123
const gitPlatformEntity: GitPlatformEntity = await gitPlatformService.getGitPlatform(projectEntity.owner_email as string, projectEntity.git_platform_name as string);
109124

@@ -127,8 +142,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
127142
call.on('data', async (response: { fileChunk: any }) => {
128143
// chunk is available, append it to the given path.
129144
if (response.fileChunk) {
130-
fs.appendFileSync(projectTarFilePath, response.fileChunk);
131-
Logger.debug(`writing tar file chunk to: ${projectTarFilePath}`);
145+
fs.appendFileSync(downloadedProjectTarFilePath, response.fileChunk);
146+
Logger.debug(`writing tar file chunk to: ${downloadedProjectTarFilePath}`);
132147
}
133148
});
134149

@@ -147,7 +162,7 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
147162
C: downloadedProjectPath
148163
});
149164
// stream on extraction on tar file
150-
const fscrs = fs.createReadStream(projectTarFilePath);
165+
const fscrs = fs.createReadStream(downloadedProjectTarFilePath);
151166
fscrs.on('error', (err: any) => {
152167
Logger.error(JSON.stringify(err));
153168
});
@@ -156,7 +171,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
156171
extract.on('finish', async () => {
157172
// clone existing repository
158173
const existingProjectGitServerRequest: ExistingProjectGitServerRequest = {
159-
clonedProjectPath: `${downloadedProjectPath}`,
174+
projectName: projectEntity.display_name,
175+
projectVersion: projectEntity.version,
160176
gitProviderDetails: {
161177
repositoryBranch: projectEntity.repository_branch as string,
162178
repositoryName: projectEntity.repository_name as string,
@@ -167,9 +183,8 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
167183
platformUrl: gitPlatformEntity.url,
168184
platformName: gitPlatformEntity.name,
169185
},
170-
projectVersion: projectEntity.version,
171-
generatedProjectPath: `${downloadedProjectPath}` + `${originalProjectPath}`,
172-
existingProject: `${downloadedProjectPath}/${projectEntity.repository_name}`,
186+
clonedProjectPath: `${clonedProjectPath}`,
187+
generatedProjectPath: `${generatedProjectPath}`,
173188
};
174189

175190
let error: string = await cloneExistingProjectFromGitServer(existingProjectGitServerRequest);
@@ -189,26 +204,26 @@ codeOperationsRouter.post('/generate', requireEmailMiddleware, async (request, r
189204
Logger.error(pushErrorMessage);
190205
return resource.status(500).json(getGenerateCodeError(pushErrorMessage));
191206
}
192-
193207
Logger.debug(`saved ${downloadedProjectPath} to github.`);
194208
cleanup(downloadedProjectPath);
209+
cleanup(clonedProjectPath);
210+
cleanup(downloadedProjectTarFilePath);
195211

196212
// update status of projectEntity
197-
const metadata = JSON.parse(projectEntity.metadata as string) || new Map<string, string>();
213+
const metadata = JSON.parse(projectEntity.metadata as string) || {} as Metadata;
198214
metadata.isGenerated = true;
199215
metadata.version = projectEntity.version;
200216
// add metadata back to projectEntity.spec
201217
projectEntity.metadata = JSON.stringify(metadata);
202218
// change version now
203-
if (projectEntity.old_versions) {
204-
const oldVersion: OldVersion = {
205-
version: projectEntity.version,
206-
json: projectEntity.json
207-
};
208-
projectEntity.version = nextVersion(projectEntity.version);
209-
projectEntity.old_versions.push(JSON.stringify(oldVersion));
210-
}
211-
console.log('projectEntity', projectEntity);
219+
const oldVersion: OldVersion = {
220+
version: projectEntity.version,
221+
json: projectEntity.json
222+
};
223+
const oldVersions = JSON.parse(projectEntity.old_versions) as OldVersion[];
224+
projectEntity.version = nextVersion(projectEntity.version);
225+
oldVersions.push(oldVersion);
226+
projectEntity.old_versions = JSON.stringify(oldVersions);
212227

213228
const isUpdated = await projectService.updateProject(projectId, projectEntity);
214229
if (isUpdated) {

app/src/store/cassandraProjectDaoImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CassandraProjectDaoImpl implements ProjectDao {
1414
old_versions,
1515
created_at, updated_at)
1616
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS`;
17-
const params = [projectEntity.id, projectEntity.display_name, projectEntity.version, projectEntity.json, projectEntity.git_platform_name, projectEntity.git_platform_user_name, projectEntity.is_repository_public, projectEntity.repository_branch, projectEntity.repository_name, projectEntity.owner_email, projectEntity.repository_url, projectEntity.metadata, projectEntity.created_at, projectEntity.updated_at];
17+
const params = [projectEntity.id, projectEntity.display_name, projectEntity.version, projectEntity.json, projectEntity.git_platform_name, projectEntity.git_platform_user_name, projectEntity.is_repository_public, projectEntity.repository_branch, projectEntity.repository_name, projectEntity.owner_email, projectEntity.repository_url, projectEntity.metadata,projectEntity.old_versions, projectEntity.created_at, projectEntity.updated_at];
1818
const resultSet = await cassandraClient.execute(query, params, {prepare: true});
1919
if (resultSet.wasApplied()) {
2020
return projectEntity;
@@ -32,7 +32,7 @@ export class CassandraProjectDaoImpl implements ProjectDao {
3232
repository_url: '',
3333
owner_email: '',
3434
metadata: '',
35-
old_versions: [],
35+
old_versions: '',
3636
created_at: '',
3737
updated_at: '',
3838
};
@@ -93,7 +93,7 @@ export class CassandraProjectDaoImpl implements ProjectDao {
9393
repository_url: '',
9494
owner_email: '',
9595
metadata: '',
96-
old_versions: [],
96+
old_versions: '',
9797
created_at: '',
9898
updated_at: '',
9999
};

app/src/store/sqliteProjectDaoImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export class SqliteProjectDaoImpl implements ProjectDao {
77
async createProject(projectEntity: ProjectEntity): Promise<ProjectEntity> {
88
projectEntity.id = generateProjectId(projectEntity);
99
return new Promise((resolve, reject) => {
10-
const stmt = db.prepare('INSERT INTO projects (id, display_name, version, json, git_platform_name, git_platform_user_name, is_repository_public, repository_branch, repository_name, owner_email, repository_url, metadata, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
11-
stmt.run(projectEntity.id, projectEntity.display_name, projectEntity.version, projectEntity.json, projectEntity.git_platform_name, projectEntity.git_platform_user_name, projectEntity.is_repository_public, projectEntity.repository_branch, projectEntity.repository_name, projectEntity.owner_email, projectEntity.repository_url, projectEntity.metadata, projectEntity.created_at, projectEntity.updated_at, (err: any) => {
10+
const stmt = db.prepare('INSERT INTO projects (id, display_name, version, json, git_platform_name, git_platform_user_name, is_repository_public, repository_branch, repository_name, owner_email, repository_url, metadata, old_versions, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
11+
stmt.run(projectEntity.id, projectEntity.display_name, projectEntity.version, projectEntity.json, projectEntity.git_platform_name, projectEntity.git_platform_user_name, projectEntity.is_repository_public, projectEntity.repository_branch, projectEntity.repository_name, projectEntity.owner_email, projectEntity.repository_url, projectEntity.metadata, projectEntity.old_versions, projectEntity.created_at, projectEntity.updated_at, (err: any) => {
1212
if (err) {
1313
reject(err);
1414
} else {
@@ -65,7 +65,7 @@ export class SqliteProjectDaoImpl implements ProjectDao {
6565
is_repository_public: false,
6666
repository_url: '',
6767
metadata: '',
68-
old_versions: [],
68+
old_versions: '',
6969
};
7070
resolve(projectEntity);
7171
}

0 commit comments

Comments
 (0)