Skip to content

Commit 19bb1b3

Browse files
authored
Add notable contributions plugin (#222)
1 parent e63dee0 commit 19bb1b3

File tree

15 files changed

+218
-4
lines changed

15 files changed

+218
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**Mocked data */
2+
export default function({faker, query, login = faker.internet.userName()}) {
3+
console.debug("metrics/compute/mocks > mocking graphql api result > notable/contributions")
4+
return /after: "MOCKED_CURSOR"/m.test(query) ? ({
5+
user:{
6+
repositoriesContributedTo:{
7+
edges:[],
8+
},
9+
},
10+
}) : ({
11+
user:{
12+
repositoriesContributedTo:{
13+
edges:[
14+
{
15+
cursor:"MOCKED_CURSOR",
16+
node:{
17+
isInOrganization:true,
18+
owner:{
19+
login:faker.internet.userName(),
20+
avatarUrl:null,
21+
},
22+
},
23+
},
24+
],
25+
},
26+
},
27+
})
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**Mocked data */
2+
export default function({faker, query, login = faker.internet.userName()}) {
3+
console.debug("metrics/compute/mocks > mocking graphql api result > notable/organizations")
4+
return ({
5+
user:{
6+
organizations:{
7+
nodes:[{
8+
login:faker.internet.userName(),
9+
avatarUrl:null,
10+
}],
11+
},
12+
},
13+
})
14+
}

source/app/web/statics/app.placeholder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@
165165
pr:{get count() { return this.open + this.merged }, open:faker.datatype.number(1000), merged:faker.datatype.number(1000)},
166166
}
167167
}) : null),
168+
//Notable
169+
...(set.plugins.enabled.notable ? ({
170+
notable:{
171+
contributions:new Array(2+faker.datatype.number(2)).fill(null).map(_ => ({name:faker.lorem.slug(), avatar:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg=="})),
172+
}
173+
}) : null),
168174
//Gists
169175
...(set.plugins.enabled.gists ? ({
170176
gists:{

source/plugins/base/metadata.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "🗃️ Base content"
2-
cost: 1 GraphQL request
2+
cost: 2 GraphQL requests + 1 GraphQL request per 100 repositories fetched
33
categorie: core
44
supports:
55
- user

source/plugins/base/queries/user.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ query BaseUser {
4848
}
4949
}
5050
}
51-
repositoriesContributedTo {
51+
repositoriesContributedTo(includeUserRepositories: true) {
5252
totalCount
5353
}
5454
followers {

source/plugins/notable/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### 🎩 Notable contributions
2+
3+
The *notable* plugin displays badges of organization where you commited at least once on main branch.
4+
5+
<table>
6+
<td align="center">
7+
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.notable.svg">
8+
<img width="900" height="1" alt="">
9+
</td>
10+
</table>
11+
12+
#### ℹ️ Examples workflows
13+
14+
[➡️ Available options for this plugin](metadata.yml)
15+
16+
```yaml
17+
- uses: lowlighter/metrics@latest
18+
with:
19+
# ... other options
20+
plugin_notable: yes
21+
```

source/plugins/notable/index.mjs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//Setup
2+
export default async function({login, q, imports, graphql, data, account, queries}, {enabled = false} = {}) {
3+
//Plugin execution
4+
try {
5+
//Check if plugin is enabled and requirements are met
6+
if ((!enabled)||(!q.notable))
7+
return null
8+
9+
//Load inputs
10+
imports.metadata.plugins.notable.inputs({data, account, q})
11+
12+
//Initialization
13+
const organizations = new Map()
14+
15+
//Load organization memberships
16+
try {
17+
const {user:{organizations:{nodes}}} = await graphql(queries.notable.organizations({login}))
18+
nodes.map(({login, avatarUrl}) => organizations.set(login, avatarUrl))
19+
}
20+
catch (error) {
21+
console.debug(`metrics/compute/${login}/plugins > notable > failed to load organizations memberships: ${error}`)
22+
}
23+
24+
//Iterate through contributed repositories from organizations
25+
{
26+
let cursor = null
27+
let pushed = 0
28+
do {
29+
console.debug(`metrics/compute/${login}/plugins > notable > retrieving contributed repositories after ${cursor}`)
30+
const {user:{repositoriesContributedTo:{edges}}} = await graphql(queries.notable.contributions({login, after:cursor ? `after: "${cursor}"` : "", repositories:100}))
31+
cursor = edges?.[edges?.length-1]?.cursor
32+
edges.map(({node}) => node.isInOrganization ? organizations.set(node.owner.login, node.owner.avatarUrl) : null)
33+
pushed = edges.length
34+
} while ((pushed)&&(cursor))
35+
}
36+
37+
//Set contributions
38+
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))
39+
console.debug(`metrics/compute/${login}/plugins > notable > found contributions to ${organizations.length} organizations`)
40+
41+
//Results
42+
return {contributions}
43+
}
44+
//Handle errors
45+
catch (error) {
46+
throw {error:{message:"An error occured", instance:error}}
47+
}
48+
}

source/plugins/notable/metadata.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: "🎩 Notable contributions"
2+
cost: 1 GraphQL request per 100 repositories fetched
3+
categorie: github
4+
index: 18
5+
supports:
6+
- user
7+
inputs:
8+
9+
# Enable or disable plugin
10+
plugin_notable:
11+
description: Display notable contributions in organizations
12+
type: boolean
13+
default: no
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
query NotableContributions {
2+
user(login: "$login") {
3+
repositoriesContributedTo($after first: $repositories, contributionTypes: COMMIT) {
4+
edges {
5+
cursor
6+
node {
7+
isInOrganization
8+
owner {
9+
login
10+
avatarUrl
11+
}
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query NotableOrganizations {
2+
user(login: "$login") {
3+
organizations(first: 100) {
4+
nodes {
5+
login
6+
avatarUrl
7+
}
8+
}
9+
}
10+
}
11+

0 commit comments

Comments
 (0)