Skip to content

Commit

Permalink
Fallback to base sponsorship detection for non-owners
Browse files Browse the repository at this point in the history
When querying sponsorship tiers, only sponsor (account/org) owners
can view the actual membership values. In order to avoid requiring
owner tokens, fallback to regular sponsorship detection instead,
but issue a warning.
  • Loading branch information
kzu committed Aug 12, 2022
1 parent 3862aa8 commit 30b960f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ or belongs to an organization that is.
sponsorable: ''
# The token to use for querying the GitHub API for sponsorship information.
# Typically set to ${{ secrets.GITHUB_TOKEN }}.
# Typically set to ${{ secrets.GH_TOKEN }}.
token: ''
```

> NOTE: in order to detect the sponsorship tier to trigger gold sponsor labeling,
> the token must be an owner of the sponsorable organization. Otherwise, only
> base sponsoring is detected.
## Example

Minimal example, using default labels, repo owner and gold label threshold:
Expand Down
43 changes: 30 additions & 13 deletions sponsor-labeler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ query($owner: String!, $endCursor: String) {
}
' || $(throw "Failed to query GH GraphQL API")

$amount =
$query |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.organization.sponsorshipsAsMaintainer.nodes}} |
select -ExpandProperty nodes |
where { $_.sponsorEntity.id -eq $env:SPONSOR_SENDER_ID } |
select -ExpandProperty tier |
select -ExpandProperty monthlyPriceInDollars
$sponsor = $query |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.organization.sponsorshipsAsMaintainer.nodes}} |
select -ExpandProperty nodes |
where { $_.sponsorEntity.id -eq $env:SPONSOR_SENDER_ID }

if ($sponsor -ne $null) {
$amount = select -ExpandProperty tier | select -ExpandProperty monthlyPriceInDollars
if ($amount -eq $null) {
# We have a sponsor, but we might not be able to get the tier amount if token
# isn't owner of the sponsorable. Asume regular sponsors in that case.
$amount = 1
Write-Warning "Sponsor tier couldn't be read. Make sure token belongs to an owner of $env:SPONSORABLE."
}
}

if ($null -eq $amount) {
# Try again with the organizations the user belongs to.
Expand Down Expand Up @@ -58,15 +65,25 @@ query ($user: String!, $endCursor: String) {
}
' || $(throw "Failed to query GH GraphQL API")

$amount = $orgs |
$sponsor = $orgs |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.organization.sponsorshipsAsMaintainer.nodes}} |
select -ExpandProperty nodes |
where { $_.sponsorEntity.id -in $userorgs } |
select -ExpandProperty tier |
sort-object -Property monthlyPriceInDollars -Descending |
select -ExpandProperty monthlyPriceInDollars -First 1
where { $_.sponsorEntity.id -in $userorgs }

if ($sponsor -ne $null) {
$amount = select -ExpandProperty tier |
sort-object -Property monthlyPriceInDollars -Descending |
select -ExpandProperty monthlyPriceInDollars -First 1

if ($amount -eq $null) {
# We have a sponsor, but we might not be able to get the tier amount if token
# isn't owner of the sponsorable. Asume regular sponsors in that case.
$amount = 1
Write-Warning "Sponsor tier couldn't be read. Make sure token belongs to an owner of $env:SPONSORABLE."
}
}

if ($null -eq $amount) {
Write-Output "User $env:SPONSOR_SENDER_LOGIN is not a sponsor of $env:SPONSORABLE and none of their organizations are:"
$user |
Expand Down

0 comments on commit 30b960f

Please sign in to comment.