diff --git a/dop b/dop index 71af7ab..763b10d 100755 --- a/dop +++ b/dop @@ -3,6 +3,7 @@ require __DIR__ . '/vendor/autoload.php'; +use DrupalPatchUtils\Command\CleanupGitBranches; use DrupalPatchUtils\Command\Configure; use DrupalPatchUtils\Command\CreateIssue; use DrupalPatchUtils\Command\IssuePruner; @@ -29,4 +30,5 @@ $application->add(new Login()); $application->add(new Logout()); $application->add(new RemainingCriticals()); $application->add(new IssuePruner()); +$application->add(new CleanupGitBranches()); $application->run(); diff --git a/src/DrupalPatchUtils/Command/CleanupGitBranches.php b/src/DrupalPatchUtils/Command/CleanupGitBranches.php new file mode 100644 index 0000000..42f294e --- /dev/null +++ b/src/DrupalPatchUtils/Command/CleanupGitBranches.php @@ -0,0 +1,59 @@ +setName('cleanupGitBranches') + ->setAliases(['cgb']) + ->setDescription('Removes all git branches which has closed issues.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $process = new Process('git branch'); + $process->run(); + + $branches = array_map('trim', explode("\n", $process->getOutput())); + + $browser = new DoBrowser(); + if (!$browser->loggedIn()) { + $browser->login($this->getConfig()->getDrupalUser(), $this->ask($output, "Enter your Drupal.org password: ", '', TRUE)); + } + + // Find all branches which don't need further work on. + foreach ($branches as $branch) { + if (is_numeric($branch)) { + $issue = $this->getIssue($branch); + + $form = $browser->getCommentForm($issue->getUri()); + if (in_array($form->getStatus(), [DoFormBase::STATUS_CLOSED_DUPLICATE, DoFormBase::STATUS_CLOSED_WONT_FIX, DoFormBase::STATUS_CLOSED_WORKS, DoFormBase::STATUS_CLOSED_CANT_REPRODUCE, DoFormBase::STATUS_CLOSED_FIXED])) { + $output->writeln(sprintf("Remove branch: %s", $branch)); + $process = new Process(sprintf('git branch -d %s', $branch)); + $process->run(); + } + } + } + } + + +} diff --git a/src/DrupalPatchUtils/DoFormBase.php b/src/DrupalPatchUtils/DoFormBase.php index ae2a515..29e0cf3 100644 --- a/src/DrupalPatchUtils/DoFormBase.php +++ b/src/DrupalPatchUtils/DoFormBase.php @@ -71,5 +71,10 @@ protected function setStatus($value) { return $this; } + public function getStatus() { + $status = $this->form->get('field_issue_status[und]'); + return $status->getValue(); + } + }