简体   繁体   English

Github WebHooks 获取分支详细信息

[英]Github WebHooks get branch details

I'm trying to do the following:我正在尝试执行以下操作:

  1. Create a Github webhook (done)创建一个 Github webhook(完成)
  2. Create a PHP/Node application GIHook points to (done)创建一个 PHP/Node 应用程序 GIHook 指向(完成)
  3. Process response (here is where problem lies)处理响应(这是问题所在)
  4. Depending on branch commited to, make a request to dev or live server to execute update.sh (done)根据提交的分支,向开发或实时服务器发出请求以执行 update.sh(完成)

Issue is the GIHook sends me this:问题是 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
},

Which doesn't tell me which branch was commited to.这并没有告诉我哪个分支被提交。

So then I read a few more bits and pieces and found there is more information to be obtained but the question is how.因此,我又阅读了一些零碎的内容,发现还有更多信息需要获取,但问题是如何获取。 So I would assumeed I would call an api and passback an id or something and after some googling it looked like this was the way.所以我假设我会调用一个 api 并回传一个 id 或其他东西,经过一番谷歌搜索后,它看起来就是这样。 So I tested:所以我测试了:

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

and done the same with php via curl and file get contents only to all return the same result:并通过 curl 对 php 进行相同操作,并且文件仅获取内容以返回相同的结果:

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

Anyone have an idea how to get the payload from the response the webhook gives me (PHP or Node)?任何人都知道如何从 webhook 给我的响应(PHP 或 Node)中获取有效负载?

Thanks谢谢

D D

RESPONSE 1 Thanks for that reply, but I don't get any information such as you get from your webhook.回复 1 感谢您的回复,但我没有得到任何信息,例如您从 webhook 中获得的信息。 Here is the code I use to test my response data when the webhook calls my url.这是我在 webhook 调用我的 url 时用来测试我的响应数据的代码。 JSON is always "" and POST,GET are always empty arrays []. JSON 始终为“”,POST、GET 始终为空 arrays []。

$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);

Check your code which is decoding the payload.检查正在解码有效负载的代码。

For "X-Github-Event":"push", and Content-Type: application/x-www-form-urlencoded the payload is percent encoded JSON not null对于"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

Which to decode you would do:要解码你会做什么:

$payload = json_decode(urldecode($_POST['payload'])); (might not need urldecode, I didnt check) (可能不需要 urldecode,我没有检查)

Whilst if you choose Content-Type: application/json the payload is raw json:如果您选择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}

Which would be:这将是:

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

Once decode it has a bunch of data, including the branch which is ref and to access it would be:一旦解码,它就会有一堆数据,包括ref的分支,并且要访问它是:

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

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

NGINX Redirect from / to /index.php was the issue, seems all data is not being fully passed via the redirect from / to index.php. NGINX 从 / 重定向到 /index.php 是问题所在,似乎所有数据都没有通过从 / 到 index.php 的重定向完全传递。

SOLUTION Set webhook to point to the file directly, in this case index.php解决方案 将 webhook 设置为直接指向文件,在本例中为 index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM