Skip to content

Commit 36639ba

Browse files
committed
Implements issue creation threshold, fixes #26
Signed-off-by: Daniel Kastl <[email protected]>
1 parent 91a20de commit 36639ba

File tree

5 files changed

+98
-57
lines changed

5 files changed

+98
-57
lines changed

app/controllers/subscription_issues_controller.rb

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,74 @@ class SubscriptionIssuesController < ApplicationController
55
accept_api_auth :create
66

77
def create
8-
# Create a new issue based on the provided parameters and the subscription template
9-
@issue = Issue.new()
10-
@issue.project = @subscription_template.project
11-
@issue.tracker = @subscription_template.tracker
12-
@issue.subject = params[:subject]
13-
@issue.description = params[:description]
14-
@issue.is_private = @subscription_template.is_private
15-
@issue.status = @subscription_template.issue_status
16-
@issue.author = User.current
17-
@issue.category = @subscription_template.issue_category
18-
@issue.priority = @subscription_template.issue_priority
19-
@issue.fixed_version = @subscription_template.version
8+
# Check if there's an existing issue with the same fiware_entity and subscription_template_id
9+
# that was created within the threshold_create time frame
10+
existing_issue = Issue.where(fiware_entity: params["entity"], subscription_template_id: @subscription_template.id)
11+
.where("created_on >= ?", Time.now - @subscription_template.threshold_create.seconds)
12+
.order(created_on: :desc)
13+
.first
2014

21-
logger.info "################################################################################"
22-
logger.info "################################################################################"
23-
logger.info "Request received to create a new issue based on a subscription template"
24-
logger.info "Request parameters: #{params}"
25-
logger.info "################################################################################"
15+
if existing_issue
16+
# Create a new journal note for the existing issue
17+
note = existing_issue.init_journal(User.current, params["notes"])
2618

27-
# If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
28-
# try to convert it to an RGeo geometry object
29-
if Redmine::Plugin.installed?(:redmine_gtt) && @subscription_template.project.module_enabled?('gtt') && params[:geometry]
30-
begin
31-
@issue.geom = RedmineGtt::Conversions.to_geom(params[:geometry].to_json)
32-
rescue => e
33-
logger.warn "Failed to convert geometry data: #{e.message}"
19+
# If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
20+
# try to convert it to an RGeo geometry object and update the issue's geometry
21+
if Redmine::Plugin.installed?(:redmine_gtt) && @subscription_template.project.module_enabled?('gtt') && params[:geometry]
22+
begin
23+
new_geom = RedmineGtt::Conversions.to_geom(params[:geometry].to_json)
24+
if new_geom != existing_issue.geom
25+
old_geom = existing_issue.geom
26+
existing_issue.geom = new_geom
27+
# Create a new JournalDetail record to track the change to the geom attribute
28+
note.details.build(property: 'attr', prop_key: 'geom', old_value: old_geom, value: new_geom)
29+
end
30+
rescue => e
31+
logger.warn "Failed to convert geometry data: #{e.message}"
32+
end
3433
end
35-
end
3634

37-
logger.info "################################################################################"
38-
logger.info "Create issue key/value:"
39-
@issue.attributes.each do |key, value|
40-
logger.info "#{key}: #{value}"
41-
end
42-
logger.info "################################################################################"
43-
logger.info "Subscription template key/value pairs:"
44-
@subscription_template.attributes.each do |key, value|
45-
logger.info "#{key}: #{value}"
46-
end
47-
logger.info "################################################################################"
48-
logger.info "################################################################################"
49-
50-
if @issue.save
51-
# Respond with the newly created issue and a 201 status code
52-
render json: @issue.as_json(include: [:status, :tracker, :author, :assigned_to]), status: :created
35+
# Save the existing issue and respond with the appropriate JSON response
36+
if existing_issue.save
37+
render json: existing_issue.as_json(include: [:status, :tracker, :author, :assigned_to, :journals]), status: :ok
38+
else
39+
render json: { errors: existing_issue.errors.full_messages }, status: :unprocessable_entity
40+
end
5341
else
54-
# If saving fails, respond with error messages and a 422 status code
55-
render json: { errors: @issue.errors.full_messages }, status: :unprocessable_entity
42+
# Create a new issue based on the provided parameters and the subscription template
43+
@issue = Issue.new()
44+
@issue.project = @subscription_template.project
45+
@issue.tracker = @subscription_template.tracker
46+
@issue.subject = params[:subject]
47+
@issue.description = params[:description]
48+
@issue.is_private = @subscription_template.is_private
49+
@issue.status = @subscription_template.issue_status
50+
@issue.author = User.current
51+
@issue.category = @subscription_template.issue_category
52+
@issue.priority = @subscription_template.issue_priority
53+
@issue.fixed_version = @subscription_template.version
54+
55+
# Set the fiware_entity and subscription_template_id attributes of the new issue
56+
@issue.fiware_entity = params["entity"]
57+
@issue.subscription_template_id = @subscription_template.id
58+
59+
# If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
60+
# try to convert it to an RGeo geometry object
61+
if Redmine::Plugin.installed?(:redmine_gtt) && @subscription_template.project.module_enabled?('gtt') && params[:geometry]
62+
begin
63+
@issue.geom = RedmineGtt::Conversions.to_geom(params[:geometry].to_json)
64+
rescue => e
65+
logger.warn "Failed to convert geometry data: #{e.message}"
66+
end
67+
end
68+
69+
if @issue.save
70+
# Respond with the newly created issue and a 201 status code
71+
render json: @issue.as_json(include: [:status, :tracker, :author, :assigned_to]), status: :created
72+
else
73+
# If saving fails, respond with error messages and a 422 status code
74+
render json: { errors: @issue.errors.full_messages }, status: :unprocessable_entity
75+
end
5676
end
5777
end
5878

app/views/subscription_templates/publish.js.erb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ var requestOptions = {
1818
redirect: 'follow'
1919
};
2020

21+
function showNotification(message) {
22+
// Get the notification box
23+
var notification = document.getElementById('temporaryNotification');
24+
25+
// Change the text of the notification box
26+
notification.textContent = message;
27+
28+
// Show the notification box
29+
notification.classList.add('visible');
30+
31+
// Hide the notification box after 3 seconds
32+
setTimeout(function() {
33+
notification.classList.remove('visible');
34+
}, 3000);
35+
}
36+
2137
// Send the POST request
2238
fetch('<%= @broker_url %>', requestOptions)
2339
.then(response => {
2440
var location = response.headers.get('Location');
41+
if (!location) {
42+
throw new Error('Location header is missing in the response');
43+
}
2544
var parts = location.split('/');
2645
var subscriptionId = parts[parts.length - 1];
2746

@@ -41,18 +60,12 @@ fetch('<%= @broker_url %>', requestOptions)
4160
var subscriptionTemplateList = document.getElementById('subscriptionTemplateList');
4261
subscriptionTemplateList.innerHTML = result;
4362

44-
// Get the notification box
45-
var notification = document.getElementById('temporaryNotification');
46-
47-
// Change the text of the notification box
48-
notification.textContent = "<%= l(:subscription_published) %>";
49-
50-
// Show the notification box
51-
notification.classList.add('visible');
52-
53-
// Hide the notification box after 3 seconds
54-
setTimeout(function() {
55-
notification.classList.remove('visible');
56-
}, 3000);
63+
// Show success notification
64+
showNotification("<%= l(:subscription_published) %>");
5765
})
58-
.catch(error => console.log('error', error));
66+
.catch(error => {
67+
console.log('error', error);
68+
69+
// Show error notification
70+
showNotification("<%= l(:subscription_published_error) %>");
71+
});

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ en:
8282
link_to_publish: "Publish"
8383
link_to_publish_hint: "Publish subscription to broker"
8484
subscription_published: "Subscription published to broker"
85+
subscription_published_error: "There was a problem publishing the subscription from the broker"
8586

8687
link_to_unpublish: "Unpublish"
8788
link_to_unpublish_hint: "Unpublish subscription to broker"

config/locales/ja.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ en:
8282
link_to_publish: "Publish"
8383
link_to_publish_hint: "Publish subscription to broker"
8484
subscription_published: "Subscription published to broker"
85+
subscription_published_error: "There was a problem publishing the subscription from the broker"
8586

8687
link_to_unpublish: "Unpublish"
8788
link_to_unpublish_hint: "Unpublish subscription to broker"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddFiwareEntityToIssues < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :issues, :fiware_entity, :string
4+
add_reference :issues, :subscription_template, index: true
5+
end
6+
end

0 commit comments

Comments
 (0)