You are here

public function RoboFile::createTestBranch in Panopoly 7

Creates a branch which includes patches from a Drupal.org issue, in order to trigger Travis-CI to test them.

@command create-test-branch

@option string $git-repo The git repo to commit to. @option string $git-old-branch The branch in the git repo to start from. @option string $git-new-branch The branch in the git repo to create. @option bool $skip-upgrade-tests If passed, this will only run tests on the current -dev, skipping the tests against upgraded versions. @option bool $profile-patch If passed, the discovered patch will be used against the profile, rather than individual components.

Parameters

int $issue_number: The issue number to run the tests for.

Return value

\Robo\Collection\CollectionBuilder

Throws

\Exception

File

./RoboFile.php, line 522

Class

RoboFile
This is project's console commands configuration for Robo task runner.

Code

public function createTestBranch($issue_number, $opts = [
  'git-repo' => self::PANOPOLY_GITHUB_REPO,
  'git-old-branch' => self::PANOPOLY_DEFAULT_BRANCH,
  'git-new-branch' => NULL,
  'skip-upgrade-tests' => FALSE,
  'profile-patch' => FALSE,
]) {
  $patch_files = $this
    ->getPatchFilesForDrupalIssue($issue_number, $opts['profile-patch']);
  if (empty($patch_files)) {
    throw new \Exception("Unable to find any patch files on issue {$issue_number}");
  }
  $old_branch = $opts['git-old-branch'];
  $new_branch = $opts['git-new-branch'] ?: 'issue-' . $issue_number;

  /** @var \Robo\Collection\CollectionBuilder|$this $collection */
  $collection = $this
    ->collectionBuilder();
  $tmp_dir = $collection
    ->taskTmpDir()
    ->cwd(TRUE)
    ->getPath();
  $collection
    ->taskGitStack()
    ->cloneShallow($opts['git-repo'], $tmp_dir, $old_branch, 1);

  // Check out the branch.
  $collection
    ->addCode(function () use ($old_branch, $new_branch) {
    $result = $this
      ->_exec("git checkout {$new_branch}");
    if ($result
      ->getExitCode() !== 0) {

      // We have to create the branch, because it doesn't exist.
      $this
        ->_exec("git checkout -b {$new_branch}");
    }
    else {

      // We have to merge from the old branch to catch any changes.
      $this
        ->_exec("git merge {$old_branch} --strategy --recursive -X theirs");
    }
  });

  // Apply the patch files.
  foreach ($patch_files as $component => $patch_url) {
    if ($component === 'profile') {
      $patch_path = '.';
    }
    else {
      $patch_path = 'modules/panopoly/' . $component;
    }
    $patch_file = $collection
      ->taskTmpFile()
      ->text(file_get_contents($patch_url))
      ->getPath();
    $collection
      ->taskExec("patch -p1 -d {$patch_path} < {$patch_file}");
  }

  // Remove .orig and .rej files
  $collection
    ->taskExec("find -name \\*.orig -exec rm \\{\\} \\;");
  $collection
    ->taskExec("find -name \\*.rej -exec rm \\{\\} \\;");

  // Regenerate the .make files (in case a patch changed them)
  $collection
    ->addCode([
    $this,
    'buildDrupalOrgMake',
  ]);

  // Modify the .travis.yml file.
  $collection
    ->addCode(function () use ($opts) {
    $travis_yml = \Symfony\Component\Yaml\Yaml::parseFile('.travis.yml');

    // We always drop the matrix -> include.
    if (isset($travis_yml['matrix']['include'])) {
      unset($travis_yml['matrix']['include']);
    }
    if ($opts['skip-upgrade-tests']) {

      // Remove all but the first 'env' entry. The rest are upgrade tests.
      $travis_yml['env']['matrix'] = [
        $travis_yml['env']['matrix'][0],
      ];
    }
    else {

      // Do just the first upgrade test.
      $travis_yml['env']['matrix'] = array_slice($travis_yml['env']['matrix'], 0, 2);
    }
    file_put_contents('.travis.yml', \Symfony\Component\Yaml\Yaml::dump($travis_yml));
  });

  // Make commit message.
  $commit_message = "Trying latest patches on Issue #{$issue_number}: https://www.drupal.org/node/{$issue_number}\n";
  foreach ($patch_files as $patch_url) {
    $commit_message .= " - {$patch_url}\n";
  }
  $collection
    ->taskGitStack()
    ->add('.')
    ->commit($commit_message);
  $collection
    ->taskExec("git push -f origin {$new_branch}");
  return $collection;
}