lp-digital / github-event-parser
PHP library to get a readable representation of events dispatched by Github API v3
Installs: 2 881
Dependents: 0
Suggesters: 0
Security: 0
Stars: 20
Watchers: 3
Forks: 5
Open Issues: 4
Requires
- php: >=5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ~1.11
- phpunit/phpunit: ~4.7
README
This is a naive PHP library to provide readable representations of json responses from Github Events Api v3.
Thanks to the Github webhooks, any administrator of a repository can access and listen theses events returned into json responses.
The only aim of this library is to parse theses responses, and create simple POPO (Plain Old PHP Object) easy to manipulate, extends or even persist in a database.
A lot of usages are available since you can listen all events:
- make statistics on your repositories
- do some tasks after a successful deployment
- send a "thanks" email for each validated contribution
- and so on ...
Installation
$ composer require "lp-digital/github-event-parser"
PHP requirements
The library may access to GitHub API to retrieve additional information.
Your PHP configuration may have allow_url_fopen
and a valid user_agent
enabled, or
some informations won't be retrieved.
You can use InvalidPhpConfigurationException
to catch the exception:
<?php try { $commits = $pullRequest->getCommits(); // use the GitHub API when called. } catch(\Lpdigital\Github\Exception\InvalidPhpConfigurationException $e) { // ... }
How to resolve a json response from Github ?
Once your webhook is set up, you should receive POST responses from github each time an event is dispatched by the platform.
For instance, let's consider you have a simple github-hook.php
file and have installed your dependency through composer:
<?php include_once('./vendor/autoload.php'); use Lpdigital\Github\Parser\WebhookResolver; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $decodedJson = json_decode(file_get_contents('php://input'), true); $resolver = new WebhookResolver(); $event = $resolver->resolve($decodedJson); // ex: an instance of `IssueCommentEvent` echo($event::name()); // IssueCommentEvent /* ... do your own business */
EventTypes
Note that this library is not complete, so only few events are available for now. But, it's realy easy to implement the missing. If you need them, please make a pull request!
IssueCommentEvent
Dispatched when someone comment an issue
You can retrieve the issue, the user and the related comment from this event.
<?php $issueCommentEvent->issue; // instance of Lpdigital/Entity/Issue $issueCommentEvent->user; // instance of Lpdigital/Entity/User $issueCommentEvent->comment; // instance of Lpdigital/Entity/Comment
IssuesEvent
Dispatched when an issue is assigned, unassigned, labeled, unlabeled, opened, closed, or reopened.
You can retrieve the action, the repository and the sender from this event. When available, you can also get assignee and label.
<?php $issuesEvent->action; // Can be one of "assigned", "unassigned", "labeled", "unlabeled", "opened", "closed", or "reopened". $issuesEvent->assignee; // optional: the assignee of the issue(Lpdigital/Entity/User) $issuesEvent->issue; // instance of Lpdigital/Entity/Issue $issuesEvent->label; // optional: the label of the issue(Lpdigital/Entity/Label) $issuesEvent->repository; // instance of Lpdigital/Entity/Repository $issuesEvent->sender; // instance of Lpdigital/Entity/User
ForkEvent
Dispatched when someone fork the repository
You can retrieve the forked repository, the owner, the new repository and the "forker".
<?php $forkEvent->forkedRepository; // instance of Lpdigital/Entity/Repository $forkEvent->owner; // instance of Lpdigital/Entity/User $forkEvent->repository; // instance of Lpdigital/Entity/Repository $forkEvent->forker; // instance of Lpdigital/Entity/User
DeploymentStatusEvent
Dispatched when a deployement's status changes
You can retrieve the deployment, the sender and the related repository.
<?php $deploymentStatusEvent->deployment; // instance of Lpdigital/Entity/Deployment $deploymentStatusEvent->sender; // instance of Lpdigital/Entity/User $deploymentStatusEvent->repository; // instance of Lpdigital/Entity/Repository
PullRequestEvent
Dispatched when a pull request is assigned, unassigned, labeled, unlabeled, opened, closed, reopened, or synchronized.
$pullRequestEvent->pullRequest; // instance of Lpdigital/Entity/PullRequest $pullRequest->action; /** * Can be one of “assigned”, “unassigned”, “labeled”, “unlabeled”, “opened”, “closed”, or “reopened”, or “synchronize”. * If the action is “closed” and the merged key is false, the pull request was closed with unmerged commits. * If the action is “closed” and the merged key is true, the pull request was merged. */ $pullRequest->number; // the pull request number $pullRequest->repository; // instance of Lpdigital/Entity/Repository
PushEvent
Dispatched when a repository branch is pushed to. In addition to branch pushes, webhook push events are also triggered when repository tags are pushed.
$pushEvent->ref // the full Git ref that was pushed ex: refs/heads/master $pushEvent->head // the SHA of the most recent commit on ref after the push $pushEvent->before // the SHA of the most recent commit on ref before the push $pushEvent->size // the number of commits in the push $pushEvent->commits // an array of objects that describe the pushed commits
StatusEvent
Dispatched when the status of a Git commit changes. Events of this type are not visible in timelines, they are only used to trigger hooks.
You can retrieve the sha, the status, the committer and the related repository. More others informations are available.
<?php $statusEvent->sha; // something like "9049f1265b7d61be4a8904a9a27120d2064dab3b" $statusEvent->status; // Can be one of "success", "failure" or "error". $statusEvent->commiter; // instance of Lpdigital/Entity/User $statusEvent->repository; // instance of Lpdigital/Entity/Repository
WatchEvent
The WatchEvent is related to starring a repository, not watching. See this API blog post for an explanation. The event’s actor is the user who starred a repository, and the event’s repository is the repository that was starred.
<?php $watchEvent->action; // "started" $watchEvent->user // instance of Lpdigital\Entity\User $watchEvent->repository // instance of Lpdigital\Entity\Repository
PullRequestReviewCommentEvent
Dispatched when a comment is created on a portion of the unified diff of a pull request.
<?php $pullRequestReviewCommentEvent->action // "created" $pullRequestReviewCommentEvent->comment // instance of Lpdigital\Entity\Comment $pullRequestReviewCommentEvent->pullRequest // instance of Lpdigital\Entity\PullRequest $pullRequestReviewCommentEvent->repository // instance of Lpdigital\Entity\Repository $pullRequestReviewCommentEvent->sender // instance of Lpdigital\Entity\User
GollumEvent
Dispatched when a Wiki page is created or updated.
<?php $gollumEvent->pages // an array of Lpdigital\Entity\Page objects $gollumEvent->repository // instance of Lpdigital\Entity\Repository $gollumEvent->sender // instance of Lpdigital\Entity\User
Entities
Each object from Github API have his PHP class.
- Comment
- Commit (and CommitUser)
- Deployment
- Issue
- Label
- Page (Wiki)
- PullRequest
- Release
- Repository
- User
ROADMAP
- Add all missing events
- Add doctrine mapping file for doctrine/dbal
Contribute
All features are tested, and all contributions need to be tested in order to be accepted.
Features from roadmap and bug fixes are prioritized. Fork the repository, create a feature branch and then launch the testsuite:
$ phpunit
Thank you for help, let us know if you use this library ;)