Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Merge branch 'redhat-developer:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
thepetk committed Apr 26, 2023
2 parents faa26e9 + baf25f6 commit bfbb738
Show file tree
Hide file tree
Showing 144 changed files with 583 additions and 114,638 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,73 @@ func (e ExpressDetector) DoPortsDetection(component *model.Component, ctx *conte
}
}

func getPortGroup(content string, matchIndexes []int, portPlaceholder string) string {
contentBeforeMatch := content[0:matchIndexes[0]]
re := regexp.MustCompile(`(let|const|var)\s+` + portPlaceholder + `\s*=\s*([^;]*)`)
return utils.FindPotentialPortGroup(re, contentBeforeMatch, 2)
}

func GetEnvPort(envPlaceholder string) int {
envPlaceholder = strings.Replace(envPlaceholder, "process.env.", "", -1)
portValue := os.Getenv(envPlaceholder)
if port, err := utils.GetValidPort(portValue); err == nil {
return port
}
return -1
}

func getPort(content string, matchIndexes []int) int {
// Express configures its port with app.listen()
portPlaceholder := content[matchIndexes[0]:matchIndexes[1]]
//we should end up with something like ".listen(PORT"
portPlaceholder = strings.Replace(portPlaceholder, ".listen(", "", -1)
// if we are lucky enough portPlaceholder contains a real PORT otherwise it would be a variable/expression

// Case: Raw port value -> return it directly
if port, err := utils.GetValidPort(portPlaceholder); err == nil {
return port
}
// of course we are unlucky ... is it an env variable?

// Case: Env var given as value in app.listen -> Get env value
// example: app.listen(process.env.PORT...
re := regexp.MustCompile(`process.env.[^ ,)]+`)
envMatchIndexes := re.FindStringSubmatchIndex(portPlaceholder)
envPortValue := portPlaceholder
// If no match was found check if port is a variable assigned elsewhere in the code
if len(envMatchIndexes) == 0 {
// Case: Var Port with env var as value
potentialPortGroup := getPortGroup(content, matchIndexes, portPlaceholder)
if potentialPortGroup != "" {
// Takes into account cases like -> var PORT = process.env.PORT || 8080
portValues := strings.Split(potentialPortGroup, " || ")
for _, portValue := range portValues {
re = regexp.MustCompile(`process.env.[^ ,)]+`)
tmpMatchIndexes := re.FindStringSubmatchIndex(portValue)
// If there is any matching update the env values
if len(tmpMatchIndexes) > 1 {
envMatchIndexes = tmpMatchIndexes
envPortValue = portValue
}
}
}
}
// After double checking for env vars try to get the value of this port
if len(envMatchIndexes) > 1 {
envPlaceholder := portPlaceholder[envMatchIndexes[0]:envMatchIndexes[1]]
// we should end up with something like process.env.PORT
envPlaceholder = strings.Replace(envPlaceholder, "process.env.", "", -1)
//envPlaceholder should contain the name of the env variable
portValue := os.Getenv(envPlaceholder)
if port, err := utils.GetValidPort(portValue); err == nil {
envPlaceholder := envPortValue[envMatchIndexes[0]:envMatchIndexes[1]]
port := GetEnvPort(envPlaceholder)
// The port will be return only if a value was found for the given env var
if port > 0 {
return port
}
} else {
// we are not dealing with an env variable, let's try to find a variable set before the listen function
contentBeforeMatch := content[0:matchIndexes[0]]
re = regexp.MustCompile(`(let|const|var)\s+` + portPlaceholder + `\s*=\s*([^;]*)`)
return utils.FindPortSubmatch(re, contentBeforeMatch, 2)
}
// Case: No env var or raw value found -> check for raw value into a var
potentialPortGroup := getPortGroup(content, matchIndexes, portPlaceholder)
if potentialPortGroup != "" {
// Takes into account cases like -> var PORT = process.env.PORT || 8080
portValues := strings.Split(potentialPortGroup, " || ")
for _, portValue := range portValues {
if port, err := utils.GetValidPort(portValue); err == nil {
return port
}
}
}
return -1
}
16 changes: 12 additions & 4 deletions go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,23 @@ func readAnyApplicationFile(root string, propsFiles []model.ApplicationFileInfo,
}

func FindPortSubmatch(re *regexp.Regexp, text string, group int) int {
potentialPortGroup := FindPotentialPortGroup(re, text, group)
if potentialPortGroup != "" {
if port, err := GetValidPort(potentialPortGroup); err == nil {
return port
}
}
return -1
}

func FindPotentialPortGroup(re *regexp.Regexp, text string, group int) string {
if text != "" {
matches := re.FindStringSubmatch(text)
if len(matches) > group {
if port, err := GetValidPort(matches[group]); err == nil {
return port
}
return matches[group]
}
}
return -1
return ""
}

func FindAllPortsSubmatch(re *regexp.Regexp, text string, group int) []int {
Expand Down
10 changes: 10 additions & 0 deletions go/test/apis/component_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ func TestPortDetectionJavascriptExpressEnv(t *testing.T) {
os.Unsetenv("TEST_EXPRESS_ENV")
}

func TestPortDetectionJavascriptExpressEnvOROperatorWithEnvVar(t *testing.T) {
os.Setenv("TEST_EXPRESS_ENV", "1111")
testPortDetectionInProject(t, "projectExpressEnvLogicalOROperator", []int{1111})
os.Unsetenv("TEST_EXPRESS_ENV")
}

func TestPortDetectionJavascriptExpressEnvOROperatorWithoutEnvVar(t *testing.T) {
testPortDetectionInProject(t, "projectExpressEnvLogicalOROperator", []int{8080})
}

func TestPortDetectionJavascriptExpressVariable(t *testing.T) {
testPortDetectionInProject(t, "projectExpressVariable", []int{3000})
}
Expand Down
16 changes: 0 additions & 16 deletions resources/projectPortTesting/projectAngularjs/.browserslistrc

This file was deleted.

16 changes: 0 additions & 16 deletions resources/projectPortTesting/projectAngularjs/.editorconfig

This file was deleted.

42 changes: 0 additions & 42 deletions resources/projectPortTesting/projectAngularjs/.gitignore

This file was deleted.

27 changes: 0 additions & 27 deletions resources/projectPortTesting/projectAngularjs/README.md

This file was deleted.

44 changes: 0 additions & 44 deletions resources/projectPortTesting/projectAngularjs/karma.conf.js

This file was deleted.

Loading

0 comments on commit bfbb738

Please sign in to comment.