Skip to content

Commit

Permalink
Make sure we also look at user orgs for sponsorships
Browse files Browse the repository at this point in the history
Organizations themselves cannot create issues and pull requests,
so we also need to check for sponsoring organizations and whether
the sender belongs to one of them. We take the higher-tier org
and use that to apply the label.

Review down the road if this is too broad of a criteria, since
large orgs like dotnet, for example, often have many users that
aren't necessarily direct employees of the parent company (Microsoft
in that case, say).
  • Loading branch information
kzu committed Aug 12, 2022
1 parent fb45fdd commit 3862aa8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ inputs:
required: false
default: ${{ github.repository_owner }}
token:
description: The token to use for querying the GitHub API for sponsorship information. Typically set to secrets.GITHUB_TOKEN.
description: The token to use for querying the GitHub API for sponsorship information. Typically set to secrets.GH_TOKEN.
required: true

branding:
Expand All @@ -41,7 +41,8 @@ runs:
SPONSOR_LABEL: ${{ inputs.label }}
SPONSOR_GOLD_LABEL: ${{ inputs.gold-label }}
SPONSOR_GOLD_AMOUNT: ${{ inputs.gold-amount }}
SPONSOR_ACTOR: ${{ github.event.issue.user.node_id || github.event.pull_request.user.node_id }}
SPONSOR_SENDER_LOGIN: ${{ github.event.sender.login }}
SPONSOR_SENDER_ID: ${{ github.event.sender.node_id }}
SPONSOR_ISSUE: ${{ github.event.issue.number || github.event.pull_request.number }}
GH_TOKEN: ${{ inputs.token }}
run: sponsor-labeler.ps1
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ❤️ sponsor

A GitHub Action that labels issues and pull requests if the creator is a sponsor.
A GitHub Action that labels issues and pull requests if the creator is a sponsor,
or belongs to an organization that is.

## Usage

Expand Down
73 changes: 67 additions & 6 deletions sponsor-labeler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,86 @@ query($owner: String!, $endCursor: String) {
}
tier { monthlyPriceInDollars }
}
pageInfo {
hasNextPage
endCursor
}
pageInfo { hasNextPage, endCursor }
}
}
}
'
' || $(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_ACTOR } |
where { $_.sponsorEntity.id -eq $env:SPONSOR_SENDER_ID } |
select -ExpandProperty tier |
select -ExpandProperty monthlyPriceInDollars

if ($null -eq $amount) {
# Try again with the organizations the user belongs to.
$user = gh api graphql --paginate -f user=$env:SPONSOR_SENDER_LOGIN -f query='
query ($user: String!, $endCursor: String) {
user(login: $user) {
organizations(first: 100, after: $endCursor) {
nodes { id, login, name }
}
}
}
' || $(throw "Failed to query GH GraphQL API")

$userorgs = $user |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.user.organizations.nodes}} |
select -ExpandProperty nodes |
select -ExpandProperty id

$orgs = gh api graphql --paginate -f owner=$env:SPONSORABLE -f query='
query($owner: String!, $endCursor: String) {
organization (login: $owner) {
sponsorshipsAsMaintainer (first: 100, after: $endCursor) {
nodes {
sponsorEntity {
... on Organization { id, login, name }
}
tier { monthlyPriceInDollars }
}
pageInfo { hasNextPage, endCursor }
}
}
}
' || $(throw "Failed to query GH GraphQL API")

$amount = $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

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 |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.user.organizations.nodes}} |
select -ExpandProperty nodes
format-table

Write-Output "`n$env:SPONSORABLE sponsoring organizations:"
$orgs |
ConvertFrom-Json |
select @{ Name='nodes'; Expression={$_.data.organization.sponsorshipsAsMaintainer.nodes}} |
select -ExpandProperty nodes |
select -ExpandProperty sponsorEntity |
format-table

return
} else {
Write-Output "User $env:SPONSOR_SENDER_LOGIN belongs to an organization sponsoring $env:SPONSORABLE."
}
} else {
Write-Output "User $env:SPONSOR_SENDER_LOGIN is a direct sponsor of $env:SPONSORABLE."
}

# ensure labels exist
Expand All @@ -38,4 +98,5 @@ gh label create "$env:SPONSOR_LABEL" -c '#D4C5F9' -d 'Sponsor' || & { $global:LA
$gold = [int]$env:SPONSOR_GOLD_AMOUNT
$label = if ([int]$amount -ge $gold) { $env:SPONSOR_GOLD_LABEL } else { $env:SPONSOR_LABEL }

Write-Output "Adding $label label to $env:SPONSOR_ISSUE."
gh issue edit $env:SPONSOR_ISSUE --add-label "$label"

0 comments on commit 3862aa8

Please sign in to comment.