/** * @param SS_HTTPRequest $request * * @return string */ public function show(\SS_HTTPRequest $request) { $targetEnvironment = null; $targetEnvironmentId = $request->getVar('environmentId'); if (!empty($targetEnvironmentId)) { $targetEnvironment = DNEnvironment::get()->byId((int) $targetEnvironmentId); } $refs = []; $prevDeploys = []; $uatEnvironment = $this->project->DNEnvironmentList()->filter('Usage', DNEnvironment::UAT)->first(); $uatBuild = $uatEnvironment ? $uatEnvironment->CurrentBuild() : null; if ($uatBuild && $uatBuild->exists() && $targetEnvironment && $targetEnvironment->Usage === DNEnvironment::PRODUCTION) { $refs[self::REF_TYPE_FROM_UAT] = ['id' => self::REF_TYPE_FROM_UAT, 'label' => 'Promote the version currently on UAT', 'description' => 'Promote the version currently on UAT', 'promote_build' => $this->formatter->getDeploymentData($uatBuild)]; } $refs[self::REF_TYPE_BRANCH] = ['id' => self::REF_TYPE_BRANCH, 'label' => 'Branch version', 'description' => 'Deploy the latest version of a branch', 'list' => $this->getGitBranches($this->project)]; $refs[self::REF_TYPE_TAG] = ['id' => self::REF_TYPE_TAG, 'label' => 'Tag version', 'description' => 'Deploy a tagged release', 'list' => $this->getGitTags($this->project)]; // @todo: the original was a tree that was keyed by environment, the // front-end dropdown needs to be changed to support that. brrrr. foreach ($this->getGitPrevDeploys($this->project) as $env) { foreach ($env as $deploy) { $prevDeploys[] = $deploy; } } $refs[self::REF_TYPE_PREVIOUS] = ['id' => self::REF_TYPE_PREVIOUS, 'label' => 'Redeploy a release that was previously deployed (to any environment)', 'description' => 'Deploy a previous release', 'list' => $prevDeploys]; $refs[self::REF_TYPE_SHA] = ['id' => self::REF_TYPE_SHA, 'label' => 'Deploy a specific SHA', 'description' => 'Deploy a specific SHA']; $options = []; if ($targetEnvironment) { foreach ($targetEnvironment->getSupportedOptions() as $option) { $options[] = ['name' => $option->getName(), 'title' => $option->getTitle(), 'defaultValue' => $option->getDefaultValue()]; } } // get the last time git fetch was run $lastFetchedDate = 'never'; $lastFetchedAgo = null; $fetch = DNGitFetch::get()->filter(['ProjectID' => $this->project->ID, 'Status' => 'Finished'])->sort('LastEdited', 'DESC')->first(); if ($fetch) { $lastFetchedDate = $fetch->obj('LastEdited')->Date(); $lastFetchedAgo = $fetch->obj('LastEdited')->Ago(); } return $this->getAPIResponse(['refs' => $refs, 'options' => $options, 'last_fetched_date' => $lastFetchedDate, 'last_fetched_ago' => $lastFetchedAgo], 200); }
/** * Construct fields to select any commit * * @param DNProject $project * @param DataList|null $pipelineCommits Optional list of pipeline-filtered commits to include * @return FormField */ protected function buildCommitSelector($project, $pipelineCommits = null) { // Branches $branches = array(); 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 = array(); 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 . '-' . $tag] = $tagValue; } $tags = array_reverse($tags); // Past deployments $redeploy = array(); foreach ($project->DNEnvironmentList() as $dnEnvironment) { $envName = $dnEnvironment->Name; foreach ($dnEnvironment->DeployHistory() as $deploy) { $sha = $deploy->SHA; if (!isset($redeploy[$envName])) { $redeploy[$envName] = array(); } 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 = array(); if ($pipelineCommits) { $releaseMethods[] = new SelectionGroup_Item('FilteredCommits', $this->buildPipelineField($pipelineCommits), 'Deploy a commit prepared for this pipeline'); } 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()); return $field; }