DNRoot::gitRevisions PHP Method

gitRevisions() public method

Deprecation: 2.0.0 - moved to GitDispatcher
public gitRevisions ( SS_HTTPRequest $request ) : SS_HTTPResponse | string
$request SS_HTTPRequest
return SS_HTTPResponse | string
    public function gitRevisions(\SS_HTTPRequest $request)
    {
        // Performs canView permission check by limiting visible projects
        $project = $this->getCurrentProject();
        if (!$project) {
            return $this->project404Response();
        }
        // Performs canView permission check by limiting visible projects
        $env = $this->getCurrentEnvironment($project);
        if (!$env) {
            return $this->environment404Response();
        }
        $options = [];
        foreach ($env->getSupportedOptions() as $option) {
            $options[] = ['name' => $option->getName(), 'title' => $option->getTitle(), 'defaultValue' => $option->getDefaultValue()];
        }
        $tabs = [];
        $id = 0;
        $data = ['id' => ++$id, 'name' => 'Deploy the latest version of a branch', 'field_type' => 'dropdown', 'field_label' => 'Choose a branch', 'field_id' => 'branch', 'field_data' => [], 'options' => $options];
        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());
            $data['field_data'][] = ['id' => $sha, 'text' => $branchValue, 'branch_name' => $name];
        }
        $tabs[] = $data;
        $data = ['id' => ++$id, 'name' => 'Deploy a tagged release', 'field_type' => 'dropdown', 'field_label' => 'Choose a tag', 'field_id' => 'tag', 'field_data' => [], 'options' => $options];
        foreach ($project->DNTagList()->setLimit(null) as $tag) {
            $name = $tag->Name();
            $data['field_data'][] = ['id' => $tag->SHA(), 'text' => sprintf("%s", $name)];
        }
        // show newest tags first.
        $data['field_data'] = array_reverse($data['field_data']);
        $tabs[] = $data;
        // Past deployments
        $data = ['id' => ++$id, 'name' => 'Redeploy a release that was previously deployed (to any environment)', 'field_type' => 'dropdown', 'field_label' => 'Choose a previously deployed release', 'field_id' => 'release', 'field_data' => [], 'options' => $options];
        // We are aiming at the format:
        // [{text: 'optgroup text', children: [{id: '<sha>', text: '<inner text>'}]}]
        $redeploy = [];
        foreach ($project->DNEnvironmentList() as $dnEnvironment) {
            $envName = $dnEnvironment->Name;
            $perEnvDeploys = [];
            foreach ($dnEnvironment->DeployHistory()->filter('State', \DNDeployment::STATE_COMPLETED) as $deploy) {
                $sha = $deploy->SHA;
                // Check if exists to make sure the newest deployment date is used.
                if (!isset($perEnvDeploys[$sha])) {
                    $pastValue = sprintf("%s (deployed %s)", substr($sha, 0, 8), $deploy->obj('LastEdited')->Ago());
                    $perEnvDeploys[$sha] = ['id' => $sha, 'text' => $pastValue];
                }
            }
            if (!empty($perEnvDeploys)) {
                $redeploy[$envName] = array_values($perEnvDeploys);
            }
        }
        // Convert the array to the frontend format (i.e. keyed to regular array)
        foreach ($redeploy as $name => $descr) {
            $data['field_data'][] = ['text' => $name, 'children' => $descr];
        }
        $tabs[] = $data;
        $data = ['id' => ++$id, 'name' => 'Deploy a specific SHA', 'field_type' => 'textfield', 'field_label' => 'Choose a SHA', 'field_id' => 'SHA', 'field_data' => [], 'options' => $options];
        $tabs[] = $data;
        // get the last time git fetch was run
        $lastFetched = 'never';
        $fetch = DNGitFetch::get()->filter(['ProjectID' => $project->ID, 'Status' => 'Finished'])->sort('LastEdited', 'DESC')->first();
        if ($fetch) {
            $lastFetched = $fetch->dbObject('LastEdited')->Ago();
        }
        $data = ['Tabs' => $tabs, 'last_fetched' => $lastFetched];
        $this->applyRedeploy($request, $data);
        return json_encode($data, JSON_PRETTY_PRINT);
    }