Repository navigation
Expand file tree
/
Copy pathdeploy.php
More file actions
392 lines (310 loc) · 8.24 KB
/
Copy pathdeploy.php
File metadata and controls
392 lines (310 loc) · 8.24 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?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;
}
/**
* Exit with error.
*
* On GitHub Actions the message is printed as an error workflow command, this
* also creates an annotation in the summary of the workflow run.
*
* @link https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#setting-an-error-message
* @param string $message
* @param int $result_code
* @return never
*/
function exit_with_error( $message, $result_code = 1 ) {
if ( 'true' === getenv( 'GITHUB_ACTIONS' ) ) {
echo '::error::', str_replace( [ '%', "\r", "\n" ], [ '%25', '%0D', '%0A' ], $message ), PHP_EOL;
} else {
echo format_error( $message ), PHP_EOL;
}
exit( $result_code );
}
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 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 ) {
exit_with_error( "Input required and not supplied: $name" );
}
return $value;
}
/**
* Default environment variables.
*
* @link https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/variables#default-environment-variables
*/
$repository = getenv( 'GITHUB_REPOSITORY' );
/**
* Setup.
*/
$username = get_required_input( 'username' );
$password = get_required_input( 'password' );
$slug = get_required_input( 'slug' );
$tag = get_required_input( 'tag' );
$version = ltrim( $tag, 'v' );
$svn_url = "https://plugins.svn.wordpress.org/$slug";
/**
* Filename.
*
* Notation: “{name}.{version}”.
*
* @link https://developer.wordpress.org/cli/commands/dist-archive/
*/
$filename = "$slug.$version.zip";
/**
* Files.
*/
$work_dir = tempnam( sys_get_temp_dir(), '' );
unlink( $work_dir );
mkdir( $work_dir );
$archives_dir = $work_dir . '/archives';
$plugins_dir = $work_dir . '/plugins';
$svn_dir = $work_dir . '/svn';
mkdir( $archives_dir );
mkdir( $plugins_dir );
mkdir( $svn_dir );
$plugin_dir = $plugins_dir . '/' . $slug;
$readme_file = $plugin_dir . '/readme.txt';
/**
* Start.
*/
start_group( 'ℹ️ Release to WordPress.org' );
echo '• ', escape_sequence( '1' ), 'Subversion URL:', escape_sequence( '0' ), ' ', $svn_url, PHP_EOL;
echo '• ', escape_sequence( '1' ), 'Subversion username:', escape_sequence( '0' ), ' ', $username, PHP_EOL;
end_group();
/**
* GitHub release.
*/
start_group( '🐙 Check GitHub release' );
run_command(
sprintf(
'gh release view %s --repo %s',
escapeshellarg( $tag ),
escapeshellarg( $repository )
)
);
end_group();
/**
* Download release.
*
* @link https://cli.github.com/manual/gh_release_download
*/
start_group( '📥 Download plugin' );
run_command(
sprintf(
'gh release download %s --pattern %s --dir %s --repo %s',
escapeshellarg( $tag ),
escapeshellarg( $filename ),
escapeshellarg( $archives_dir ),
escapeshellarg( $repository )
)
);
end_group();
/**
* Unzip.
*/
start_group( '📦 Unzip plugin' );
run_command(
sprintf(
'unzip %s -d %s',
escapeshellarg( $archives_dir . '/' . $filename ),
escapeshellarg( $plugins_dir )
)
);
end_group();
/**
* Parse stable tag.
*/
if ( ! is_file( $readme_file ) ) {
exit_with_error( "The archive $filename does not contain the file $slug/readme.txt." );
}
$readme_content = file_get_contents( $readme_file );
$pattern = "/Stable tag: (.*)/";
$stable_tag = '';
if ( 1 === preg_match( $pattern, $readme_content, $matches ) ) {
$stable_tag = trim( $matches[1] );
}
if ( $stable_tag !== $version ) {
exit_with_error( "Stable tag in readme.txt ($stable_tag) is not equal to release version ($version)." );
}
/**
* Check tag existence.
*/
start_group( '🔎 Check tag existence' );
$svn_url_tag = "$svn_url/tags/$version";
$result_code = run_command(
sprintf(
'svn info %s',
escapeshellarg( $svn_url_tag )
),
null
);
if ( 0 === $result_code ) {
exit_with_error( "There is already a tag for version $version: $svn_url_tag" );
}
end_group();
/**
* Subversion.
*
* @link https://stackoverflow.com/a/122291
*/
start_group( '⬇ Subversion checkout WordPress.org' );
run_command(
sprintf(
'svn checkout %s %s --depth immediates',
escapeshellarg( $svn_url ),
escapeshellarg( $svn_dir )
)
);
chdir( $svn_dir );
run_command( 'svn update trunk --set-depth infinity' );
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 --verbose %s %s',
escapeshellarg( $plugin_dir . '/' ),
escapeshellarg( $svn_dir . '/trunk/' )
)
);
end_group();
/**
* Subversion modifications.
*/
start_group( '💾 Subversion modifications' );
run_command( 'svn status' );
$output = shell_exec( 'svn status --xml' );
$xml = simplexml_load_string( $output );
if ( false === $xml ) {
exit_with_error( 'A problem occurred while reading the `svn status --xml`.' );
}
foreach ( $xml->target->entry as $entry ) {
$path = (string) $entry['path'];
$wc_status = (string) $entry->{'wc-status'}['item'];
/**
* Peg revision.
*
* A path can point to different files over time, because files can be
* renamed, deleted or replaced. Subversion therefore allows a revision
* to be appended to a path, as in `path@REV`, to identify which file is
* meant: the one that had this path in revision `REV`. This is called
* a peg revision.
*
* Subversion applies this notation to every path argument and treats
* everything after the last at sign as the peg revision. A file like
* `icon@2x.png` is therefore read as the path `icon` with peg revision
* `2x.png`, and `svn add` and `svn rm` fail with “a peg revision is not
* allowed here”.
*
* Because only the last at sign counts, we append one to every path:
* `icon@2x.png@` is read as the path `icon@2x.png` with an empty peg
* revision. This is harmless for paths without an at sign.
*
* @link https://svnbook.red-bean.com/en/1.7/svn.advanced.pegrevs.html
*/
switch ( $wc_status ) {
case 'missing':
run_command(
sprintf(
'svn rm %s',
escapeshellarg( $path . '@' )
)
);
break;
case 'modified':
// Modified entry will be commited.
break;
case 'unversioned':
run_command(
sprintf(
'svn add %s',
escapeshellarg( $path . '@' )
)
);
break;
default:
exit_with_error( "Unsupported working copy status: $wc_status - $path." );
}
}
end_group();
/**
* Commit.
*/
start_group( '⬆ Subversion commit WordPress.org' );
run_command(
sprintf(
'svn commit --message %s --non-interactive --username %s --password %s',
escapeshellarg( 'Update' ),
escapeshellarg( $username ),
escapeshellarg( $password )
)
);
end_group();
/**
* Tag.
*/
start_group( '🏷️ Subversion tag WordPress.org' );
run_command(
sprintf(
'svn cp %s %s --message %s --non-interactive --username %s --password %s',
escapeshellarg( "$svn_url/trunk" ),
escapeshellarg( $svn_url_tag ),
escapeshellarg( "Tagging version $version" ),
escapeshellarg( $username ),
escapeshellarg( $password )
)
);
end_group();
/**
* Clean up.
*/
start_group( '🗑️ Clean up' );
run_command(
sprintf(
'rm -f -R %s',
escapeshellarg( $work_dir )
)
);
end_group();