-
Notifications
You must be signed in to change notification settings - Fork 1
181 lines (148 loc) · 5.75 KB
/
sync_prs.yml
File metadata and controls
181 lines (148 loc) · 5.75 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: PR Target Sync
on:
push:
branches:
- '*'
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
list-prs:
runs-on: ubuntu-latest
outputs:
pr_list: ${{ steps.list_prs.outputs.pr_list }}
steps:
- name: List Open PRs
id: list_prs
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PLATFORM_SYNC_COMPONENT_FORKS_TOKEN }}
script: |
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
});
const prList = pullRequests.map(pr => ({
number: pr.number,
headRef: pr.head.ref,
baseRef: pr.base.ref,
headRepo: pr.head.repo.full_name,
baseRepo: pr.base.repo.full_name,
}));
console.log(`PR List: ${JSON.stringify(prList)}`);
core.setOutput('pr_list', JSON.stringify(prList));
process-prs:
needs: list-prs
runs-on: ubuntu-latest
outputs:
forks: ${{ steps.filter_prs.outputs.forks }}
same_repo: ${{ steps.filter_prs.outputs.same_repo }}
steps:
- name: Filter PRs Targeting the Updated Branch
id: filter_prs
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PLATFORM_SYNC_COMPONENT_FORKS_TOKEN }}
script: |
const branchUpdated = context.payload.ref.replace('refs/heads/', '');
const rawPrList = `${{ needs.list-prs.outputs.pr_list }}`;
console.log(`Raw PR List: ${rawPrList}`);
let pullRequests;
try {
pullRequests = JSON.parse(rawPrList);
console.log(`Parsed PR List: ${JSON.stringify(pullRequests)}`);
} catch (error) {
console.error('Error parsing PR list:', error);
pullRequests = [];
}
if (pullRequests.length === 0) {
console.log('No PRs found in the list.');
core.setOutput('forks', '[]');
core.setOutput('same_repo', '[]');
return;
}
const prsToProcess = pullRequests.filter(pr => pr.baseRef === branchUpdated);
console.log(`PRs targeting branch ${branchUpdated}: ${JSON.stringify(prsToProcess)}`);
if (prsToProcess.length === 0) {
console.log(`No PRs found targeting the branch: ${branchUpdated}`);
core.setOutput('forks', '[]');
core.setOutput('same_repo', '[]');
return;
}
const forks = [];
const sameRepo = [];
for (const pr of prsToProcess) {
if (pr.headRepo === `${context.repo.owner}/${context.repo.repo}`) {
sameRepo.push(pr);
} else {
forks.push(pr);
}
}
console.log(`Fork PRs: ${JSON.stringify(forks)}`);
console.log(`Same-repo PRs: ${JSON.stringify(sameRepo)}`);
core.setOutput('forks', JSON.stringify(forks));
core.setOutput('same_repo', JSON.stringify(sameRepo));
sync-forks:
needs: process-prs
if: needs.process-prs.outputs.forks != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
pr: ${{ fromJson(needs.process-prs.outputs.forks || '[]') }}
steps:
- name: Checkout Fork Repository
uses: actions/checkout@v4
with:
repository: ${{ matrix.pr.headRepo }}
ref: ${{ matrix.pr.headRef }}
fetch-depth: 0
token: ${{ secrets.PLATFORM_SYNC_COMPONENT_FORKS_TOKEN }}
- name: Add Upstream Remote and Fetch All Branches
run: |
git remote add upstream https://x-access-token:${{ secrets.PLATFORM_SYNC_COMPONENT_FORKS_TOKEN }}@github.com/${{ matrix.pr.baseRepo }}.git
git fetch --all --prune
- name: Configure Committer Identity
run: |
git config --global user.email "teamcoreexternal@ccpgames.com"
git config --global user.name "Platform Automation"
- name: Merge Parent Branch into Fork Branch
run: |
echo "Merging base branch '${{ matrix.pr.baseRef }}' into fork branch '${{ matrix.pr.headRef }}'."
git checkout ${{ matrix.pr.headRef }}
git merge upstream/${{ matrix.pr.baseRef }} --no-edit
git push origin ${{ matrix.pr.headRef }}
- name: Log Merge Result
run: |
echo "Successfully merged '${{ matrix.pr.baseRef }}' from upstream into '${{ matrix.pr.headRef }}' in fork '${{ matrix.pr.headRepo }}'."
sync-same-repo:
needs: process-prs
if: needs.process-prs.outputs.same_repo != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
pr: ${{ fromJson(needs.process-prs.outputs.same_repo || '[]') }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch All Remote Branches
run: |
git fetch --all --prune
- name: Configure Committer Identity
run: |
git config --global user.email "teamcoreexternal@ccpgames.com"
git config --global user.name "Platform Automation"
- name: Merge Parent Branch into Child Branch
run: |
echo "Merging parent branch '${{ matrix.pr.baseRef }}' into child branch '${{ matrix.pr.headRef }}'."
git checkout ${{ matrix.pr.headRef }}
git merge origin/${{ matrix.pr.baseRef }} --no-edit
git push origin ${{ matrix.pr.headRef }}
- name: Log Merge Result
run: |
echo "Successfully merged '${{ matrix.pr.baseRef }}' into '${{ matrix.pr.headRef }}'."