message: chore(ci): close and resubmit PR if exists, comment when succeeds [skip ci]
diff: @@ -134,11 +134,45 @@ Note: this pull request is submitted [automatically](https://github.com/nextstra
  }
  # Submit the PR, reading message body from stdin
  +function submit_pr() {
  pr_body | gh pr create \
  --title "Update nextclade and nextalign to ${version}" \
  --body-file - \
  --repo "bioconda/bioconda-recipes"
  +}
  +
  +# Get PR number from the output of gh command
  +function get_pr_number() {
  + # The message from gh command typically ends with a URL to the PR and PR number is the last component of the path.
  + # Split the string on '/' and take the last component.
  + echo "${1##*/}"
  +}
  +
  +# Close PR given its number
  +function close_pr() {
  + gh pr close "${1:?}" --delete-branch --repo "bioconda/bioconda-recipes"
  +}
  +# Comment on PR given its number and a message from stdin
  +function comment_on_pr() {
  + gh pr comment "${1:?}" --body-file -
  +}
  +
  +if ! result=$(submit_pr 2>&1); then
  + echo "${result}" >&2
  + if [[ "${result}" == *"already exists"* ]]; then
  + # Failed because PR already exists. Close and resubmit.
  + pr_number=$(get_pr_number "${result}")
  + close_pr "${pr_number}"
  + submit_pr
  + else
  + exit 1
  + fi
  +else
  + echo "${result}"
  + pr_number=$(get_pr_number "${result}")
  + echo "@BiocondaBot please add label" | comment_on_pr "${pr_number}"
  +fi
  popd >/dev/null
  