Skip to content

Commit

Permalink
feat: allow an integration to be deleted via the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 13, 2019
1 parent 5456eda commit f520520
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 47 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module PactBroker
add ['test','error'], Api::Resources::ErrorTest, {resource_name: "error_test"}

add ['integrations'], Api::Resources::Integrations, {resource_name: "integrations"}
add ['integrations', 'provider', :provider_name, 'consumer', :consumer_name], Api::Resources::Integration, {resource_name: "integration"}
add [], Api::Resources::Index, {resource_name: "index"}
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def pact_versions_url consumer_name, provider_name, base_url = ""
"#{base_url}/pacts/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}/versions"
end

def integration_url consumer_name, provider_name, base_url = ""
"#{base_url}/integrations/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
end

def previous_distinct_diff_url pact, base_url
pact_url(base_url, pact) + "/diff/previous-distinct"
end
Expand Down
22 changes: 22 additions & 0 deletions lib/pact_broker/api/resources/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'pact_broker/api/resources/base_resource'

module PactBroker
module Api
module Resources
class Integration < BaseResource
def allowed_methods
["OPTIONS", "DELETE"]
end

def resource_exists?
consumer && provider
end

def delete_resource
integration_service.delete(consumer_name, provider_name)
true
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class PactPublication < Sequel::Model(:pact_publications)

dataset_module do
include PactBroker::Repositories::Helpers

def tag tag_name
filter = name_like(Sequel.qualify(:tags, :name), tag_name)
join(:tags, {version_id: :consumer_version_id}).where(filter)
end
end

def before_create
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/ui/view_models/index_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def pact_versions_url
PactBroker::Api::PactBrokerUrls.pact_versions_url(consumer_name, provider_name, PactBroker.configuration.base_url)
end

def integration_url
PactBroker::Api::PactBrokerUrls.integration_url(consumer_name, provider_name, PactBroker.configuration.base_url)
end

def webhook_label
return "" unless show_webhook_status?
case @relationship.webhook_status
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/ui/views/index/_css_and_js.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%link{rel: 'stylesheet', href: '/stylesheets/index.css'}
%link{rel: 'stylesheet', href: '/stylesheets/material-menu.css'}
%link{rel: 'stylesheet', href: '/stylesheets/material-icon.css'}
%link{rel: 'stylesheet', href: '/stylesheets/jquery-confirm.min.css'}
%script{type: 'text/javascript', src:'/javascripts/jquery.tablesorter.min.js'}
%script{type: 'text/javascript', src:'/javascripts/material-menu.js'}
%script{type: 'text/javascript', src:'/javascripts/index.js'}
%script{type: 'text/javascript', src:'/javascripts/jquery-confirm.min.js'}
55 changes: 27 additions & 28 deletions lib/pact_broker/ui/views/index/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,34 @@
Last<br>verified
%th
%tbody
- index_items.each do | index_item |
%tr{'data-pact-versions-url': index_item.pact_versions_url, 'data-consumer-name': index_item.consumer_name, 'data-provider-name': index_item.provider_name, 'data-integration-url': index_item.integration_url}
%td
%td.consumer
%a{ href: index_item.consumer_group_url }
= escape_html(index_item.consumer_name)
%td.pact
%span.pact
%a{ href: index_item.latest_pact_url, :title => "View pact" }
%span.pact-matrix
%a{ href: index_item.pact_matrix_url, title: "View pact matrix" }
%td.provider
%a{ href: index_item.provider_group_url }
= escape_html(index_item.provider_name)
%td
%td
= index_item.publication_date_of_latest_pact
%td{class: index_item.webhook_status}
%a{ href: index_item.webhook_url }
= index_item.webhook_label

- index_items.each do | index_item |
%tr
%td
%td.consumer
%a{ href: index_item.consumer_group_url }
= escape_html(index_item.consumer_name)
%td.pact
%span.pact
%a{ href: index_item.latest_pact_url, :title => "View pact" }
%span.pact-matrix
%a{ href: index_item.pact_matrix_url, title: "View pact matrix" }
%td.provider
%a{ href: index_item.provider_group_url }
= escape_html(index_item.provider_name)
%td
%td
= index_item.publication_date_of_latest_pact
%td{class: index_item.webhook_status}
%a{ href: index_item.webhook_url }
= index_item.webhook_label

%td{ class: index_item.verification_status, title: index_item.verification_tooltip, "data-toggle": "tooltip", "data-placement": "left" }
%div
= index_item.last_verified_date
- if index_item.warning?
%span.glyphicon.glyphicon-warning-sign{ 'aria-hidden': true }
%td
%span.integration-settings.glyphicon.glyphicon-cog{ 'aria-hidden': true, 'data-pact-versions-url': index_item.pact_versions_url, 'data-consumer-name': index_item.consumer_name, 'data-provider-name': index_item.provider_name}
%td{ class: index_item.verification_status, title: index_item.verification_tooltip, "data-toggle": "tooltip", "data-placement": "left" }
%div
= index_item.last_verified_date
- if index_item.warning?
%span.glyphicon.glyphicon-warning-sign{ 'aria-hidden': true }
%td
%span.integration-settings.glyphicon.glyphicon-cog{ 'aria-hidden': true }
%div.relationships-size
= index_items.size_label
Expand Down
137 changes: 119 additions & 18 deletions public/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ $(document).ready(function() {
items: [
{
type: "normal",
text: "Delete all pact versions...",
click: function(e) {
promptToDeletePactVersions($(e).data(), $(e).closest("tr"));
}
text: "Delete pacts ...",
click: handleDeletePactsSelected
},
{
type: "normal",
text: "Delete integration...",
click: handleDeleteIntegrationsSelected
}
]
})
Expand All @@ -18,23 +21,117 @@ $(document).ready(function() {
});
});

function promptToDeletePactVersions(rowData, row) {
function handleDeletePactsSelected(clickedElement) {
const tr = $(clickedElement).closest("tr");
const confirmationText = createPactVersionsDeletionConfirmationText(tr.data());
promptToDeleteResources(
tr,
tr.data().pactVersionsUrl,
confirmationText
);
}

function handleDeleteIntegrationsSelected(clickedElement) {
const tr = $(clickedElement).closest("tr");
const confirmationText = createIntegrationDeletionConfirmationText(
tr.data()
);
promptToDeleteResources(
tr,
tr.data().integrationUrl,
confirmationText
);
}

function createIntegrationDeletionConfirmationText(rowData) {
return `This will delete ${rowData.consumerName} and ${
rowData.providerName
}, and all associated data (pacts, verifications, application versions, tags and webhooks) that are not associated with other integrations. Do you wish to continue?`;
}

function promptToDeleteIntegration(rowData, row) {
const agree = confirm(
`This will delete all versions of the pact between ${
rowData.consumerName
} and ${rowData.providerName}. It will keep ${rowData.consumerName} and ${
`This will delete ${rowData.consumerName} and ${
rowData.providerName
}, and all other data related to them (webhooks, application versions, and tags). Do you wish to continue?`
}, and all associated data (pacts, verifications, application versions, tags and webhooks). Do you wish to continue?`
);
if (agree) {
}

function highlightRowsToBeDeleted(table, consumerName, providerName) {
table
.children("tbody")
.find(`[data-consumer-name="${consumerName}"]`)
.children("td")
.addClass("to-be-deleted");
table
.children("tbody")
.find(`[data-provider-name="${providerName}"]`)
.children("td")
.addClass("to-be-deleted");
}

function highlightRowToBeDeleted(row) {
row.children("td").addClass("to-be-deleted");
}

function unHighlightRows(table) {
table.find(".to-be-deleted").removeClass("to-be-deleted");
}

function createPactVersionsDeletionConfirmationText(rowData) {
return `This will delete all versions of the pact between ${
rowData.consumerName
} and ${rowData.providerName}. It will keep ${rowData.consumerName} and ${
rowData.providerName
}, and all other data related to them (webhooks, verifications, application versions, and tags). Do you wish to continue?`;
}

function confirmDeletePactVersions(
rowData,
confirmationText,
confirmCallbak,
cancelCallback
) {
$.confirm({
title: "Confirm!",
content: confirmationText,
buttons: {
delete: {
text: "DELETE",
btnClass: "alert alert-danger",
keys: ["enter", "shift"],
action: confirmCallbak
},
cancel: cancelCallback
}
});
}

function promptToDeleteResources(row, deletionUrl, confirmationText) {
const rowData = row.data();
const table = row.closest("table");
const cancel = function() {
unHighlightRows(table);
};
const confirm = function() {
deletePactVersions(
rowData.pactVersionsUrl,
deletionUrl,
function() {
handleDeletionSuccess(row);
},
handleDeletionFailure
function(response) {
handleDeletionFailure(table, response);
}
);
}
};

highlightRowToBeDeleted(row);
confirmDeletePactVersions(
rowData,
confirmationText,
confirm,
cancel
);
}

function handleDeletionSuccess(row) {
Expand All @@ -50,21 +147,25 @@ function handleDeletionSuccess(row) {
});
}

function handleDeletionFailure(response) {
function handleDeletionFailure(table, response) {
unHighlightRows(table);
let errorMessage = null;

if (response.error && response.error.message && response.error.reference) {
errorMessage =
"Could not delete resources due to error: " +
"<p>Could not delete resources due to error: " +
response.error.message +
"\nError reference: " +
response.error.reference;
"</p><p>Error reference: " +
response.error.reference + "</p>";
} else {
errorMessage =
"Could not delete resources due to error: " + JSON.stringify(response);
}

alert(errorMessage);
$.alert({
title: 'Error',
content: errorMessage,
});
}

function deletePactVersions(url, successCallback, errorCallback) {
Expand Down
10 changes: 10 additions & 0 deletions public/javascripts/jquery-confirm.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion public/stylesheets/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ div.tag {
div.getting-started {
max-width: 600px;
margin-bottom: 50px;
}
}

td.to-be-deleted {
background-color: #f2dede !important
}
9 changes: 9 additions & 0 deletions public/stylesheets/jquery-confirm.min.css

Large diffs are not rendered by default.

0 comments on commit f520520

Please sign in to comment.