/**
  * {@inheritdoc}
  */
 public function run(JobInterface $job, $data = NULL)
 {
     // TODO: Ensure that all 'required' arguments are defined
     $definition = $job->getDefinition();
     $failflag = FALSE;
     foreach ($job->getRequiredArguments() as $env_var => $yaml_loc) {
         if (!empty($job->getBuildVars()[$env_var])) {
             continue;
         } else {
             // Look for the appropriate array structure in the job definition file
             // eg: environment:db
             $keys = explode(":", $yaml_loc);
             $eval = $definition;
             foreach ($keys as $key) {
                 if (!empty($eval[$key])) {
                     // Check if the next level contains a numeric [0] key, indicating a
                     // nested array of parameters.  If found, skip this level of the
                     // array.
                     if (isset($eval[$key][0])) {
                         $eval = $eval[$key][0];
                     } else {
                         $eval = $eval[$key];
                     }
                 } else {
                     // Missing a required key in the array key chain
                     $failflag = TRUE;
                     break;
                 }
             }
             if (!$failflag) {
                 continue;
             }
         }
         // If processing gets to here, we're missing a required variable
         $job->errorOutput("Failed", "Required test parameter <options=bold>'{$env_var}'</options=bold> not found in environment variables, and <options=bold>'{$yaml_loc}'</options=bold> not found in job definition file.");
         // TODO: Graceful handling of failed exit states
         return FALSE;
     }
     // TODO: Strip out arguments which are not defined in the 'Available' arguments array
     return TRUE;
 }
 protected function buildvarsToDefinition(JobInterface $job)
 {
     $buildvars = $job->getBuildVars();
     $job_definition = $job->jobDefinition;
     // Process dependencies
     if (!empty($buildvars['DCI_DEPENDENCIES'])) {
         // Format: module1,module2,module3
         $dependencies = explode(',', trim($buildvars['DCI_DEPENDENCIES'], '"'));
         foreach ($dependencies as $dependency) {
             // TODO: Remove the hardcoded git.drupal.org!!!
             // Perhaps we extend this with a DrupalConfigurator class?
             $directory = 'sites/all/modules';
             // TODO: We can't assume a branch here. Need to determine the Drupal version earlier!
             $job_definition['setup']['checkout'][] = array('protocol' => 'git', 'repo' => "git://git.drupal.org/project/{$dependency}.git", 'branch' => 'master', 'checkout_dir' => $directory);
         }
     }
     // Process GIT dependencies
     if (!empty($buildvars['DCI_DEPENDENCIES_GIT'])) {
         // Format: gitrepo1,branch;gitrepo2,branch;
         $dependencies = explode(';', trim($buildvars['DCI_DEPENDENCIES_GIT'], '"'));
         foreach ($dependencies as $dependency) {
             if (!empty($dependency)) {
                 list($repo, $branch) = explode(',', $dependency);
                 // TODO: Remove this hardcoded drupalism!!!
                 $directory = 'sites/all/modules/' . basename(parse_url($repo, PHP_URL_PATH), ".git");
                 $job_definition['setup']['checkout'][] = array('protocol' => 'git', 'repo' => $repo, 'branch' => $branch, 'checkout_dir' => $directory);
             }
         }
     }
     $job->job_definition = $job_definition;
     /*
     ### ./run.sh Options
     # Any valid Drupal branch or tag, like 8.0.x, 7.x or 7.30:
     DCI_DrupalBRANCH="8.0.x"
     # The identifier used by jenkins to name the Drupal docroot where all is stored:
     DCI_IDENTIFIER="build_$(date +%Y_%m_%d_%H%M%S)" # Only [a-z0-9-_.] allowed
     # The place where Drupal repos and DrupalDocRoot identifiers are kept:
     DCI_REPODIR="$HOME/testbotdata"
     # Request the runner to update the Drupal local repo before local cloning:
     DCI_UPDATEREPO="false"  # true to force repos update
     # By default we put the Drupal repo and docroots on the same place, but you can have BUILDSDIR elsewhere:
     DCI_BUILDSDIR="$DCI_REPODIR"
     # Same for the workspace:
     DCI_WORKSPACE="$DCI_BUILDSDIR/$DCI_IDENTIFIER/"
     # Install modules:
     DCI_DEPENDENCIES=""     # module1,module2,module2...
     # Git clone sandboxes:
     DCI_DEPENDENCIES_GIT="" # gitrepo1,branch;gitrepo2,branch;...
     # Download tgz modules:
     DCI_DEPENDENCIES_TGZ="" # module1_url.tgz,module1_url.tgz,...
     # Download and patch one or several patches:
     DCI_PATCH=""            # patch_url,apply_dir;patch_url,apply_dir;...
     */
 }
 public function validate_checkout_dir(JobInterface $job)
 {
     $arguments = $job->getBuildVars();
     $path = realpath($arguments['DCI_CheckoutDir']);
     $tmpdir = sys_get_temp_dir();
     if (strpos($path, $tmpdir) === 0) {
         return TRUE;
     }
     return FALSE;
 }