-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sponsor-labeler.ps1
162 lines (143 loc) · 5.95 KB
/
sponsor-labeler.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
$verbose = [bool]::Parse($env:SPONSOR_VERBOSE ?? "false")
# ensure labels exist
gh label create "$env:SPONSOR_LABEL" -c '#D4C5F9' -d 'Sponsor' || & { $global:LASTEXITCODE=0 }
gh label create "$env:SPONSOR_CONTRIB_LABEL" -c '#BFFFD3' -d 'Sponsor via contributions' || & { $global:LASTEXITCODE=0 }
gh label create "$env:SPONSOR_SILVER_LABEL" -c '#C0C0C0' -d 'Silver Sponsor' || & { $global:LASTEXITCODE=0 }
gh label create "$env:SPONSOR_GOLD_LABEL" -c '#FEF2C0' -d 'Gold Sponsor' || & { $global:LASTEXITCODE=0 }
$team = gh api graphql -f query='
query {
viewer {
organizations(first: 100) {
nodes {
login
isVerified
email
websiteUrl
}
}
}
}' --jq "[.data.viewer.organizations.nodes.[] | select(.login == `"$env:SPONSORABLE`")]" || $(throw "Failed to query GH GraphQL API")
if ($team -ne $null) {
Write-Output "User $env:SPONSOR_SENDER_LOGIN belongs to the $env:SPONSORABLE organization."
return
}
$owners = gh api graphql --paginate -f login=$env:SPONSOR_SENDER_LOGIN -f query='
query($login: String!, $endCursor: String) {
user(login: $login) {
repositoriesContributedTo(first: 5, includeUserRepositories: true, contributionTypes: [COMMIT], after: $endCursor) {
nodes {
nameWithOwner,
owner {
login
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}' --jq '[.data.user.repositoriesContributedTo.nodes.[].owner.login]' | jq -s 'flatten | unique' || $(throw "Failed to query GH GraphQL API")
if (($owners | jq ".[] | select(. == `"$env:SPONSORABLE`")") -ne $null) {
Write-Output "User $env:SPONSOR_SENDER_LOGIN is considered an implicit sponsor as a former contributor to the organization."
Write-Output "Adding $env:SPONSOR_CONTRIB_LABEL label to #$env:SPONSOR_ISSUE."
gh issue edit $env:SPONSOR_ISSUE --add-label "$env:SPONSOR_CONTRIB_LABEL"
return
}
$sponsor = 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, name }
... on User { id, name }
}
tier { monthlyPriceInDollars }
}
pageInfo { hasNextPage, endCursor }
}
}
}
' --jq ".data.organization.sponsorshipsAsMaintainer.nodes | map(select(.sponsorEntity.id == `"$env:SPONSOR_SENDER_ID`")) | .[]" || $(throw "Failed to query GH GraphQL API")
if ($sponsor -ne $null) {
$amount = $sponsor | jq '.tier.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.
$userorgs = 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 }
}
}
}
' --jq '[.data.user.organizations.nodes.[].id]' || $(throw "Failed to query GH GraphQL API")
$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 }
}
}
}
' --jq '[.data.organization.sponsorshipsAsMaintainer.nodes[] | select(.sponsorEntity | has("id"))]' || $(throw "Failed to query GH GraphQL API")
$sponsors = $orgs | jq "[.[] | select(.sponsorEntity.id as `$id | $userorgs | index(`$id))]"
if (($sponsors | convertfrom-json | measure).count -ne 0) {
$amount = $sponsors | jq '[.[] | .tier.monthlyPriceInDollars | select(. != null)] | sort' | select -last 1
if ($amount -eq $null) {
# We have at least one 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."
if ($verbose) {
$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."
}
$gold = [int]$env:SPONSOR_GOLD_AMOUNT
$silver = [int]$env:SPONSOR_SILVER_AMOUNT
$label = if ([int]$amount -ge $gold) {
$env:SPONSOR_GOLD_LABEL
} else {
if ([int]$amount -ge $silver) {
$env:SPONSOR_SILVER_LABEL
} else {
$env:SPONSOR_LABEL
}
}
Write-Output "Adding $label label to #$env:SPONSOR_ISSUE."
gh issue edit $env:SPONSOR_ISSUE --add-label "$label"