Skip to content

Commit 53a4291

Browse files
committed
Fix example data creation
1 parent 1a943e7 commit 53a4291

1 file changed

Lines changed: 53 additions & 91 deletions

File tree

lib/tasks/create_test_data.rake

Lines changed: 53 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,70 @@
11
namespace :redmine do
2-
desc "Create a test project and a test issue"
2+
desc "Create a test project and a test issue with a detailed history"
33
task create_test_data: :environment do
4-
# Check if a project with the identifier already exists
5-
project_identifier = "test-project"
6-
existing_project = Project.find_by(identifier: project_identifier)
7-
if existing_project
8-
puts "Project with identifier '#{project_identifier}' already exists. Skipping creation."
9-
project = existing_project
10-
else
11-
# Create the test project
12-
project = Project.new(
13-
name: "Test Project",
14-
identifier: project_identifier,
15-
description: "This is a test project."
16-
)
17-
if project.save
18-
puts "Test project created with ID: #{project.id}"
19-
else
20-
puts "Failed to create test project: #{project.errors.full_messages.join(', ')}"
21-
exit 1
22-
end
4+
# --- 1. SETUP PROJECT AND AUTHOR ---
5+
project = Project.find_or_create_by!(identifier: "test-project") do |p|
6+
p.name = "Test Project"
7+
p.description = "This is a test project."
8+
puts "Test project created."
239
end
2410

25-
# Fetch the first user as the author (assuming default data is loaded)
2611
author = User.active.first
2712
unless author
28-
puts "No active user found to set as the author of the issue. Please ensure users exist."
29-
exit 1
13+
puts "No active user found. Please ensure users exist."
14+
return
3015
end
3116

32-
# Create a test issue if it doesn't already exist
17+
# --- 2. CREATE THE ISSUE (if it doesn't exist) ---
3318
issue_subject = "Signup confirmation emails not sent"
34-
existing_issue = Issue.find_by(subject: issue_subject, project_id: project.id)
35-
if existing_issue
36-
puts "Issue with subject '#{issue_subject}' already exists in the project. Skipping creation."
37-
else
38-
issue = Issue.new(
39-
project_id: project.id,
40-
tracker: Tracker.first, # Assuming you have at least one tracker
41-
subject: issue_subject,
42-
description: "Users are not receiving signup confirmation emails in production.\nObserved: multiple users report they never receive the confirmation email after registering.\nExpected: users should receive a confirmation email that contains an activation link.",
43-
author_id: author.id
44-
)
45-
46-
if issue.save
47-
puts "Test issue created with ID: #{issue.id}"
48-
49-
# reload to obtain the latest lock_version before further updates
50-
issue.reload
51-
52-
# 1) Add an initial note with reproduction steps and observations
53-
issue.init_journal(author, "Initial analysis: no emails found in the mailer logs for recent signups. Repro steps: 1) Go to /signup 2) Create a new account 3) Observe no confirmation email received.")
54-
if issue.save
55-
puts "Added initial note to issue ##{issue.id}."
56-
else
57-
puts "Failed to add initial note: #{issue.errors.full_messages.join(', ')}"
58-
end
19+
issue = Issue.find_or_initialize_by(subject: issue_subject, project_id: project.id)
5920

60-
issue.reload
61-
62-
# 2) Assign to a developer and raise priority with a note describing next actions
63-
assignee = User.active.where.not(id: author.id).first || author
64-
high_priority = IssuePriority.order(:id).last || IssuePriority.first
65-
issue.init_journal(author, "Assigning to #{assignee.login} to investigate SMTP and mailer settings. Will check environment variables and mailer host.")
66-
issue.assigned_to = assignee
67-
issue.priority = high_priority
68-
if issue.save
69-
puts "Assigned issue ##{issue.id} to #{assignee.login} and set priority to '#{high_priority.name}'."
70-
else
71-
puts "Failed to assign or set priority: #{issue.errors.full_messages.join(', ')}"
72-
end
73-
74-
issue.reload
21+
if issue.new_record?
22+
issue.assign_attributes(
23+
tracker: Tracker.first,
24+
description: "Users are not receiving signup confirmation emails in production...",
25+
author: author
26+
)
27+
issue.save!
28+
puts "Test issue created with ID: #{issue.id}"
7529

76-
# 3) Change status to 'In Progress' without a note (simulating a status-only update)
77-
in_progress = IssueStatus.find_by(name: 'In Progress') || IssueStatus.find_by(is_default: false) || IssueStatus.first
78-
issue.init_journal(author)
79-
issue.status = in_progress
80-
# Ensure no notes are saved for this transition
81-
issue.notes = nil
82-
if issue.save
83-
puts "Changed status of issue ##{issue.id} to '#{in_progress.name}' without a note."
84-
else
85-
puts "Failed to change status: #{issue.errors.full_messages.join(', ')}"
86-
end
30+
# --- 3. ADD SEPARATE UPDATES TO THE ISSUE HISTORY ---
31+
# Each block now starts by re-finding the issue to guarantee a fresh object.
8732

88-
issue.reload
33+
# Update 1: Add initial analysis note
34+
issue = Issue.find(issue.id) # Re-find the issue
35+
issue.init_journal(author, "Initial analysis: no emails found in the mailer logs for recent signups. Repro steps: 1) Go to /signup 2) Create a new account 3) Observe no confirmation email received.")
36+
issue.save!
37+
puts "Added initial note to issue ##{issue.id}."
8938

90-
# 4) Developer adds a follow-up note with findings (e.g. missing MAILER_HOST) and marks for fix
91-
issue.init_journal(assignee, "Follow-up: found MAILER_HOST environment variable missing in production. Will add default and redeploy to staging for verification.")
92-
issue.status = in_progress
93-
if issue.save
94-
puts "Added developer follow-up note to issue ##{issue.id}."
95-
else
96-
puts "Failed to add developer note: #{issue.errors.full_messages.join(', ')}"
97-
end
39+
# Update 2: Assign, raise priority, and add a note
40+
issue = Issue.find(issue.id) # Re-find the issue
41+
assignee = User.active.where.not(id: author.id).first || author
42+
high_priority = IssuePriority.order(:position).last
43+
issue.init_journal(author, "Assigning to #{assignee.login} to investigate SMTP and mailer settings.")
44+
issue.assigned_to = assignee
45+
issue.priority = high_priority
46+
issue.save!
47+
puts "Assigned issue ##{issue.id} to #{assignee.login} and set priority."
9848

99-
else
100-
puts "Failed to create test issue: #{issue.errors.full_messages.join(', ')}"
101-
exit 1
49+
# Update 3: Change status to 'In Progress' without a note
50+
issue = Issue.find(issue.id) # Re-find the issue
51+
in_progress_status = IssueStatus.find_by(name: 'In Progress')
52+
if in_progress_status
53+
issue.init_journal(assignee) # Journal entry is created by the new assignee
54+
issue.status = in_progress_status
55+
issue.save!
56+
puts "Changed status of issue ##{issue.id} to '#{in_progress_status.name}'."
10257
end
58+
59+
# Update 4: Developer adds a follow-up note
60+
issue = Issue.find(issue.id) # Re-find the issue
61+
issue.init_journal(assignee, "Follow-up: found MAILER_HOST environment variable missing in production. Will add default and redeploy.")
62+
issue.save!
63+
puts "Added developer follow-up note to issue ##{issue.id}."
64+
65+
puts "Successfully created issue ##{issue.id} with multiple history entries."
66+
else
67+
puts "Issue with subject '#{issue_subject}' already exists. Skipping."
10368
end
10469
end
105-
end
106-
107-
108-
70+
end

0 commit comments

Comments
 (0)