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

feat(engine): improve the possible dockerfile detection #6981

Merged
merged 8 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 26 additions & 1 deletion pkg/utils/get_extension.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package utils

import (
"bufio"
"bytes"
"os"
"path/filepath"
"strings"

"github.com/rs/zerolog/log"
"golang.org/x/tools/godoc/util"
Expand All @@ -21,13 +23,36 @@
if Contains(base, targets) {
ext = base
} else if isTextFile(path) {
ext = "possibleDockerfile"
if readPossibleDockerFile(path) {
ext = "possibleDockerfile"
}
}
}

return ext
}

func readPossibleDockerFile(path string) bool {
file, err := os.Open(path)
Fixed Show fixed Hide fixed
if err != nil {
return false
}
defer file.Close()
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(file)
// Read lines from the file
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "FROM") {
return true
} else if strings.HasPrefix(scanner.Text(), "#") {
continue
} else {
return false
}
}
return false
}

func isTextFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/utils/get_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func TestGetExtension(t *testing.T) {
want: ".tf",
filePath: "../../test/fixtures/all_auth_users_get_read_access/test/positive.tf",
},
{
name: "Get empty extension from a file not named as Dockerfile and without extension defined",
want: "",
filePath: "../../test/fixtures/negative_dockerfile/CW671X02_EBM_EVENT_RULE",
},
}

for _, test := range tests {
Expand Down
45 changes: 45 additions & 0 deletions test/fixtures/negative_dockerfile/CW671X02_EBM_EVENT_RULE
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/ksh93
. ${PROC_DIR:=${0%/*}}/CWPARAM
export UDB_DB=${WAREHOUSE_DB}
. ${PROC_DIR}/DB2COMMON

db2 -f ${UDB_CMD}

TABLE=TECW.EBM_EVENT_RULE
CURSOR_NAME=TSTG_${RUN_TYPE}_EBM_EVENT_RULE
QUERY="
SELECT
RUN_ID
, RPT_CASE_ID
, EVENT
, RPT_RULE_ID
, CHAR('SYMMETRY')
, MBR_MPI_ID
, CAST(TASKNBR as CHAR(3))
, RULE_TYPE_NBR
, RESULT_FLG
, CASE WHEN EBM_FLG = '0' AND COMPLIANCE_FLG = '0' THEN '0'
ELSE '1'
END AS EBM_QUAL_IND
, EBM_FLG
, COMPLIANCE_FLG
, CURRENT_TIMESTAMP
, CURRENT_TIMESTAMP
, '${SCHED_JOB_NAME}'
FROM TSTG.${RUN_TYPE}_SYMM_EBM_SUMMARY_RESULT_FL
"

db2 "declare ${CURSOR_NAME} cursor for ${QUERY}"
EXIT_CODE=$?

if [[ ${EXIT_CODE} -ne 0 ]]
then
echo "Unable to declare cursor. RC:$EXIT_CODE}"
exit ${EXIT_CODE}
fi

db2 "load from ${CURSOR_NAME} of cursor insert into ${TABLE} nonrecoverable"
EXIT_CODE=$?

exit $EXIT_CODE

Loading