From 17401b07db01ffe7664310514f918a9cc1f91577 Mon Sep 17 00:00:00 2001 From: Pepijn van Vlaanderen Date: Wed, 29 Jul 2020 17:00:03 +0200 Subject: [PATCH 1/4] Add Github Workflow Run deployment --- src/Field.php | 42 ++++++++++++++++++++++++++++++++++++++---- src/Settings.php | 20 ++++++++++++++++++-- src/WebhookTrigger.php | 35 +++++++++++++++++++++++++++-------- src/functions.php | 24 ++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 14 deletions(-) diff --git a/src/Field.php b/src/Field.php index cd89493..ed6d914 100644 --- a/src/Field.php +++ b/src/Field.php @@ -4,6 +4,28 @@ class Field { + /** + * Render an input[type=text] field + * + * @param array $args + * @return void + */ + public static function text($args = []) + { + self::render_field('text', $args); + } + + /** + * Render an input[type=password] field + * + * @param array $args + * @return void + */ + public static function password($args = []) + { + self::render_field('password', $args); + } + /** * Render an input[type=url] field * @@ -11,11 +33,23 @@ class Field * @return void */ public static function url($args = []) + { + self::render_field('url', $args); + } + + /** + * Render an input[type=url] field + * + * @param string $type + * @param array $args + * @return void + */ + private static function render_field($type, $args = []) { ?>
- - {$args['description']}

" : ''; ?> -
" class="regular-text" name="" value=""> + {$args['description']}

" : ''; ?> +
diff --git a/src/Settings.php b/src/Settings.php index 5ea798b..63334b9 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -33,8 +33,20 @@ public static function register() add_settings_field('webhook_url', __( 'Build Hook URL', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'url'], $key, 'general', [ 'name' => "{$key}[webhook_url]", 'value' => jamstack_deployments_get_webhook_url(), - 'description' => sprintf( __( 'Your Build Hook URL. This is the URL that is pinged to start building/deploying the JAMstack site. See Netlify docs or see Zeit docs.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/configure-builds/build-hooks/', 'https://zeit.co/docs/v2/advanced/deploy-hooks/' ) - ]); + 'description' => sprintf( __( 'Your Build Hook URL. This is the URL that is pinged to start building/deploying the JAMstack site. See Netlify docs or see Zeit docs or see Github API docs.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/configure-builds/build-hooks/', 'https://zeit.co/docs/v2/advanced/deploy-hooks/', 'https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event' ) + ]); + + add_settings_field('access_token', __( 'Access Token', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'password'], $key, 'general', [ + 'name' => "{$key}[access_token]", + 'value' => jamstack_deployments_get_access_token(), + 'description' => sprintf( __( 'Your Build Hook access token. Needed for Github API authentication. Create a personal access token for your Github account here', 'wp-jamstack-deployments' ), 'https://github.com/settings/tokens' ) + ]); + + add_settings_field('workflow_run_reference', __( 'Workflow Run Reference', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'text'], $key, 'general', [ + 'name' => "{$key}[workflow_run_reference]", + 'value' => jamstack_deployments_get_workflow_run_reference(), + 'description' => __( 'The reference of a Github workflow run. The reference can be a branch, tag, or a commit SHA.', 'wp-jamstack-deployments' ) + ]); add_settings_field('webhook_method', __( 'Hook Method', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'select'], $key, 'general', [ 'name' => "{$key}[webhook_method]", @@ -135,6 +147,10 @@ public static function sanitize($input) $input['webhook_url'] = trim($input['webhook_url']); } + if (!empty($input['access_token'])) { + $input['access_token'] = trim($input['access_token']); + } + if (isset($input['webhook_method']) && !in_array($input['webhook_method'], ['get', 'post'])) { $input['webhook_method'] = 'post'; } diff --git a/src/WebhookTrigger.php b/src/WebhookTrigger.php index 869e44f..f329a5c 100644 --- a/src/WebhookTrigger.php +++ b/src/WebhookTrigger.php @@ -16,7 +16,7 @@ public static function init() add_action('admin_footer', [__CLASS__, 'adminBarCssAndJs']); add_action('wp_footer', [__CLASS__, 'adminBarCssAndJs']); - + add_action('wp_enqueue_scripts', [__CLASS__, 'enqueueScripts']); add_action('admin_enqueue_scripts', [__CLASS__, 'enqueueScripts']); @@ -103,7 +103,7 @@ public static function triggerEditTerm($id, $tax_id, $tax_slug) if (!self::canFireForTaxonomy($id, $tax_id, $tax_slug)) { return; } - + self::fireWebhook(); } @@ -125,7 +125,7 @@ protected static function canFireForTaxonomy($id, $tax_id, $tax_slug) /** * Show the admin bar css & js - * + * * @todo move this somewhere else * @return void */ @@ -162,7 +162,7 @@ public static function adminBarCssAndJs() /** * Enqueue js to the admin & frontend - * + * * @return void */ public static function enqueueScripts() @@ -233,7 +233,7 @@ public static function trigger() if (!isset($_GET['action']) || 'jamstack-deployment-trigger' !== $_GET['action']) { return; } - + check_admin_referer('crgeary_jamstack_deployment_trigger', 'crgeary_jamstack_deployment_trigger'); self::fireWebhook(); @@ -255,7 +255,7 @@ public static function triggerPostTransition($new, $old, $post) { $id = $post->ID; $option = jamstack_deployments_get_options(); - + $saved_post_types = isset($option['webhook_post_types']) ? $option['webhook_post_types'] : []; $post_types = apply_filters('jamstack_deployments_post_types', $saved_post_types, $id, $post); @@ -304,9 +304,28 @@ public static function fireWebhook() return; } - $args = apply_filters('jamstack_deployments_webhook_request_args', [ + $args = [ 'blocking' => false - ]); + ]; + + $access_token = jamstack_deployments_get_access_token(); + + if ($access_token) { + $args['headers'] = [ + 'Content-Type' => 'application/json', + 'Authorization' => $access_token + ]; + } + + $workflow_run_reference = jamstack_deployments_get_workflow_run_reference(); + + if ($workflow_run_reference) { + $args['body'] = wp_json_encode([ + 'ref' => $workflow_run_reference + ]); + } + + $args = apply_filters('jamstack_deployments_webhook_request_args', $args); $method = jamstack_deployments_get_webhook_method(); diff --git a/src/functions.php b/src/functions.php index 037c40f..098a610 100644 --- a/src/functions.php +++ b/src/functions.php @@ -23,6 +23,30 @@ function jamstack_deployments_get_webhook_url() { } } +if (!function_exists('jamstack_deployments_get_access_token')) { + /** + * Return the webhook url + * + * @return string|null + */ + function jamstack_deployments_get_access_token() { + $options = jamstack_deployments_get_options(); + return isset($options['access_token']) ? $options['access_token'] : null; + } +} + +if (!function_exists('jamstack_deployments_get_workflow_run_reference')) { + /** + * Return the webhook url + * + * @return string|null + */ + function jamstack_deployments_get_workflow_run_reference() { + $options = jamstack_deployments_get_options(); + return isset($options['workflow_run_reference']) ? $options['workflow_run_reference'] : null; + } +} + if (!function_exists('jamstack_deployments_get_webhook_method')) { /** * Return the webhook method (get/post) From 5de8482050b9d4739efb0ebe53e65dc2ff67cb23 Mon Sep 17 00:00:00 2001 From: Pepijn van Vlaanderen Date: Wed, 29 Jul 2020 17:05:09 +0200 Subject: [PATCH 2/4] Only esc_url when type is url --- src/Field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Field.php b/src/Field.php index ed6d914..473662c 100644 --- a/src/Field.php +++ b/src/Field.php @@ -47,7 +47,7 @@ public static function url($args = []) private static function render_field($type, $args = []) { ?>
- + {$args['description']}

" : ''; ?>
Date: Wed, 29 Jul 2020 17:34:38 +0200 Subject: [PATCH 3/4] Fix headers for Github trigger --- src/WebhookTrigger.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/WebhookTrigger.php b/src/WebhookTrigger.php index f329a5c..6c401ef 100644 --- a/src/WebhookTrigger.php +++ b/src/WebhookTrigger.php @@ -312,8 +312,8 @@ public static function fireWebhook() if ($access_token) { $args['headers'] = [ - 'Content-Type' => 'application/json', - 'Authorization' => $access_token + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer {$access_token}" ]; } @@ -321,8 +321,10 @@ public static function fireWebhook() if ($workflow_run_reference) { $args['body'] = wp_json_encode([ - 'ref' => $workflow_run_reference + 'ref' => $workflow_run_reference ]); + $args['method'] = 'POST'; + $args['data_format'] = 'body'; } $args = apply_filters('jamstack_deployments_webhook_request_args', $args); From ab0a9ff935eb8dd11dc55df4b295619537623ef6 Mon Sep 17 00:00:00 2001 From: Pepijn van Vlaanderen Date: Wed, 29 Jul 2020 17:43:21 +0200 Subject: [PATCH 4/4] Update README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c388135..d8ddb29 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,9 @@ You can access the plugin's settings in WordPress by accessing the 'Settings' pa From this screen you can configure the following: -- **Webhook URL** - The webhook URL that you have created to trigger a deployment. For more information on webhooks with Netlify [visit the Netlify documentation](https://www.netlify.com/docs/webhooks/). +- **Webhook URL** - The webhook URL that you have created to trigger a deployment. For more information on webhooks with Netlify [visit the Netlify documentation](https://www.netlify.com/docs/webhooks/) or a workflow dispatch in Github Actions [visit Github API documentation](https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event). +- **Access Token** - Your Build Hook access token. Needed for Github API authentication. Create a personal access token for your Github account [here](https://github.com/settings/tokens) +- **Workflow Run Reference** - The reference of a Github workflow run. The reference can be a branch, tag, or a commit SHA. - **Webhook Method** - This is the required method for the webhook request. The available options are `GET` or `POST`. By default the plugin will automatically select `POST`. - **Badge Image URL** - An optional field to specify the `src` of a badge, for services that support badges. - **Badge Link** - An optional field to specify the `href` of a badge, for services that support badges.