Skip to content

Commit

Permalink
ceph-backport.sh/PR quoting: eliminate unwanted newlines
Browse files Browse the repository at this point in the history
Sometimes the PR body has multiple newlines which do not need
to be quoted. Also, the way we interact with the GitHub API, we
were getting some newline duplication.

This patch reduces all clumps of multiple blank lines down to a single
blank line.

Signed-off-by: Nathan Cutler <[email protected]>
  • Loading branch information
smithfarm committed Feb 10, 2020
1 parent c1cdd78 commit 2e78b3f
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/script/ceph-backport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,29 @@ function clear_line {
log overwrite " \r"
}

function clip_pr_body {
local pr_body="$*"
local clipped=""
local last_line_was_blank=""
local line=""
local pr_json_tempfile=$(mktemp)
echo "$pr_body" | sed -n '/<!--.*/q;p' > "$pr_json_tempfile"
while IFS= read -r line; do
if [ "$(trim_whitespace "$line")" ] ; then
last_line_was_blank=""
clipped="${clipped}${line}\n"
else
if [ "$last_line_was_blank" ] ; then
true
else
clipped="${clipped}\n"
fi
fi
done < "$pr_json_tempfile"
rm "$pr_json_tempfile"
echo "$clipped"
}

function debug {
log debug "$@"
}
Expand Down Expand Up @@ -388,12 +411,8 @@ function existing_pr_routine {
info "Cowardly refusing to work on a backport to $base_branch"
false
fi
pr_json_tempfile=$(mktemp)
echo "$pr_body" | sed -n '/<!--.*/q;p' > "$pr_json_tempfile"
# clipped_pr_body=$(cat "$pr_json_tempfile" | xargs)
clipped_pr_body="$(xargs < "$pr_json_tempfile")"
rm "$pr_json_tempfile"
verbose_en "Clipped body of existing PR ${backport_pr_number}:\n${clipped_pr_body}\n"
clipped_pr_body="$(clip_pr_body "$pr_body")"
verbose_en "Clipped body of existing PR ${backport_pr_number}:\n${clipped_pr_body}"
if [[ "$backport_pr_title" =~ ^${milestone}: ]] ; then
verbose "Existing backport PR ${backport_pr_number} title has ${milestone} prepended"
else
Expand Down Expand Up @@ -1013,6 +1032,15 @@ function tracker_component_was_updated {
echo "$was_updated"
}

function trim_whitespace {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}

function troubleshooting_advice {
cat <<EOM
Troubleshooting notes
Expand Down

0 comments on commit 2e78b3f

Please sign in to comment.