protected function buildCommitSelector($project)
{
// Branches
$branches = [];
foreach ($project->DNBranchList() as $branch) {
$sha = $branch->SHA();
$name = $branch->Name();
$branchValue = sprintf("%s (%s, %s old)", $name, substr($sha, 0, 8), $branch->LastUpdated()->TimeDiff());
$branches[$sha . '-' . $name] = $branchValue;
}
// Tags
$tags = [];
foreach ($project->DNTagList()->setLimit(null) as $tag) {
$sha = $tag->SHA();
$name = $tag->Name();
$tagValue = sprintf("%s (%s, %s old)", $name, substr($sha, 0, 8), $branch->LastUpdated()->TimeDiff());
$tags[$sha . '-' . $name] = $tagValue;
}
$tags = array_reverse($tags);
// Past deployments
$redeploy = [];
foreach ($project->DNEnvironmentList() as $dnEnvironment) {
$envName = $dnEnvironment->Name;
foreach ($dnEnvironment->DeployHistory()->filter('State', \DNDeployment::STATE_COMPLETED) as $deploy) {
$sha = $deploy->SHA;
if (!isset($redeploy[$envName])) {
$redeploy[$envName] = [];
}
if (!isset($redeploy[$envName][$sha])) {
$pastValue = sprintf("%s (deployed %s)", substr($sha, 0, 8), $deploy->obj('LastEdited')->Ago());
$redeploy[$envName][$sha] = $pastValue;
}
}
}
// Merge fields
$releaseMethods = [];
if (!empty($branches)) {
$releaseMethods[] = new SelectionGroup_Item('Branch', new DropdownField('Branch', 'Select a branch', $branches), 'Deploy the latest version of a branch');
}
if ($tags) {
$releaseMethods[] = new SelectionGroup_Item('Tag', new DropdownField('Tag', 'Select a tag', $tags), 'Deploy a tagged release');
}
if ($redeploy) {
$releaseMethods[] = new SelectionGroup_Item('Redeploy', new GroupedDropdownField('Redeploy', 'Redeploy', $redeploy), 'Redeploy a release that was previously deployed (to any environment)');
}
$releaseMethods[] = new SelectionGroup_Item('SHA', new Textfield('SHA', 'Please specify the full SHA'), 'Deploy a specific SHA');
$field = new TabbedSelectionGroup('SelectRelease', $releaseMethods);
$field->setValue(reset($releaseMethods)->getValue());
$field->addExtraClass('clearfix');
return $field;
}