Giter Site home page Giter Site logo

jirawebhook's Introduction

What is this?

This is PHP library for processing and handling Atlassian JIRA webhook data.

It contains classes that can parse data from JIRA webhooks, create slack client message objects and events.

The package is meant to be used with the Vicky slackbot, but it can also be used independently.

Installation

With composer, create a new composer.json file and add the following code:

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

Then run command composer install.

Usage

JIRA data events

To create a new event use following example code:

use JiraWebhook\JiraWebhook;

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

    if (!$data) {
        throw new JiraWebhookException('There is no data in the Jira webhook');
    }
} catch (JiraWebhookException $e) {
    error_log($e->getMessage());
}

$jiraWebhook = new JiraWebhook($data);
$jiraWebhook->addListener('jira:issue_updated', function($event, $data)
{
    if ($data->isIssueCommented()) {
        // Instantiate a new Slack Client (refer to https://github.com/maknz/slack for documentation)
        $slackClient = new Client($incomingWebhookUrl, $clientSettings);
        
        // Get Slack Client message object
        $slackClientMessage = $slackClient->createMessage();
        
        // Set up the slack message format
        JiraWebhook::convert('JiraDefaultToSlack', $data, $slackClientMessage);
        
        // Send off the message to Slack's API
        $slackClientMessage->to('#channel');
        $slackClientMessage->send();
    }
});

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

JIRA data converters

To create a new converter create a new class that implements the JiraWebhookDataConverter interface. Then to set and use a new converter use the following example code:

use Maknz\Slack\Message;
use JiraWebhook\JiraWebhook;
use JiraWebhook\JiraWebhookDataConverter;

class NewConverterClass implements JiraWebhookDataConverter
{

    public function convert(JiraWebhookData $data, Message $clientMessage)
    {
        $issue = $data->getIssue();

        $message = vsprintf(
            "Key: %s, Status: %s, Assignee: %s",
            [
                $issue->getKey(),
                $issue->getStatus(),
                $issue->getAssignee(),
            ]
        );
        
        $attachment = [
            "color" => $issue->getColour(),
            "fallback" => $message,
            "pretext" => $data->getIssueEventDescription(),
            "title" => vsprintf("(%s) %s", [$issue->getKey(), $issue->getSummary()]),
            "title_link" => $issue->getUrl(),

            'fields' => [
                [
                    'title' => 'Status',
                    'value' => $issue->getStatus(),
                    'short' => true // whether the field is short enough to sit side-by-side other fields
                ],
                [
                    'title' => 'Priority',
                    'value' => $issue->getPriority(),
                    'short' => true
                ]
            ],
        ];

        $clientMessage->attach($attachment);

        return $clientMessage;
    }
}

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

Please refer to Slack's documentation for message formatting for more details on the $attachment variable above.

Testing

Run vendor/bin/phpunit or just phpunit if you have it installed globally.

jirawebhook's People

Contributors

chewbacca96 avatar elenakolevska avatar l80 avatar kommuna avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.