| 1293 |
dpurdie |
1 |
###############################################################################
|
|
|
2 |
# Codestriker: Copyright (c) 2001, 2002 David Sitsky. All rights reserved.
|
|
|
3 |
# sits@users.sourceforge.net
|
|
|
4 |
#
|
|
|
5 |
# This program is free software; you can redistribute it and modify it under
|
|
|
6 |
# the terms of the GPL.
|
|
|
7 |
|
|
|
8 |
# Action object for handling the submission of a new topic.
|
|
|
9 |
|
|
|
10 |
package Codestriker::Action::SubmitNewTopic;
|
|
|
11 |
|
|
|
12 |
use strict;
|
|
|
13 |
|
|
|
14 |
use File::Temp qw/ tempfile /;
|
|
|
15 |
use FileHandle;
|
|
|
16 |
|
|
|
17 |
use Codestriker::Model::Topic;
|
|
|
18 |
use Codestriker::Http::Render;
|
|
|
19 |
use Codestriker::Repository::RepositoryFactory;
|
|
|
20 |
use Codestriker::FileParser::Parser;
|
|
|
21 |
use Codestriker::Model::Project;
|
|
|
22 |
use Codestriker::TopicListeners::Manager;
|
|
|
23 |
|
|
|
24 |
# If the input is valid, create the appropriate topic into the database.
|
|
|
25 |
sub process($$$) {
|
|
|
26 |
my ($type, $http_input, $http_response) = @_;
|
|
|
27 |
|
|
|
28 |
my $query = $http_response->get_query();
|
|
|
29 |
|
|
|
30 |
# Check that the appropriate fields have been filled in.
|
|
|
31 |
my $topic_title = $http_input->get('topic_title');
|
|
|
32 |
my $topic_description = $http_input->get('topic_description');
|
|
|
33 |
my $reviewers = $http_input->get('reviewers');
|
|
|
34 |
my $email = $http_input->get('email');
|
|
|
35 |
my $cc = $http_input->get('cc');
|
|
|
36 |
my $fh = $http_input->get('fh');
|
|
|
37 |
my $topic_file = $http_input->get('fh_filename');
|
|
|
38 |
my $fh_mime_type = $http_input->get('fh_mime_type');
|
|
|
39 |
my $bug_ids = $http_input->get('bug_ids');
|
|
|
40 |
my $repository_name = $http_input->get('repository');
|
|
|
41 |
my $projectid = $http_input->get('projectid');
|
|
|
42 |
my $project_name = $http_input->get('project_name');
|
|
|
43 |
my $start_tag = $http_input->get('start_tag');
|
|
|
44 |
my $end_tag = $http_input->get('end_tag');
|
|
|
45 |
my $module = $http_input->get('module');
|
|
|
46 |
my $obsoletes = $http_input->get('obsoletes');
|
|
|
47 |
my $default_to_head = $http_input->get('default_to_head');
|
|
|
48 |
|
|
|
49 |
my $feedback = "";
|
|
|
50 |
my $topic_text = "";
|
|
|
51 |
|
|
|
52 |
my $url_builder = Codestriker::Http::UrlBuilder->new($query);
|
|
|
53 |
|
|
|
54 |
# Indicate whether the topic text needs to be retrieved by the repository
|
|
|
55 |
# object.
|
|
|
56 |
my $retrieve_text_from_rep = 0;
|
|
|
57 |
if (($start_tag ne "" || $end_tag ne "") && $module ne "") {
|
|
|
58 |
$retrieve_text_from_rep = 1;
|
|
|
59 |
|
|
|
60 |
# Check if this action is permitted.
|
|
|
61 |
if (scalar(@Codestriker::valid_repositories) == 0) {
|
|
|
62 |
$feedback .= "Repository functionality has been disabled. " .
|
|
|
63 |
"Can't create topic text usings tags.\n";
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if ($topic_title eq "") {
|
|
|
68 |
$feedback .= "No topic title was entered.\n";
|
|
|
69 |
}
|
|
|
70 |
if ($topic_description eq "") {
|
|
|
71 |
$feedback .= "No topic description was entered.\n";
|
|
|
72 |
}
|
|
|
73 |
if ($email eq "") {
|
|
|
74 |
$feedback .= "No email address was entered.\n";
|
|
|
75 |
}
|
|
|
76 |
if (!defined $fh && $retrieve_text_from_rep == 0) {
|
|
|
77 |
$feedback .= "No filename or module/tags were entered.\n";
|
|
|
78 |
}
|
|
|
79 |
if ($reviewers eq "") {
|
|
|
80 |
$feedback .= "No reviewers were entered.\n";
|
|
|
81 |
}
|
|
|
82 |
if ($retrieve_text_from_rep && defined $fh) {
|
|
|
83 |
$feedback .= "Topic text specified using tags and uploaded file.\n";
|
|
|
84 |
$feedback .= "Please choose one topic text method, and try again.\n";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$http_response->generate_header(topic_title=>"Create New Topic",
|
|
|
88 |
email=>$email, reviewers=>$reviewers,
|
|
|
89 |
cc=>$cc, repository=>$repository_name,
|
|
|
90 |
projectid=>$projectid,
|
|
|
91 |
reload=>0, cache=>0);
|
|
|
92 |
|
|
|
93 |
# Set the error_vars in case of any errors that will require forwarding
|
|
|
94 |
# to the create topic screen again.
|
|
|
95 |
my $error_vars = {};
|
|
|
96 |
$error_vars->{'version'} = $Codestriker::VERSION;
|
|
|
97 |
$error_vars->{'feedback'} = $feedback;
|
|
|
98 |
$error_vars->{'email'} = $email;
|
|
|
99 |
$error_vars->{'reviewers'} = $reviewers;
|
|
|
100 |
$error_vars->{'cc'} = $cc;
|
|
|
101 |
$error_vars->{'topic_file'} = $topic_file;
|
|
|
102 |
$error_vars->{'topic_description'} = $topic_description;
|
|
|
103 |
$error_vars->{'topic_title'} = $topic_title;
|
|
|
104 |
$error_vars->{'bug_ids'} = $bug_ids;
|
|
|
105 |
$error_vars->{'default_repository'} = $repository_name;
|
|
|
106 |
$error_vars->{'repositories'} = \@Codestriker::valid_repository_names;
|
|
|
107 |
$error_vars->{'start_tag'} = $start_tag;
|
|
|
108 |
$error_vars->{'end_tag'} = $end_tag;
|
|
|
109 |
$error_vars->{'module'} = $module;
|
|
|
110 |
$error_vars->{'obsoletes'} = $obsoletes;
|
|
|
111 |
$error_vars->{'default_to_head'} = $default_to_head;
|
|
|
112 |
$error_vars->{'default_projectid'} = $projectid;
|
|
|
113 |
|
|
|
114 |
my $repository = undef;
|
|
|
115 |
my $repository_url = undef;
|
|
|
116 |
if (scalar(@Codestriker::valid_repositories)) {
|
|
|
117 |
# Set the repository to the default if it is not entered.
|
|
|
118 |
if ($repository_name eq "" || scalar(@Codestriker::valid_repository_names) == 1) {
|
|
|
119 |
$repository_name = $Codestriker::valid_repository_names[0];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
# Check if the repository argument is in fact a configured
|
|
|
123 |
# repository.
|
|
|
124 |
$repository_url = $Codestriker::repository_url_map->{$repository_name};
|
|
|
125 |
|
|
|
126 |
if (defined $repository_url) {
|
|
|
127 |
$repository =
|
|
|
128 |
Codestriker::Repository::RepositoryFactory->get($repository_url);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
if (! defined $repository) {
|
|
|
132 |
$feedback .=
|
|
|
133 |
"The repository value set for \"$repository_name\" is invalid.\n" .
|
|
|
134 |
"Please correct this value in your codestriker.conf file, " .
|
|
|
135 |
"and try again.\n";
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
# Set the projectid to the first (default) if it is invalid.
|
|
|
140 |
my @projects = Codestriker::Model::Project->list();
|
|
|
141 |
my $found_project = 0;
|
|
|
142 |
foreach my $project (@projects) {
|
|
|
143 |
if ((defined $projectid && $project->{id} == $projectid) ||
|
|
|
144 |
(defined $project_name && $project->{name} eq $project_name)) {
|
|
|
145 |
$projectid = $project->{id};
|
|
|
146 |
$found_project = 1;
|
|
|
147 |
last;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
if ($found_project == 0) {
|
|
|
151 |
$projectid = $projects[0]->{id};
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
# Make sure all the conditions from the topic listeners are satisified.
|
|
|
155 |
$feedback .= Codestriker::TopicListeners::Manager::topic_pre_create
|
|
|
156 |
($email, $topic_title, $topic_description,
|
|
|
157 |
$bug_ids, $reviewers, $cc,
|
|
|
158 |
$repository_url, $projectid);
|
|
|
159 |
|
|
|
160 |
# If there is a problem with the input, redirect to the create screen
|
|
|
161 |
# with the message.
|
|
|
162 |
if ($feedback ne "") {
|
|
|
163 |
if (defined $fh) {
|
|
|
164 |
$feedback .=
|
|
|
165 |
"For security reasons, please re-enter the " .
|
|
|
166 |
"file name to upload, if required.\n";
|
|
|
167 |
}
|
|
|
168 |
_forward_create_topic($error_vars, $feedback, $url_builder);
|
|
|
169 |
$http_response->generate_footer();
|
|
|
170 |
return;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
my $topicid = Codestriker::Model::Topic::create_new_topicid();
|
|
|
174 |
|
|
|
175 |
# If the topic text needs to be retrieved from the repository object,
|
|
|
176 |
# create a temporary file to store the topic text.
|
|
|
177 |
my $temp_topic_fh;
|
|
|
178 |
my $temp_error_fh;
|
|
|
179 |
|
|
|
180 |
if ($retrieve_text_from_rep && defined $repository) {
|
|
|
181 |
# Store the topic text into temporary files.
|
|
|
182 |
if (defined $Codestriker::tmpdir && $Codestriker::tmpdir ne "") {
|
|
|
183 |
$temp_topic_fh = tempfile(DIR => $Codestriker::tmpdir);
|
|
|
184 |
$temp_error_fh = tempfile(DIR => $Codestriker::tmpdir);
|
|
|
185 |
}
|
|
|
186 |
else {
|
|
|
187 |
$temp_topic_fh = tempfile();
|
|
|
188 |
$temp_error_fh = tempfile();
|
|
|
189 |
}
|
|
|
190 |
binmode $temp_topic_fh;
|
|
|
191 |
binmode $temp_error_fh;
|
|
|
192 |
|
|
|
193 |
my $rc = $repository->getDiff($start_tag, $end_tag, $module,
|
|
|
194 |
$temp_topic_fh, $temp_error_fh,
|
|
|
195 |
$default_to_head);
|
|
|
196 |
|
|
|
197 |
# Make sure the data has been flushed to disk.
|
|
|
198 |
$temp_topic_fh->flush;
|
|
|
199 |
$temp_error_fh->flush;
|
|
|
200 |
|
|
|
201 |
# Check if the generated diff was too big, and if so, throw an error
|
|
|
202 |
# message on the screen.
|
|
|
203 |
if ($rc == $Codestriker::DIFF_TOO_BIG) {
|
|
|
204 |
$feedback .= "Generated diff file is too big.\n";
|
|
|
205 |
} elsif ($rc == $Codestriker::UNSUPPORTED_OPERATION) {
|
|
|
206 |
$feedback .= "Repository \"" . $repository_name .
|
|
|
207 |
"\" does not support tag retrieval, you have to use the text file upload.\n";
|
|
|
208 |
} elsif ($rc != $Codestriker::OK) {
|
|
|
209 |
$feedback .= "Unexpected error $rc retrieving diff text.\n";
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
# Seek to the beginning of the temporary file so it can be parsed.
|
|
|
213 |
seek($temp_topic_fh, 0, 0);
|
|
|
214 |
|
|
|
215 |
# Set $fh to this file reference which contains the topic data.
|
|
|
216 |
$fh = $temp_topic_fh;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
my @deltas = ();
|
|
|
220 |
if ($feedback eq "") {
|
|
|
221 |
# Try to parse the topic text into its diff chunks.
|
|
|
222 |
@deltas =
|
|
|
223 |
Codestriker::FileParser::Parser->parse($fh, "text/plain", $repository,
|
|
|
224 |
$topicid, $topic_file);
|
|
|
225 |
if ($#deltas == -1) {
|
|
|
226 |
# Nothing in the file, report an error.
|
|
|
227 |
$feedback .= "Reviewable text in topic is empty.\n";
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
if ($feedback ne "") {
|
|
|
232 |
# If there was a problem generating the diff file, remove the
|
|
|
233 |
# temporary files, and direct control to the create screen again.
|
|
|
234 |
$temp_topic_fh->close if defined $temp_topic_fh;
|
|
|
235 |
$temp_error_fh->close if defined $temp_error_fh;
|
|
|
236 |
_forward_create_topic($error_vars, $feedback, $url_builder);
|
|
|
237 |
$http_response->generate_footer();
|
|
|
238 |
return;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
# If the topic text has been uploaded from a file, read from it now.
|
|
|
242 |
if (defined $fh) {
|
|
|
243 |
while (<$fh>) {
|
|
|
244 |
$topic_text .= Codestriker::decode_topic_text($_);
|
|
|
245 |
}
|
|
|
246 |
if ($topic_text eq "") {
|
|
|
247 |
if (defined $temp_error_fh) {
|
|
|
248 |
seek($temp_error_fh, 0, 0);
|
|
|
249 |
$feedback .= "Problem generating topic text:\n\n";
|
|
|
250 |
my $buf = "";
|
|
|
251 |
while (read $temp_error_fh, $buf, 16384) {
|
|
|
252 |
$feedback .= $buf;
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
else {
|
|
|
256 |
$feedback = "Uploaded file doesn't exist or is empty.\n";
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
# Remove the temporary files if required, and forward control
|
|
|
260 |
# back to the create topic page.
|
|
|
261 |
$temp_topic_fh->close if defined $temp_topic_fh;
|
|
|
262 |
$temp_error_fh->close if defined $temp_error_fh;
|
|
|
263 |
_forward_create_topic($error_vars, $feedback, $url_builder);
|
|
|
264 |
$http_response->generate_footer();
|
|
|
265 |
return;
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
# Remove the temporary files if required.
|
|
|
270 |
$temp_topic_fh->close if defined $temp_topic_fh;
|
|
|
271 |
$temp_error_fh->close if defined $temp_error_fh;
|
|
|
272 |
|
|
|
273 |
# Remove \r from the topic text.
|
|
|
274 |
$topic_text =~ s/\r//g;
|
|
|
275 |
|
|
|
276 |
# Make sure the topic is not too large, count the number of \n
|
|
|
277 |
# in the topic content text.
|
|
|
278 |
my $new_topic_length = 0;
|
|
|
279 |
++$new_topic_length while ($topic_text =~ /\n/g);
|
|
|
280 |
|
|
|
281 |
if (defined($Codestriker::maximum_topic_size_lines) &&
|
|
|
282 |
$Codestriker::maximum_topic_size_lines ne "" &&
|
|
|
283 |
$Codestriker::maximum_topic_size_lines < $new_topic_length)
|
|
|
284 |
{
|
|
|
285 |
$feedback .=
|
|
|
286 |
"The topic length of $new_topic_length lines is too long. " .
|
|
|
287 |
"Topics cannot exceed $Codestriker::maximum_topic_size_lines " .
|
|
|
288 |
"lines long. Please remove content from topic, or break the " .
|
|
|
289 |
"topic into several independent topics.\n";
|
|
|
290 |
|
|
|
291 |
_forward_create_topic($error_vars, $feedback, $url_builder);
|
|
|
292 |
$http_response->generate_footer();
|
|
|
293 |
return;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
# Make sure the specified topicids to be obsoleted are in fact valid.
|
|
|
297 |
if (defined $obsoletes && $obsoletes ne '') {
|
|
|
298 |
my @data = split ',', $obsoletes;
|
|
|
299 |
for (my $i = 0; $i <= $#data; $i+=2) {
|
|
|
300 |
my $id = $data[$i];
|
|
|
301 |
my $version = $data[$i+1];
|
|
|
302 |
|
|
|
303 |
if (! Codestriker::Model::Topic::exists($id)) {
|
|
|
304 |
$feedback .= "Obsoleted topics specified do not exist.\n";
|
|
|
305 |
_forward_create_topic($error_vars, $feedback, $url_builder);
|
|
|
306 |
$http_response->generate_footer();
|
|
|
307 |
return;
|
|
|
308 |
}
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
# Create the topic in the model.
|
|
|
313 |
my $topic = Codestriker::Model::Topic->new($topicid);
|
|
|
314 |
$topic->create($topicid, $email, $topic_title,
|
|
|
315 |
$bug_ids, $reviewers, $cc,
|
|
|
316 |
$topic_description, $topic_text,
|
|
|
317 |
$start_tag, $end_tag, $module,
|
|
|
318 |
$repository_url, $projectid,
|
|
|
319 |
\@deltas, $obsoletes);
|
|
|
320 |
|
|
|
321 |
# Obsolete any required topics.
|
|
|
322 |
if (defined $obsoletes && $obsoletes ne '') {
|
|
|
323 |
my @data = split ',', $obsoletes;
|
|
|
324 |
for (my $i = 0; $i <= $#data; $i+=2) {
|
|
|
325 |
my $id = $data[$i];
|
|
|
326 |
my $version = $data[$i+1];
|
|
|
327 |
Codestriker::Action::SubmitEditTopicsState
|
|
|
328 |
->update_state($id, $version, 'Obsoleted', $email);
|
|
|
329 |
}
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
# Tell all of the topic listener classes that a topic has
|
|
|
333 |
# just been created.
|
|
|
334 |
$feedback = Codestriker::TopicListeners::Manager::topic_create($topic);
|
|
|
335 |
|
|
|
336 |
# Obtain a URL builder object and determine the URL to the topic.
|
|
|
337 |
my $topic_url = $url_builder->view_url_extended($topicid, -1, "", "", "",
|
|
|
338 |
$query->url(), 0);
|
|
|
339 |
|
|
|
340 |
# Indicate to the user that the topic has been created and an email has
|
|
|
341 |
# been sent.
|
|
|
342 |
my $vars = {};
|
|
|
343 |
$vars->{'topic_title'} = $topic->{title};
|
|
|
344 |
$vars->{'email'} = $email;
|
|
|
345 |
$vars->{'topic_url'} = $topic_url;
|
|
|
346 |
$vars->{'reviewers'} = $reviewers;
|
|
|
347 |
$vars->{'cc'} = (defined $cc) ? $cc : "";
|
|
|
348 |
$vars->{'feedback'} = $feedback;
|
|
|
349 |
|
|
|
350 |
my $template = Codestriker::Http::Template->new("submitnewtopic");
|
|
|
351 |
$template->process($vars);
|
|
|
352 |
|
|
|
353 |
$http_response->generate_footer();
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
# Direct output to the create topic screen again, with the appropriate feedback
|
|
|
357 |
# message.
|
|
|
358 |
sub _forward_create_topic($$$) {
|
|
|
359 |
my ($vars, $feedback, $url_builder) = @_;
|
|
|
360 |
|
|
|
361 |
$feedback =~ s/\n/<BR>/g;
|
|
|
362 |
$vars->{'feedback'} = $feedback;
|
|
|
363 |
my @projects = Codestriker::Model::Project->list();
|
|
|
364 |
$vars->{'projects'} = \@projects;
|
|
|
365 |
Codestriker::Action::CreateTopic->
|
|
|
366 |
set_obsoleted_topics_parameter($vars, $url_builder);
|
|
|
367 |
|
|
|
368 |
my $template = Codestriker::Http::Template->new("createtopic");
|
|
|
369 |
$template->process($vars);
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
1;
|