Index: generate/project_issue_generate.inc
===================================================================
--- generate/project_issue_generate.inc	(revision 1)
+++ generate/project_issue_generate.inc	(working copy)
@@ -39,8 +39,7 @@
   for ($i = 0; $i < $number; $i++) {
     $project = $projects[array_rand($projects)];
     $project_node = node_load($project->nid);
-    $components = unserialize($project->components);
-    $releases = project_release_get_releases($project_node, FALSE);
+    $components = unserialize($project->components);    
     $issue = array();
     $issue['pid'] = $project->nid;
     $issue['category'] = $categories[array_rand($categories)];
@@ -48,8 +47,14 @@
     $issue['priority'] = array_rand($priorities);
     $issue['title'] = devel_create_greeking(rand(2, 15), true);
     $issue['body'] = devel_create_content();
-    $issue['rid'] = array_rand($releases);
 
+    if (module_exists('project_release')) {
+      // Fetch an array of release versions for this project, and randomly assign
+      // one of them to this issue.
+      $releases = project_release_get_releases($project_node, FALSE);
+      $issue['rid'] = ($releases ? array_rand($releases) : 0);      
+    }
+
     // The user must be chosen before status (sid) so that we can make sure
     // that the status is set such that the user would have permission to
     // set the status as such.
@@ -108,23 +113,41 @@
     // Update our copy of the issue to randomly decide what to change with
     // the new comment. This way, if we decide not to alter something, we
     // can leave the current value alone.
-    $account = user_load(array('uid' => $users[array_rand($users)]->uid));
-    $project = $projects[array_rand($projects)];
+    $account = user_load(array('uid' => $users[array_rand($users)]->uid));    
     if (rand(0, 1)) {
-      $issues[$k]->pid = $project->nid;
-      // If the project changes, the version must also change.
-      $project_node = node_load($issues[$k]->pid, NULL, TRUE);
+      $project = $projects[array_rand($projects)];      
+      $project_changed = ($issues[$k]->pid != $project->nid);
+      $issues[$k]->pid = $project->nid;      
+    } else {
+      $project_changed = FALSE;
+    }
+    
+    // Fetch the project node from the database. If the project for this issue
+    // was changed in the previous step, this will load the new project, not the
+    // one that was assigned to the issue.
+    $project_node = node_load($issues[$k]->pid, NULL, TRUE);
+    
+    if ($project_changed) {
+      // project_issue_comment() uses a hack to detect a change in the project-value
+      // when adding a comment to the issue. The following line was added to trigger 
+      // the hack when using drupal_execute.
+      $_POST['project_info']['pid'] = $project_node->nid;
+    }
+  
+    if (module_exists('project_release') && (rand(0,1) || $project_changed)) {
+      // If the project changes, the version must also change. If the project
+      // hasn't changed, randomly decide whether or not the change the version.
       $releases = project_release_get_releases($project_node, FALSE);
       $issues[$k]->rid = array_rand($releases);
-
-      // And so must the component.
-      $components = $project_node->components;
-      $issues[$k]->component = $components[array_rand($components)];
     }
-    elseif (rand(0, 1)) {
-      $components = unserialize($project->components);
+    // Change the component when either luck decides that it should be changed, or when
+    // the currently assigned component is no longer valid because of a project change.
+    // Components can be the same across different projects (for example, "Code"), so 
+    // a component change is not always required when the project is changed.    
+    $components = $project_node->components;      
+    if (rand(0, 1) || !in_array($issues[$k]->component, $components, TRUE)) {      
       $issues[$k]->component = $components[array_rand($components)];
-    }
+    }  
     if (rand(0, 1)) {
       $issues[$k]->category = $categories[array_rand($categories)];
     }
@@ -141,11 +164,7 @@
         continue;
       }
     }
-    if (rand(0, 1)) {
-      $project_node = node_load($issues[$k]->pid, NULL, TRUE);
-      $releases = project_release_get_releases($project_node, FALSE);
-      $issues[$k]->rid = array_rand($releases);
-    }
+    
     // Construct a comment to put the issue into the state we just decided.
     $comment = array();
     $comment['uid'] = $account->uid;
