komuna / jirawebhook

用于处理和操作Atlassian JIRA webhook数据结构的PHP类,包含事件

1.0.1 2017-03-27 17:42 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:25:26 UTC


README

这是一个PHP库,用于处理和操作Atlassian JIRA webhook数据。

它包含可以解析来自JIRA webhook的数据的类,创建自定义事件的监听器以及解析数据的转换器接口。

该包旨在与Vicky slackbot一起使用,但也可以独立使用。

安装

使用composer,创建一个新的composer.json文件,并添加以下代码

{
    "minimum-stability" : "dev",
    "require": {
        "kommuna/jirawebhook": "dev-master"
    }
}

然后运行命令composer install

使用方法

JIRA数据事件

要创建一个新的事件,请参考以下示例代码

use JiraWebhook\JiraWebhook;
use JiraWebhook\Models\JiraWebhookData;

require __DIR__.'/vendor/autoload.php';
$config = require '/etc/vicky/config.php';

$jiraWebhook = new JiraWebhook();

$jiraWebhook->addListener('jira:event_name', function($e, $data)
{
    /**
     * Your code here
     */ 
});

try {
    $f = fopen('php://input', 'r');
    $data = stream_get_contents($f);

    if (!$data) {
        throw new JiraWebhookException('There is no data in the Jira webhook');
    }

    $jiraWebhook->run();
} catch (\Exception $e) {
     error_log($e->getMessage());
}

JIRA数据转换器

要创建一个新转换器,创建一个新的类,实现JiraWebhookDataConverter接口。然后要设置和使用一个新的转换器,请使用以下示例代码

use JiraWebhook\JiraWebhookDataConverter;
use JiraWebhook\Models\JiraWebhookData;

class NewConverterClass implements JiraWebhookDataConverter
{
    public function convert(JiraWebhookData $data)
    {
        $issue        = $data->getIssue();
        $assigneeName = $issue->getAssignee()->getName();
        $comment      = $issue->getIssueComments()->getLastComment();
        
        $message = vsprintf(
            ":no_entry_sign: <%s|%s> %s: %s ➠ @%s\n@%s ➠ %s",
            [
                $issue->getUrl(),
                $issue->getKey(),
                $issue->getStatusName(),
                $issue->getSummary(),
                $assigneeName,
                $comment->getAuthor()->getName(),
                $comment->getBody(0, 178)
            ]
        );
        
        return $message;
    }
}

JiraWebhook::setConverter('converterName', new NewConverterClass());
$message = JiraWebhook::convert('converterName', $data);

有关详细信息,请参阅Slack的消息格式化文档

测试

运行vendor/bin/phpunit或如果已全局安装,则直接运行phpunit