Skip to content

update-sponsors

update-sponsors #345

name: update-sponsors
on:
schedule:
- cron: "0 0 * * *" # Run every day at midnight
branches:
- master
workflow_dispatch:
jobs:
update-sponsors:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Fetch sponsors
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs').promises;
const query = `
query {
user(login: "huxingyi") {
sponsorshipsAsMaintainer(first: 100) {
totalCount
nodes {
id
createdAt
tier {
name
}
sponsor {
login
avatarUrl,
url,
name
}
}
}
}
}
`;
const response = await github.graphql(query);
console.log(response);
let sponsorsHtml = '';
for (const node of response.user.sponsorshipsAsMaintainer.nodes) {
sponsorsHtml += '<a href="' + node.sponsor.url + '" title="' + node.sponsor.login + (node.sponsor.name ? (' (' + node.sponsor.name + ')') : '') + '" target=_blank><image src="' + node.sponsor.avatarUrl + '" alt="@' + node.sponsor.login + '" width="35" height="35" style="border-radius: 17px;" /></a>\n';
}
if ('' !== sponsorsHtml) {
sponsorsHtml = '\n## Sponsors \n\n' + sponsorsHtml + '\n _The list shown represent active sponsors on GitHub and a full list can be viewed at [SUPPORTERS](https://github.com/huxingyi/dust3d/blob/master/SUPPORTERS)._\n';
}
const readme = await fs.readFile('README.md', 'utf8');
const beginString = '<!-- Sponsors begin -->';
const endString = '<!-- Sponsors end -->';
let startIndex = readme.indexOf(beginString);
if (-1 !== startIndex) {
startIndex = startIndex + beginString.length;
const endIndex = readme.indexOf(endString);
if (-1 !== endIndex) {
const newContent = readme.substring(0, startIndex) + sponsorsHtml + readme.substring(endIndex);
await fs.writeFile('README.md', newContent);
} else {
throw new Error('Could not find end marker');
}
} else {
throw new Error('Could not find begin marker');
}
const supportersContent = await fs.readFile('SUPPORTERS', 'utf8');
const regGithubId = /https:\/\/github\.com\/([^>]+)>/g;
const matched = [...supportersContent.matchAll(regGithubId)];
const supporters = {};
for (const user of matched) {
supporters[user[1].toLowerCase()] = user[1];
}
let supportersText = '';
for (const node of response.user.sponsorshipsAsMaintainer.nodes) {
if (undefined !== supporters[node.sponsor.login.toLowerCase()]) {
continue;
}
supportersText += '\n' + (node.sponsor.name ? node.sponsor.name : node.sponsor.login) + ' <' + node.sponsor.url + '>';
}
if ('' !== supportersText) {
const newContent = supportersContent.trim() + supportersText;
await fs.writeFile('SUPPORTERS', newContent);
}
- name: Upload README.md
run: |
if git diff --name-only HEAD | grep -q "README.md\|SUPPORTERS"; then
git config --global user.email ""
git config --global user.name "bot"
git add README.md
git add SUPPORTERS
timestamp=$(TZ=":Australia/Sydney" date)
git commit -m "Update sponsors at ${timestamp}"
git push origin HEAD
fi