Skip to content

Commit 2f771e6

Browse files
committed
Create external PR Workflow (#118)
Create external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Separating Internal and External Workflows (#119) * Separating Internal and External Workflows * Update continuous_integration.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Update external-pr.yml Create external-pr-sync.yml (#121) * Create external-pr-sync.yml * Update external-pr.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr.yml Update external-pr.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr-sync.yml Update external-pr-sync.yml Create external-pr-close.yml Fixing External PR Workflows Fixing External PR Workflows Fixing External PR Workflows
1 parent 8dad951 commit 2f771e6

File tree

4 files changed

+318
-1
lines changed

4 files changed

+318
-1
lines changed

.github/workflows/continuous_integration.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ jobs:
4747
build-type: ['RelWithDebInfo']
4848

4949
runs-on: ${{ matrix.runner }}${{ github.ref == 'refs/heads/amd-npi' && '-npi' || '' }}-emu-runner-set
50+
if: github.event.pull_request.head.repo.full_name == 'AMD-ROCm-Internal/aqlprofile'
5051

5152
permissions:
5253
contents: read
@@ -94,7 +95,7 @@ jobs:
9495
-S ./dashboard.cmake
9596

9697
core-rpm:
97-
if: github.ref != 'refs/heads/amd-npi'
98+
if: github.ref != 'refs/heads/amd-npi' && github.event.pull_request.head.repo.full_name == 'AMD-ROCm-Internal/aqlprofile'
9899
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
99100
strategy:
100101
fail-fast: false
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Close external PR
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
branches:
7+
- amd-staging
8+
9+
jobs:
10+
git-mirror:
11+
runs-on: ubuntu-latest
12+
if: github.event.pull_request.head.repo.full_name == 'AMD-ROCm-Internal/aqlprofile' && contains(github.event.pull_request.head.ref, 'external-pr')
13+
steps:
14+
- name: Get Current PR Body
15+
id: current_pr
16+
run: |
17+
# Use the PR body directly from the event payload
18+
# This is the body as it was when the 'opened' or 'edited' event was triggered.
19+
RAW_PR_BODY="${{ github.event.pull_request.body }}"
20+
21+
# Handle cases where the body might be null (e.g., an empty PR description)
22+
# In bash, an unset or null variable in quotes becomes an empty string,
23+
# but it's good practice to be explicit or test.
24+
# If RAW_PR_BODY is null from the JSON payload, it will be treated as an empty string here by bash.
25+
# For more robust null handling if needed elsewhere: PR_BODY_FOR_SCRIPT="${RAW_PR_BODY:-}"
26+
PR_BODY_FOR_SCRIPT="$RAW_PR_BODY"
27+
28+
echo "PR Body from event payload (first 500 chars):"
29+
echo "${PR_BODY_FOR_SCRIPT:0:500}" # Print a snippet for logging
30+
echo "-------------------"
31+
32+
# If you need to pass this body to subsequent steps via GITHUB_OUTPUT,
33+
# the multiline escaping is still crucial.
34+
ESCAPED_PR_BODY="${RAW_PR_BODY//'%'/'%25'}"
35+
ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\n'/'%0A'}"
36+
ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\r'/'%0D'}"
37+
38+
echo "PR_BODY_CONTENT<<EOF" >> $GITHUB_OUTPUT
39+
echo "$ESCAPED_PR_BODY" >> $GITHUB_OUTPUT
40+
echo "EOF" >> $GITHUB_OUTPUT
41+
42+
- name: Extract Remote PR URL and Info
43+
id: remote_pr_info
44+
run: |
45+
PR_BODY="${{ steps.current_pr.outputs.PR_BODY_CONTENT }}"
46+
echo "Current PR Body:"
47+
echo "${PR_BODY}"
48+
echo "-------------------"
49+
50+
# Regex to find GitHub PR URLs. This is a common pattern.
51+
# It captures owner, repo, and pr_number.
52+
REMOTE_PR_URL_REGEX="https://github.com/([^/]+)/([^/]+)/pull/([0-9]+)"
53+
54+
if [[ "$PR_BODY" =~ $REMOTE_PR_URL_REGEX ]]; then
55+
REMOTE_PR_URL="${BASH_REMATCH[0]}"
56+
REMOTE_OWNER="${BASH_REMATCH[1]}"
57+
REMOTE_REPO="${BASH_REMATCH[2]}"
58+
REMOTE_PR_NUMBER="${BASH_REMATCH[3]}"
59+
60+
echo "Found Remote PR URL: $REMOTE_PR_URL"
61+
echo "Remote Owner: $REMOTE_OWNER"
62+
echo "Remote Repo: $REMOTE_REPO"
63+
echo "Remote PR Number: $REMOTE_PR_NUMBER"
64+
65+
echo "REMOTE_PR_URL=$REMOTE_PR_URL" >> $GITHUB_OUTPUT
66+
echo "REMOTE_OWNER=$REMOTE_OWNER" >> $GITHUB_OUTPUT
67+
echo "REMOTE_REPO=$REMOTE_REPO" >> $GITHUB_OUTPUT
68+
echo "REMOTE_PR_NUMBER=$REMOTE_PR_NUMBER" >> $GITHUB_OUTPUT
69+
echo "FOUND_URL=true" >> $GITHUB_OUTPUT
70+
else
71+
echo "::warning::No GitHub PR URL found in the current PR body."
72+
echo "FOUND_URL=false" >> $GITHUB_OUTPUT
73+
fi
74+
75+
- name: Output Results
76+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true'
77+
run: |
78+
echo "Successfully retrieved branch info from remote PR."
79+
echo "Remote PR URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}"
80+
81+
- name: Handle No URL Found
82+
if: steps.remote_pr_info.outputs.FOUND_URL == 'false'
83+
run: |
84+
echo "No remote PR URL was found in the body of PR #${{ github.event.pull_request.number }}."
85+
86+
- name: Comment on and Close External PR
87+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true'
88+
env:
89+
GH_TOKEN: ${{ secrets.EXT_TOKEN }}
90+
EXTERNAL_PR_NUMBER: ${{ steps.remote_pr_info.outputs.REMOTE_PR_NUMBER }}
91+
EXTERNAL_REPO: "ROCm/aqlprofile"
92+
run: |
93+
COMMENT_BODY="This pull request has been closed in the internal repository. Thank you for your contribution!"
94+
95+
gh pr comment "$EXTERNAL_PR_NUMBER" \
96+
--repo "$EXTERNAL_REPO" \
97+
--body "$COMMENT_BODY"
98+
99+
gh pr close "$EXTERNAL_PR_NUMBER" \
100+
--repo "$EXTERNAL_REPO"
101+
102+
echo "Commented on and Closed external PR #${EXTERNAL_PR_NUMBER}"
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
name: Sync external PR branch to public repository
2+
3+
on:
4+
pull_request_target:
5+
branches:
6+
- amd-staging
7+
8+
jobs:
9+
git-mirror:
10+
runs-on: ubuntu-latest
11+
if: github.event.pull_request.head.repo.full_name == 'AMD-ROCm-Internal/aqlprofile' && contains(github.event.pull_request.head.ref, 'external-pr')
12+
steps:
13+
- name: Get Current PR Body
14+
id: current_pr
15+
run: |
16+
# Use the PR body directly from the event payload
17+
# This is the body as it was when the 'opened' or 'edited' event was triggered.
18+
RAW_PR_BODY="${{ github.event.pull_request.body }}"
19+
20+
# Handle cases where the body might be null (e.g., an empty PR description)
21+
# In bash, an unset or null variable in quotes becomes an empty string,
22+
# but it's good practice to be explicit or test.
23+
# If RAW_PR_BODY is null from the JSON payload, it will be treated as an empty string here by bash.
24+
# For more robust null handling if needed elsewhere: PR_BODY_FOR_SCRIPT="${RAW_PR_BODY:-}"
25+
PR_BODY_FOR_SCRIPT="$RAW_PR_BODY"
26+
27+
echo "PR Body from event payload (first 500 chars):"
28+
echo "${PR_BODY_FOR_SCRIPT:0:500}" # Print a snippet for logging
29+
echo "-------------------"
30+
31+
# If you need to pass this body to subsequent steps via GITHUB_OUTPUT,
32+
# the multiline escaping is still crucial.
33+
ESCAPED_PR_BODY="${RAW_PR_BODY//'%'/'%25'}"
34+
ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\n'/'%0A'}"
35+
ESCAPED_PR_BODY="${ESCAPED_PR_BODY//$'\r'/'%0D'}"
36+
37+
echo "PR_BODY_CONTENT<<EOF" >> $GITHUB_OUTPUT
38+
echo "$ESCAPED_PR_BODY" >> $GITHUB_OUTPUT
39+
echo "EOF" >> $GITHUB_OUTPUT
40+
41+
- name: Extract Remote PR URL and Info
42+
id: remote_pr_info
43+
run: |
44+
PR_BODY="${{ steps.current_pr.outputs.PR_BODY_CONTENT }}"
45+
echo "Current PR Body:"
46+
echo "${PR_BODY}"
47+
echo "-------------------"
48+
49+
# Regex to find GitHub PR URLs. This is a common pattern.
50+
# It captures owner, repo, and pr_number.
51+
REMOTE_PR_URL_REGEX="https://github.com/([^/]+)/([^/]+)/pull/([0-9]+)"
52+
53+
if [[ "$PR_BODY" =~ $REMOTE_PR_URL_REGEX ]]; then
54+
REMOTE_PR_URL="${BASH_REMATCH[0]}"
55+
REMOTE_OWNER="${BASH_REMATCH[1]}"
56+
REMOTE_REPO="${BASH_REMATCH[2]}"
57+
REMOTE_PR_NUMBER="${BASH_REMATCH[3]}"
58+
59+
echo "Found Remote PR URL: $REMOTE_PR_URL"
60+
echo "Remote Owner: $REMOTE_OWNER"
61+
echo "Remote Repo: $REMOTE_REPO"
62+
echo "Remote PR Number: $REMOTE_PR_NUMBER"
63+
64+
echo "REMOTE_PR_URL=$REMOTE_PR_URL" >> $GITHUB_OUTPUT
65+
echo "REMOTE_OWNER=$REMOTE_OWNER" >> $GITHUB_OUTPUT
66+
echo "REMOTE_REPO=$REMOTE_REPO" >> $GITHUB_OUTPUT
67+
echo "REMOTE_PR_NUMBER=$REMOTE_PR_NUMBER" >> $GITHUB_OUTPUT
68+
echo "FOUND_URL=true" >> $GITHUB_OUTPUT
69+
else
70+
echo "::warning::No GitHub PR URL found in the current PR body."
71+
echo "FOUND_URL=false" >> $GITHUB_OUTPUT
72+
fi
73+
74+
- name: Fetch Remote PR Branch Name
75+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true'
76+
id: remote_pr_branch
77+
env:
78+
GH_TOKEN: ${{ secrets.EXT_TOKEN }}
79+
REMOTE_OWNER: ${{ steps.remote_pr_info.outputs.REMOTE_OWNER }}
80+
REMOTE_REPO: ${{ steps.remote_pr_info.outputs.REMOTE_REPO }}
81+
REMOTE_PR_NUMBER: ${{ steps.remote_pr_info.outputs.REMOTE_PR_NUMBER }}
82+
run: |
83+
if [ -n "$GH_TOKEN" ]; then
84+
echo "Using provided TOKEN."
85+
ACTUAL_GH_TOKEN="$GH_TOKEN"
86+
else
87+
echo "::error::No token available."
88+
exit 1
89+
fi
90+
91+
echo "Fetching branch name for $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER"
92+
REMOTE_BRANCH_NAME=$(GH_TOKEN="$ACTUAL_GH_TOKEN" gh pr view "$REMOTE_PR_NUMBER" \
93+
--repo "$REMOTE_OWNER/$REMOTE_REPO" \
94+
--json headRefName --jq .headRefName)
95+
96+
if [ -n "$REMOTE_BRANCH_NAME" ]; then
97+
echo "Remote PR Branch Name: $REMOTE_BRANCH_NAME"
98+
echo "REMOTE_BRANCH_NAME=$REMOTE_BRANCH_NAME" >> $GITHUB_OUTPUT
99+
else
100+
echo "::error::Could not retrieve branch name for remote PR $REMOTE_OWNER/$REMOTE_REPO/pull/$REMOTE_PR_NUMBER. Check PAT permissions or PR validity."
101+
# Optionally exit 1 if this is critical
102+
# exit 1
103+
fi
104+
105+
- name: Output Results
106+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
107+
run: |
108+
echo "Successfully retrieved branch name from remote PR."
109+
echo "Remote PR URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}"
110+
echo "Remote PR Branch Name: ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}"
111+
# You can now use ${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }} in subsequent steps
112+
# For example, create an artifact, comment on PR A, trigger another workflow, etc.
113+
114+
- name: Handle No URL Found
115+
if: steps.remote_pr_info.outputs.FOUND_URL == 'false'
116+
run: |
117+
echo "No remote PR URL was found in the body of PR #${{ github.event.pull_request.number }}."
118+
119+
- name: Handle Branch Not Found
120+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && !steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
121+
run: |
122+
echo "A remote PR URL was found, but its branch name could not be retrieved."
123+
echo "URL: ${{ steps.remote_pr_info.outputs.REMOTE_PR_URL }}"
124+
125+
- name: Sync
126+
if: steps.remote_pr_info.outputs.FOUND_URL == 'true' && steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME
127+
uses: AMD-ROCm-Internal/rocprofiler-github-actions@git-sync-v3
128+
with:
129+
source_repo: "https://${{ secrets.TOKEN }}@github.com/AMD-ROCm-Internal/aqlprofile.git"
130+
source_branch: "${{ github.event.pull_request.head.ref }}"
131+
destination_repo: "https://${{ secrets.EXT_TOKEN }}@github.com/ROCm/aqlprofile.git"
132+
destination_branch: "${{ steps.remote_pr_branch.outputs.REMOTE_BRANCH_NAME }}"

.github/workflows/external-pr.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Mirror External PR to Internal Repo
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, synchronize]
6+
branches:
7+
- amd-staging
8+
9+
jobs:
10+
mirror_pr:
11+
runs-on: ubuntu-latest
12+
if: github.event.pull_request.head.repo.full_name != 'AMD-ROCm-Internal/aqlprofile'
13+
14+
steps:
15+
- name: Checkout PR base
16+
uses: actions/checkout@v4
17+
18+
- name: Setup Git User
19+
run: |
20+
git config --global user.name 'External PR Mirror Bot'
21+
git config --global user.email 'bot@users.noreply.github.com'
22+
23+
- uses: wei/git-sync@v3
24+
name: Push branch to Internal Repository
25+
with:
26+
source_repo: "https://${{ secrets.EXTERNAL_REPO_TOKEN }}@github.com/ROCm/aqlprofile.git"
27+
source_branch: "${{ github.event.pull_request.head.ref }}"
28+
destination_repo: "https://${{ secrets.INTERNAL_REPO_TOKEN }}@github.com/AMD-ROCm-Internal/aqlprofile.git"
29+
destination_branch: "external-pr/${{ github.event.pull_request.number }}"
30+
31+
- name: Create Pull Request in Internal Repository
32+
if: ${{ github.event.action == 'opened' }}
33+
id: create_internal_pr
34+
env:
35+
GH_TOKEN: ${{ secrets.INTERNAL_REPO_TOKEN }}
36+
INTERNAL_REPO: "AMD-ROCm-Internal/aqlprofile"
37+
INTERNAL_BASE_BRANCH: "amd-staging"
38+
HEAD_BRANCH: "external-pr/${{ github.event.pull_request.number }}"
39+
PR_TITLE: "Mirror: ${{ github.event.pull_request.title }} (Ext PR #${{ github.event.pull_request.number }})"
40+
PR_BODY: |
41+
This PR mirrors changes from external pull request: ${{ github.event.pull_request.html_url }}
42+
43+
Original PR Body:
44+
-------------------
45+
${{ github.event.pull_request.body }}
46+
run: |
47+
# Create PR and capture its URL
48+
INTERNAL_PR_URL=$(gh pr create \
49+
--repo "$INTERNAL_REPO" \
50+
--base "$INTERNAL_BASE_BRANCH" \
51+
--head "$HEAD_BRANCH" \
52+
--title "$PR_TITLE" \
53+
--body "$PR_BODY")
54+
55+
if [ -z "$INTERNAL_PR_URL" ]; then
56+
echo "Failed to create internal PR. URL is empty."
57+
# Check if PR already exists (gh pr create might not fail if branch has open PR)
58+
EXISTING_PR_URL=$(gh pr list --repo "$INTERNAL_REPO" --head "$HEAD_BRANCH" --json url -q '.[0].url')
59+
if [ -n "$EXISTING_PR_URL" ]; then
60+
echo "Internal PR already exists: $EXISTING_PR_URL"
61+
echo "INTERNAL_PR_URL=$EXISTING_PR_URL" >> $GITHUB_OUTPUT
62+
else
63+
echo "::error::Failed to create or find existing internal PR."
64+
exit 1
65+
fi
66+
fi
67+
68+
69+
- name: Comment on and Close External PR
70+
if: steps.create_internal_pr.outputs.INTERNAL_PR_URL != ''
71+
env:
72+
GH_TOKEN: ${{ secrets.EXTERNAL_REPO_TOKEN }}
73+
EXTERNAL_PR_NUMBER: ${{ github.event.pull_request.number }}
74+
EXTERNAL_REPO: ${{ github.repository }}
75+
run: |
76+
COMMENT_BODY="This pull request has been mirrored to our internal repository for review and integration. Thank you for your contribution!"
77+
78+
gh pr comment "$EXTERNAL_PR_NUMBER" \
79+
--repo "$EXTERNAL_REPO" \
80+
--body "$COMMENT_BODY"
81+
82+
echo "Commented on external PR #${EXTERNAL_PR_NUMBER}"

0 commit comments

Comments
 (0)