Skip to content

Commit

Permalink
feat: allow pact to be deleted through the UI
Browse files Browse the repository at this point in the history
Closes: #179
  • Loading branch information
bethesque committed Apr 14, 2019
1 parent 1be9f1b commit df0f35c
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 23 deletions.
25 changes: 20 additions & 5 deletions lib/pact_broker/api/renderers/html_pact_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ def head
<link rel='stylesheet' type='text/css' href='/stylesheets/github.css'>
<link rel='stylesheet' type='text/css' href='/stylesheets/pact.css'>
<link rel='stylesheet' type='text/css' href='/stylesheets/github-json.css'>
<link rel='stylesheet' type='text/css' href='/css/bootstrap.min.css'>
<link rel='stylesheet' type='text/css' href='/stylesheets/material-menu.css'>
<link rel='stylesheet' type='text/css' href='/stylesheets/jquery-confirm.min.css'>
<script src='/javascripts/highlight.pack.js'></script>
<script src='/javascripts/jquery-3.3.1.min.js'></script>
<script src='/js/bootstrap.min.js'></script>
<script src='/javascripts/material-menu.js'></script>
<script src='/javascripts/pact.js'></script>
<script src='/javascripts/jquery-confirm.min.js'></script>
<script>hljs.initHighlightingOnLoad();</script>"
end

Expand All @@ -69,19 +74,26 @@ def pact_metadata
<li>
<a href=\"/\">Home</a>
</li>
<li>
<span data-api-browser-url=\"#{json_url}\"
data-consumer-name=\"#{@pact.consumer.name}\"
data-consumer-version-number=\"#{@pact.consumer_version_number}\"
data-pact-url=\"#{pact_url}\"
class='more-options glyphicon glyphicon-option-horizontal' aria-hidden='true'></span>
</li>
</ul>
</div>"
end

def badge_list_item
"<li class='badge'>
"<li class='pact-badge'>
<img src='#{badge_url}'/>
</li>
"
end

def badge_markdown_item
"<li class='badge-markdown' style='display:none'>
"<li class='pact-badge-markdown' style='display:none'>
<textarea rows='3' cols='100'>#{badge_markdown}</textarea>
</li>"
end
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/pact_broker/api/resources/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def to_html

def delete_resource
pact_service.delete(pact_params)
set_post_deletion_response
true
end

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions public/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -100,7 +100,7 @@ function confirmDeletePactVersions(
text: "DELETE",
btnClass: "alert alert-danger",
keys: ["enter", "shift"],
action: confirmCallbak
action: confirmCallback
},
cancel: cancelCallback
}
Expand All @@ -114,7 +114,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) {
unHighlightRows(table);
};
const confirm = function() {
deletePactVersions(
deleteResources(
deletionUrl,
function() {
handleDeletionSuccess(row);
Expand All @@ -126,7 +126,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) {
};

highlightRowToBeDeleted(row);
confirmDeletePactVersions(
confirmDeleteResources(
rowData,
confirmationText,
confirm,
Expand Down Expand Up @@ -168,7 +168,7 @@ function handleDeletionFailure(table, response) {
});
}

function deletePactVersions(url, successCallback, errorCallback) {
function deleteResources(url, successCallback, errorCallback) {
$.ajax({
url: url,
dataType: "json",
Expand Down
140 changes: 137 additions & 3 deletions public/javascripts/pact.js
Original file line number Diff line number Diff line change
@@ -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 =
"<p>Could not delete resources due to error: " +
response.error.message +
"</p><p>Error reference: " +
response.error.reference + "</p>";
} 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);
}
});
}
6 changes: 3 additions & 3 deletions public/stylesheets/pact.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions spec/features/delete_pact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions spec/lib/pact_broker/api/resources/pact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -164,7 +164,6 @@ module Resources
it "returns a 404" do
expect(subject.status).to eq 404
end

end
end
end
Expand Down

0 comments on commit df0f35c

Please sign in to comment.