forked from vyos/vyos-1x
-
Notifications
You must be signed in to change notification settings - Fork 0
113 lines (102 loc) · 4.36 KB
/
mirror-pr-and-sync-current.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: Create Mirror PR and Repo Sync (Current)
on:
pull_request_target:
types:
- closed
branches:
- current
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 }}
concurrency:
group: mirror-pr-repo-sync
cancel-in-progress: false
jobs:
mirror-pr-repo-sync:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Bullfrog Secure Runner
uses: bullfrogsec/bullfrog@v0
with:
egress-policy: audit
- 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 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)
echo "merge_commit=${merge_commit}" >> $GITHUB_ENV
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: Get PR body
run: |
cd /tmp/public
pr_body=$(gh pr view $pr_number --json body -q .body)
echo "pr_body<<EOF" >> $GITHUB_ENV
echo "$pr_body" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Create temp remote branch
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 remote PR
run: |
run_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
title="${pr_title} (mirror ${pr_number})"
echo "**Note**: This pull request is mirrored from PR [$pr_number](https://github.com/${source_repo}/pull/${pr_number}) using [workflow run](${run_url})." > pr_body.txt
echo "" >> pr_body.txt
echo "${pr_body}" >> pr_body.txt
gh pr create --repo ${target_repo} --head $pr_merge_branch --base $pr_target_branch --title "${title}" --body "$(cat pr_body.txt)"
- name: Trigger repo sync
shell: bash
run: |
echo "Triggering sync workflow for ${{ secrets.REMOTE_OWNER }}/${{ secrets.REMOTE_REPO }}"
echo "Triggering sync workflow with PAT ${{ secrets.PAT }}"
curl -X POST \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Authorization: Bearer ${{ secrets.PAT }}" \
https://api.github.com/repos/${{ secrets.REMOTE_OWNER }}/${{ secrets.REMOTE_REPO }}/actions/workflows/repo-sync-current.yml/dispatches \
-d '{"ref":"git-actions", "inputs": {"source_repo": "${{ env.source_repo }}", "source_commit_sha": "${{ env.merge_commit }}", "destination_branch": "${{ env.pr_target_branch }}"}}'