Skip to content

Commit

Permalink
Merge pull request #1 from pimcore/pr_clone
Browse files Browse the repository at this point in the history
PR clone workflow
  • Loading branch information
bluvulture authored Oct 16, 2023
2 parents 07a9b0d + 282bc0c commit 10c51cd
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* @bluvulture @cipribucur @brusch @herbertroth
*.yml @bluvulture @cipribucur @brusch
*.yaml @bluvulture @cipribucur @brusch
144 changes: 144 additions & 0 deletions .github/workflows/reusable-pr-clone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: Clone PR

on:
workflow_call:
inputs:
head_ref:
required: true
type: string
html_url:
required: true
type: string
sha:
required: true
type: string
ref_name:
required: true
type: string
target_repo:
required: true
type: string

jobs:
create-pr:
runs-on: ubuntu-latest
env:
PAT: ${{ secrets.SYNC_TOKEN }}
TARGET_REPO: ${{ inputs.target_repo }}
TARGET_BRANCH: ${{ inputs.head_ref }}
COMMIT_HASH: ${{ inputs.sha }}
BASE_BRANCH: ${{ inputs.ref_name }}
SOURCE_PR: ${{ inputs.html_url }}
GIT_NAME: ${{ secrets.GIT_NAME }}
GIT_EMAIL: ${{ secrets.GIT_EMAIL}}
steps:
- name: Checkout repository CE
uses: actions/checkout@v4
with:
ref: ${{ inputs.head_ref }}
token: ${{ secrets.SYNC_TOKEN }}
fetch-depth: 0

- name: Set up Git
run: |
git config user.email "$GIT_EMAIL"
git config user.name "$GIT_NAME"
- name: Prepare base target branch PR in EE repo
run: |
SHORT_SHA=$(git rev-parse --short "$COMMIT_HASH")
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV
DESTINATION_BRANCH="$TARGET_BRANCH-from-ce-$SHORT_SHA"
echo "DESTINATION_BRANCH=$DESTINATION_BRANCH" >> $GITHUB_ENV
# Ensure we're working with the most recent code and branches
git fetch origin
git fetch https://github.com/pimcore/$TARGET_REPO.git
# Cleanup old remote if it exists and create a fresh remote reference
git remote | grep $TARGET_REPO && git remote remove $TARGET_REPO
git remote add $TARGET_REPO https://github.com/pimcore/$TARGET_REPO.git
# Check does base target branch exists in target repo and creates it if it doesnt exist
REPO_RESPONSE=$(curl -X GET --location "https://api.github.com/repos/pimcore/$TARGET_REPO/branches/$BASE_BRANCH" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $PAT")
BASE_TARGET_BRANCH=$(echo $REPO_RESPONSE | jq -r '.name')
if [ -z "$BASE_TARGET_BRANCH" ] || [ "$BASE_TARGET_BRANCH" = "null" ]; then
echo "BASE_TARGET_BRANCH $BASE_BRANCH is missing on target. Creating..."
git checkout $BASE_BRANCH
git pull
else
echo "$BASE_BRANCH exists on target. Skipping ..."
fi
# Checkout and push to the destination branch in the target repo
git checkout $TARGET_BRANCH
git pull
git checkout -b $DESTINATION_BRANCH
git push $TARGET_REPO $DESTINATION_BRANCH
- name: Create PR in target repo
run: |
# Create a PR in Repo EE
REQ_BODY=$(cat << EOF
{
"title": "Automated PR from CE repository $SHORT_SHA",
"body": "This PR is automatically created from CE repository pull request $SOURCE_PR",
"head": "$DESTINATION_BRANCH",
"base": "$BASE_BRANCH"
}
EOF
)
CREATE_PR=$(curl -X POST --location "https://api.github.com/repos/pimcore/$TARGET_REPO/pulls" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $PAT" \
-d "$REQ_BODY")
# Extract the PR URL and status code from the response
PR_URL=$(echo $CREATE_PR | jq -r '.html_url')
# Get PR number to update labels
PR_NUM=$(echo $CREATE_PR | jq -r '.number')
echo "PR_NUM=$PR_NUM" >> $GITHUB_ENV
if [ -z "$PR_URL" ] || [ "$PR_URL" = "null" ] || [ -z "$PR_NUM" ] || [ "$PR_NUM" = "null" ]; then
echo "PR_URL/PR_NUM is missing. Invalidating the step."
echo "Target repository: $TARGET_REPO"
echo "Base branch: $TARGET_BRANCH"
echo "URL VALUE: $PR_URL"
echo "Response value: $CREATE_PR"
exit 1
else
echo "Pull Request created in Repo EE: $PR_URL"
echo "Pull Request number: $PR_NUM"
fi
- name: Set label value based on branch name
run: |
if [[ $TARGET_BRANCH == bugfix_* ]]; then
echo "LABEL=bug" >> $GITHUB_ENV
elif [[ $TARGET_BRANCH == improvement_* ]]; then
echo "LABEL=improvement" >> $GITHUB_ENV
else
echo "LABEL=other" >> $GITHUB_ENV
fi
- name: Set a label on PR
continue-on-error: true
run: |
# Set a label for PR
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" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $PAT" \
-d "{
\"labels\":[\"$LABEL\", \"ClonedPR\"]
}")
if [[ $SET_LABEL -ne 200 ]]; then
echo "Failed to add labels to PR. Please add manually..."
fi

0 comments on commit 10c51cd

Please sign in to comment.