Add JSON response format to webhooks (#2671)
* Add JSON response format to webhooks Support JSON responses for all webhook endpoints: index, show, create, update, destroy, and activation. Adds jbuilder views, updates controllers to use respond_to, and documents the API in docs/API.md. * Address PR review feedback - Use distinct board ID in API docs webhook example - Use render "webhooks/show" in activations controller for consistency * Address PR review feedback from flavorjones - Include webhook.board in cache key to avoid stale board data - Use _url instead of _path for location header consistency * Fix webhook create test to match Location header format Use board_webhook_url instead of board_webhook_path since the controller returns a full URL in the Location header. * Fix board ID in Location header example in API docs Use distinct board ID in the Location URL to match the JSON body example.
This commit is contained in:
@@ -4,9 +4,12 @@ class Webhooks::ActivationsController < ApplicationController
|
||||
before_action :ensure_admin
|
||||
|
||||
def create
|
||||
webhook = @board.webhooks.find(params[:webhook_id])
|
||||
webhook.activate
|
||||
@webhook = @board.webhooks.find(params[:webhook_id])
|
||||
@webhook.activate
|
||||
|
||||
redirect_to webhook
|
||||
respond_to do |format|
|
||||
format.html { redirect_to @webhook }
|
||||
format.json { render "webhooks/show", status: :created }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,21 +16,45 @@ class WebhooksController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
webhook = @board.webhooks.create!(webhook_params)
|
||||
redirect_to webhook
|
||||
@webhook = @board.webhooks.new(webhook_params)
|
||||
|
||||
if @webhook.save
|
||||
respond_to do |format|
|
||||
format.html { redirect_to @webhook }
|
||||
format.json { render :show, status: :created, location: board_webhook_url(@webhook.board, @webhook, format: :json) }
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @webhook.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
@webhook.update!(webhook_params.except(:url))
|
||||
redirect_to @webhook
|
||||
if @webhook.update(webhook_params.except(:url))
|
||||
respond_to do |format|
|
||||
format.html { redirect_to @webhook }
|
||||
format.json { render :show }
|
||||
end
|
||||
else
|
||||
respond_to do |format|
|
||||
format.html { render :edit, status: :unprocessable_entity }
|
||||
format.json { render json: @webhook.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@webhook.destroy!
|
||||
redirect_to board_webhooks_path
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to board_webhooks_path }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
json.cache! [ webhook, webhook.board ] do
|
||||
json.(webhook, :id, :name, :active, :signing_secret, :subscribed_actions)
|
||||
json.payload_url webhook.url
|
||||
json.created_at webhook.created_at.utc
|
||||
json.url board_webhook_url(webhook.board, webhook)
|
||||
|
||||
json.board webhook.board, partial: "boards/board", as: :board
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
json.array! @page.records, partial: "webhooks/webhook", as: :webhook
|
||||
@@ -0,0 +1 @@
|
||||
json.partial! "webhooks/webhook", webhook: @webhook
|
||||
Reference in New Issue
Block a user