Added GitHub webhook functionality and configuration for API interaction

This commit introduces functionality for GitHub webhook interaction and API calls. The git operations are handled by a custom GitHub client in PHP, details for API endpoints are outlined in a new OpenAPI schema, and the project setup includes PHP FPM and NGINX configuration using Docker. A webhook receiver is also added to process incoming webhook payloads.
This commit is contained in:
stephan.kasdorf
2024-05-29 15:32:08 +02:00
commit 40139d144b
8 changed files with 482 additions and 0 deletions

39
src/GitHubClient.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
class GitHubClient
{
private $client;
private $token;
public function __construct($token)
{
$this->client = new Client([
'base_uri' => 'https://api.github.com/',
'headers' => [
'Authorization' => "token $token",
'Accept' => 'application/vnd.github.v3+json',
],
]);
$this->token = $token;
}
public function getRepositoryContent($owner, $repo, $path, $ref = 'main')
{
$response = $this->client->get("repos/$owner/$repo/contents/$path", [
'query' => ['ref' => $ref]
]);
return json_decode($response->getBody(), true);
}
public function listCommits($owner, $repo, $params = [])
{
$response = $this->client->get("repos/$owner/$repo/commits", [
'query' => $params
]);
return json_decode($response->getBody(), true);
}
}

29
src/composer.json Normal file
View File

@@ -0,0 +1,29 @@
{
"name": "vendor_name/nibiru-framework-agent",
"description": "A project to interact with GitHub API using PHP",
"authors": [
{
"name": "Stephan Kasdorf",
"email": "stephan.kasdorf@bittomine.com"
}
],
"require": {
"php": "^8.3",
"guzzlehttp/guzzle": "^7.3"
},
"autoload": {
"psr-4": {
"": "src/"
}
},
"scripts": {
"post-install-cmd": [
"chmod -R 777 storage"
]
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
}
}

21
src/webhookReceiver.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
require 'GitHubClient.php';
$token = getenv('GITHUB_TOKEN');
$client = new GitHubClient($token);
// Get the webhook payload
$payload = file_get_contents('php://input');
$data = json_decode($payload, true);
if (isset($data['commits'])) {
foreach ($data['commits'] as $commit) {
// Process each commit
echo "Commit message: " . $commit['message'] . "\n";
}
}
// Respond to GitHub
http_response_code(200);
echo json_encode(['status' => 'Webhook received']);