Skip to content

Commit a5d8933

Browse files
authored
GitHub Sponsors donations (#242)
Adds action for fetching GitHub Sponsors data to make available to static site build.
1 parent 547f332 commit a5d8933

File tree

6 files changed

+79
-26
lines changed

6 files changed

+79
-26
lines changed

.github/workflows/main.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ jobs:
2929
- name: Clone repository
3030
uses: actions/checkout@v4
3131

32+
- name: Fetch GH Sponsors
33+
uses: ./FetchGHSponsors
34+
env:
35+
GH_TOKEN: ${{ secrets.SPONSORS_TOKEN }}
36+
GH_LOGIN: your-org
37+
3238
- name: Setup .NET Core SDK
3339
uses: actions/setup-dotnet@v4
3440
with:

.github/workflows/pullrequest.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ jobs:
2626
- name: Clone Repository
2727
uses: actions/checkout@v4
2828

29+
- name: Fetch GH Sponsors
30+
uses: ./FetchGHSponsors
31+
env:
32+
GH_TOKEN: ${{ secrets.SPONSORS_TOKEN }}
33+
GH_LOGIN: your-org
34+
2935
- name: Setup .NET Core SDK
3036
uses: actions/setup-dotnet@v4
3137
with:
@@ -40,6 +46,11 @@ jobs:
4046
- name: Build Site
4147
run: npm run build
4248

49+
- name: Upload Site Artifact
50+
uses: actions/upload-pages-artifact@v3
51+
with:
52+
path: _site
53+
4354
complete:
4455
runs-on: ubuntu-latest
4556
needs: test

FetchGHSponsors/action.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Fetch GH Sponsors
2+
description: 'Updates the GitHub spondors list for the Monogame website'
3+
author: 'MonoGame Foundation'
4+
runs:
5+
using: "composite"
6+
steps:
7+
- name: Fetch
8+
if: runner.os == 'Linux'
9+
run: |
10+
QUERY='query($login:String!){
11+
organization(login:$login){
12+
monthlyEstimatedSponsorsIncomeInCents
13+
sponsorshipsAsMaintainer(first:1){ totalCount }
14+
}
15+
}'
16+
17+
BODY=$(jq -n --arg q "$QUERY" --arg login "$GH_LOGIN" '{query:$q, variables:{login:$login}}')
18+
19+
RESP=$(curl -s https://api.github.com/graphql -H "Authorization: Bearer $GH_TOKEN" -H "Content-Type: application/json" -d "$BODY")
20+
21+
CENTS=$(echo "$RESP" | jq '.data.organization.monthlyEstimatedSponsorsIncomeInCents // 0')
22+
COUNT=$(echo "$RESP" | jq '.data.organization.sponsorshipsAsMaintainer.totalCount // 0')
23+
jq -n --argjson sum "$CENTS" --argjson count "$COUNT" \
24+
'{ sponsor_sum: ($sum/100|floor), sponsor_count: $count }' \
25+
> website/_data/gh_sponsors.json
26+
shell: bash

website/_data/gh_sponsors.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sponsor_sum": 0,
3+
"sponsor_count": 0
4+
}

website/content/donate.njk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,9 @@ title: Donate
119119
{% endblock %}
120120

121121
{% block scripts %}
122+
<script type="text/javascript">
123+
let gh_sponsor_sum = {{ gh_sponsors.sponsor_sum }};
124+
let gh_sponsor_count = {{ gh_sponsors.sponsor_count }};
125+
</script>
122126
<script async type="text/javascript" src="/js/patreon.js"></script>
123127
{% endblock %}

website/content/public/js/patreon.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,33 @@
1919
}
2020
}
2121

22-
const response = await fetch('https://api.patreon.com/platform/users?filter[patreon_url]=https://www.patreon.com/MonoGame');
23-
const data = await response.json();
24-
25-
if(data.included && data.included.length > 0) {
26-
const attributes = data.included[0].attributes;
27-
const patron_count = attributes.patron_count;
28-
let pledge_sum = attributes.pledge_sum;
29-
30-
// HACK: Add in a few things from other services.
31-
{
32-
// Re-logic's monthly donation.
33-
pledge_sum += 100000;
34-
35-
// TODO: Get GitHub sponsors and merge them with this.
36-
// Requires a personal access token to do so.
37-
}
38-
39-
const total_patrons = document.getElementById('total-patrons');
40-
countUp(total_patrons, patron_count, (x) => x);
41-
42-
const per_month = document.getElementById('per-month');
43-
countUp(per_month, pledge_sum, (x) => (x / 100).toLocaleString('en-US', {
44-
style: 'currency',
45-
currency: attributes.pledge_sum_currency
46-
}));
47-
}
22+
// We have the GH Sponsors data updated from a workflow.
23+
let patron_count = gh_sponsor_count;
24+
let pledge_sum = gh_sponsor_sum;
25+
26+
// Fetch Patreon data.
27+
{
28+
const response = await fetch('https://api.patreon.com/platform/users?filter[patreon_url]=https://www.patreon.com/MonoGame');
29+
const data = await response.json();
30+
if(data.included && data.included.length > 0)
31+
{
32+
const attributes = data.included[0].attributes;
33+
patron_count += attributes.patron_count;
34+
pledge_sum += attributes.pledge_sum;
35+
}
36+
}
37+
38+
// Re-logic's fixed monthly donation.
39+
pledge_sum += 100000;
40+
41+
// Animate the results.
42+
const total_patrons = document.getElementById('total-patrons');
43+
countUp(total_patrons, patron_count, (x) => x);
44+
45+
const per_month = document.getElementById('per-month');
46+
countUp(per_month, pledge_sum, (x) => (x / 100).toLocaleString('en-US', {
47+
style: 'currency',
48+
currency: 'USD'
49+
}));
4850

4951
})();

0 commit comments

Comments
 (0)