From 90d3e4f802ea0706682ad0183a2f882a50f97c2b Mon Sep 17 00:00:00 2001 From: Jing Qi Date: Thu, 5 Dec 2024 11:58:48 +0800 Subject: [PATCH] feat(RELEASE-1252): make run-file-updates task idempotent Signed-off-by: Jing Qi --- .../process-file-updates-task.yaml | 77 ++++++++++++++++++- .../process-file-updates-task/tests/mocks.sh | 6 ++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/internal/tasks/process-file-updates-task/process-file-updates-task.yaml b/internal/tasks/process-file-updates-task/process-file-updates-task.yaml index 739fdc6e3..f6fcea5ab 100644 --- a/internal/tasks/process-file-updates-task/process-file-updates-task.yaml +++ b/internal/tasks/process-file-updates-task/process-file-updates-task.yaml @@ -243,7 +243,82 @@ spec: fi echo -e "\n*** START LOCAL CHANGES ***\n" - git diff + git diff | tee "${TEMP}"/tmpPR.diff + + # There is no difference in the content + if [[ -s "${TEMP}"/tmpPR.diff ]]; then + addLinesCount=$(wc -l < "${targetFile}") + openMRs=$(glab mr list -R "${UPSTREAM_REPO}" --search "Konflux release" |grep "^!"| cut -f1 | tr -d '!') + for mrNum in ${openMRs} ; do + glab mr diff "${mrNum}" --repo "${UPSTREAM_REPO}" > "${TEMP}"/MR.diff + grep "^+[^+]" "${TEMP}/MR.diff" > "${TEMP}/MR_add_lines" + mrAddLinesCount=$(wc -l < "${TEMP}/MR_add_lines") + + # for only seed a file + if [[ "$mrAddLinesCount" -ne "$addLinesCount" ]] ; then + # echo "Line counts do not match for MR #${mrNum}, skipping..." + continue + fi + + notFound=0 + while IFS= read -r oneLine; do + if ! grep -Fq "${oneLine}" "${TEMP}"/MR_add_lines ; then + notFound=$((notFound + 1)) + # echo "Not found in MR_replace_lines: ${replaceLine}" + break + fi + done < "${targetFile}" + + if [[ ${notFound} == 0 ]] ; then + # it should exit 0 if the same MR exists + echo -n "Success" | tee "$(results.fileUpdatesState.path)" + exit 0 + fi + done + else + replacedLines=$(grep "^-[^-]" "${TEMP}"/tmpPR.diff) + replacedLineCount=$(echo "$replacedLines" | wc -l) + replaceLines=$(grep "^+[^+]" "${TEMP}"/tmpPR.diff) + replaceLineCount=$(echo "$replaceLines" | wc -l) + openMRs=$(glab mr list -R "${UPSTREAM_REPO}" --search "Konflux release" |grep "^!"| cut -f1 | tr -d '!') + for mrNum in ${openMRs} ; do + glab mr diff "${mrNum}" --repo "${UPSTREAM_REPO}" > "${TEMP}"/MR.diff + grep "^-[^-]" "${TEMP}/MR.diff" > "${TEMP}/MR_replaced_lines" + mrReplacedCount=$(wc -l < "${TEMP}/MR_replaced_lines") + grep "^+[^+]" "${TEMP}/MR.diff" > "${TEMP}/MR_replace_lines" + mrReplaceCount=$(wc -l < "${TEMP}/MR_replace_lines") + + if [[ "$replacedLineCount" -ne "$mrReplacedCount" ]] ||\ + [[ "$replaceLineCount" -ne "$mrReplaceCount" ]] ; then + # echo "Line counts do not match for MR #${mrNum}, skipping..." + continue + fi + + notFound=0 + while IFS= read -r replacedLine; do + if ! grep -Fq "${replacedLine#-}" "${TEMP}"/MR_replaced_lines ; then + notFound=$((notFound + 1)) + # echo "Not found in MR_replaced_lines: ${replacedLine}" + break + fi + done <<< "$replacedLines" + + while IFS= read -r replaceLine; do + if ! grep -Fq "${replaceLine#+}" "${TEMP}"/MR_replace_lines ; then + notFound=$((notFound + 1)) + # echo "Not found in MR_replace_lines: ${replaceLine}" + break + fi + done <<< "$replaceLines" + + # if all the lines are found and line counts are matched, the MR is the same one + if [[ ${notFound} == 0 ]] ; then + # it should exit 0 if the same MR exists + echo -n "Success" | tee "$(results.fileUpdatesState.path)" + exit 0 + fi + done + fi echo -e "\n*** END LOCAL CHANGES ***\n" WORKING_BRANCH=$(uuidgen |awk '{print substr($1, 1, 8)}') diff --git a/internal/tasks/process-file-updates-task/tests/mocks.sh b/internal/tasks/process-file-updates-task/tests/mocks.sh index c693b9502..22a69bc94 100644 --- a/internal/tasks/process-file-updates-task/tests/mocks.sh +++ b/internal/tasks/process-file-updates-task/tests/mocks.sh @@ -34,5 +34,11 @@ function glab() { if [[ "$*" == *"mr create"* ]]; then gitRepo=$(echo "$*" | cut -f5 -d/ | cut -f1 -d.) echo "/merge_request/1" + elif [[ "$*" == *"mr list"* ]]; then + echo '!1 ' + elif [[ "$*" == *"mr diff"* ]]; then + echo "+indexImage: Tom ++ +" fi }