Skip to content

Create Mirror PR (Current) #2

Create Mirror PR (Current)

Create Mirror PR (Current) #2

name: Create Mirror PR (Current)
on:
workflow_call:
workflow_dispatch:
inputs:
pr_title:
description: 'Title of the PR'
required: false
pr_number:
description: 'Number of the PR'
required: false
env:
GH_TOKEN: ${{ secrets.PAT }}
jobs:
Mirror-PR:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set vars
run: |
source_repo="${{ github.repository }}"
echo "source_repo=${source_repo}" >> $GITHUB_ENV
target_repo=$(echo $source_repo | sed 's/^[^\/]*\//vyos-networks\//')
echo "target_repo=${target_repo}" >> $GITHUB_ENV
pr_source_branch=$(git branch --show-current)
echo "pr_source_branch=${pr_source_branch}" >> $GITHUB_ENV
echo "pr_target_branch=${pr_source_branch}" >> $GITHUB_ENV
pr_number=${{ github.event_name == 'workflow_dispatch' && github.event.inputs.pr_number || github.event.pull_request.number }}
echo "pr_number=${pr_number}" >> $GITHUB_ENV
pr_title="${{ github.event_name == 'workflow_dispatch' && github.event.inputs.pr_title || github.event.pull_request.title }}"
echo "pr_title=${pr_title}" >> $GITHUB_ENV
echo "pr_merge_branch=mirror/current/$pr_number" >> $GITHUB_ENV
- name: Get PR last commit
run: |
mkdir -p /tmp/public
cd /tmp/public
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
git init
git remote add origin https://github.com/${source_repo}.git
git fetch
git checkout -b $pr_source_branch
pr_info=$(gh pr view $pr_number --json commits,mergeCommit)
last_commit=$(echo $pr_info | jq -r '.commits[-1].oid')
merge_commit=$(echo $pr_info | jq -r '.mergeCommit.oid')
merge_commit_message=$(git log -1 --pretty=%B $merge_commit)
if [[ "$merge_commit_message" == "Merge pull request #${pr_number} from"* ]]; then
echo "last_commit=${last_commit}" >> $GITHUB_ENV
else
echo "last_commit=${merge_commit}" >> $GITHUB_ENV
fi
- name: Create merge branch from last commit
run: |
cd /tmp/public
git checkout -b $pr_merge_branch $last_commit
- name: Push merge branch to target repo
run: |
cd /tmp/public
git remote add target https://x-access-token:${{ secrets.PAT }}@github.com/${target_repo}.git
git push target $pr_merge_branch
- name: Create PR from merge branch to target branch
run: |
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
title="${pr_title} (Mirrored from ${source_repo} PR ${pr_number})"
body="${pr_title}<br/>**Note**: This pull request is mirrored from PR [$pr_number](https://github.com/${source_repo}/pull/${pr_number}) using [workflow run](${run_url})."
gh pr create --repo ${target_repo} --head $pr_merge_branch --base $pr_target_branch --title "${title}" --body "${body}"