-
Notifications
You must be signed in to change notification settings - Fork 2
220 lines (205 loc) · 7.39 KB
/
Copy pathsubmodule-update.yml
File metadata and controls
220 lines (205 loc) · 7.39 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
name: Update Mobile Submodules
on:
workflow_dispatch:
inputs:
update_target:
description: "Select which mobile submodules to update"
required: true
type: choice
options:
- ios
- android
- both
ios_ref:
description: "Ref (branch, tag, or SHA) for apps/react-native/ios"
required: false
default: main
android_ref:
description: "Ref (branch, tag, or SHA) for apps/react-native/android"
required: false
default: main
update_mode:
description: "Use create-pr (default) or update-existing-pr"
required: true
type: choice
default: create-pr
options:
- create-pr
- update-existing-pr
target_pr_number:
description: "Existing PR number when update_mode=update-existing-pr"
required: false
base_branch:
description: "Base branch when creating a new PR"
required: false
type: choice
default: main
options:
- main
jobs:
update-submodules:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: false
- name: Configure git user
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Validate inputs
if: ${{ inputs.update_mode == 'update-existing-pr' && (inputs.target_pr_number == '' || inputs.target_pr_number == null) }}
run: |
echo "target_pr_number is required when update_mode=update-existing-pr" >&2
exit 1
- name: Lookup target pull request
if: ${{ inputs.update_mode == 'update-existing-pr' }}
id: pr_info
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pr_number: ${{ inputs.target_pr_number }}
script: |
const raw = core.getInput('pr_number');
const prNumber = parseInt(raw, 10);
if (!Number.isFinite(prNumber)) {
core.setFailed(`Invalid PR number provided: ${raw}`);
return;
}
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
if (pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
core.setFailed(`PR #${prNumber} originates from ${pr.head.repo.full_name}; cannot push updates.`);
return;
}
core.setOutput('head_ref', pr.head.ref);
core.setOutput('base_ref', pr.base.ref);
core.setOutput('pr_url', pr.html_url);
- name: Checkout base branch
if: ${{ inputs.update_mode != 'update-existing-pr' }}
env:
BASE_BRANCH: ${{ inputs.base_branch }}
run: |
git fetch origin "$BASE_BRANCH"
git checkout -B "$BASE_BRANCH" "origin/$BASE_BRANCH"
- name: Checkout PR branch
if: ${{ inputs.update_mode == 'update-existing-pr' }}
env:
HEAD_REF: ${{ steps.pr_info.outputs.head_ref }}
run: |
git fetch origin "$HEAD_REF"
git checkout -B "$HEAD_REF" "origin/$HEAD_REF"
- name: Update submodules to requested refs
id: update
env:
IOS_REF: ${{ inputs.ios_ref }}
ANDROID_REF: ${{ inputs.android_ref }}
UPDATE_TARGET: ${{ inputs.update_target }}
run: |
set -euo pipefail
args=()
case "${UPDATE_TARGET}" in
ios)
args+=(--skip-android)
;;
android)
args+=(--skip-ios)
;;
both)
;;
*)
echo "Unknown update target: ${UPDATE_TARGET}" >&2
exit 1
;;
esac
if [[ "${UPDATE_TARGET}" != "android" && -n "${IOS_REF}" ]]; then
args+=(--ios-ref "${IOS_REF}")
fi
if [[ "${UPDATE_TARGET}" != "ios" && -n "${ANDROID_REF}" ]]; then
args+=(--android-ref "${ANDROID_REF}")
fi
scripts/update-submodule-refs.sh "${args[@]}"
- name: Check for changes
id: diff
run: |
if git diff --cached --quiet; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
git submodule status > submodule-status.txt
{
echo "status<<'EOF'"
cat submodule-status.txt
echo 'EOF'
} >> "$GITHUB_OUTPUT"
fi
- name: No changes detected
if: ${{ steps.diff.outputs.changed == 'false' }}
run: |
echo "No submodule updates required."
- name: Create pull request
if: ${{ steps.diff.outputs.changed == 'true' && inputs.update_mode != 'update-existing-pr' }}
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "chore: update mobile submodules"
branch: automation/update-mobile-submodules
branch-suffix: timestamp
base: ${{ inputs.base_branch }}
title: "chore: update mobile submodules"
add-paths: |
apps/react-native/ios
apps/react-native/android
body: |
## Summary
Mobile submodule pointers updated via workflow run ${{ github.run_number }}.
```
${{ steps.diff.outputs.status }}
```
Triggered by: ${{ github.actor }}
- name: Commit changes to existing PR branch
if: ${{ steps.diff.outputs.changed == 'true' && inputs.update_mode == 'update-existing-pr' }}
run: |
git commit -m "chore: update mobile submodules"
- name: Push changes
if: ${{ steps.diff.outputs.changed == 'true' && inputs.update_mode == 'update-existing-pr' }}
env:
HEAD_REF: ${{ steps.pr_info.outputs.head_ref }}
run: |
git push origin HEAD:"$HEAD_REF"
- name: Comment on existing PR
if: ${{ steps.diff.outputs.changed == 'true' && inputs.update_mode == 'update-existing-pr' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pr_number: ${{ inputs.target_pr_number }}
status: ${{ steps.diff.outputs.status }}
script: |
const raw = core.getInput('pr_number');
const prNumber = parseInt(raw, 10);
if (!Number.isFinite(prNumber)) {
core.setFailed(`Invalid PR number provided: ${raw}`);
return;
}
const status = core.getInput('status');
const lines = [
`Submodule pointers updated via workflow run ${context.runNumber} by @${context.actor}.`,
'',
'```',
status || 'No submodule diff output captured.',
'```'
];
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: lines.join('\n'),
});