Skip to content

Commit 75be173

Browse files
authoredOct 8, 2021
feat(plugins/notable): add support for plugin_notable_from to display contributions from repositories hosted by user accounts (#560)
1 parent 67da0b8 commit 75be173

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed
 

‎source/app/metrics/utils.mjs

+13-4
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,25 @@ export async function markdown(text, {mode = "inline", codelines = Infinity} = {
248248
/**Check GitHub filter against object */
249249
export function ghfilter(text, object) {
250250
console.debug(`metrics/svg/ghquery > checking ${text} against ${JSON.stringify(object)}`)
251-
const result = text.split(" ").map(x => x.trim()).filter(x => x).map(criteria => {
251+
const result = text.split(/(?<!NOT) /).map(x => x.trim()).filter(x => x).map(criteria => {
252252
const [key, filters] = criteria.split(":")
253-
const value = object[key]
253+
const value = object[/^NOT /.test(key) ? key.substring(3).trim() : key.trim()]
254254
console.debug(`metrics/svg/ghquery > checking ${criteria} against ${value}`)
255-
return filters.split(",").map(x => x.trim()).filter(x => x).map(filter => {
255+
return filters?.split(",").map(x => x.trim()).filter(x => x).map(filter => {
256+
if (!Number.isFinite(Number(value))) {
257+
if (/^NOT /.test(filter))
258+
return value !== filter.substring(3).trim()
259+
return value === filter.trim()
260+
}
256261
switch (true) {
257262
case /^>\d+$/.test(filter):
258263
return value > Number(filter.substring(1))
264+
case /^>=\d+$/.test(filter):
265+
return value >= Number(filter.substring(2))
259266
case /^<\d+$/.test(filter):
260267
return value < Number(filter.substring(1))
268+
case /^<=\d+$/.test(filter):
269+
return value <= Number(filter.substring(2))
261270
case /^\d+$/.test(filter):
262271
return value === Number(filter)
263272
case /^\d+..\d+$/.test(filter): {
@@ -267,7 +276,7 @@ export function ghfilter(text, object) {
267276
default:
268277
return false
269278
}
270-
}).reduce((a, b) => a || b, false)
279+
}).reduce((a, b) => a || b, false) ?? false
271280
}).reduce((a, b) => a && b, true)
272281
console.debug(`metrics/svg/ghquery > ${result ? "matching" : "not matching"}`)
273282
return result

‎source/plugins/notable/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The *notable* plugin displays badges of organization where you commited at least
2121
with:
2222
# ... other options
2323
plugin_notable: yes
24-
plugin_notable_filter: stars:>500 # Only display repositories with 500 stars or more (syntax based on GitHub search query)
24+
plugin_notable_filter: stars:>500 # Only display repositories with 500 stars or more (syntax based on GitHub search query)
25+
plugin_notable_from: organization # Only display contributions within organization repositories
2526
plugin_notable_repositories: yes # Display repositories name instead of only organization name
2627
```

‎source/plugins/notable/index.mjs

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ export default async function({login, q, imports, graphql, data, account, querie
77
return null
88

99
//Load inputs
10-
let {filter, repositories} = imports.metadata.plugins.notable.inputs({data, account, q})
10+
let {filter, repositories, from} = imports.metadata.plugins.notable.inputs({data, account, q})
1111

12-
//Initialization
13-
const organizations = new Map()
14-
15-
//Iterate through contributed repositories from organizations
12+
//Iterate through contributed repositories
13+
const notable = new Map()
1614
{
1715
let cursor = null
1816
let pushed = 0
@@ -21,16 +19,16 @@ export default async function({login, q, imports, graphql, data, account, querie
2119
const {user:{repositoriesContributedTo:{edges}}} = await graphql(queries.notable.contributions({login, after:cursor ? `after: "${cursor}"` : "", repositories:data.shared["repositories.batch"] || 100}))
2220
cursor = edges?.[edges?.length - 1]?.cursor
2321
edges
24-
.filter(({node}) => node.isInOrganization)
25-
.filter(({node}) => imports.ghfilter(filter, {name:node.nameWithOwner, stars:node.stargazers.totalCount, watchers:node.watchers.totalCount, forks:node.forks.totalCount}))
26-
.map(({node}) => organizations.set(repositories ? node.nameWithOwner : node.owner.login, node.owner.avatarUrl))
22+
.filter(({node}) => ({all:true, organization:node.isInOrganization, user:!node.isInOrganization}[from]))
23+
.filter(({node}) => imports.ghfilter(filter, {name:node.nameWithOwner, user:node.owner.login, stars:node.stargazers.totalCount, watchers:node.watchers.totalCount, forks:node.forks.totalCount}))
24+
.map(({node}) => notable.set((repositories || !node.isInOrganization) ? node.nameWithOwner : node.owner.login, {organization:node.isInOrganization, avatarUrl:node.owner.avatarUrl}))
2725
pushed = edges.length
2826
} while ((pushed) && (cursor))
2927
}
3028

3129
//Set contributions
32-
const contributions = (await Promise.all([...organizations.entries()].map(async ([name, avatarUrl]) => ({name, avatar:await imports.imgb64(avatarUrl)})))).sort((a, b) => a.name.localeCompare(b.name))
33-
console.debug(`metrics/compute/${login}/plugins > notable > found contributions to ${organizations.length} organizations`)
30+
const contributions = (await Promise.all([...notable.entries()].map(async ([name, {avatarUrl, organization}]) => ({name, avatar:await imports.imgb64(avatarUrl), organization})))).sort((a, b) => a.name.localeCompare(b.name))
31+
console.debug(`metrics/compute/${login}/plugins > notable > found ${contributions.length} notable contributions`)
3432

3533
//Results
3634
return {contributions}

‎source/plugins/notable/metadata.yml

+11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ inputs:
2121
default: ""
2222
example: stars:>500 forks:>100
2323

24+
# Filter repositories depending on which type of account it is hosted
25+
plugin_notable_from:
26+
description: Filter by repository host account type
27+
type: string
28+
default: organization
29+
values:
30+
- all #
31+
- organization # Only hosted by organization accounts
32+
- user # Only hosted by user accounts
33+
2434
# Also display repository name along with organization name
35+
# Note that repositories hosted by user account will always be displayed fully
2536
plugin_notable_repositories:
2637
description: Also display repository name
2738
type: boolean

‎source/templates/classic/partials/notable.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
<% } else { %>
1717
<% if (plugins.notable.contributions.length) { %>
1818
<div class="row organization contributions">
19-
<% for (const {name, avatar} of plugins.notable.contributions) { %>
19+
<% for (const {name, avatar, organization} of plugins.notable.contributions) { %>
2020
<div class="organization contribution">
21-
<img class="organization avatar" src="<%= avatar %>" width="16" height="16" />
22-
<span class="organization name">@<%= name %></span>
21+
<img class="<%= organization ? "organization" : "" %> avatar" src="<%= avatar %>" width="16" height="16" />
22+
<span class="name">@<%= name %></span>
2323
</div>
2424
<% } %>
2525
</div>

0 commit comments

Comments
 (0)
Failed to load comments.