Skip to content

Commit

Permalink
feat: add /api/sponsors_goal endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Dec 7, 2024
1 parent a7dd6bc commit db46cea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _backend/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
'stripe_sk' => 'sk_test_hoigesrjgoisrhgilgjrsfjs',
'stripe_pk' => 'pk_test_hoigesrjgoisrhgilgjrsfjs',

// Only needs read-only access to public repos
'gh_sponsors_token' => 'ghp_1234567890abcdefghij',

'previous_stripe_sk' => 'sk_test_hoigesrjgoisrhgilgjrsfjs',

'slack_token' => 'asdf-1234567890-7418529630-a7854123692-8412487519',
Expand Down
50 changes: 50 additions & 0 deletions api/sponsors_goal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

require_once __DIR__ . '/../_backend/bootstrap.php';

// POST to the GitHub GraphQL API to get the sponsorship goal
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.github.com/graphql",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: bearer $config[gh_sponsors_token]",
"Content-Type: application/json",
"User-Agent: elementary Website"
],
CURLOPT_POSTFIELDS => json_encode([
"query" => "query { organization(login: \"elementary\") { sponsorsListing { activeGoal { percentComplete, targetValue } } } }"
])
]);

if(!$response = curl_exec($curl)) {
// Return error code if the request fails
http_response_code(500);
curl_close($curl);
die();
}

curl_close($curl);

$response = json_decode($response, true);

// Check the response has the data we asked for
if(!isset($response['data']['organization']['sponsorsListing']['activeGoal'])) {
// Return error code if the response is invalid
http_response_code(500);
die();
}

$goal = $response['data']['organization']['sponsorsListing']['activeGoal'];
$percent = $goal['percentComplete'];
$target = $goal['targetValue'];

// Return the goal as a JSON object
header('Content-Type: application/json');
echo json_encode([
"percent" => $percent,
"target" => $target
]);

?>

0 comments on commit db46cea

Please sign in to comment.