love-oss/github-event-parser

此包已被废弃,不再维护。未建议替代包。

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

0.8.4 2021-07-07 09:33 UTC

This package is auto-updated.

Last update: 2022-08-19 05:44:49 UTC


README

PHPStan

这个库是 2016 年由 Mickaël Andrieu 为法国巴黎的 Lp Digital 网络机构创建的库的重新上传。我们已获得前创作者的授权,将许可从 GNU-GPL v2 更改为 MIT。PHP 开发者:请考虑使用 love-oss/github-event-parser 作为 lp-digital/github-event-parser 的直接替代品。

Github Event Parser 是一个简单的 PHP 库,旨在提供来自 Github Events Api v3 的 json 响应的可读表示。

由于 Github webhooks,任何仓库管理员都可以访问并监听这些以 json 格式返回的事件。

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

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

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

安装

$ composer require "love-oss/github-event-parser"

PHP 要求

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

您可以使用 InvalidPhpConfigurationException 来捕获异常

<?php

use LoveOSS\Github\Exception\InvalidPhpConfigurationException;

try {
    $commits = $pullRequest->getCommits(); // use the GitHub API when called.
} catch(InvalidPhpConfigurationException $exception) {
    // ...
}

如何解决 Github 的 json 响应?

一旦您的 webhook 设置完成,每当平台派发事件时,您应该会收到来自 github 的 POST 响应。

例如,假设您有一个简单的 github-hook.php 文件,并且已通过 composer 安装了您的依赖项

<?php
include_once('./vendor/autoload.php');
use LoveOSS\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 */
}

事件类型

请注意,此库尚不完整,因此目前只有少数事件可用。但实现缺失的事件非常简单。如果您需要它们,请提交一个 pull request!

IssueCommentEvent

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

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

<?php
$issueCommentEvent->issue;    // instance of LoveOSS/Entity/Issue
$issueCommentEvent->user;     // instance of LoveOSS/Entity/User
$issueCommentEvent->comment;  // instance of LoveOSS/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(LoveOSS/Entity/User)
$issuesEvent->issue;       // instance of LoveOSS/Entity/Issue
$issuesEvent->label;       // optional: the label of the issue(LoveOSS/Entity/Label)
$issuesEvent->repository;  // instance of LoveOSS/Entity/Repository
$issuesEvent->sender;      // instance of LoveOSS/Entity/User

ForkEvent

当有人对仓库进行分支操作时触发

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

<?php
$forkEvent->forkedRepository;  // instance of LoveOSS/Entity/Repository
$forkEvent->owner;             // instance of LoveOSS/Entity/User
$forkEvent->repository;        // instance of LoveOSS/Entity/Repository
$forkEvent->forker;            // instance of LoveOSS/Entity/User

部署状态事件

当部署状态改变时触发

您可以检索部署、发送者及相关仓库。

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

拉取请求事件

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

$pullRequestEvent->pullRequest;   // instance of LoveOSS/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 LoveOSS/Entity/Repository

推送事件

当仓库分支被推送时触发。除了分支推送外,当仓库标签被推送时,也会触发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 

状态事件

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

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

<?php
$statusEvent->sha;           // something like "9049f1265b7d61be4a8904a9a27120d2064dab3b"
$statusEvent->status;        // Can be one of "success", "failure" or "error".
$statusEvent->commiter;      // instance of LoveOSS/Entity/User
$statusEvent->repository;    // instance of LoveOSS/Entity/Repository

观察事件

观察事件与仓库星标相关,而不是关注。请参阅此API博客文章以获取解释。事件的执行者是星标仓库的用户,事件的相关仓库是被星标的仓库。

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

拉取请求评论事件

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

<?php
$pullRequestReviewCommentEvent->action          // "created"
$pullRequestReviewCommentEvent->comment         // instance of LoveOSS\Entity\Comment
$pullRequestReviewCommentEvent->pullRequest     // instance of LoveOSS\Entity\PullRequest
$pullRequestReviewCommentEvent->repository      // instance of LoveOSS\Entity\Repository
$pullRequestReviewCommentEvent->sender          // instance of LoveOSS\Entity\User

Wiki事件

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

<?php
$gollumEvent->pages          // an array of LoveOSS\Entity\Page objects
$gollumEvent->repository     // instance of LoveOSS\Entity\Repository
$gollumEvent->sender         // instance of LoveOSS\Entity\User

实体

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

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

路线图

  • 改进和监控此库的质量
  • 添加缺失的事件
  • 添加doctrine/dbal的映射文件

如何贡献?

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

路线图中的功能和错误修复具有优先级。分叉仓库,创建功能分支,然后启动测试套件

$ ./vendor/bin/phpunit

感谢您的帮助,如果您使用此库,请让我们知道;)