Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 5ccb784

Browse files
author
James Wallis
committed
Refactor File-watcher projectName, add subdir to location when available
Signed-off-by: James Wallis <james.wallis1@ibm.com>
1 parent b345665 commit 5ccb784

File tree

10 files changed

+148
-33
lines changed

10 files changed

+148
-33
lines changed

src/pfe/file-watcher/server/src/controllers/projectsController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function createProject(req: ICreateProjectParams): Promise<ICreateP
146146
return { "statusCode": 400, "error": { "msg": "projectID, projectType and location are required parameters" }};
147147
}
148148

149-
const projectName = projectLocation.split("/").pop();
149+
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
150150

151151
// create log storing directory for the project
152152
logger.logInfo("Creating project logs directory");
@@ -658,7 +658,7 @@ export async function deleteProject(projectID: string): Promise<IDeleteProjectSu
658658

659659
const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
660660
const projectLocation = projectInfo.location;
661-
const projectName = projectLocation.split("/").pop();
661+
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
662662

663663
logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
664664
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
@@ -842,7 +842,7 @@ export async function projectDeletion(projectID: string): Promise<number> {
842842
const projectMetadata = getProjectMetadataById(projectID);
843843
const projectInfo: ProjectInfo = await getProjectInfoFromFile(projectMetadata.infoFile);
844844
const projectLocation = projectInfo.location;
845-
const projectName = projectLocation.split("/").pop();
845+
const projectName = projectUtil.getProjectNameFromPath(projectLocation);
846846

847847
logger.logProjectTrace("Retrieved project information for project " + projectMetadata.infoFile, projectID);
848848
logger.logProjectTrace(JSON.stringify(projectInfo), projectID);
@@ -1154,7 +1154,7 @@ export function saveProjectInfo(projectID: string, projectInfo: ProjectInfo, sav
11541154
const projectJSON = JSON.stringify(projectInfo);
11551155
const infoFile = getProjectMetadataById(projectID).infoFile;
11561156
projectInfoCache[infoFile] = projectJSON;
1157-
const projectName = projectInfo.location.split("/").pop();
1157+
const projectName = projectUtil.getProjectNameFromPath(projectInfo.location);
11581158
logger.logProjectTrace(JSON.stringify(projectInfoCache), projectID);
11591159
if (saveIntoJsonFile) {
11601160
fs.writeFile(infoFile, projectJSON, "utf8", (err) => {

src/pfe/file-watcher/server/src/projects/Validator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as io from "../utils/socket";
1414
import * as locale from "../utils/locale";
1515
import * as logger from "../utils/logger";
1616
import { Operation } from "./operation";
17+
import { getProjectNameFromPath } from "./projectUtil";
1718

1819
/**
1920
* @class
@@ -57,7 +58,7 @@ export class Validator {
5758
async validateRequiredFiles (requiredFiles: string[]): Promise<void> {
5859
if (requiredFiles) {
5960
const projectID = this.projectID;
60-
const projectName = this.location.split("/").pop();
61+
const projectName = getProjectNameFromPath(this.location);
6162
const OR_SPLIT = "|";
6263

6364
try {
@@ -179,7 +180,7 @@ export class Validator {
179180
*/
180181
sendResult(): void {
181182
const projectID = this.projectID;
182-
const projectName = this.location.split("/").pop();
183+
const projectName = getProjectNameFromPath(this.location);
183184
logger.logProjectInfo("Sending validation result", projectID, projectName);
184185
io.emitOnListener("projectValidated", this.result());
185186
}

src/pfe/file-watcher/server/src/projects/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const validate = async function(args: IProjectActionParams): Promise<{ op
5757
"projectType": projectType,
5858
"location": location
5959
} as ProjectInfo;
60-
const projectName = location.split("/").pop();
60+
const projectName = projectUtil.getProjectNameFromPath(location);
6161
if (args.extensionID) {
6262
projectInfo.extensionID = args.extensionID;
6363
}
@@ -131,7 +131,7 @@ export const enableautobuild = async function (args: IProjectActionParams): Prom
131131
async function enableAndBuild(projectInfo: ProjectInfo): Promise<void> {
132132
const projectHandler = await projectExtensions.getProjectHandler(projectInfo);
133133
const projectID = projectInfo.projectID;
134-
const projectName = projectInfo.location.split("/").pop();
134+
const projectName = projectUtil.getProjectNameFromPath(projectInfo.location);
135135

136136
if (projectHandler.hasOwnProperty("setAutoBuild")) {
137137
const operation = new Operation("enableautobuild", projectInfo);

src/pfe/file-watcher/server/src/projects/projectUtil.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export async function containerCreate(operation: Operation, script: string, comm
104104
const event = "projectCreation";
105105
const projectLocation = operation.projectInfo.location;
106106
const projectID = operation.projectInfo.projectID;
107-
const projectName = projectLocation.split("/").pop();
107+
const projectName = getProjectNameFromPath(projectLocation);
108108
const projectType = operation.projectInfo.projectType;
109109
if (projectList.indexOf(projectID) === -1)
110110
projectList.push(projectID);
@@ -173,6 +173,7 @@ export async function containerCreate(operation: Operation, script: string, comm
173173
(operation.projectInfo.forceAction) ? String(operation.projectInfo.forceAction) : "NONE", logDir, imagePushRegistry, userMavenSettings];
174174
} else if (["odo", "odo-devfile"].includes(projectType)) {
175175
const componentName: string = await getComponentName(projectName);
176+
const logDir: string = await logHelper.getLogDir(projectID, projectName);
176177

177178
args = [
178179
projectLocation,
@@ -211,7 +212,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
211212

212213
const projectLocation = operation.projectInfo.location;
213214
const projectID = operation.projectInfo.projectID;
214-
const projectName = projectLocation.split("/").pop();
215+
const projectName = getProjectNameFromPath(projectLocation);
215216
const projectType = operation.projectInfo.projectType;
216217
logger.logProjectInfo("Updating container for " + operation.projectInfo.projectType + " project " + projectLocation, projectID, projectName);
217218
operation.containerName = await getContainerName(operation.projectInfo);
@@ -280,6 +281,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
280281
(operation.projectInfo.forceAction) ? String(operation.projectInfo.forceAction) : "NONE", logDir, imagePushRegistry, userMavenSettings];
281282
} else if (["odo", "odo-devfile"].includes(projectType)) {
282283
const componentName: string = await getComponentName(projectName);
284+
const logDir: string = await logHelper.getLogDir(projectID, projectName);
283285

284286
args = [
285287
projectLocation,
@@ -316,7 +318,7 @@ export async function containerUpdate(operation: Operation, script: string, comm
316318
async function executeBuildScript(operation: Operation, script: string, args: Array<string>, event: string): Promise<void> {
317319
const projectID = operation.projectInfo.projectID;
318320
const projectLocation = operation.projectInfo.location;
319-
const projectName = projectLocation.split("/").pop();
321+
const projectName = getProjectNameFromPath(projectLocation);
320322
const projectInfo = {
321323
operationId: operation.operationId,
322324
projectID: operation.projectInfo.projectID
@@ -612,7 +614,7 @@ export async function getProjectMavenSettings(projectInfo: ProjectInfo): Promise
612614
export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectLog> {
613615
const projectID = projectInfo.projectID;
614616
const projectLocation = projectInfo.location;
615-
const projectName = projectLocation.split("/").pop();
617+
const projectName = getProjectNameFromPath(projectLocation);
616618
const projectType = projectInfo.projectType;
617619
const projectLogDir = await logHelper.getLogDir(projectID, projectName);
618620
const logDirectory = path.join(projectConstants.projectsLogDir, projectLogDir);
@@ -655,7 +657,7 @@ export async function getProjectLogs(projectInfo: ProjectInfo): Promise<ProjectL
655657
export async function containerDelete(projectInfo: ProjectInfo, script: string): Promise<void> {
656658

657659
const projectID = projectInfo.projectID;
658-
const projectName = projectInfo.location.split("/").pop();
660+
const projectName = getProjectNameFromPath(projectInfo.location);
659661
const containerName = await getContainerName(projectInfo);
660662
const imagePushRegistry = projectInfo.deploymentRegistry;
661663
logger.logProjectInfo("containerDelete: Kill running processes and remove container... ", projectID, projectName);
@@ -725,7 +727,7 @@ export function getLogName(projectID: string, projectLocation: string): string {
725727
const hash = crypto.createHash("sha1", <TransformOptions>"utf8").update(projectLocation);
726728

727729
let logName = projectConstants.containerPrefix + projectID + "-" + hash.digest("hex");
728-
const projectName = projectLocation.split("/").pop();
730+
const projectName = getProjectNameFromPath(projectLocation);
729731

730732
if (process.env.IN_K8 === "true" && logName.length > 53) {
731733
logName = logName.substring(0, 53);
@@ -1255,6 +1257,7 @@ export async function runScript(projectInfo: ProjectInfo, script: string, comman
12551257

12561258
if (["odo", "odo-devfile"].includes(projectInfo.projectType)) {
12571259
const componentName: string = await getComponentName(projectName);
1260+
const logDir: string = await logHelper.getLogDir(projectID, projectName);
12581261

12591262
args = [
12601263
projectInfo.location,
@@ -1290,7 +1293,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis
12901293

12911294
const projectLocation = operation.projectInfo.location;
12921295
const projectID = operation.projectInfo.projectID;
1293-
const projectName = projectLocation.split("/").pop();
1296+
const projectName = getProjectNameFromPath(projectLocation);
12941297

12951298
if (projectList.indexOf(projectID) === -1)
12961299
projectList.push(projectID);
@@ -1446,7 +1449,7 @@ export async function buildAndRun(operation: Operation, command: string): Promis
14461449
*/
14471450
async function containerBuildAndRun(event: string, buildInfo: BuildRequest, operation: Operation): Promise<void> {
14481451
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
1449-
const projectName = normalizedProjectLocation.split("/").reverse()[0];
1452+
const projectName = getProjectNameFromPath(normalizedProjectLocation);
14501453
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
14511454
const dockerBuildLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.buildLogs.dockerBuild + logHelper.logExtension);
14521455
if (process.env.IN_K8 === "true") {
@@ -1668,7 +1671,7 @@ async function containerBuildAndRun(event: string, buildInfo: BuildRequest, oper
16681671
*/
16691672
async function runLocalContainer(buildInfo: BuildRequest): Promise<void> {
16701673
const normalizedProjectLocation = path.resolve(buildInfo.projectLocation);
1671-
const projectName = normalizedProjectLocation.split("/").reverse()[0];
1674+
const projectName = getProjectNameFromPath(normalizedProjectLocation);
16721675
const logDir = await logHelper.getLogDir(buildInfo.projectID, projectName);
16731676
const appLog = path.resolve(buildInfo.projectLocation + "/../.logs/" + logDir, logHelper.appLogs.app + logHelper.logExtension);
16741677
try {
@@ -1799,7 +1802,7 @@ export async function isApplicationPodUp(buildInfo: BuildRequest, projectName: s
17991802
export async function removeProject(projectInfo: ProjectInfo): Promise<void> {
18001803

18011804
const projectID = projectInfo.projectID;
1802-
const projectName = projectInfo.location.split("/").pop();
1805+
const projectName = getProjectNameFromPath(projectInfo.location);
18031806
const containerName = await getContainerName(projectInfo);
18041807
logger.logProjectInfo("removeProject: Kill running processes and remove container... ", projectID, projectName);
18051808
logger.logProjectInfo("Project ID: " + projectInfo.projectID, projectID, projectName);
@@ -1903,7 +1906,7 @@ async function getPODInfoAndSendToPortal(operation: Operation, event: string = "
19031906
const projectInfo = operation.projectInfo;
19041907
const projectLocation = projectInfo.location;
19051908
const projectID = projectInfo.projectID;
1906-
const projectName = projectLocation.split("/").pop();
1909+
const projectName = getProjectNameFromPath(projectLocation);
19071910
const keyValuePair: UpdateProjectInfoPair = {
19081911
key: "buildRequest",
19091912
value: false
@@ -2203,8 +2206,26 @@ export async function updateDetailedAppStatus(projectID: string, ip: string, por
22032206
export async function exposeOverIngress(projectInfo: ProjectInfo, appPort?: number): Promise<void> {
22042207
if (process.env.IN_K8) {
22052208
const projectID = projectInfo.projectID;
2206-
const projectName = projectInfo.location.split("/").pop();
2209+
const projectName = getProjectNameFromPath(projectInfo.location);
22072210
projectInfo.appBaseURL = await kubeutil.exposeOverIngress(projectID, projectName, projectInfo.isHttps, appPort, projectInfo.appBaseURL);
22082211
await projectsController.saveProjectInfo(projectID, projectInfo, true);
22092212
}
22102213
}
2214+
2215+
/**
2216+
* Get the projectName from a path
2217+
*
2218+
* @param location The project's location
2219+
*/
2220+
export function getProjectNameFromPath(location: string): string {
2221+
const splitPaths = location.split("/");
2222+
const cwIndex = splitPaths.indexOf("codewind-workspace");
2223+
if (cwIndex === -1) {
2224+
logger.logError("Unable to get project name from path: codewind-workspace isn't in the path");
2225+
// Fall back to old method if codewind-workspace isn't in the path
2226+
return splitPaths.pop();
2227+
}
2228+
// Project name is the directory after codewind-workspace
2229+
const projectName = splitPaths[cwIndex + 1];
2230+
return projectName;
2231+
}

src/pfe/file-watcher/server/src/utils/logger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as path from "path";
1515
import { promisify } from "util";
1616
import * as constants from "../projects/constants";
1717
import * as stackTrace from "stack-trace";
18+
import { getProjectNameFromPath } from "../projects/projectUtil";
1819
const chalk = require("chalk"); // tslint:disable-line:no-require-imports
1920

2021
const GENERAL_LOG_FILE_NAME = "Turbine.log";
@@ -101,7 +102,7 @@ export async function getProjectNameByProjectID(projectID: string): Promise<stri
101102
const data = await readFileAsync(projectDataFile, "utf8");
102103
const projectData = JSON.parse(data);
103104
const location = projectData.location;
104-
const projectName = location.split("/").pop();
105+
const projectName = getProjectNameFromPath(location);
105106
return projectName;
106107
}
107108

src/pfe/file-watcher/server/test/unit-test/tests/projectUtil.module.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,35 @@ export function projectUtilTestModule(): void {
449449
});
450450
}
451451
});
452-
}
452+
453+
describe.only("getProjectNameFromPath", () => {
454+
const tests = [
455+
{
456+
title: "should return 'projectName' when it's the directory after 'codewind-workspace'",
457+
path: '/codewind-workspace/projectName',
458+
want: 'projectName',
459+
},
460+
{
461+
title: "should return 'projectName' when the path is normalized",
462+
path: path.resolve('/codewind-workspace/projectName'),
463+
want: 'projectName',
464+
},
465+
{
466+
title: "should return 'projectName' when subdirectories are given",
467+
path: path.resolve('/codewind-workspace/projectName/templates/default'),
468+
want: 'projectName',
469+
},
470+
{
471+
title: "returns 'finalDir' as 'codewind-workspace' isn't in the path",
472+
path: path.resolve('/projectName/templates/default/finalDir'),
473+
want: 'finalDir',
474+
},
475+
];
476+
tests.forEach(({ title, path, want }) => {
477+
it(`should return '${want}' when the input is '${path}'`, () => {
478+
const got = projectUtil.getProjectNameFromPath(path);
479+
expect(got).to.equal(want);
480+
});
481+
});
482+
});
483+
}

src/pfe/portal/modules/FileWatcher.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,11 @@ function createProjectActionForBuildAndRun(project, settings) {
736736
autoBuild,
737737
ports,
738738
} = project;
739-
const location = project.projectPath();
739+
740+
let location = project.projectPath();
741+
if (extension && extension.projectSubDirectory) {
742+
location = path.join(location, extension.projectSubDirectory);
743+
}
740744

741745
const projectAction = {
742746
projectID,

src/pfe/portal/modules/Templates.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ async function constructRepositoryObject(url, description, name, isRepoProtected
326326
}
327327
if (gitCredentials) {
328328
if (gitCredentials.username) {
329-
repository.authentication = { username: gitCredentials.username };
329+
repository.authentication = { username: gitCredentials.username };
330330
} else if (gitCredentials.personalAccessToken) {
331331
repository.authentication = {};
332332
}
@@ -503,6 +503,9 @@ async function getTemplatesFromRepo(repository, gitCredentials) {
503503
if (summary.projectStyle) {
504504
template.projectStyle = summary.projectStyle;
505505
}
506+
if (summary.subDirectory || summary.subDirectory === "") {
507+
template.subDirectory = summary.subDirectory;
508+
}
506509
if (repository.name) {
507510
template.source = repository.name;
508511
}

0 commit comments

Comments
 (0)