Skip to content

Commit 57b63ed

Browse files
authored
Create external-pr-sync.yml (#121)
* Create external-pr-sync.yml * Update external-pr.yml
1 parent 99c7811 commit 57b63ed

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
name: Sync external PR branch to public repository
2+
3+
on:
4+
pull_request_target:
5+
types: [synchronize]
6+
branches: [ external-pr-* ]
7+
8+
jobs:
9+
git-mirror:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Get Current PR Body
13+
id: current_pr
14+
run: |
15+
# Fetching fresh PR body via gh cli is more reliable than event payload for 'edited'
16+
PR_BODY=$(gh pr view ${{ github.event.pull_request.number }} --json body --jq .body)
17+
# Make body safe for multiline output
18+
PR_BODY="${PR_BODY//'%'/'%25'}"
19+
PR_BODY="${PR_BODY//$'\n'/'%0A'}"
20+
PR_BODY="${PR_BODY//$'\r'/'%0D'}"
21+
echo "PR_BODY_CONTENT<<EOF" >> $GITHUB_OUTPUT
22+
echo "$PR_BODY" >> $GITHUB_OUTPUT
23+
echo "EOF" >> $GITHUB_OUTPUT
24+
25+
- name: Extract Remote PR URL and Info
26+
id: remote_pr_info
27+
run: |
28+
PR_BODY="${{ steps.current_pr.outputs.PR_BODY_CONTENT }}"
29+
echo "Current PR Body:"
30+
echo "${PR_BODY}"
31+
echo "-------------------"
32+
33+
# Regex to find GitHub PR URLs. This is a common pattern.
34+
# It captures owner, repo, and pr_number.
35+
REMOTE_PR_URL_REGEX="https://github.com/([^/]+)/([^/]+)/pull/([0-9]+)"
36+
37+
if [[ "$PR_BODY" =~ $REMOTE_PR_URL_REGEX ]]; then
38+
REMOTE_PR_URL="${BASH_REMATCH[0]}"
39+
REMOTE_OWNER="${BASH_REMATCH[1]}"
40+
REMOTE_REPO="${BASH_REMATCH[2]}"
41+
REMOTE_PR_NUMBER="${BASH_REMATCH[3]}"
42+
43+
echo "Found Remote PR URL: $REMOTE_PR_URL"
44+
echo "Remote Owner: $REMOTE_OWNER"
45+
echo "Remote Repo: $REMOTE_REPO"
46+
echo "Remote PR Number: $REMOTE_PR_NUMBER"
47+
48+
echo "REMOTE_PR_URL=$REMOTE_PR_URL" >> $GITHUB_OUTPUT
49+
echo "REMOTE_OWNER=$REMOTE_OWNER" >> $GITHUB_OUTPUT
50+
echo "REMOTE_REPO=$REMOTE_REPO" >> $GITHUB_OUTPUT
51+
echo "REMOTE_PR_NUMBER=$REMOTE_PR_NUMBER" >> $GITHUB_OUTPUT
52+
echo "FOUND_URL=true" >> $GITHUB_OUTPUT
53+
else
54+
echo "::warning::No GitHub PR URL found in the current PR body."
55+
echo "FOUND_URL=false" >> $GITHUB_OUTPUT
56+
fi
57+
58+
- name: Fetch Remote PR Branch Name
59+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true'
60+
id: remote_pr_branch
61+
env:
62+
GH_TOKEN: ${{ secrets.EXT_TOKEN }}
63+
REMOTE_OWNER: ${{ steps.remote_pr_info.outputs.REMOTE_OWNER }}
64+
REMOTE_REPO: ${{ steps.remote_pr_info.outputs.REMOTE_REPO }}
65+
REMOTE_PR_NUMBER: ${{ steps.remote_pr_info.outputs.REMOTE_PR_NUMBER }}
66+
run: |
67+
if [ -n "$GH_TOKEN" ]; then
68+
echo "Using provided TOKEN."
69+
ACTUAL_GH_TOKEN="$GH_TOKEN"
70+
else
71+
echo "::error::No token available."
72+
exit 1
73+
fi
74+
75+
echo "Fetching branch name for $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER"
76+
REMOTE_BRANCH_NAME=$(GH_TOKEN="$ACTUAL_GH_TOKEN" gh pr view "$REMOTE_PR_NUMBER" \
77+
--repo "$REMOTE_OWNER/$REMOTE_REPO" \
78+
--json headRefName --jq .headRefName)
79+
80+
if [ -n "$REMOTE_BRANCH_NAME" ]; then
81+
echo "Remote PR Branch Name: $REMOTE_BRANCH_NAME"
82+
echo "REMOTE_BRANCH_NAME=$REMOTE_BRANCH_NAME" >> $GITHUB_OUTPUT
83+
else
84+
echo "::error::Could not retrieve branch name for remote PR $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER. Check PAT permissions or PR validity."
85+
# Optionally exit 1 if this is critical
86+
# exit 1
87+
fi
88+
89+
- name: Output Results
90+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
91+
run: |
92+
echo "Successfully retrieved branch name from remote PR."
93+
echo "Remote PR URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}"
94+
echo "Remote PR Branch Name: ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}"
95+
# You can now use ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }} in subsequent steps
96+
# For example, create an artifact, comment on PR A, trigger another workflow, etc.
97+
98+
- name: Handle No URL Found
99+
if: steps.remote_pr_info.outputs.FOUND_URL == 'false'
100+
run: |
101+
echo "No remote PR URL was found in the body of PR #${{ github.event.pull_request.number }}."
102+
103+
- name: Handle Branch Not Found
104+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && !steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
105+
run: |
106+
echo "A remote PR URL was found, but its branch name could not be retrieved."
107+
echo "URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}"
108+
109+
- name: Sync
110+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
111+
uses: AMD-ROCm-Internal/rocprofiler-github-actions@git-sync-v3
112+
with:
113+
source_repo: "https://${{ secrets.TOKEN }}@github.com/AMD-ROCm-Internal/aqlprofile.git"
114+
source_branch: "${{ github.event.pull_request.head.ref }}"
115+
destination_repo: "https://${{ secrets.EXT_TOKEN }}@github.com/ROCm/aqlprofile.git"
116+
destination_branch: "${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}"

.github/workflows/external-pr.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: Mirror External PR to Internal Repo
22

33
on:
4-
pull_request_target: # IMPORTANT: Use pull_request_target for security
5-
types: [opened, synchronize]
4+
pull_request_target:
5+
types: [opened]
66
branches:
77
- amd-staging
88

0 commit comments

Comments
 (0)