Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add /api/sponsors_goal endpoint #3690

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions api/sponsors_goal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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
]);
Loading