Skip to content

Commit df0f35c

Browse files
committed
feat: allow pact to be deleted through the UI
Closes: pact-foundation#179
1 parent 1be9f1b commit df0f35c

File tree

8 files changed

+185
-23
lines changed

8 files changed

+185
-23
lines changed

lib/pact_broker/api/renderers/html_pact_renderer.rb

+20-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ def head
4040
<link rel='stylesheet' type='text/css' href='/stylesheets/github.css'>
4141
<link rel='stylesheet' type='text/css' href='/stylesheets/pact.css'>
4242
<link rel='stylesheet' type='text/css' href='/stylesheets/github-json.css'>
43+
<link rel='stylesheet' type='text/css' href='/css/bootstrap.min.css'>
44+
<link rel='stylesheet' type='text/css' href='/stylesheets/material-menu.css'>
45+
<link rel='stylesheet' type='text/css' href='/stylesheets/jquery-confirm.min.css'>
4346
<script src='/javascripts/highlight.pack.js'></script>
4447
<script src='/javascripts/jquery-3.3.1.min.js'></script>
4548
<script src='/js/bootstrap.min.js'></script>
49+
<script src='/javascripts/material-menu.js'></script>
4650
<script src='/javascripts/pact.js'></script>
51+
<script src='/javascripts/jquery-confirm.min.js'></script>
4752
<script>hljs.initHighlightingOnLoad();</script>"
4853
end
4954

@@ -69,19 +74,26 @@ def pact_metadata
6974
<li>
7075
<a href=\"/\">Home</a>
7176
</li>
77+
<li>
78+
<span data-api-browser-url=\"#{json_url}\"
79+
data-consumer-name=\"#{@pact.consumer.name}\"
80+
data-consumer-version-number=\"#{@pact.consumer_version_number}\"
81+
data-pact-url=\"#{pact_url}\"
82+
class='more-options glyphicon glyphicon-option-horizontal' aria-hidden='true'></span>
83+
</li>
7284
</ul>
7385
</div>"
7486
end
7587

7688
def badge_list_item
77-
"<li class='badge'>
89+
"<li class='pact-badge'>
7890
<img src='#{badge_url}'/>
7991
</li>
8092
"
8193
end
8294

8395
def badge_markdown_item
84-
"<li class='badge-markdown' style='display:none'>
96+
"<li class='pact-badge-markdown' style='display:none'>
8597
<textarea rows='3' cols='100'>#{badge_markdown}</textarea>
8698
</li>"
8799
end
@@ -120,11 +132,15 @@ def json_url
120132
end
121133

122134
def pact_url
123-
PactBroker::Api::PactBrokerUrls.pact_url '', @pact
135+
PactBroker::Api::PactBrokerUrls.pact_url base_url, @pact
124136
end
125137

126138
def matrix_url
127-
PactBroker::Api::PactBrokerUrls.matrix_url_from_params consumer_name: @pact.consumer.name, provider_name: @pact.provider.name
139+
PactBroker::Api::PactBrokerUrls.matrix_url_from_params({ consumer_name: @pact.consumer.name, provider_name: @pact.provider.name }, base_url)
140+
end
141+
142+
def latest_pact_url
143+
PactBroker::Api::PactBrokerUrls.latest_pact_url base_url, @pact
128144
end
129145

130146
def badge_target_url
@@ -162,7 +178,6 @@ def consumer_contract
162178
logger.info "Could not parse the following content to a Pact due to #{e.class} #{e.message}, showing raw content instead: #{@json_content}"
163179
raise NotAPactError
164180
end
165-
166181
end
167182
end
168183
end

lib/pact_broker/api/resources/pact.rb

+14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def to_html
8888

8989
def delete_resource
9090
pact_service.delete(pact_params)
91+
set_post_deletion_response
9192
true
9293
end
9394

@@ -104,6 +105,19 @@ def pact_params
104105
def update_matrix_after_request?
105106
request.put? || request.patch?
106107
end
108+
109+
def set_post_deletion_response
110+
latest_pact = pact_service.find_latest_pact(pact_params)
111+
response_body = { "_links" => {} }
112+
if latest_pact
113+
response_body["_links"]["pb:latest-pact-version"] = {
114+
href: latest_pact_url(base_url, latest_pact),
115+
title: "Latest pact"
116+
}
117+
end
118+
response.body = response_body.to_json
119+
response.headers["Content-Type" => "application/hal+json;charset=utf-8"]
120+
end
107121
end
108122
end
109123
end

public/javascripts/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ function createPactVersionsDeletionConfirmationText(rowData) {
8686
}, and all other data related to them (webhooks, verifications, application versions, and tags). Do you wish to continue?`;
8787
}
8888

89-
function confirmDeletePactVersions(
89+
function confirmDeleteResources(
9090
rowData,
9191
confirmationText,
92-
confirmCallbak,
92+
confirmCallback,
9393
cancelCallback
9494
) {
9595
$.confirm({
@@ -100,7 +100,7 @@ function confirmDeletePactVersions(
100100
text: "DELETE",
101101
btnClass: "alert alert-danger",
102102
keys: ["enter", "shift"],
103-
action: confirmCallbak
103+
action: confirmCallback
104104
},
105105
cancel: cancelCallback
106106
}
@@ -114,7 +114,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) {
114114
unHighlightRows(table);
115115
};
116116
const confirm = function() {
117-
deletePactVersions(
117+
deleteResources(
118118
deletionUrl,
119119
function() {
120120
handleDeletionSuccess(row);
@@ -126,7 +126,7 @@ function promptToDeleteResources(row, deletionUrl, confirmationText) {
126126
};
127127

128128
highlightRowToBeDeleted(row);
129-
confirmDeletePactVersions(
129+
confirmDeleteResources(
130130
rowData,
131131
confirmationText,
132132
confirm,
@@ -168,7 +168,7 @@ function handleDeletionFailure(table, response) {
168168
});
169169
}
170170

171-
function deletePactVersions(url, successCallback, errorCallback) {
171+
function deleteResources(url, successCallback, errorCallback) {
172172
$.ajax({
173173
url: url,
174174
dataType: "json",

public/javascripts/pact.js

+137-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,139 @@
1-
$( document ).ready(function() {
2-
$('.badge').click(function(){
3-
$('.badge-markdown').toggle();
1+
$(document).ready(function() {
2+
$(".pact-badge").click(function() {
3+
$(".pact-badge-markdown").toggle();
44
});
5+
6+
$(".more-options")
7+
.materialMenu("init", {
8+
position: "overlay",
9+
animationSpeed: 1,
10+
items: [
11+
// {
12+
// type: "normal",
13+
// text: "View in API Browser",
14+
// click: function(openMenuElement) {
15+
// window.location.href = openMenuElement.data().apiBrowserUrl;
16+
// }
17+
// },
18+
// {
19+
// type: "normal",
20+
// text: "View Matrix",
21+
// click: function(openMenuElement) {
22+
// window.location.href = openMenuElement.data().matrixUrl;
23+
// }
24+
// },
25+
{
26+
type: "normal",
27+
text: "Delete ...",
28+
click: function(openMenuElement) {
29+
promptToDeleteResource(openMenuElement.data().pactUrl, createDeletionConfirmationText(openMenuElement.data()))
30+
}
31+
}
32+
]
33+
})
34+
.click(function() {
35+
$(this).materialMenu("open");
36+
});
537
});
38+
39+
function createDeletionConfirmationText(data) {
40+
return `Do you wish to delete the pact for version ${data.consumerVersionNumber} of ${data.consumerName}?`;
41+
}
42+
43+
44+
function confirmDeleteResource(
45+
confirmationText,
46+
confirmCallbak,
47+
cancelCallback
48+
) {
49+
$.confirm({
50+
title: "Confirm!",
51+
content: confirmationText,
52+
buttons: {
53+
delete: {
54+
text: "DELETE",
55+
btnClass: "alert alert-danger",
56+
keys: ["enter", "shift"],
57+
action: confirmCallbak
58+
},
59+
cancel: cancelCallback
60+
}
61+
});
62+
}
63+
64+
function promptToDeleteResource(deletionUrl, confirmationText) {
65+
const cancel = function() {};
66+
const confirm = function() {
67+
deleteResource(
68+
deletionUrl,
69+
handleDeletionSuccess,
70+
handleDeletionFailure
71+
);
72+
};
73+
74+
confirmDeleteResource(
75+
confirmationText,
76+
confirm,
77+
cancel
78+
);
79+
}
80+
81+
function handleDeletionSuccess(responseBody) {
82+
if(responseBody._links['pb:latest-pact-version']) {
83+
$.confirm({
84+
title: "Pact deleted",
85+
content: "Where to next?",
86+
buttons: {
87+
latest: {
88+
text: "Latest pact",
89+
keys: ["enter", "shift"],
90+
action: function() { window.location.href = responseBody._links['pb:latest-pact-version'].href }
91+
},
92+
home: {
93+
text: "Home",
94+
action: function() { window.location.href = "/"; }
95+
}
96+
}
97+
});
98+
99+
} else {
100+
window.location.href = "/";
101+
}
102+
}
103+
104+
function handleDeletionFailure(response) {
105+
let errorMessage = null;
106+
107+
if (response.error && response.error.message && response.error.reference) {
108+
errorMessage =
109+
"<p>Could not delete resources due to error: " +
110+
response.error.message +
111+
"</p><p>Error reference: " +
112+
response.error.reference + "</p>";
113+
} else {
114+
errorMessage =
115+
"Could not delete resources due to error: " + JSON.stringify(response);
116+
}
117+
118+
$.alert({
119+
title: 'Error',
120+
content: errorMessage,
121+
});
122+
}
123+
124+
function deleteResource(url, successCallback, errorCallback) {
125+
$.ajax({
126+
url: url,
127+
dataType: "json",
128+
type: "delete",
129+
accepts: {
130+
text: "application/hal+json"
131+
},
132+
success: function(data, textStatus, jQxhr) {
133+
successCallback(data);
134+
},
135+
error: function(jqXhr, textStatus, errorThrown) {
136+
errorCallback(jqXhr.responseJSON);
137+
}
138+
});
139+
}

public/stylesheets/pact.css

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ pre {
1515
font-size: 1em
1616
}
1717

18-
li.badge {
18+
li.pact-badge {
1919
cursor: pointer;
2020
}
2121

22-
li.badge > img {
22+
li.pact-badge > img {
2323
cursor: pointer;
2424
height: 20px;
2525
}
2626

27-
div.badge-markdown {
27+
div.pact-badge-markdown {
2828
background-color: #f8f8f8
2929
}

spec/features/delete_pact_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
expect{ subject }.to change{ PactBroker::Pacts::PactPublication.count }.by(-1)
2020
end
2121

22-
it "returns a 204" do
23-
expect(subject.status).to be 204
22+
it "returns a 200" do
23+
expect(subject.status).to be 200
2424
end
2525
end
2626

spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Renderers
1111
ENV['BACKUP_TZ'] = ENV['TZ']
1212
ENV['TZ'] = "Australia/Melbourne"
1313
PactBroker.configuration.enable_public_badge_access = true
14-
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('', pact).and_return(pact_url)
14+
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).with('http://base', pact).and_return(pact_url)
1515
allow_any_instance_of(HtmlPactRenderer).to receive(:logger).and_return(logger)
1616

1717
Timecop.freeze(created_at + 3)

spec/lib/pact_broker/api/resources/pact_spec.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ module Resources
153153
subject
154154
end
155155

156-
it "returns a 204" do
157-
expect(subject.status).to eq 204
156+
it "returns a 200" do
157+
expect(subject.status).to eq 200
158158
end
159159
end
160160

@@ -164,7 +164,6 @@ module Resources
164164
it "returns a 404" do
165165
expect(subject.status).to eq 404
166166
end
167-
168167
end
169168
end
170169
end

0 commit comments

Comments
 (0)