Example #1
0
 private function _switchBranches($targetStory)
 {
     // Don't switch branches if we're not switching stories
     $currentStoryId = GitPlanbox_Util::currentStoryId();
     if ($targetStory->id == $currentStoryId) {
         return;
     }
     // Confirm that they want to branch off the current branch
     $currentBranch = GitPlanbox_Util::currentGitBranchName();
     print "Starting story: {$targetStory->name}\n";
     print "  (branching off {$currentBranch})\n";
     // Ask the user what they'd like to call the branch
     $config = GitPlanbox_Config::get();
     $branch = NULL;
     while ($branch === NULL) {
         $branch = GitPlanbox_Util::readline("What should we name the branch? (leave blank to stay on {$currentBranch}) ");
         if ($branch !== '' && !preg_match($config->branchregex(), $branch)) {
             print "Error: Branch names must match regex: '{$config->branchregex()}'.\n";
             $branch = NULL;
         }
     }
     if ($branch != '') {
         // Build the branch name based on the branch-name-template in config
         $branchName = $this->_buildBranchName($branch, $targetStory, $currentBranch);
         // Create the branch
         $command = "git checkout -b {$branchName} {$currentBranch}";
         exec($command, $output, $returnCode);
         if ($returnCode !== 0) {
             throw new Exception("Error creating new branch.");
         }
     }
 }
Example #2
0
 public function run($arguments, CLImaxController $cliController)
 {
     // If we have an argument, show the story with _that_ id
     $storyId = NULL;
     if (isset($arguments[0])) {
         $storyId = ltrim($arguments[0], "# ");
     }
     // Otherwise try to figure out what story we're working on
     // based on the current branch name
     if (!$storyId) {
         $currentBranchName = GitPlanbox_Util::currentGitBranchName();
         // Try to parse out a story id
         preg_match("/[0-9]{5,8}/", $currentBranchName, $matches);
         if ($matches) {
             $storyId = array_pop($matches);
         }
     }
     // If we STILL don't know what story to show, throw a usage exception
     if (!$storyId) {
         throw new Exception("Please specify a story_id to show.");
     }
     // Fetch the story
     $session = GitPlanbox_Session::create();
     try {
         $story = $session->post('get_story', array('story_id' => $storyId));
     } catch (GitPlanbox_ApplicationErrorException $e) {
         throw new Exception("Unable to fetch story {$storyId} from Planbox.");
     }
     // Lines for all stories
     $storyText = '';
     $lineFormat = "%-10s %s\n";
     $storyLines = array('Story' => "#{$story->id}", 'Name' => $story->name, 'Type' => $story->type, 'Status' => $story->status, 'Created' => GitPlanbox_Util::timeAgo($story->created_on));
     foreach ($storyLines as $key => $value) {
         $storyText .= sprintf($lineFormat, "{$key}:", $value);
     }
     // Optional fields
     $optionalLines = array();
     if ($story->completed_on) {
         $optionalLines['Completed'] = GitPlanbox_Util::timeAgo($story->completed_on);
     }
     foreach ($optionalLines as $key => $value) {
         $storyText .= sprintf($lineFormat, "{$key}:", $value);
     }
     if ($story->description) {
         $storyText .= "\nDescription:\n  {$story->description}\n";
     }
     if ($story->tasks) {
         $storyText .= "\nTasks:\n";
         foreach ($story->tasks as $task) {
             $storyText .= sprintf("  #%-10s %-10s %1.1f/%1.1f hours   %-30s\n", $task->id, $task->status, $task->duration, $task->estimate, $task->name);
         }
     }
     if ($story->comments) {
         $storyText .= "\nComments:\n";
         foreach ($story->comments as $comment) {
             $storyText .= "  " . GitPlanbox_Util::timeAgo($comment->date) . "\n  {$comment->text}\n\n";
         }
     }
     print $storyText;
     return 0;
 }