Skip to content

Commit

Permalink
feat(RELEASE-1252): make run-file-updates task idempotent
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Qi <jinqi@redhat.com>
  • Loading branch information
jinqi7 committed Dec 6, 2024
1 parent 13fe5ad commit 90d3e4f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
Expand Down
6 changes: 6 additions & 0 deletions internal/tasks/process-file-updates-task/tests/mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 90d3e4f

Please sign in to comment.