name: Sync external PR branch to public repository on: pull_request_target: branches: - amd-staging jobs: git-mirror: runs-on: ubuntu-latest if: github.event.pull_request.head.repo.full_name == 'AMD-ROCm-Internal/aqlprofile' && contains(github.event.pull_request.head.ref, 'external-pr') steps: - name: Get Current PR Body id: current_pr run: | # Use the PR body directly from the event payload # This is the body as it was when the 'opened' or 'edited' event was triggered. RAW_PR_BODY="${{ github.event.pull_request.body }}" # Handle cases where the body might be null (e.g., an empty PR description) # In bash, an unset or null variable in quotes becomes an empty string, # but it's good practice to be explicit or test. # If RAW_PR_BODY is null from the JSON payload, it will be treated as an empty string here by bash. # For more robust null handling if needed elsewhere: PR_BODY_FOR_SCRIPT="${RAW_PR_BODY:-}" PR_BODY_FOR_SCRIPT="$RAW_PR_BODY" echo "PR Body from event payload (first 500 chars):" echo "${PR_BODY_FOR_SCRIPT:0:500}" # Print a snippet for logging echo "-------------------" # If you need to pass this body to subsequent steps via GITHUB_OUTPUT, # the multiline escaping is still crucial. ESCAPED_PR_BODY="${RAW_PR_BODY//'%'/'%25'}" ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\n'/'%0A'}" ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\r'/'%0D'}" echo "PR_BODY_CONTENT<> $GITHUB_OUTPUT echo "$ESCAPED_PR_BODY" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT - name: Extract Remote PR URL and Info id: remote_pr_info run: | PR_BODY="${{ steps.current_pr.outputs.PR_BODY_CONTENT }}" echo "Current PR Body:" echo "${PR_BODY}" echo "-------------------" # Regex to find GitHub PR URLs. This is a common pattern. # It captures owner, repo, and pr_number. REMOTE_PR_URL_REGEX="https://github.com/([^/]+)/([^/]+)/pull/([0-9]+)" if [[ "$PR_BODY" =~ $REMOTE_PR_URL_REGEX ]]; then REMOTE_PR_URL="${BASH_REMATCH[0]}" REMOTE_OWNER="${BASH_REMATCH[1]}" REMOTE_REPO="${BASH_REMATCH[2]}" REMOTE_PR_NUMBER="${BASH_REMATCH[3]}" echo "Found Remote PR URL: $REMOTE_PR_URL" echo "Remote Owner: $REMOTE_OWNER" echo "Remote Repo: $REMOTE_REPO" echo "Remote PR Number: $REMOTE_PR_NUMBER" echo "REMOTE_PR_URL=$REMOTE_PR_URL" >> $GITHUB_OUTPUT echo "REMOTE_OWNER=$REMOTE_OWNER" >> $GITHUB_OUTPUT echo "REMOTE_REPO=$REMOTE_REPO" >> $GITHUB_OUTPUT echo "REMOTE_PR_NUMBER=$REMOTE_PR_NUMBER" >> $GITHUB_OUTPUT echo "FOUND_URL=true" >> $GITHUB_OUTPUT else echo "::warning::No GitHub PR URL found in the current PR body." echo "FOUND_URL=false" >> $GITHUB_OUTPUT fi - name: Fetch Remote PR Branch Name if: steps.remote_pr_info.outputs.FOUND_URL == 'true' id: remote_pr_branch env: GH_TOKEN: ${{ secrets.EXT_TOKEN }} REMOTE_OWNER: ${{ steps.remote_pr_info.outputs.REMOTE_OWNER }} REMOTE_REPO: ${{ steps.remote_pr_info.outputs.REMOTE_REPO }} REMOTE_PR_NUMBER: ${{ steps.remote_pr_info.outputs.REMOTE_PR_NUMBER }} run: | if [ -n "$GH_TOKEN" ]; then echo "Using provided TOKEN." ACTUAL_GH_TOKEN="$GH_TOKEN" else echo "::error::No token available." exit 1 fi echo "Fetching branch name for $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER" REMOTE_BRANCH_NAME=$(GH_TOKEN="$ACTUAL_GH_TOKEN" gh pr view "$REMOTE_PR_NUMBER" \ --repo "$REMOTE_OWNER/$REMOTE_REPO" \ --json headRefName --jq .headRefName) if [ -n "$REMOTE_BRANCH_NAME" ]; then echo "Remote PR Branch Name: $REMOTE_BRANCH_NAME" echo "REMOTE_BRANCH_NAME=$REMOTE_BRANCH_NAME" >> $GITHUB_OUTPUT else echo "::error::Could not retrieve branch name for remote PR $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER. Check PAT permissions or PR validity." # Optionally exit 1 if this is critical # exit 1 fi - name: Output Results if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME run: | echo "Successfully retrieved branch name from remote PR." echo "Remote PR URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}" echo "Remote PR Branch Name: ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}" # You can now use ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }} in subsequent steps # For example, create an artifact, comment on PR A, trigger another workflow, etc. - name: Handle No URL Found if: steps.remote_pr_info.outputs.FOUND_URL == 'false' run: | echo "No remote PR URL was found in the body of PR #${{ github.event.pull_request.number }}." - name: Handle Branch Not Found if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && !steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME run: | echo "A remote PR URL was found, but its branch name could not be retrieved." echo "URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}" - name: Sync if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME uses: AMD-ROCm-Internal/rocprofiler-github-actions@git-sync-v3 with: source_repo: "https://${{ secrets.TOKEN }}@github.com/AMD-ROCm-Internal/aqlprofile.git" source_branch: "${{ github.event.pull_request.head.ref }}" destination_repo: "https://${{ secrets.EXT_TOKEN }}@github.com/ROCm/aqlprofile.git" destination_branch: "${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}"