Skip to content

Commit 7406f98

Browse files
authored
Merge pull request #2 from pimcore/clone_destination_branch
Added branch clone workflow
2 parents 10c51cd + 2bd3314 commit 7406f98

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Clone branch
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
base_ref:
7+
required: true
8+
type: string
9+
ref_name:
10+
required: true
11+
type: string
12+
target_repo:
13+
required: true
14+
type: string
15+
secrets:
16+
SYNC_TOKEN:
17+
required: true
18+
GIT_NAME:
19+
required: true
20+
GIT_EMAIL:
21+
required: true
22+
23+
jobs:
24+
create-pr:
25+
runs-on: ubuntu-latest
26+
env:
27+
PAT: ${{ secrets.SYNC_TOKEN }}
28+
TARGET_REPO: ${{ inputs.target_repo }}
29+
TARGET_BRANCH: ${{ inputs.base_ref }}
30+
BASE_BRANCH: ${{ inputs.ref_name }}
31+
GIT_NAME: ${{ secrets.GIT_NAME }}
32+
GIT_EMAIL: ${{ secrets.GIT_EMAIL}}
33+
steps:
34+
- name: Checkout repository CE
35+
uses: actions/checkout@v4
36+
with:
37+
ref: ${{ inputs.base_ref }}
38+
token: ${{ secrets.SYNC_TOKEN }}
39+
fetch-depth: 0
40+
41+
- name: Set up Git
42+
run: |
43+
git config user.email "$GIT_EMAIL"
44+
git config user.name "$GIT_NAME"
45+
46+
- name: Prepare base target branch PR in EE repo
47+
run: |
48+
DESTINATION_BRANCH="$TARGET_BRANCH-from-CE"
49+
echo "DESTINATION_BRANCH=$DESTINATION_BRANCH" >> $GITHUB_ENV
50+
51+
# Ensure we're working with the most recent code and branches
52+
git fetch origin
53+
git fetch https://github.com/pimcore/$TARGET_REPO.git
54+
55+
# Cleanup old remote if it exists and create a fresh remote reference
56+
git remote | grep $TARGET_REPO && git remote remove $TARGET_REPO
57+
git remote add $TARGET_REPO https://github.com/pimcore/$TARGET_REPO.git
58+
59+
# Checkout and push to the destination branch in the target repo
60+
git checkout $TARGET_BRANCH
61+
git pull
62+
git checkout -b $DESTINATION_BRANCH
63+
git push $TARGET_REPO $DESTINATION_BRANCH
64+
65+
- name: Create PR in target repo
66+
run: |
67+
# Create a PR in Repo EE
68+
REQ_BODY=$(cat << EOF
69+
{
70+
"title": "Automated PR from CE repository",
71+
"body": "This PR is automatically created from CE repository",
72+
"head": "$DESTINATION_BRANCH",
73+
"base": "$BASE_BRANCH"
74+
}
75+
EOF
76+
)
77+
78+
CREATE_PR=$(curl -X POST --location "https://api.github.com/repos/pimcore/$TARGET_REPO/pulls" \
79+
-H "X-GitHub-Api-Version: 2022-11-28" \
80+
-H "Accept: application/vnd.github+json" \
81+
-H "Authorization: Bearer $PAT" \
82+
-d "$REQ_BODY")
83+
84+
# Extract the PR URL and status code from the response
85+
PR_URL=$(echo $CREATE_PR | jq -r '.html_url')
86+
87+
# Get PR number to update labels
88+
PR_NUM=$(echo $CREATE_PR | jq -r '.number')
89+
echo "PR_NUM=$PR_NUM" >> $GITHUB_ENV
90+
91+
if [ -z "$PR_URL" ] || [ "$PR_URL" = "null" ] || [ -z "$PR_NUM" ] || [ "$PR_NUM" = "null" ]; then
92+
echo "PR_URL/PR_NUM is missing. Invalidating the step."
93+
echo "Target repository: $TARGET_REPO"
94+
echo "Base branch: $TARGET_BRANCH"
95+
echo "URL VALUE: $PR_URL"
96+
echo "Response value: $CREATE_PR"
97+
exit 1
98+
else
99+
echo "Pull Request created in Repo EE: $PR_URL" >> $GITHUB_STEP_SUMMARY
100+
echo "Pull Request number: $PR_NUM" >> $GITHUB_STEP_SUMMARY
101+
fi
102+
103+
- name: Set label value based on branch name
104+
run: |
105+
if [[ $TARGET_BRANCH == bugfix_* ]]; then
106+
echo "LABEL=bug" >> $GITHUB_ENV
107+
elif [[ $TARGET_BRANCH == improvement_* ]]; then
108+
echo "LABEL=improvement" >> $GITHUB_ENV
109+
else
110+
echo "LABEL=other" >> $GITHUB_ENV
111+
fi
112+
113+
- name: Set a label on PR
114+
continue-on-error: true
115+
run: |
116+
# Set a label for PR
117+
SET_LABEL=$(curl -s -o /dev/null -w "%{http_code}" -X POST --location "https://api.github.com/repos/pimcore/$TARGET_REPO/issues/$PR_NUM/labels" \
118+
-H "Accept: application/vnd.github+json" \
119+
-H "Authorization: Bearer $PAT" \
120+
-d "{
121+
\"labels\":[\"$LABEL\", \"ClonedPR\"]
122+
}")
123+
124+
if [[ $SET_LABEL -ne 200 ]]; then
125+
echo "Failed to add labels to PR. Please add manually..."
126+
fi

0 commit comments

Comments
 (0)