Repository navigation
Expand file tree
/
Copy pathclass-restapi.php
More file actions
244 lines (218 loc) · 6.48 KB
/
Copy pathclass-restapi.php
File metadata and controls
244 lines (218 loc) · 6.48 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
<?php
namespace PTR;
use WP_Error;
class RestAPI {
/**
* Register REST API routes.
*
* @action rest_api_init
*/
public static function register_routes() {
register_rest_route(
'wp-unit-test-api/v1',
'results',
array(
'methods' => 'POST',
'callback' => array( __CLASS__, 'add_results_callback' ),
'args' => array(
'commit' => array(
'required' => true,
'description' => 'The SVN commit changeset number.',
'type' => 'integer',
),
'results' => array(
'required' => true,
'description' => 'phpunit results in JSON format.',
'type' => 'string',
'validate_callback' => array( __CLASS__, 'validate_callback' ),
),
'message' => array(
'required' => true,
'description' => 'The SVN commit message.',
'type' => 'string',
'validate_callback' => array( __CLASS__, 'validate_callback' ),
),
'env' => array(
'required' => true,
'description' => 'JSON blob containing information about the environment.',
'type' => 'string',
'validate_callback' => array( __CLASS__, 'validate_callback' ),
),
),
'permission_callback' => array( __CLASS__, 'permission' ),
)
);
}
public static function validate_callback( $value, $request, $key ) {
switch ( $key ) {
case 'message':
if ( empty( $value ) || ! is_string( $value ) ) {
return new WP_Error(
'rest_invalid',
__( 'Value must be a non-empty string.', 'ptr' ),
array(
'status' => 400,
)
);
}
return true;
case 'env':
case 'results':
if ( null === json_decode( $value ) ) {
return new WP_Error(
'rest_invalid',
__( 'Value must be encoded JSON.', 'ptr' ),
array(
'status' => 400,
)
);
}
return true;
}
return new WP_Error(
'rest_invalid',
__( 'Invalid key specified.', 'ptr' ),
array(
'status' => 400,
)
);
}
public static function permission() {
if ( ! current_user_can( 'edit_results' ) ) {
return new WP_Error(
'rest_unauthorized',
__( 'Sorry, you are not allowed to create results.', 'ptr' ),
array(
'status' => is_user_logged_in() ? 403 : 401,
)
);
}
return true;
}
public static function add_results_callback( $data ) {
$parameters = $data->get_params();
$slug = 'r' . $parameters['commit'];
$post = get_page_by_path( $slug, 'OBJECT', 'result' );
if ( $post ) {
$parent_id = $post->ID;
} else {
$parent_id = wp_insert_post(
array(
'post_title' => $parameters['message'],
'post_name' => $slug,
'post_status' => 'publish',
'post_type' => 'result',
)
);
}
$current_user = wp_get_current_user();
$args = array(
'post_parent' => $parent_id,
'post_type' => 'result',
'numberposts' => 1,
'author' => $current_user->ID,
);
// Check to see if the test result already exist.
$results = get_posts( $args );
if ( $results ) {
$post_id = $results[0]->ID;
} else {
$results = array(
'post_title' => $current_user->user_login . ' - ' . $slug,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'result',
'post_parent' => $parent_id,
);
// Store the results.
$post_id = wp_insert_post( $results, true );
}
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
$env = isset( $parameters['env'] ) ? json_decode( $parameters['env'], true ) : array();
$results = isset( $parameters['results'] ) ? json_decode( $parameters['results'], true ) : array();
update_post_meta( $post_id, 'env', $env );
update_post_meta( $post_id, 'results', $results );
self::maybe_send_email_notifications( $parent_id );
// Create the response object.
$response = new \WP_REST_Response(
array(
'id' => $post_id,
'link' => get_permalink( $post_id ),
)
);
// Add a custom status code.
$response->set_status( 201 );
$response->header( 'Content-Type', 'application/json' );
return $response;
}
/**
* Maybe send an email notification if 'wpdevbot' has reported
* a success and others have reported failures.
*/
private static function maybe_send_email_notifications( $parent_id ) {
// 'wpdevbot' doesn't exist on this system, so nothing to compare.
$user = get_user_by( 'login', 'wpdevbot' );
if ( ! $user ) {
return;
}
$wporgbot_id = $user->ID;
$args = array(
'post_parent' => $parent_id,
'post_type' => 'result',
'numberposts' => -1,
);
$results = get_posts( $args );
$wpdevbot_results = wp_filter_object_list( $results, array( 'post_author' => $wporgbot_id ) );
// 'wpdevbot' hasn't reported yet
if ( empty( $wpdevbot_results ) ) {
return;
}
$wpdevbot_result = array_shift( $wpdevbot_results );
// If 'wpdevbot' is failed, we already know the test failure
// and don't need to report host testing bots failures.
if ( self::is_failed_result( $wpdevbot_result ) ) {
return;
}
foreach ( $results as $result ) {
// Doesn't make sense to report wpdevbot to itself
if ( $wpdevbot_result->ID === $result->ID ) {
continue;
}
// If the test result is failed and we haven't yet sent an
// email notification, then let the reporter know.
if ( self::is_failed_result( $result )
&& ! get_post_meta( $result->ID, 'ptr_reported_failure', true ) ) {
$user = get_user_by( 'id', $result->post_author );
if ( $user ) {
$subject = '[Host Test Results] Test failure for ' . $result->post_name;
$body = 'Hi there,' . PHP_EOL . PHP_EOL
. "We've detected a WordPress PHPUnit test failure on your hosting environment. Please review when you have a moment: "
. get_permalink( $result->ID ) . PHP_EOL . PHP_EOL
. 'Thanks,' . PHP_EOL . PHP_EOL
. 'WordPress.org Hosting Community';
wp_mail( $user->user_email, $subject, $body );
update_post_meta( $result->ID, 'ptr_reported_failure', true );
}
}
}
}
/**
* Whether or not a given result is a failed result.
*
* @param WP_Post $post Result post object.
* @return boolean
*/
private static function is_failed_result( $post ) {
$is_failed = false;
$results = get_post_meta( $post->ID, 'results', true );
if ( isset( $results['failures'] ) ) {
if ( 0 !== (int) $results['failures'] || 0 !== (int) $results['errors'] ) {
$is_failed = true;
}
}
return $is_failed;
}
}