forked from winglang/wing
-
Notifications
You must be signed in to change notification settings - Fork 0
163 lines (144 loc) · 6.01 KB
/
mutation.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
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
name: "Pull Request Mutation"
run-name: "Pull Request Mutation: ${{ github.event.workflow_run.head_branch }}"
on:
workflow_run:
workflows:
- Build
types:
- completed
concurrency:
group: "mutation-${{ github.event.workflow_run.head_branch }}"
cancel-in-progress: true
permissions:
contents: read
packages: read
statuses: write
jobs:
mutate:
runs-on: ubuntu-latest
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)
steps:
- name: Download artifacts
id: download-artifacts
uses: dawidd6/action-download-artifact@v2
with:
github_token: ${{secrets.GITHUB_TOKEN}}
run_id: ${{ github.event.workflow_run.id }}
name: .+\.diff$
name_is_regexp: true
if_no_artifact_found: ignore
path: patches
- uses: marocchino/action-workflow_run-status@54b6e87d6cb552fc5f36dbe9a722a6048725917a
if: steps.download-artifacts.outputs.found_artifact == 'true'
with:
github_token: ${{secrets.GITHUB_TOKEN}}
- name: Token check
if: steps.download-artifacts.outputs.found_artifact == 'true'
run: |
if ${{ secrets.MUTATION_TOKEN && 'true' || 'false' }}; then
echo "Token available, enabling self mutation"
exit 0
else
echo "Add a MUTATION_TOKEN repository secret with a personal access token to enable self mutation.
It requires private repo read/write permissions." >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Disable Git Hooks
if: steps.download-artifacts.outputs.found_artifact == 'true'
run: |
git config --global core.hooksPath /dev/null
- name: Update PR Branch
uses: actions/github-script@v6
if: github.event.workflow_run.event == 'pull_request' && steps.download-artifacts.outputs.found_artifact == 'true'
with:
github-token: ${{ secrets.MUTATION_TOKEN }}
script: |
// use API to get the PR data since we can't rely on the context across forks
const pulls = await github.rest.pulls.list({
per_page: 1,
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.payload.workflow_run.head_repository.full_name}:${context.payload.workflow_run.head_branch}`
});
const prContextData = pulls.data[0];
const prNumber = prContextData.number;
const originalSha = prContextData.head.sha;
try {
console.log("Updating PR branch");
await github.rest.pulls.updateBranch({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
console.log("PR branch updated");
let updatedSha = originalSha;
let retries = 0;
const MAX_RETRIES = 10;
while (updatedSha == originalSha && retries++ < MAX_RETRIES) {
console.log(`Waiting for PR branch to update (attempt ${retries}/${MAX_RETRIES})`);
await new Promise(r => setTimeout(r, 500));
const updatedPR = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
updatedSha = updatedPR.data.head.sha;
}
} catch (error) {
// The branch is already up to date or can't otherwise be updated
// That's fine, we tried our best
console.warn(error);
}
- name: Checkout Workflow Branch
if: steps.download-artifacts.outputs.found_artifact == 'true'
uses: actions/checkout@v3
with:
token: ${{ secrets.MUTATION_TOKEN }}
ref: ${{ github.event.workflow_run.head_branch }}
repository: ${{ github.event.workflow_run.head_repository.full_name }}
path: repo
- id: self_mutation
if: steps.download-artifacts.outputs.found_artifact == 'true'
name: Apply downloaded patches
working-directory: repo
env:
HEAD_REF: ${{ github.event.workflow_run.head_branch }}
run: |
git config user.name "monada-bot[bot]"
git config user.email "[email protected]"
for f in $(find ../patches/*.diff/*.diff); do
echo "Applying $f"
git apply --binary $f
if [ $? -eq 0 ]; then
git add --all
git commit -s -m "chore: self mutation ($(basename $f))"
echo "Patch applied successfully"
rm $f
else
echo "Patch failed to apply"
cat $f
exit 1
fi
done
git push origin HEAD:$HEAD_REF
- name: Add label to block auto merge
uses: actions/github-script@v6
if: github.event.workflow_run.event == 'pull_request' && steps.download-artifacts.outputs.found_artifact == 'true'
with:
github-token: ${{ secrets.MUTATION_TOKEN }}
script: |
// use API to get the PR number since we can't rely on the context across forks
const pulls = await github.rest.pulls.list({
per_page: 1,
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.payload.workflow_run.head_repository.full_name}:${context.payload.workflow_run.head_branch}`
});
const prNumber = pulls.data[0].number;
const labels = ["⚠️ pr/review-mutation"];
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: labels
});