@@ -21,8 +21,7 @@ permissions:
21
21
jobs :
22
22
mutate :
23
23
runs-on : ubuntu-latest
24
- # Run if the workflow run is a pull request
25
- if : github.event.workflow_run.conclusion == 'failure' && (!contains(fromJSON('["main", "dev"]'), github.event.workflow_run.head_branch) || github.event.workflow_run.head_repository.fork)
24
+ if : github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && (!contains(fromJSON('["main", "dev"]'), github.event.workflow_run.head_branch) || github.event.workflow_run.head_repository.fork)
26
25
steps :
27
26
- name : Download artifacts
28
27
id : download-artifacts
@@ -57,48 +56,78 @@ jobs:
57
56
run : |
58
57
git config --global core.hooksPath /dev/null
59
58
59
+ - name : Update PR Branch
60
+ uses : actions/github-script@v6
61
+ if : github.event.workflow_run.event == 'pull_request' && steps.download-artifacts.outputs.found_artifact == 'true'
62
+ with :
63
+ github-token : ${{ secrets.MUTATION_TOKEN }}
64
+ script : |
65
+ // use API to get the PR data since we can't rely on the context across forks
66
+ const pulls = await github.rest.pulls.list({
67
+ per_page: 1,
68
+ owner: context.repo.owner,
69
+ repo: context.repo.repo,
70
+ head: `${context.payload.workflow_run.head_repository.full_name}:${context.payload.workflow_run.head_branch}`
71
+ });
72
+
73
+ const prContextData = pulls.data[0];
74
+ const prNumber = prContextData.number;
75
+ const originalSha = prContextData.head.sha;
76
+
77
+ try {
78
+ console.log("Updating PR branch");
79
+ await github.rest.pulls.updateBranch({
80
+ owner: context.repo.owner,
81
+ repo: context.repo.repo,
82
+ pull_number: prNumber
83
+ });
84
+ console.log("PR branch updated");
85
+
86
+ let updatedSha = originalSha;
87
+ let retries = 0;
88
+ const MAX_RETRIES = 10;
89
+ while (updatedSha == originalSha && retries++ < MAX_RETRIES) {
90
+ console.log(`Waiting for PR branch to update (attempt ${retries}/${MAX_RETRIES})`);
91
+
92
+ await new Promise(r => setTimeout(r, 500));
93
+ const updatedPR = await github.rest.pulls.get({
94
+ owner: context.repo.owner,
95
+ repo: context.repo.repo,
96
+ pull_number: prNumber
97
+ });
98
+ updatedSha = updatedPR.data.head.sha;
99
+ }
100
+ } catch (error) {
101
+ // The branch is already up to date or can't otherwise be updated
102
+ // That's fine, we tried our best
103
+ console.warn(error);
104
+ }
105
+
60
106
- name : Checkout Workflow Branch
61
107
if : steps.download-artifacts.outputs.found_artifact == 'true'
62
108
uses : actions/checkout@v3
63
109
with :
64
- token : ${{secrets.MUTATION_TOKEN}}
110
+ token : ${{ secrets.MUTATION_TOKEN }}
65
111
ref : ${{ github.event.workflow_run.head_branch }}
66
112
repository : ${{ github.event.workflow_run.head_repository.full_name }}
67
113
path : repo
68
114
69
115
- id : self_mutation
70
116
if : steps.download-artifacts.outputs.found_artifact == 'true'
71
- name : Apply downloaded pathes
117
+ name : Apply downloaded patches
72
118
working-directory : repo
73
119
env :
74
120
HEAD_REF : ${{ github.event.workflow_run.head_branch }}
75
121
run : |
76
122
git config user.name "monada-bot[bot]"
77
123
git config user.email "[email protected] "
78
124
79
- # if ../patches/update.diff/update.diff exists, apply it first
80
- UPDATE_PATCH_FILE="../patches/update.diff/update.diff"
81
- if [ -f $UPDATE_PATCH_FILE ]; then
82
- echo "Updating branch"
83
- git apply --binary $UPDATE_PATCH_FILE
84
- if [ $? -eq 0 ]; then
85
- git add --all
86
- git commit -s -m "Merge branch 'main' into $HEAD_REF"
87
- echo "Patch applied successfully"
88
- rm $UPDATE_PATCH_FILE
89
- else
90
- echo "Patch failed to apply"
91
- cat $UPDATE_PATCH_FILE
92
- exit 1
93
- fi
94
- fi
95
-
96
125
for f in $(find ../patches/*.diff/*.diff); do
97
126
echo "Applying $f"
98
127
git apply --binary $f
99
128
if [ $? -eq 0 ]; then
100
129
git add --all
101
- git commit -s -m "chore: self mutation ($f )"
130
+ git commit -s -m "chore: self mutation ($(basename $f) )"
102
131
echo "Patch applied successfully"
103
132
rm $f
104
133
else
@@ -116,12 +145,13 @@ jobs:
116
145
with :
117
146
github-token : ${{ secrets.MUTATION_TOKEN }}
118
147
script : |
148
+ // use API to get the PR number since we can't rely on the context across forks
119
149
const pulls = await github.rest.pulls.list({
150
+ per_page: 1,
120
151
owner: context.repo.owner,
121
152
repo: context.repo.repo,
122
153
head: `${context.payload.workflow_run.head_repository.full_name}:${context.payload.workflow_run.head_branch}`
123
154
});
124
-
125
155
const prNumber = pulls.data[0].number;
126
156
const labels = ["⚠️ pr/review-mutation"];
127
157
0 commit comments