Skip to content

Commit b04e213

Browse files
committed
contribute.jquery.org: Properly handle audit errors
Uses an updated data format that includes stack traces for errors. Detects and converts the old data format to the new data format, including the simple "undefined" result for old errors. Adds support for an error section on the status page.
1 parent cb74040 commit b04e213

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

themes/contribute.jquery.org/cla-check.php

+43-7
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,33 @@ function getData() {
4141
}
4242

4343
$data = json_decode( $data );
44+
$data = normalizeData( $data );
4445
$data->repo = $repo;
4546
return $data;
4647
}
4748

49+
// The web hook used to only log the audit result, not the full data.
50+
// So we normalize old data to the new format
51+
function normalizeData( $data ) {
52+
53+
// Since we didn't store error info before, errors got logged as undefined,
54+
// which come across as null in PHP
55+
if ( is_null( $data ) ) {
56+
return (object) array(
57+
'error' => 'Unknown error'
58+
);
59+
}
60+
61+
// If we're missing the fully structured data, just move the audit result
62+
// to the proper location
63+
if ( !empty( $data->commits ) ) {
64+
return (object) array( 'data' => $data );
65+
}
66+
67+
// We have proper data, just return it as is
68+
return $data;
69+
}
70+
4871
function validateData( $data ) {
4972
if ( is_null( $data ) ) {
5073
global $wp_query;
@@ -59,21 +82,34 @@ function getProcessedPost( $data ) {
5982
the_post();
6083
$content = get_the_content();
6184

62-
if ( count( $data->neglectedAuthors ) ) {
63-
$content = preg_replace( "/<!-- ifsuccess -->.*<!-- endifsuccess -->/s", '', $content );
64-
$content = preg_replace( "/<!-- neglected-authors -->/", neglectedAuthors( $data ), $content );
85+
if ( !empty( $data->error ) ) {
86+
$content = preg_replace( '/<!-- ifsuccess -->.*<!-- endifsuccess -->/s', '', $content );
87+
$content = preg_replace( '/<!-- iffailure -->.*<!-- endiffailure -->/s', '', $content );
88+
$content = preg_replace(
89+
'/<!-- error -->/',
90+
'<pre>' . htmlspecialchars( $data->error ) . '</pre>',
91+
$content
92+
);
93+
94+
return $content;
95+
}
96+
97+
if ( count( $data->data->neglectedAuthors ) ) {
98+
$content = preg_replace( '/<!-- ifsuccess -->.*<!-- endifsuccess -->/s', '', $content );
99+
$content = preg_replace( '/<!-- neglected-authors -->/', neglectedAuthors( $data ), $content );
65100
} else {
66-
$content = preg_replace( "/<!-- iferror -->.*<!-- endiferror -->/s", '', $content );
101+
$content = preg_replace( '/<!-- iffailure -->.*<!-- endiffailure -->/s', '', $content );
67102
}
68103

69-
$content = preg_replace( "/<!-- commit-log -->/", commitLog( $data ), $content );
104+
$content = preg_replace( '/<!-- iferror -->.*<!-- endiferror -->/s', '', $content );
105+
$content = preg_replace( '/<!-- commit-log -->/', commitLog( $data ), $content );
70106

71107
return $content;
72108
}
73109

74110
function neglectedAuthors( $data ) {
75111
$html = "<ul>\n";
76-
foreach ( $data->neglectedAuthors as $author ) {
112+
foreach ( $data->data->neglectedAuthors as $author ) {
77113
$html .= "<li>" . htmlspecialchars( "$author->name <$author->email>" ) . "</li>\n";
78114
}
79115
$html .= "</ul>\n";
@@ -84,7 +120,7 @@ function commitLog( $data ) {
84120
$commitPrefix = "http://github.com/jquery/$data->repo/commit/";
85121

86122
$html = "<dl>\n";
87-
foreach ( $data->commits as $commit ) {
123+
foreach ( $data->data->commits as $commit ) {
88124
$html .= "<dt><a href='$commitPrefix$commit->hash'>$commit->hash</a></dt>\n";
89125
$html .= "<dd>" . htmlspecialchars( "$commit->name <$commit->email>" ) . "</dd\n";
90126
}

0 commit comments

Comments
 (0)