Mohammed Naser | 2ea922e | 2023-03-01 22:36:23 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | PROJECT=${1} |
| 4 | PATCH=${2} |
| 5 | BRANCH=${3:-master} |
| 6 | |
| 7 | if [[ ${PATCH} =~ ^https://review.opendev.org ]]; then |
| 8 | FORMAT=gerrit |
| 9 | PATCH_ID=$(echo ${PATCH} | awk -F/ '{print $NF}') |
| 10 | elif [[ ${PATCH} =~ ^https://github.com ]]; then |
| 11 | FORMAT=github |
| 12 | PATCH_ID=$(echo ${PATCH} | awk -F/ '{print $NF}') |
| 13 | else |
| 14 | # Exit if we don't know how to handle this patch |
| 15 | echo "Unknown patch format: ${PATCH}" |
| 16 | exit 1 |
| 17 | fi |
| 18 | |
| 19 | # Clone the repository in a temporary directory if it doesn't exist |
| 20 | if [ ! -d "/tmp/vexxhost-${PROJECT}" ]; then |
| 21 | gh repo clone vexxhost/${PROJECT} /tmp/vexxhost-${PROJECT} |
| 22 | fi |
| 23 | |
| 24 | # Switch to the repository |
| 25 | cd /tmp/vexxhost-${PROJECT} |
| 26 | |
| 27 | # Update the repository |
| 28 | git fetch origin |
| 29 | |
| 30 | # Switch to the branch that we're cherry-picking into |
| 31 | git checkout -B patch/${BRANCH}/${PATCH_ID} origin/${BRANCH} |
| 32 | |
| 33 | # Cherry-pick the change |
| 34 | if [[ ${FORMAT} == "gerrit" ]]; then |
| 35 | LATEST_REV=$(git ls-remote https://opendev.org/openstack/${PROJECT} | grep -E "refs/changes/[[:digit:]]+/${PATCH_ID}/" | sort -t / -k 5 -g | tail -n1 | awk '{print $2}') |
| 36 | git fetch https://review.opendev.org/openstack/${PROJECT} ${LATEST_REV} && git cherry-pick FETCH_HEAD |
| 37 | elif [[ ${FORMAT} == "github" ]]; then |
| 38 | gh pr checkout --branch patch/${BRANCH}/${PATCH_ID} -f ${PATCH} |
| 39 | fi |
| 40 | |
| 41 | # Push this branch to the remote |
| 42 | git push -u origin patch/${BRANCH}/${PATCH_ID} |
| 43 | |
| 44 | # Create a PR for this change |
| 45 | gh repo set-default vexxhost/${1} |
| 46 | gh pr create \ |
| 47 | --title "$(git show -s --format=%s)" \ |
| 48 | --body "$(git show -s --format=%B)" \ |
| 49 | --base ${BRANCH} \ |
| 50 | --head patch/${BRANCH}/${PATCH_ID} |