John Davidson

php - Github WebHooks get branch details

0 comments
Message:


I'm trying to do the following:



  1. Create a Github webhook (done)

  2. Create a PHP/Node application GIHook points to (done)

  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)


Issue is the GIHook sends me this:


{
"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. 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:


{
"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)?


Thanks


D


RESPONSE 1
Thanks for that reply, but I don't get any information such as you get from your webhook. Here is the code I use to test my response data when the webhook calls my url. JSON is always "" and POST,GET are always empty 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);


My Answer:
It seems like you are trying to retrieve the payload data from the Github webhook response. The payload data contains information about the commit that triggered the webhook, including the branch name.

In your PHP code, you are trying to access the payload data using `json_decode(file_get_contents('php://input'))` or `$_POST`, but it seems like the payload data is not being received in the request body.

To access the payload data in the Github webhook response, you can try the following:

1. Make sure that your webhook is configured to send the payload data. You can check the webhook settings in your Github repository to ensure that the payload data is included in the webhook request.

2. Modify your PHP code to access the payload data from the webhook request. You can try accessing the raw request body using `file_get_contents('php://input')` and then decode the JSON data to extract the branch information.

Here is an example of how you can access the payload data in your PHP code:

php
// Get the raw request body
$payload = file_get_contents('php://input');

// Decode the JSON data
$data = json_decode($payload, true);

// Extract the branch information
$branch = $data['ref'];

// Log the branch information
file_put_contents('log.txt', 'Branch: ' . $branch . PHP_EOL, FILE_APPEND);

// Process the branch information as needed


By accessing the raw request body and decoding the JSON data, you should be able to extract the branch information from the Github webhook response. Make sure to check the webhook settings and verify that the payload data is being sent in the webhook request.

Rate this post

5 of 5 based on 2701 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us