feat(plugins/sponsorships): add plugin (#1358)

This commit is contained in:
Simon Lecoq
2023-01-16 22:28:33 -05:00
committed by GitHub
parent cdf2c03b2a
commit 220deb05e3
13 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
<!--header-->
<!--/header-->
## ➡️ Available options
<!--options-->
<!--/options-->
## Examples workflows
<!--examples-->
<!--/examples-->

View File

@@ -0,0 +1,7 @@
- name: 💝 GitHub sponsorships
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.sponsorships.svg
token: ${{ secrets.METRICS_TOKEN }}
base: ""
plugin_sponsorships: yes

View File

@@ -0,0 +1,51 @@
//Setup
export default async function({login, q, imports, data, graphql, queries, account}, {enabled = false, extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.sponsorships) || (!imports.metadata.plugins.sponsorships.enabled(enabled, {extras})))
return null
//Load inputs
let {sections, size} = await imports.metadata.plugins.sponsorships.inputs({data, account, q})
//Query description and goal
let amount = NaN, image = null, started = null
if (sections.includes("amount")) {
console.debug(`metrics/compute/${login}/plugins > sponsorships > querying total amount spend`)
const {totalSponsorshipAmountAsSponsorInCents, sponsorshipsAsSponsor} = (await graphql(queries.sponsorships({login, account})))[account]
amount = totalSponsorshipAmountAsSponsorInCents/100
image = "https://github.githubassets.com/images/icons/emoji/hearts_around.png"
started = sponsorshipsAsSponsor.nodes[0]?.createdAt ? new Date(sponsorshipsAsSponsor.nodes[0]?.createdAt) : null
}
image = await imports.imgb64(image)
//Query sponsorships
const list = []
if (sections.includes("sponsorships")) {
console.debug(`metrics/compute/${login}/plugins > sponsorships > querying sponsorships`)
{
const fetched = []
let cursor = null
let pushed = 0
do {
console.debug(`metrics/compute/${login}/sponsorships > retrieving sponsorships after ${cursor}`)
const {[account]: {sponsorshipsAsSponsor: {edges, nodes}}} = await graphql(queries.sponsorships.all({login, account, after: cursor ? `after: "${cursor}"` : "", size: Math.round(size * 1.5)}))
cursor = edges?.[edges?.length - 1]?.cursor
fetched.push(...nodes)
pushed = nodes.length
console.debug(`metrics/compute/${login}/sponsorships > retrieved ${pushed} sponsorships events after ${cursor}`)
} while ((pushed) && (cursor))
list.push(...fetched.map(({sponsorable: {login, avatarUrl, url: organization = null}, tier:{name:tier}, privacyLevel:privacy, isActive:active}) => ({login, avatarUrl, type: organization ? "organization" : "user", tier, private: privacy !== "PUBLIC", past:!active})))
}
await Promise.all(list.map(async user => user.avatar = await imports.imgb64(user.avatarUrl)))
}
//Results
return {amount, list, sections, size, image, started}
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}

View File

@@ -0,0 +1,41 @@
name: 💝 GitHub Sponsorships
category: github
description: |
This plugin displays sponsorships funded through [GitHub sponsors](https://github.com/sponsors/).
examples:
default: https://github.com/lowlighter/metrics/blob/examples/metrics.plugin.sponsorships.svg
supports:
- user
- organization
scopes:
- read:user
- read:org
inputs:
plugin_sponsorships:
description: |
Enable sponsorships plugin
type: boolean
default: no
plugin_sponsorships_sections:
description: |
Displayed sections
- `amount`: display total amount sponsored
- `sponsorships`: display GitHub sponsorships
type: array
format: comma-separated
default: amount, sponsorships
example: amount, sponsorships
values:
- amount
- sponsorships
plugin_sponsorships_size:
description: |
Profile picture display size
type: number
default: 24
min: 8
max: 64

View File

@@ -0,0 +1,29 @@
query SponsorshipsAll {
$account(login: "$login") {
sponsorshipsAsSponsor($after first: 100, activeOnly: false, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
cursor
}
nodes {
createdAt
isActive
isOneTimePayment
tier {
name
}
privacyLevel
sponsorable {
... on User {
avatarUrl(size: $size)
login
}
... on Organization {
login
avatarUrl(size: $size)
url
}
}
}
}
}
}

View File

@@ -0,0 +1,10 @@
query SponsorshipsDefault {
$account(login: "$login") {
totalSponsorshipAmountAsSponsorInCents
sponsorshipsAsSponsor(first: 1, activeOnly: false, orderBy: {field: CREATED_AT, direction: ASC}) {
nodes {
createdAt
}
}
}
}