blob: 295e981654d1bf824b00790c0830ad309693ebdc [file] [log] [blame]
Mohammed Naser2ea922e2023-03-01 22:36:23 +00001#!/bin/bash
2
3PROJECT=${1}
4PATCH=${2}
5BRANCH=${3:-master}
6
7if [[ ${PATCH} =~ ^https://review.opendev.org ]]; then
8 FORMAT=gerrit
9 PATCH_ID=$(echo ${PATCH} | awk -F/ '{print $NF}')
10elif [[ ${PATCH} =~ ^https://github.com ]]; then
11 FORMAT=github
12 PATCH_ID=$(echo ${PATCH} | awk -F/ '{print $NF}')
13else
14 # Exit if we don't know how to handle this patch
15 echo "Unknown patch format: ${PATCH}"
16 exit 1
17fi
18
19# Clone the repository in a temporary directory if it doesn't exist
20if [ ! -d "/tmp/vexxhost-${PROJECT}" ]; then
21 gh repo clone vexxhost/${PROJECT} /tmp/vexxhost-${PROJECT}
22fi
23
24# Switch to the repository
25cd /tmp/vexxhost-${PROJECT}
26
27# Update the repository
28git fetch origin
29
30# Switch to the branch that we're cherry-picking into
31git checkout -B patch/${BRANCH}/${PATCH_ID} origin/${BRANCH}
32
33# Cherry-pick the change
34if [[ ${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
37elif [[ ${FORMAT} == "github" ]]; then
38 gh pr checkout --branch patch/${BRANCH}/${PATCH_ID} -f ${PATCH}
39fi
40
41# Push this branch to the remote
42git push -u origin patch/${BRANCH}/${PATCH_ID}
43
44# Create a PR for this change
45gh repo set-default vexxhost/${1}
46gh 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}