【问题标题】:Github WebHooks get branch detailsGithub WebHooks 获取分支详细信息
【发布时间】:2021-03-11 01:18:07
【问题描述】:

我正在尝试执行以下操作:

  1. 创建一个 Github webhook(完成)
  2. 创建一个 GIHook 指向的 PHP/Node 应用程序(完成)
  3. 处理响应(这是问题所在)
  4. 根据提交的分支,向开发或实时服务器发出请求以执行 update.sh(完成)

问题是 GIHook 发给我这个:

{
"REQUEST_METHOD":"GET",
"CONTENT_TYPE":"application\/json",
"headers":{
"Content-Type":"application\/json",
"X-Hub-Signature-256":"sha256=xxxx",
"X-Hub-Signature":"sha1=xxx",
"X-Github-Hook-Installation-Target-Type":"repository",
"X-Github-Hook-Installation-Target-Id":"xxx",
"X-Github-Hook-Id":"xxx",
"X-Github-Event":"push",
"X-Github-Delivery":"xxx",
"Referer":"webhook url called",
"Accept":"*\/*",
"User-Agent":"GitHub-Hookshot\/8338482",
"Connection":"close",
"X-Accel-Internal":"\/internal-nginx-static-location",
"X-Real-Ip":"xxx",
"Host":"xxxx"
},
"JSON":"",
"POST":[],
"GET":[],
"payload":null
},

这并没有告诉我提交到哪个分支。

然后我又阅读了一些零碎的内容,发现还有更多信息需要获取,但问题是如何获取。所以我假设我会调用一个 api 并回传一个 id 或其他东西,经过一番谷歌搜索后,它看起来就像这样。所以我测试了:

# :HOOK_ID = X-Github-Hook-Id
curl -o -v - https://api.github.com/repos/:GitHubUserNameInUrl/:PrivateRepo/hooks/:HOOK_ID

通过 curl 和文件获取内容对 php 执行相同操作,只返回相同的结果:

{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository-webhook"
}

有人知道如何从 webhook 给我的响应(PHP 或 Node)中获取有效负载吗?

谢谢

D

响应 1 感谢您的回复,但我没有得到任何信息,例如您从 webhook 中获得的信息。这是我在 webhook 调用我的 url 时用来测试我的响应数据的代码。 JSON 总是 "" 并且 POST,GET 总是空数组 []。

$file = 'log.json';

$dte = date("d-m-y H:i:s");
$headers = getallheaders();
$data = [];
$data["date"] = $dte;
$data["REQUEST_METHOD"] = $_SERVER["REQUEST_METHOD"];
$data["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
$data["headers"] = $headers;
$data["JSON"] = json_decode(file_get_contents('php://input')); // Always null || ""
$data["POST"] = $_POST; // Always []
$data["GET"] = $_GET; // Always []


$hookid = $headers["X-Github-Hook-Id"];
$data["hook_id"] = $hookid;

$APIUrl = "https://api.github.com/repos";
$url = $APIUrl . "/GITHUB_USER/REPO/hooks/".$hookid;
$data["callback"] = $url;

// Using file_get_contents
// $result = file_get_contents($url);
// $payload = json_decode($result, true);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
$payload = json_decode($result, true);
$data["payload"] = $payload;

$out = json_encode($data) . ",\n";
file_put_contents($file, $out, FILE_APPEND | LOCK_EX);

【问题讨论】:

    标签: php git github webhooks


    【解决方案1】:

    检查正在解码有效负载的代码。

    对于"X-Github-Event":"push",Content-Type: application/x-www-form-urlencoded,有效负载是百分比编码的JSON,而不是null

    POST /test HTTP/1.1
    Host: lcherone.requestcatcher.com
    Connection: close
    Accept: */*
    Connection: close
    Content-Length: 11363
    Content-Type: application/x-www-form-urlencoded
    User-Agent: GitHub-Hookshot/8338482
    X-Github-Delivery: e39cc102-318e-11eb-8301-d4f46caa1c8a
    X-Github-Event: push
    X-Github-Hook-Id: 265270862
    X-Github-Hook-Installation-Target-Id: 316492816
    X-Github-Hook-Installation-Target-Type: repository
    X-Hub-Signature: sha1=0baa6d15c266c08111508642607acc86b15f1f43
    X-Hub-Signature-256: sha256=6c58a95d4a5a3cde91bcc2621d6608a5190b1178be197db00ed6dd4364137898
    
    payload=%7B%22ref%22%3A%22refs%2Fheads%2Fmain%22%2C%22before%22%3A%220000000000000000000000000000000000000000%22%2C%22after%22%3A%224872b45f3df6d2b0485a69adb8c7476934be7629%22%2C%22repository%22%3A%7B%22id%22%3A316492816%2C%22node_id%22%3A%22MDEwOlJlcG9zaXRvcnkzMTY0OTI4MTY%3D%22%2C%22name%22%3A%22wightsystems%22%2C%22full_name%22%3A%22wightsystems%2Fwightsystems%22%2C%22private%22%3Afalse%2C%22owner%22%3A%7...snip
    

    你会做什么解码:

    $payload = json_decode(urldecode($_POST['payload']));(可能不需要urldecode,我没查)

    如果您选择Content-Type: application/json,则有效负载是原始 json:

    POST /test HTTP/1.1
    Host: lcherone.requestcatcher.com
    Connection: close
    Accept: */*
    Connection: close
    Content-Length: 8176
    Content-Type: application/json
    User-Agent: GitHub-Hookshot/8338482
    X-Github-Delivery: 2c350428-3190-11eb-90a9-13e895014cf2
    X-Github-Event: push
    X-Github-Hook-Id: 265270862
    X-Github-Hook-Installation-Target-Id: 316492816
    X-Github-Hook-Installation-Target-Type: repository
    X-Hub-Signature: sha1=20ea83c65ee1771913829db5553a13c09d5d44c0
    X-Hub-Signature-256: sha256=eb89d594cfa830be18d11f00d502bd0fdb118fa9017496a8bf52c6a286b1ced6
    
    {"ref":"refs/heads/main","before":"4872b45f3df6d2b0485a69adb8c7476934be7629","after":"ab03e7b705440088a6e22f06d31a660c8185bcb6","repository":{"id":316492816,"node_id":"MDEwOlJlcG9zaXRvcnkzMTY0OTI4MTY=","name":"wightsystems","full_name":"wightsystems/wightsystems","private":false,"owner":...snip}
    

    应该是:

    $payload = json_decode(file_get_contents('php://input'));

    解码后会有一堆数据,包括ref的分支,访问它是:

    echo str_replace('refs/heads/', '', $payload->ref);

    stdClass Object
    (
        [ref] => refs/heads/main
        [before] => 0000000000000000000000000000000000000000
        [after] => 4872b45f3df6d2b0485a69adb8c7476934be7629
        ...snip
    

    【讨论】:

    • 我没有得到这些详细信息,在主要问题的 RESPONSE 1 下添加了代码以回复您。我的 JSON php://input 始终为 null,webhook 设置为 json 模式。
    • 检查底部的编辑 webhook 页面中发送的内容(最近的交付)。我非常怀疑他们是否会发送 null 如果是这样,那么您应该报告它,这就是为什么我说 检查正在解码有效负载的代码。 因为有效负载不包含 }, "JSON":"", "POST":[], "GET":[], 所以它必须是你的代码。如果您添加了 webhook 接收器代码,它会有所帮助。
    • 在 Github webhooks 中,日志显示它发送了一个有效负载以及我需要的信息,但是当我使用 file_get_contents("php://input") 时,我的脚本接收到的内容为空。 "JSON":"", "POST":[], "GET":[] 只是我的 php 测试代码将内容转储到文本文件中,这样我就可以看到发生了什么。
    • 我还设置了 github webhook 来发送所有内容。
    • 您是否设置为发送应用程序/json?如果是这样,您可以添加您的代码.. 基本上,webhook 不会发送 null
    【解决方案2】:

    NGINX 重定向从 / 到 /index.php 是问题,似乎所有数据都没有通过从 / 到 index.php 的重定向完全传递。

    解决方案 将 webhook 设置为直接指向文件,在本例中为 index.php

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2019-05-03
      • 2013-03-15
      • 2012-06-02
      • 2020-10-08
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多