Skip to content

Commit 88f9e1a

Browse files
authored
Add new Discussions plugin (lowlighter#430) [skip ci]
1 parent a320692 commit 88f9e1a

File tree

11 files changed

+213
-0
lines changed

11 files changed

+213
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**Mocked data */
2+
export default function({faker, query, login = faker.internet.userName()}) {
3+
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
4+
return /after: "MOCKED_CURSOR"/m.test(query)
5+
? ({
6+
user:{
7+
repositoryDiscussions:{
8+
edges:[],
9+
nodes:[],
10+
}
11+
}
12+
})
13+
: ({
14+
user:{
15+
repositoryDiscussions:{
16+
edges:new Array(100).fill(null).map(_ => ({cursor:"MOCKED_CURSOR"})),
17+
nodes:new Array(100).fill(null).map(_ => ({
18+
category:{
19+
emoji:faker.random.arrayElement([":chart_with_upwards_trend:", ":chart_with_downwards_trend:", ":bar_char:"]),
20+
name:faker.lorem.slug()
21+
}
22+
}))
23+
}
24+
}
25+
})
26+
}
27+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**Mocked data */
2+
export default function({faker, query, login = faker.internet.userName()}) {
3+
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
4+
return ({
5+
user:{
6+
started:{totalCount:faker.datatype.number(1000)},
7+
comments:{totalCount:faker.datatype.number(1000)},
8+
answers:{totalCount:faker.datatype.number(1000)}
9+
}
10+
})
11+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,20 @@
586586
},
587587
})
588588
: null),
589+
//Discussions
590+
...(set.plugins.enabled.discussions
591+
? ({
592+
discussions: {
593+
categories: {
594+
stats: { '🙏 Q&A': faker.datatype.number(100), '📣 Announcements': faker.datatype.number(100), '💡 Ideas': faker.datatype.number(100), '💬 General': faker.datatype.number(100) },
595+
favorite: '📣 Announcements'
596+
},
597+
started: faker.datatype.number(1000),
598+
comments: faker.datatype.number(1000),
599+
answers: faker.datatype.number(1000),
600+
},
601+
})
602+
: null),
589603
//Posts
590604
...(set.plugins.enabled.posts
591605
? ({
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### 💬 Discussions
2+
3+
The *discussions* plugin displays your GitHub discussions metrics.
4+
5+
<table>
6+
<td align="center">
7+
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.discussions.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_discussions: yes
21+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//Setup
2+
export default async function({login, q, imports, graphql, queries, data, account}, {enabled = false} = {}) {
3+
//Plugin execution
4+
try {
5+
//Check if plugin is enabled and requirements are met
6+
if ((!enabled)||(!q.discussions))
7+
return null
8+
9+
//Load inputs
10+
imports.metadata.plugins.discussions.inputs({data, account, q})
11+
const discussions = {categories:{}}
12+
13+
//Fetch general statistics
14+
const stats = Object.fromEntries(Object.entries((await graphql(queries.discussions.statistics({login}))).user).map(([key, value]) => [key, value.totalCount]))
15+
Object.assign(discussions, stats)
16+
17+
//Load started discussions
18+
{
19+
const fetched = []
20+
const categories = {}
21+
let cursor = null
22+
let pushed = 0
23+
do {
24+
console.debug(`metrics/compute/${login}/discussions > retrieving discussions after ${cursor}`)
25+
const {user:{repositoryDiscussions:{edges = [], nodes = []} = {}}} = await graphql(queries.discussions.categories({login, after:cursor ? `after: "${cursor}"` : ""}))
26+
cursor = edges?.[edges?.length - 1]?.cursor
27+
fetched.push(...nodes)
28+
pushed = nodes.length
29+
console.debug(`metrics/compute/${login}/discussions > retrieved ${pushed} discussions after ${cursor}`)
30+
} while ((pushed) && (cursor))
31+
32+
//Compute favorite category
33+
for (const category of [...fetched.map(({category:{emoji, name}}) => `${imports.emoji.get(emoji)} ${name}`)])
34+
categories[category] = (categories[category] ?? 0) + 1
35+
discussions.categories.stats = categories
36+
discussions.categories.favorite = Object.entries(categories).sort((a, b) => b[1] - a[1]).map(([name]) => name).shift() ?? null
37+
}
38+
39+
//Results
40+
return discussions
41+
}
42+
//Handle errors
43+
catch (error) {
44+
throw {error:{message:"An error occured", instance:error}}
45+
}
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: "💬 Discussions"
2+
cost: 1 GraphQL request + 1 GraphQL request per 100 discussions started
3+
category: github
4+
supports:
5+
- user
6+
inputs:
7+
8+
# Enable or disable plugin
9+
plugin_discussions:
10+
description: GitHub discussions metrics
11+
type: boolean
12+
default: no
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
query DiscussionsCategories {
2+
user(login: "$login") {
3+
repositoryDiscussions($after first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
4+
edges {
5+
cursor
6+
}
7+
nodes {
8+
category {
9+
emoji
10+
name
11+
}
12+
}
13+
}
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
query DiscussionsStatistics {
2+
user(login: "$login") {
3+
started: repositoryDiscussions {
4+
totalCount
5+
}
6+
comments: repositoryDiscussionComments {
7+
totalCount
8+
}
9+
answers: repositoryDiscussionComments(onlyAnswers: true) {
10+
totalCount
11+
}
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- name: Discussions plugin (default)
2+
uses: lowlighter/metrics@latest
3+
with:
4+
token: MOCKED_TOKEN
5+
plugin_discussions: yes

source/templates/classic/partials/_.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"anilist",
2626
"wakatime",
2727
"skyline",
28+
"discussions",
2829
"support",
2930
"stackoverflow",
3031
"stock",

0 commit comments

Comments
 (0)