<NME> EloquentDeploymentRepository.php
<BEF> <?php

namespace REBELinBLUE\Deployer\Repositories;

use Carbon\Carbon;
use Illuminate\Foundation\Bus\DispatchesJobs;
use REBELinBLUE\Deployer\Deployment;
use REBELinBLUE\Deployer\Jobs\AbortDeployment;
use REBELinBLUE\Deployer\Jobs\QueueDeployment;
use REBELinBLUE\Deployer\Repositories\Contracts\DeploymentRepositoryInterface;

/**
 * The deployment repository.
 */
class EloquentDeploymentRepository extends EloquentRepository implements DeploymentRepositoryInterface
{
    use DispatchesJobs;

    /**
     * EloquentDeploymentRepository constructor.
     *
     * @param Deployment $model
     */
    public function __construct(Deployment $model)
    {
        $this->model = $model;
    }

    /**
     * Creates a new instance of the model.
     *
     * @param array $fields
     *
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function create(array $fields)
    {
        $optional = [];
        if (array_key_exists('optional', $fields)) {
            $optional = $fields['optional'];
            unset($fields['optional']);
        }

        $deployment = $this->model->create($fields);

        $this->dispatch(new QueueDeployment(
            $deployment->project,
            $deployment,
            $optional
        ));

        return $deployment;
    }

    /**
     * @param int $model_id
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function abort($model_id)
    {
        $deployment = $this->getById($model_id);

        if (!$deployment->isAborting()) {
            $deployment->status = Deployment::ABORTING;
            $deployment->save();

            $this->dispatch(new AbortDeployment($deployment));
        }
    }

    /**
     * @param int $project_id
     */
    public function abortQueued($project_id)
    {
        $deployments = $this->model->where('project_id', $project_id)
                                   ->whereIn('status', [Deployment::DEPLOYING, Deployment::PENDING])
                                   ->orderBy('started_at', 'DESC')
                                   ->get();

        foreach ($deployments as $deployment) {
            $deployment->status = Deployment::ABORTING;
            $deployment->save();

            $this->dispatch(new AbortDeployment($deployment));

            if ($deployment->is_webhook) {
                $deployment->delete();
            }
        }
    }

        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return $this->model->whereRaw($raw_sql)
                           ->with('project')
                           ->take(15)
                           ->orderBy('started_at', 'DESC')
     */
    public function rollback($model_id, $reason = '', array $optional = [])
    {
        $previous = $this->getById($model_id);

        return $this->create([
            'committer'       => $previous->committer,
            'committer_email' => $previous->committer_email,
            'commit'          => $previous->commit,
            'project_id'      => $previous->project_id,
            'branch'          => $previous->branch,
            'reason'          => $reason,
            'optional'        => $optional,
        ]);
    }

    /**
     * @param int $project_id
     * @param int $paginate
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getLatest($project_id, $paginate = 15)
    {
        return $this->model->where('project_id', $project_id)
                           ->with('user', 'project')
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
                           ->paginate($paginate);
    }

    /**
     * @param int $project_id
     *
     * @return Deployment
     */
    public function getLatestSuccessful($project_id)
    {
        return $this->model->where('project_id', $project_id)
                           ->where('status', Deployment::COMPLETED)
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
                           ->first();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getTimeline()
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return $this->model->whereRaw($raw_sql)
                           ->whereNotNull('started_at')
                           ->with('project')
                           ->take(15)
                           ->orderBy('started_at', 'DESC')
                           ->get();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getPending()
    {
        return $this->getStatus(Deployment::PENDING);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getRunning()
    {
        return $this->getStatus(Deployment::DEPLOYING);
    }

    /**
     * @param int $project_id
     *
     * @return int
     */
    public function getTodayCount($project_id)
    {
        $now = Carbon::now();

        return $this->getBetweenDates($project_id, $now, $now);
    }

    /**
     * @param int $project_id
     *
     * @return int
     */
    public function getLastWeekCount($project_id)
    {
        $lastWeek  = Carbon::now()->subWeek();
        $yesterday = Carbon::now()->yesterday();

        return $this->getBetweenDates($project_id, $lastWeek, $yesterday);
    }

    /**
     * @param int    $project_id
     * @param Carbon $startDate
     * @param Carbon $endDate
     *
     * @return int
     */
    private function getBetweenDates($project_id, Carbon $startDate, Carbon $endDate)
    {
        return $this->model->where('project_id', $project_id)
                           ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')
                           ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')
                           ->count();
    }

    /**
     * @param int $status
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    private function getStatus($status)
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return $this->model->whereRaw($raw_sql)
                           ->where('status', $status)
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
                           ->get();
    }
}

<MSG> #128 hide from the timeline deployments which failed to queue

<DFF> @@ -94,6 +94,7 @@ class EloquentDeploymentRepository extends EloquentRepository implements Deploym
         $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';
 
         return $this->model->whereRaw($raw_sql)
+                           ->whereNotNull('started_at')
                            ->with('project')
                            ->take(15)
                            ->orderBy('started_at', 'DESC')

{'commit': "diff --git a/app/Repositories/EloquentDeploymentRepository.php b/app/Repositories/EloquentDeploymentRepository.php\nindex 3c45abf1..3d45211e 100644\n--- a/app/Repositories/EloquentDeploymentRepository.php\n+++ b/app/Repositories/EloquentDeploymentRepository.php\n@@ -94,6 +94,7 @@ class EloquentDeploymentRepository extends EloquentRepository implements Deploym\n         $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';\n \n         return $this->model->whereRaw($raw_sql)\n+                           ->whereNotNull('started_at')\n                            ->with('project')\n                            ->take(15)\n                            ->orderBy('started_at', 'DESC')\n", 'commit_subject': '#128 hide from the timeline deployments which failed to queue', 'repo_name': 'REBELinBLUE/deployer', 'committer': {'name': 'Stephen Ball', 'email': '06fa905d7f2aaced6dc72e9511c71a2a51e8aead@rebelinblue.com', 'time_sec': '1447608393', 'tz_offset': '0', 'date': {'seconds': '1447608393'}}, 'commit_tree': '8e4769184897e88f58edaf1158f13ee7f1e3e689', 'commit_message': '#128 hide from the timeline deployments which failed to queue\n', 'language_name': 'PHP', 'license': 'mit', 'hunks': "@@ -94,6 +94,7 @@ class EloquentDeploymentRepository extends EloquentRepository implements Deploym\n         $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';\n \n         return $this->model->whereRaw($raw_sql)\n+                           ->whereNotNull('started_at')\n                            ->with('project')\n                            ->take(15)\n                            ->orderBy('started_at', 'DESC')\n", 'addition_count': 1, 'deletion_count': 0, 'src_file': 'a/app/Repositories/EloquentDeploymentRepository.php', 'tgt_file': 'b/app/Repositories/EloquentDeploymentRepository.php', 'file_extension': '.php', 'before_file': "<?php\n\nnamespace REBELinBLUE\\Deployer\\Repositories;\n\nuse Carbon\\Carbon;\nuse Illuminate\\Foundation\\Bus\\DispatchesJobs;\nuse REBELinBLUE\\Deployer\\Deployment;\nuse REBELinBLUE\\Deployer\\Jobs\\AbortDeployment;\nuse REBELinBLUE\\Deployer\\Jobs\\QueueDeployment;\nuse REBELinBLUE\\Deployer\\Repositories\\Contracts\\DeploymentRepositoryInterface;\n\n/**\n * The deployment repository.\n */\nclass EloquentDeploymentRepository extends EloquentRepository implements DeploymentRepositoryInterface\n{\n    use DispatchesJobs;\n\n    /**\n     * EloquentDeploymentRepository constructor.\n     *\n     * @param Deployment $model\n     */\n    public function __construct(Deployment $model)\n    {\n        $this->model = $model;\n    }\n\n    /**\n     * Creates a new instance of the model.\n     *\n     * @param array $fields\n     *\n     * @return \\Illuminate\\Database\\Eloquent\\Model\n     */\n    public function create(array $fields)\n    {\n        $optional = [];\n        if (array_key_exists('optional', $fields)) {\n            $optional = $fields['optional'];\n            unset($fields['optional']);\n        }\n\n        $deployment = $this->model->create($fields);\n\n        $this->dispatch(new QueueDeployment(\n            $deployment->project,\n            $deployment,\n            $optional\n        ));\n\n        return $deployment;\n    }\n\n    /**\n     * @param int $model_id\n     *\n     * @throws \\Illuminate\\Database\\Eloquent\\ModelNotFoundException\n     */\n    public function abort($model_id)\n    {\n        $deployment = $this->getById($model_id);\n\n        if (!$deployment->isAborting()) {\n            $deployment->status = Deployment::ABORTING;\n            $deployment->save();\n\n            $this->dispatch(new AbortDeployment($deployment));\n        }\n    }\n\n    /**\n     * @param int $project_id\n     */\n    public function abortQueued($project_id)\n    {\n        $deployments = $this->model->where('project_id', $project_id)\n                                   ->whereIn('status', [Deployment::DEPLOYING, Deployment::PENDING])\n                                   ->orderBy('started_at', 'DESC')\n                                   ->get();\n\n        foreach ($deployments as $deployment) {\n            $deployment->status = Deployment::ABORTING;\n            $deployment->save();\n\n            $this->dispatch(new AbortDeployment($deployment));\n\n            if ($deployment->is_webhook) {\n                $deployment->delete();\n            }\n        }\n    }\n\n        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';\n\n        return $this->model->whereRaw($raw_sql)\n                           ->with('project')\n                           ->take(15)\n                           ->orderBy('started_at', 'DESC')\n     */\n    public function rollback($model_id, $reason = '', array $optional = [])\n    {\n        $previous = $this->getById($model_id);\n\n        return $this->create([\n            'committer'       => $previous->committer,\n            'committer_email' => $previous->committer_email,\n            'commit'          => $previous->commit,\n            'project_id'      => $previous->project_id,\n            'branch'          => $previous->branch,\n            'reason'          => $reason,\n            'optional'        => $optional,\n        ]);\n    }\n\n    /**\n     * @param int $project_id\n     * @param int $paginate\n     *\n     * @return \\Illuminate\\Database\\Eloquent\\Collection\n     */\n    public function getLatest($project_id, $paginate = 15)\n    {\n        return $this->model->where('project_id', $project_id)\n                           ->with('user', 'project')\n                           ->whereNotNull('started_at')\n                           ->orderBy('started_at', 'DESC')\n                           ->paginate($paginate);\n    }\n\n    /**\n     * @param int $project_id\n     *\n     * @return Deployment\n     */\n    public function getLatestSuccessful($project_id)\n    {\n        return $this->model->where('project_id', $project_id)\n                           ->where('status', Deployment::COMPLETED)\n                           ->whereNotNull('started_at')\n                           ->orderBy('started_at', 'DESC')\n                           ->first();\n    }\n\n    /**\n     * @return \\Illuminate\\Database\\Eloquent\\Collection\n     */\n    public function getTimeline()\n    {\n        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';\n\n        return $this->model->whereRaw($raw_sql)\n                           ->whereNotNull('started_at')\n                           ->with('project')\n                           ->take(15)\n                           ->orderBy('started_at', 'DESC')\n                           ->get();\n    }\n\n    /**\n     * @return \\Illuminate\\Database\\Eloquent\\Collection\n     */\n    public function getPending()\n    {\n        return $this->getStatus(Deployment::PENDING);\n    }\n\n    /**\n     * @return \\Illuminate\\Database\\Eloquent\\Collection\n     */\n    public function getRunning()\n    {\n        return $this->getStatus(Deployment::DEPLOYING);\n    }\n\n    /**\n     * @param int $project_id\n     *\n     * @return int\n     */\n    public function getTodayCount($project_id)\n    {\n        $now = Carbon::now();\n\n        return $this->getBetweenDates($project_id, $now, $now);\n    }\n\n    /**\n     * @param int $project_id\n     *\n     * @return int\n     */\n    public function getLastWeekCount($project_id)\n    {\n        $lastWeek  = Carbon::now()->subWeek();\n        $yesterday = Carbon::now()->yesterday();\n\n        return $this->getBetweenDates($project_id, $lastWeek, $yesterday);\n    }\n\n    /**\n     * @param int    $project_id\n     * @param Carbon $startDate\n     * @param Carbon $endDate\n     *\n     * @return int\n     */\n    private function getBetweenDates($project_id, Carbon $startDate, Carbon $endDate)\n    {\n        return $this->model->where('project_id', $project_id)\n                           ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')\n                           ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')\n                           ->count();\n    }\n\n    /**\n     * @param int $status\n     *\n     * @return \\Illuminate\\Database\\Eloquent\\Collection\n     */\n    private function getStatus($status)\n    {\n        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';\n\n        return $this->model->whereRaw($raw_sql)\n                           ->where('status', $status)\n                           ->whereNotNull('started_at')\n                           ->orderBy('started_at', 'DESC')\n                           ->get();\n    }\n}\n", 'message': '#128 hide from the timeline deployments which failed to queue\n', 'author_name': 'Stephen Ball', 'lang': 'php'}