<commit_before><?php
/**
 * Class that pars JIRA issue data and gives access to it.
 *
 * @credits https://github.com/kommuna
 * @author  <EMAIL>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace JiraWebhook\Models;

use JiraWebhook\Exceptions\JiraWebhookDataException;

class JiraIssue
{
    /**
     * JIRA issue id
     *
     * @var
     */
    protected $id;

    /**
     * JIRA issue self URL
     *
     * @var
     */
    protected $self;

    /**
     * JIRA issue key
     *
     * @var
     */
    protected $key;

    /**
     * JIRA issue type
     *
     * @var
     */
    protected $issueType;

    /**
     * JIRA issue project name
     *
     * @var
     */
    protected $projectName;

    /**
     * JIRA issue priority
     *
     * @var
     */
    protected $priority;

    /**
     * JIRA issue colour, based on priority
     *
     * @var
     */
    protected $colour;

    /**
     * JIRA issue assignee user
     *
     * @var
     */
    protected $assignee;

    /**
     * JIRA issue status
     *
     * @var
     */
    protected $status;

    /**
     * JIRA issue summary
     *
     * @var
     */
    protected $summary;

    /**
     * JiraWebhook\Models\JiraIssueComments
     *
     * @var
     */
    protected $issueComments;

    /**
     * Parsing JIRA issue $data
     *
     * @param null $data
     *
     * @return JiraIssue
     *
     * @throws JiraWebhookDataException
     */
    public static function parse($data = null)
    {
        $issueData = new self;

        if (!$data) {
            return $issueData;
        }

        $issueData->validate($data);

        $issueFields = $data['fields'];

        $issueData->setID($data['id']);
        $issueData->setSelf($data['self']);
        $issueData->setUrl($data['key'], $data['self']);
        $issueData->setKey($data['key']);
        $issueData->setIssueType($issueFields['issuetype']['name']);
        $issueData->setProjectName($issueFields['project']['name']);
        $issueData->setPriority($issueFields['priority']['name']);
        $issueData->setColour($issueFields['priority']['name']);
        $issueData->setAssignee(JiraUser::parse($issueFields['assignee']));
        $issueData->setStatus($issueFields['status']['name']);
        $issueData->setSummary($issueFields['summary']);
        $issueData->setIssueComments(JiraIssueComments::parse($data['fields']['comment']));

        return $issueData;
    }

    /**
     * @param $data
     * @throws JiraWebhookDataException
     */
    public function validate($data)
    {
        if (empty($data['id'])) {
            throw new JiraWebhookDataException('JIRA issue id does not exist!');
        }

        if (empty($data['self'])) {
            throw new JiraWebhookDataException('JIRA issue self URL does not exist!');
        }

        if (empty($data['key'])) {
            throw new JiraWebhookDataException('JIRA issue key does not exist!');
        }

        if (empty($data['fields'])) {
            throw new JiraWebhookDataException('JIRA issue fields does not exist!');
        }

        if (empty($data['fields']['issuetype']['name'])) {
            throw new JiraWebhookDataException('JIRA issue type does not exist!');
        }

        if (empty($data['fields']['priority']['name'])) {
            throw new JiraWebhookDataException('JIRA issue priority does not exist!');
        }
    }

    /**
     * Check JIRA issue priority is Blocker
     *
     * @return bool
     */
    public function isPriorityBlocker()
    {
        return $this->getPriority() === 'Blocker';
    }

    /**
     * Check JIRA issue type is Operations
     *
     * @return bool
     */
    public function isTypeOperations()
    {
        return $this->getIssueType() === 'Operations';
    }

    /**
     * Check JIRA issue type is Urgent bug
     *
     * @return bool
     */
    public function isTypeUrgentBug()
    {
        return $this->getIssueType() === 'Urgent bug';
    }

    /**
     * Check JIRA issue status is Resolved
     *
     * @return bool|int<commit_msg>Account for japanese characters in issue type name<commit_after><?php
/**
 * Class that pars JIRA issue data and gives access to it.
 *
 * @credits https://github.com/kommuna
 * @author  <EMAIL>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace JiraWebhook\Models;

use JiraWebhook\Exceptions\JiraWebhookDataException;

class JiraIssue
{
    /**
     * JIRA issue id
     *
     * @var
     */
    protected $id;

    /**
     * JIRA issue self URL
     *
     * @var
     */
    protected $self;

    /**
     * JIRA issue key
     *
     * @var
     */
    protected $key;

    /**
     * JIRA issue type
     *
     * @var
     */
    protected $issueType;

    /**
     * JIRA issue project name
     *
     * @var
     */
    protected $projectName;

    /**
     * JIRA issue priority
     *
     * @var
     */
    protected $priority;

    /**
     * JIRA issue colour, based on priority
     *
     * @var
     */
    protected $colour;

    /**
     * JIRA issue assignee user
     *
     * @var
     */
    protected $assignee;

    /**
     * JIRA issue status
     *
     * @var
     */
    protected $status;

    /**
     * JIRA issue summary
     *
     * @var
     */
    protected $summary;

    /**
     * JiraWebhook\Models\JiraIssueComments
     *
     * @var
     */
    protected $issueComments;

    /**
     * Parsing JIRA issue $data
     *
     * @param null $data
     *
     * @return JiraIssue
     *
     * @throws JiraWebhookDataException
     */
    public static function parse($data = null)
    {
        $issueData = new self;

        if (!$data) {
            return $issueData;
        }

        $issueData->validate($data);

        $issueFields = $data['fields'];

        $issueData->setID($data['id']);
        $issueData->setSelf($data['self']);
        $issueData->setUrl($data['key'], $data['self']);
        $issueData->setKey($data['key']);
        $issueData->setIssueType($issueFields['issuetype']['name']);
        $issueData->setProjectName($issueFields['project']['name']);
        $issueData->setPriority($issueFields['priority']['name']);
        $issueData->setColour($issueFields['priority']['name']);
        $issueData->setAssignee(JiraUser::parse($issueFields['assignee']));
        $issueData->setStatus($issueFields['status']['name']);
        $issueData->setSummary($issueFields['summary']);
        $issueData->setIssueComments(JiraIssueComments::parse($data['fields']['comment']));

        return $issueData;
    }

    /**
     * @param $data
     * @throws JiraWebhookDataException
     */
    public function validate($data)
    {
        if (empty($data['id'])) {
            throw new JiraWebhookDataException('JIRA issue id does not exist!');
        }

        if (empty($data['self'])) {
            throw new JiraWebhookDataException('JIRA issue self URL does not exist!');
        }

        if (empty($data['key'])) {
            throw new JiraWebhookDataException('JIRA issue key does not exist!');
        }

        if (empty($data['fields'])) {
            throw new JiraWebhookDataException('JIRA issue fields does not exist!');
        }

        if (empty($data['fields']['issuetype']['name'])) {
            throw new JiraWebhookDataException('JIRA issue type does not exist!');
        }

        if (empty($data['fields']['priority']['name'])) {
            throw new JiraWebhookDataException('JIRA issue priority does not exist!');
        }
    }

    /**
     * Check JIRA issue priority is Blocker
     *
     * @return bool
     */
    public function isPriorityBlocker()
    {
        return $this->getPriority() === 'Blocker';
    }

    /**
     * Check JIRA issue type is Operations
     *
     * @return bool
     */
    public function isTypeOperations()
    {
        return strpos($this->getIssueType(), 'Operations') !== false;
    }

    /**
     * Check JIRA issue type is Urgent bug
     *
     * @return bool
     */
    public function isTypeUrgentBug()
    {
        return strpos($this->getIssueType(), 'Urgent bug') !== false;
    }

    /**
     * Check JIRA issue status is Resolved
     *
     * @return bool|int
{'fraction_non_alphanumeric': 0.14239636841476527, 'fraction_numerical': 0.0, 'mean_word_length': 2.0510204081632653, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '4859123', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2473, 'n_words': 552}