-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (176 loc) · 6.93 KB
/
create_pr_for_submodule.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: 'Create PR for submodule'
on:
workflow_dispatch:
inputs:
submodule:
description: 'Submodule to create a PR for'
required: true
type: choice
options:
- 'gw2auth.com-api'
- 'gw2auth.com-canary'
- 'gw2auth.com-cdk'
- 'gw2auth.com-ui'
- 'opentelemetry-lambda'
submodule_ref:
description: 'Ref of the submodule to create a PR for'
required: true
type: string
submodule_branch:
description: 'Branch of the submodule'
required: false
type: string
target_override:
description: 'Target branch override'
required: false
type: string
jobs:
prepare_matrix:
name: 'Prepare matrix'
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.featureset_match.outputs.result }}
steps:
- name: 'Checkout'
uses: actions/checkout@v4
with:
sparse-checkout: |
featuresets.js
sparse-checkout-cone-mode: false
- name: 'Featureset match'
id: 'featureset_match'
uses: actions/github-script@v7
env:
SUBMODULE: ${{ inputs.submodule }}
SUBMODULE_REF: ${{ inputs.submodule_ref }}
SUBMODULE_BRANCH: ${{ inputs.submodule_branch }}
TARGET_OVERRIDE: ${{ inputs.target_override }}
with:
script: |
const submodule = process.env.SUBMODULE;
const submodule_ref = process.env.SUBMODULE_REF;
const submodule_branch = process.env.SUBMODULE_BRANCH;
const target_override = process.env.TARGET_OVERRIDE;
const featuresets = require('./featuresets.js');
const matrix = {include: []};
if (target_override && target_override.length > 0) {
matrix.include.push({target: target_override});
} else {
for (const [feature_branch, v] of Object.entries(featuresets)) {
const allowed_sources = v[submodule];
if (allowed_sources) {
for (const pattern of allowed_sources) {
if (new RegExp(pattern).test(submodule_ref) || new RegExp(pattern).test(submodule_branch)) {
matrix.include.push({target: feature_branch});
break;
}
}
}
}
}
console.info(`matrix: ${JSON.stringify(matrix)}`);
return matrix;
create_pr_branch:
name: 'Prepare PR branch'
runs-on: ubuntu-latest
needs:
- prepare_matrix
strategy:
matrix: ${{ fromJSON(needs.prepare_matrix.outputs.matrix) }}
permissions:
contents: write
steps:
- name: 'Checkout'
uses: actions/checkout@v4
with:
ref: 'beta'
ssh-key: ${{ secrets.CDK_SSH_KEY }}
submodules: true
fetch-depth: 0
- name: 'Checkout target branch'
run: 'git checkout ${{ matrix.target }} || git checkout -b ${{ matrix.target }}'
- name: 'Push target branch'
run: 'git push "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" ${{ matrix.target }}'
- name: 'Checkout PR branch (1)'
run: 'git checkout -b "pr/${{ matrix.target }}/${{ inputs.submodule }}" && git reset --hard ${{ matrix.target }}'
- name: 'Update submodule ref (1)'
working-directory: ${{ inputs.submodule }}
run: 'git fetch origin "${{ inputs.submodule_ref }}" && git checkout "${{ inputs.submodule_ref }}"'
- name: 'Commit changes (1)'
run: |
git config --local user.name "${{ github.actor }}"
git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git add ${{ inputs.submodule }}
git commit -m "update ${{ inputs.submodule }} to ${{ inputs.submodule_ref }}" || echo "ignore commit"
- name: 'Push PR branch (1)'
run: 'git push --force "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" "pr/${{ matrix.target }}/${{ inputs.submodule }}"'
- name: 'Checkout PR branch (2)'
run: 'git checkout ${{ matrix.target }} && (git checkout "pr/${{ matrix.target }}/all" || git checkout -b "pr/${{ matrix.target }}/all")'
- name: 'Update submodule ref (2)'
working-directory: ${{ inputs.submodule }}
run: 'git fetch origin "${{ inputs.submodule_ref }}" && git checkout "${{ inputs.submodule_ref }}"'
- name: 'Commit changes (2)'
run: |
git config --local user.name "${{ github.actor }}"
git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git add ${{ inputs.submodule }}
git commit -m "update ${{ inputs.submodule }} to ${{ inputs.submodule_ref }}" || echo "ignore commit"
- name: 'Push PR branch (2)'
run: 'git push --force "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" "pr/${{ matrix.target }}/all"'
create_pr:
name: 'Create PR'
runs-on: ubuntu-latest
needs:
- prepare_matrix
- create_pr_branch
strategy:
matrix: ${{ fromJSON(needs.prepare_matrix.outputs.matrix) }}
permissions:
pull-requests: write
steps:
- name: 'Create PR'
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const base = '${{ matrix.target }}';
const heads = [
'pr/${{ matrix.target }}/${{ inputs.submodule }}',
'pr/${{ matrix.target }}/all',
];
for (const head of heads) {
const pulls = await github.rest.pulls.list({
owner: owner,
repo: repo,
head: `${owner}:${head}`,
base: base,
state: 'open',
});
if (pulls.data.length < 1) {
try {
await github.rest.pulls.create({
title: `[CI] Merge ${head} into ${base}`,
owner: owner,
repo: repo,
head: head,
base: base,
body: [
'This PR is auto-generated by',
'[actions/github-script](https://github.com/actions/github-script)',
].join('\n'),
});
} catch (e) {
console.warn(e);
}
} else {
const existingPR = pulls.data[0];
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: existingPR.number,
body: [
`Updated by Job ${context.job}`,
].join('\n'),
});
}
}