<commit_before><?php

/*
 * This file is part of Piplin.
 *
 * Copyright (C) 2016-2017 piplin.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Piplin\Http\Controllers\Api;

use Illuminate\Http\Request;
use Piplin\Bus\Jobs\AbortTaskJob;
use Piplin\Bus\Jobs\CreateTaskJob;
use Piplin\Http\Controllers\Controller;
use Piplin\Models\Task;
use Piplin\Models\Project;
use Piplin\Services\Webhooks\Beanstalkapp;
use Piplin\Services\Webhooks\Bitbucket;
use Piplin\Services\Webhooks\Custom;
use Piplin\Services\Webhooks\Github;
use Piplin\Services\Webhooks\Gitlab;
use Piplin\Services\Webhooks\Gogs;
use Piplin\Services\Webhooks\Oschina;

/**
 * The deployment incoming-webhook controller.
 */
class IncomingWebhookController extends Controller
{
    /**
     * List of supported service classes.
     * @var array
     */
    private $services = [
        Beanstalkapp::class,
        Bitbucket::class,
        Github::class,
        Gitlab::class,
        Gogs::class,
        Oschina::class,
    ];

    /**
     * Class constructor.
     *
     * @return void
     */
    public function __construct()
    {
        $this->services[] = Custom::class;
    }

    /**
     * Handles incoming requests to trigger deploy.
     *
     * @param Request $request
     * @param string  $hash
     *
     * @return Response
     */
    public function deploy(Request $request, $hash)
    {
        $project = Project::where('hash', $hash)->firstOrFail();

        $deployPlan = $project->deployPlan;

        $success = false;
        if ($deployPlan && $deployPlan->environments->count() > 0) {
            $payload = $this->parseWebhookRequest($request, $project);

            if (is_array($payload) && ($project->allow_other_branch || $project->branch === $payload['branch'])) {
                $this->abortQueued($project->id);
                $payload['targetalbe_type'] = get_class($deployPlan);
                $paylaod['targetable_id'] = $deployPlan->id;
                dispatch(new CreateTaskJob($project, $payload));

                $success = true;
            }
        }

        return [
            'success' => $success,
        ];
    }

    /**
     * Handles incoming requests to trigger build.
     *
     * @param Request $request
     * @param string  $hash
     *
     * @return Response
     */
    public function build(Request $request, $hash)
    {
        $project = Project::where('hash', $hash)->firstOrFail();

        $buildPlan = $project->buildPlan;

        $success = false;
        if ($buildPlan && $buildPlan->servers->count() > 0) {
            $payload = $this->parseWebhookRequest($request, $project);

            if (is_array($payload) && ($project->allow_other_branch || $project->branch === $payload['branch'])) {
                $this->abortQueued($project->id);
                $payload['targetalbe_type'] = get_class($buildPlan);
                $paylaod['targetable_id'] = $buildPlan->id;
                dispatch(new CreateTaskJob($project, $payload));

                $success = true;
            }
        }

        return [
            'success' => $success,
        ];
    }

    /**<commit_msg>Fix typo<commit_after><?php

/*
 * This file is part of Piplin.
 *
 * Copyright (C) 2016-2017 piplin.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Piplin\Http\Controllers\Api;

use Illuminate\Http\Request;
use Piplin\Bus\Jobs\AbortTaskJob;
use Piplin\Bus\Jobs\CreateTaskJob;
use Piplin\Http\Controllers\Controller;
use Piplin\Models\Task;
use Piplin\Models\Project;
use Piplin\Services\Webhooks\Beanstalkapp;
use Piplin\Services\Webhooks\Bitbucket;
use Piplin\Services\Webhooks\Custom;
use Piplin\Services\Webhooks\Github;
use Piplin\Services\Webhooks\Gitlab;
use Piplin\Services\Webhooks\Gogs;
use Piplin\Services\Webhooks\Oschina;

/**
 * The deployment incoming-webhook controller.
 */
class IncomingWebhookController extends Controller
{
    /**
     * List of supported service classes.
     * @var array
     */
    private $services = [
        Beanstalkapp::class,
        Bitbucket::class,
        Github::class,
        Gitlab::class,
        Gogs::class,
        Oschina::class,
    ];

    /**
     * Class constructor.
     *
     * @return void
     */
    public function __construct()
    {
        $this->services[] = Custom::class;
    }

    /**
     * Handles incoming requests to trigger deploy.
     *
     * @param Request $request
     * @param string  $hash
     *
     * @return Response
     */
    public function deploy(Request $request, $hash)
    {
        $project = Project::where('hash', $hash)->firstOrFail();

        $deployPlan = $project->deployPlan;

        $success = false;
        if ($deployPlan && $deployPlan->environments->count() > 0) {
            $payload = $this->parseWebhookRequest($request, $project);

            if (is_array($payload) && ($project->allow_other_branch || $project->branch === $payload['branch'])) {
                $this->abortQueued($project->id);
                $payload['targetable_type'] = get_class($deployPlan);
                $payload['targetable_id'] = $deployPlan->id;
                dispatch(new CreateTaskJob($project, $payload));

                $success = true;
            }
        }

        return [
            'success' => $success,
        ];
    }

    /**
     * Handles incoming requests to trigger build.
     *
     * @param Request $request
     * @param string  $hash
     *
     * @return Response
     */
    public function build(Request $request, $hash)
    {
        $project = Project::where('hash', $hash)->firstOrFail();

        $buildPlan = $project->buildPlan;

        $success = false;
        if ($buildPlan && $buildPlan->servers->count() > 0) {
            $payload = $this->parseWebhookRequest($request, $project);

            if (is_array($payload) && ($project->allow_other_branch || $project->branch === $payload['branch'])) {
                $this->abortQueued($project->id);
                $payload['targetable_type'] = get_class($buildPlan);
                $payload['targetable_id'] = $buildPlan->id;
                dispatch(new CreateTaskJob($project, $payload));

                $success = true;
            }
        }

        return [
            'success' => $success,
        ];
    }

    /**
{'fraction_non_alphanumeric': 0.13648293963254593, 'fraction_numerical': 0.003087849312953528, 'mean_word_length': 2.2950152594099693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3134828', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1896, 'n_words': 385}