-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.php
311 lines (238 loc) Β· 5.47 KB
/
mirror.php
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Functions.
*/
function escape_sequence( $code ) {
return "\e[" . $code . 'm';
}
function format_command( $value ) {
return escape_sequence( '36' ) . $value . escape_sequence( '0' );
}
function format_error( $value ) {
return escape_sequence( '31' ) . escape_sequence( '1' ) . 'Error:' . escape_sequence( '0' ) . ' ' . $value;
}
function run_command( $command, $expected_result_code = 0 ) {
echo format_command( $command ), PHP_EOL;
passthru( $command, $result_code );
if ( null !== $expected_result_code && $expected_result_code !== $result_code ) {
exit( $result_code );
}
return $result_code;
}
function run_shell_exec( $command ) {
echo format_command( $command ), PHP_EOL;
return shell_exec( $command );
}
function start_group( $name ) {
echo '::group::', $name, PHP_EOL;
}
function end_group() {
echo '::endgroup::', PHP_EOL;
}
/**
* Get input.
*
* @link https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
* @link https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepswith
* @link https://github.com/actions/checkout/blob/cd7d8d697e10461458bc61a30d094dc601a8b017/dist/index.js#L2699-L2717
* @param string $name
* @return string|array|false
*/
function get_input( $name ) {
$env_name = 'INPUT_' . strtoupper( $name );
return getenv( $env_name );
}
function get_required_input( $name ) {
$value = get_input( $name );
if ( false === $value || '' === $value ) {
echo format_error( escape_sequence( '90' ) . 'Input required and not supplied:' . escape_sequence( '0' ) . ' ' . $name );
exit( 1 );
}
return $value;
}
/**
* Setup.
*/
$yoast_url = get_required_input( 'yoast-url' );
$yoast_product_slug = get_required_input( 'yoast-product-slug' );
$plugin_basename = get_required_input( 'plugin-basename' );
$plugin_slug = dirname( $plugin_basename );
/**
* Check Yoast.com.
*/
start_group( 'π Check Yoast.com' );
$url = 'https://my.yoast.com/api/sites/current';
$url .= '?' . http_build_query(
[
'url' => $yoast_url,
]
);
$data = run_shell_exec(
sprintf(
'curl --request GET %s',
escapeshellarg( $url )
)
);
$result = json_decode( $data );
if ( ! is_object( $result ) ) {
throw new Exception(
sprintf(
'Unknow response from: %s.',
$url
)
);
exit( 1 );
}
if ( ! property_exists( $result, 'subscriptions' ) ) {
echo format_error( 'No subscriptions information.' );
exit( 1 );
}
$subscriptions = $result->subscriptions;
$subscriptions = array_filter(
$subscriptions,
function ( $ubscription ) use ( $yoast_product_slug ) {
return ( $yoast_product_slug === $ubscription->product->slug );
}
);
$subscription = reset( $subscriptions );
if ( false === $subscription ) {
echo format_error( 'No subscription found.' );
exit( 1 );
}
$version = $subscription->product->version;
$zip_url = $subscription->product->download;
end_group();
$tag = 'v' . $version;
/**
* GitHub release view.
*/
$result_code = run_command( "gh release view $tag", null );
$release_not_found = ( 1 === $result_code );
if ( ! $release_not_found ) {
echo 'Release exists.';
exit( 0 );
}
/**
* Files.
*/
$work_dir = tempnam( sys_get_temp_dir(), '' );
unlink( $work_dir );
mkdir( $work_dir );
$archives_dir = $work_dir . '/archives';
$plugins_dir = $work_dir . '/plugins';
mkdir( $archives_dir );
mkdir( $plugins_dir );
$plugin_dir = $plugins_dir . '/' . $plugin_slug;
$zip_file = $archives_dir . '/' . $plugin_slug . '-' . $version . '.zip';
/**
* Download ZIP.
*/
start_group( 'π₯ Download plugin' );
run_command(
sprintf(
'curl %s --output %s',
escapeshellarg( $zip_url ),
escapeshellarg( $zip_file )
)
);
end_group();
/**
* Unzip.
*/
start_group( 'π¦ Unzip plugin' );
run_command(
sprintf(
'unzip %s -d %s',
escapeshellarg( $zip_file ),
escapeshellarg( $plugins_dir )
)
);
end_group();
/**
* Synchronize.
*
* @link http://stackoverflow.com/a/14789400
* @link http://askubuntu.com/a/476048
*/
start_group( 'π Synchronize plugin' );
run_command(
sprintf(
'rsync --archive --delete-before --exclude=%s --exclude=%s --verbose %s %s',
escapeshellarg( '.git' ),
escapeshellarg( '.github' ),
escapeshellarg( $plugin_dir . '/' ),
escapeshellarg( '.' )
)
);
end_group();
/**
* Git user.
*
* @link https://github.com/roots/wordpress/blob/13ba8c17c80f5c832f29cf4c2960b11489949d5f/bin/update-repo.php#L62-L67
*/
start_group( 'π Version control' );
run_command(
sprintf(
'git config user.email %s',
escapeshellarg( '[email protected]' )
)
);
run_command(
sprintf(
'git config user.name %s',
escapeshellarg( 'Yoast' )
)
);
/**
* Git commit.
*
* @link https://git-scm.com/docs/git-commit
*/
run_command( 'git add --all' );
run_command(
sprintf(
'git commit --all -m %s',
escapeshellarg(
sprintf(
'Updates to %s',
$version
)
)
),
null
);
run_command( 'gh auth status' );
run_command( 'git push origin main', null );
end_group();
/**
* Notes.
*/
$notes = $subscription->product->changelog;
/**
* GitHub release.
*
* @todo https://memberpress.com/wp-json/wp/v2/pages?slug=change-log
* @link https://cli.github.com/manual/gh_release_create
*/
start_group( 'π GitHub release' );
run_command(
sprintf(
'gh release create %s %s --title %s --notes %s',
$tag,
$zip_file,
escapeshellarg( $version ),
escapeshellarg( $notes )
)
);
end_group();
/**
* Cleanup.
*/
start_group( 'ποΈ Clean up' );
run_command(
sprintf(
'rm -f -R %s',
escapeshellarg( $work_dir )
)
);
end_group();