From df0f35cb125958e107da6951ae70eb72c5f88e92 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 14 Apr 2019 14:06:24 +1000 Subject: [PATCH] feat: allow pact to be deleted through the UI Closes: https://github.com/pact-foundation/pact_broker/issues/179 --- .../api/renderers/html_pact_renderer.rb | 25 +++- lib/pact_broker/api/resources/pact.rb | 14 ++ public/javascripts/index.js | 12 +- public/javascripts/pact.js | 140 +++++++++++++++++- public/stylesheets/pact.css | 6 +- spec/features/delete_pact_spec.rb | 4 +- .../api/renderers/html_pact_renderer_spec.rb | 2 +- .../pact_broker/api/resources/pact_spec.rb | 5 +- 8 files changed, 185 insertions(+), 23 deletions(-) diff --git a/lib/pact_broker/api/renderers/html_pact_renderer.rb b/lib/pact_broker/api/renderers/html_pact_renderer.rb index 2f6560474..3a627393d 100644 --- a/lib/pact_broker/api/renderers/html_pact_renderer.rb +++ b/lib/pact_broker/api/renderers/html_pact_renderer.rb @@ -40,10 +40,15 @@ def head + + + + + " end @@ -69,19 +74,26 @@ def pact_metadata
  • Home
  • +
  • + +
  • " end def badge_list_item - "
  • + "
  • " end def badge_markdown_item - "" end @@ -120,11 +132,15 @@ def json_url end def pact_url - PactBroker::Api::PactBrokerUrls.pact_url '', @pact + PactBroker::Api::PactBrokerUrls.pact_url base_url, @pact end def matrix_url - PactBroker::Api::PactBrokerUrls.matrix_url_from_params consumer_name: @pact.consumer.name, provider_name: @pact.provider.name + PactBroker::Api::PactBrokerUrls.matrix_url_from_params({ consumer_name: @pact.consumer.name, provider_name: @pact.provider.name }, base_url) + end + + def latest_pact_url + PactBroker::Api::PactBrokerUrls.latest_pact_url base_url, @pact end def badge_target_url @@ -162,7 +178,6 @@ def consumer_contract logger.info "Could not parse the following content to a Pact due to #{e.class} #{e.message}, showing raw content instead: #{@json_content}" raise NotAPactError end - end end end diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index 9867a76ed..0cd40228f 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -88,6 +88,7 @@ def to_html def delete_resource pact_service.delete(pact_params) + set_post_deletion_response true end @@ -104,6 +105,19 @@ def pact_params def update_matrix_after_request? request.put? || request.patch? end + + def set_post_deletion_response + latest_pact = pact_service.find_latest_pact(pact_params) + response_body = { "_links" => {} } + if latest_pact + response_body["_links"]["pb:latest-pact-version"] = { + href: latest_pact_url(base_url, latest_pact), + title: "Latest pact" + } + end + response.body = response_body.to_json + response.headers["Content-Type" => "application/hal+json;charset=utf-8"] + end end end end diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 1d75afebe..1394d9519 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -86,10 +86,10 @@ function createPactVersionsDeletionConfirmationText(rowData) { }, and all other data related to them (webhooks, verifications, application versions, and tags). Do you wish to continue?`; } -function confirmDeletePactVersions( +function confirmDeleteResources( rowData, confirmationText, - confirmCallbak, + confirmCallback, cancelCallback ) { $.confirm({ @@ -100,7 +100,7 @@ function confirmDeletePactVersions( text: "DELETE", btnClass: "alert alert-danger", keys: ["enter", "shift"], - action: confirmCallbak + action: confirmCallback }, cancel: cancelCallback } @@ -114,7 +114,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) { unHighlightRows(table); }; const confirm = function() { - deletePactVersions( + deleteResources( deletionUrl, function() { handleDeletionSuccess(row); @@ -126,7 +126,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) { }; highlightRowToBeDeleted(row); - confirmDeletePactVersions( + confirmDeleteResources( rowData, confirmationText, confirm, @@ -168,7 +168,7 @@ function handleDeletionFailure(table, response) { }); } -function deletePactVersions(url, successCallback, errorCallback) { +function deleteResources(url, successCallback, errorCallback) { $.ajax({ url: url, dataType: "json", diff --git a/public/javascripts/pact.js b/public/javascripts/pact.js index bc34801f8..89e5f42e1 100644 --- a/public/javascripts/pact.js +++ b/public/javascripts/pact.js @@ -1,5 +1,139 @@ -$( document ).ready(function() { - $('.badge').click(function(){ - $('.badge-markdown').toggle(); +$(document).ready(function() { + $(".pact-badge").click(function() { + $(".pact-badge-markdown").toggle(); }); + + $(".more-options") + .materialMenu("init", { + position: "overlay", + animationSpeed: 1, + items: [ + // { + // type: "normal", + // text: "View in API Browser", + // click: function(openMenuElement) { + // window.location.href = openMenuElement.data().apiBrowserUrl; + // } + // }, + // { + // type: "normal", + // text: "View Matrix", + // click: function(openMenuElement) { + // window.location.href = openMenuElement.data().matrixUrl; + // } + // }, + { + type: "normal", + text: "Delete ...", + click: function(openMenuElement) { + promptToDeleteResource(openMenuElement.data().pactUrl, createDeletionConfirmationText(openMenuElement.data())) + } + } + ] + }) + .click(function() { + $(this).materialMenu("open"); + }); }); + +function createDeletionConfirmationText(data) { + return `Do you wish to delete the pact for version ${data.consumerVersionNumber} of ${data.consumerName}?`; +} + + +function confirmDeleteResource( + confirmationText, + confirmCallbak, + cancelCallback +) { + $.confirm({ + title: "Confirm!", + content: confirmationText, + buttons: { + delete: { + text: "DELETE", + btnClass: "alert alert-danger", + keys: ["enter", "shift"], + action: confirmCallbak + }, + cancel: cancelCallback + } + }); +} + +function promptToDeleteResource(deletionUrl, confirmationText) { + const cancel = function() {}; + const confirm = function() { + deleteResource( + deletionUrl, + handleDeletionSuccess, + handleDeletionFailure + ); + }; + + confirmDeleteResource( + confirmationText, + confirm, + cancel + ); +} + +function handleDeletionSuccess(responseBody) { + if(responseBody._links['pb:latest-pact-version']) { + $.confirm({ + title: "Pact deleted", + content: "Where to next?", + buttons: { + latest: { + text: "Latest pact", + keys: ["enter", "shift"], + action: function() { window.location.href = responseBody._links['pb:latest-pact-version'].href } + }, + home: { + text: "Home", + action: function() { window.location.href = "/"; } + } + } + }); + + } else { + window.location.href = "/"; + } +} + +function handleDeletionFailure(response) { + let errorMessage = null; + + if (response.error && response.error.message && response.error.reference) { + errorMessage = + "

    Could not delete resources due to error: " + + response.error.message + + "

    Error reference: " + + response.error.reference + "

    "; + } else { + errorMessage = + "Could not delete resources due to error: " + JSON.stringify(response); + } + + $.alert({ + title: 'Error', + content: errorMessage, + }); +} + +function deleteResource(url, successCallback, errorCallback) { + $.ajax({ + url: url, + dataType: "json", + type: "delete", + accepts: { + text: "application/hal+json" + }, + success: function(data, textStatus, jQxhr) { + successCallback(data); + }, + error: function(jqXhr, textStatus, errorThrown) { + errorCallback(jqXhr.responseJSON); + } + }); +} diff --git a/public/stylesheets/pact.css b/public/stylesheets/pact.css index 446481cdf..c669a0c58 100644 --- a/public/stylesheets/pact.css +++ b/public/stylesheets/pact.css @@ -15,15 +15,15 @@ pre { font-size: 1em } -li.badge { +li.pact-badge { cursor: pointer; } -li.badge > img { +li.pact-badge > img { cursor: pointer; height: 20px; } -div.badge-markdown { +div.pact-badge-markdown { background-color: #f8f8f8 } diff --git a/spec/features/delete_pact_spec.rb b/spec/features/delete_pact_spec.rb index f43725fae..c1baa779b 100644 --- a/spec/features/delete_pact_spec.rb +++ b/spec/features/delete_pact_spec.rb @@ -19,8 +19,8 @@ expect{ subject }.to change{ PactBroker::Pacts::PactPublication.count }.by(-1) end - it "returns a 204" do - expect(subject.status).to be 204 + it "returns a 200" do + expect(subject.status).to be 200 end end diff --git a/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb b/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb index cbff5da6c..be2d88689 100644 --- a/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb +++ b/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb @@ -11,7 +11,7 @@ module Renderers ENV['BACKUP_TZ'] = ENV['TZ'] ENV['TZ'] = "Australia/Melbourne" PactBroker.configuration.enable_public_badge_access = true - allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('', pact).and_return(pact_url) + allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('http://base', pact).and_return(pact_url) allow_any_instance_of(HtmlPactRenderer).to receive(:logger).and_return(logger) Timecop.freeze(created_at + 3) diff --git a/spec/lib/pact_broker/api/resources/pact_spec.rb b/spec/lib/pact_broker/api/resources/pact_spec.rb index 87cc9fc34..0e2b8b78f 100644 --- a/spec/lib/pact_broker/api/resources/pact_spec.rb +++ b/spec/lib/pact_broker/api/resources/pact_spec.rb @@ -153,8 +153,8 @@ module Resources subject end - it "returns a 204" do - expect(subject.status).to eq 204 + it "returns a 200" do + expect(subject.status).to eq 200 end end @@ -164,7 +164,6 @@ module Resources it "returns a 404" do expect(subject.status).to eq 404 end - end end end