lp-digital/github-event-parser

此包已被弃用且不再维护。作者建议使用 love-oss/github-event-parser 包。

PHP 库,用于获取 Github API v3 发送事件的可读表示形式

v0.8.3 2017-04-19 20:00 UTC

This package is auto-updated.

Last update: 2021-08-18 13:40:26 UTC


README

SensioLabsInsight

这是一个简单的 PHP 库,用于提供来自 Github 事件 API v3 的 json 响应的可读表示形式。

感谢 Github webhooks,任何仓库管理员都可以访问和监听这些以 json 响应返回的事件。

此库的唯一目的是解析这些响应,并创建简单的 POPO (Plain Old PHP Object) 对象,便于操作、扩展,甚至持久化到数据库。

由于您可以监听所有事件,因此有很多用法可用

  • 对您的仓库进行统计
  • 在成功的部署后执行一些任务
  • 为每个验证过的贡献发送“感谢”电子邮件
  • 等等...

安装

$ composer require "lp-digital/github-event-parser"

PHP 要求

库可能需要访问 GitHub API 以检索更多信息。您的 PHP 配置可能已启用 allow_url_fopen 和有效的 user_agent,否则一些信息将无法检索。

您可以使用 InvalidPhpConfigurationException 捕获异常

<?php
try {
    $commits = $pullRequest->getCommits(); // use the GitHub API when called.
} catch(\Lpdigital\Github\Exception\InvalidPhpConfigurationException $e) {
    // ...
}

如何解决 Github 的 json 响应?

一旦您的 webhook 设置完成,每次平台派发事件时,您都应该从 github 收到 POST 响应。

例如,假设您有一个简单的 github-hook.php 文件,并通过 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 */

事件类型

请注意,此库尚不完整,因此目前只有少量事件可用。但是,实现缺少的事件非常简单。如果您需要它们,请发起一个拉取请求!

IssueCommentEvent

当有人对问题进行评论时触发

您可以从此事件中检索问题、用户和相关的评论。

<?php
$issueCommentEvent->issue;    // instance of Lpdigital/Entity/Issue
$issueCommentEvent->user;     // instance of Lpdigital/Entity/User
$issueCommentEvent->comment;  // instance of Lpdigital/Entity/Comment

IssuesEvent

当问题被分配、取消分配、标记、取消标记、打开、关闭或重新打开时触发。

您可以从此事件中检索操作、仓库和发送者。当可用时,您还可以获取分配者和标签。

<?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

当有人复制仓库时触发

您可以从此事件中检索复制的仓库、所有者、新仓库和“复制者”。

<?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

当部署状态发生变化时触发

您可以从此事件中检索部署、发送者和相关的仓库。

<?php
$deploymentStatusEvent->deployment;   // instance of Lpdigital/Entity/Deployment
$deploymentStatusEvent->sender;       // instance of Lpdigital/Entity/User
$deploymentStatusEvent->repository;   // instance of Lpdigital/Entity/Repository

PullRequestEvent

当拉取请求被分配、取消分配、标记、取消标记、打开、关闭、重新打开或同步时触发。

$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

当仓库分支被推送时触发。除了分支推送外,当仓库标签被推送时,webhook推送事件也会被触发。

$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

当Git提交的状态发生变化时触发。此类事件在时间轴中不可见,仅用于触发钩子。

您可以检索sha、状态、提交者和相关仓库。还有更多其他信息可供获取。

<?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

WatchEvent与收藏仓库相关,而不是关注。请参阅此API博客文章以获取解释。事件的参与者是收藏仓库的用户,事件的仓库是被收藏的仓库。

<?php
$watchEvent->action;        // "started"
$watchEvent->user           // instance of Lpdigital\Entity\User
$watchEvent->repository     // instance of Lpdigital\Entity\Repository

PullRequestReviewCommentEvent

当在拉取请求的统一差异的某部分创建评论时触发。

<?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

当Wiki页面被创建或更新时触发。

<?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

来自Github API的每个对象都有一个PHP类。

  • 评论
  • 提交(和提交者)
  • 部署
  • 问题
  • 标签
  • 页面(Wiki
  • 拉取请求
  • 发布
  • 仓库
  • 用户

ROADMAP

  • 添加所有缺失的事件
  • 为doctrine/dbal添加doctrine映射文件

Contribute

所有功能都经过测试,所有贡献都需要经过测试才能被接受。

优先考虑路线图上的功能和错误修复。从仓库分叉,创建功能分支,然后启动测试套件

$ phpunit

感谢您的帮助,如果您使用了这个库,请告诉我们 ;)