Add new Discussions plugin (#430) [skip ci]

This commit is contained in:
Simon Lecoq
2021-07-30 23:05:12 +02:00
committed by GitHub
parent a32069297a
commit 88f9e1a41f
11 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
/**Mocked data */
export default function({faker, query, login = faker.internet.userName()}) {
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
return /after: "MOCKED_CURSOR"/m.test(query)
? ({
user:{
repositoryDiscussions:{
edges:[],
nodes:[],
}
}
})
: ({
user:{
repositoryDiscussions:{
edges:new Array(100).fill(null).map(_ => ({cursor:"MOCKED_CURSOR"})),
nodes:new Array(100).fill(null).map(_ => ({
category:{
emoji:faker.random.arrayElement([":chart_with_upwards_trend:", ":chart_with_downwards_trend:", ":bar_char:"]),
name:faker.lorem.slug()
}
}))
}
}
})
}

View File

@@ -0,0 +1,11 @@
/**Mocked data */
export default function({faker, query, login = faker.internet.userName()}) {
console.debug("metrics/compute/mocks > mocking graphql api result > contributors/commit")
return ({
user:{
started:{totalCount:faker.datatype.number(1000)},
comments:{totalCount:faker.datatype.number(1000)},
answers:{totalCount:faker.datatype.number(1000)}
}
})
}

View File

@@ -586,6 +586,20 @@
},
})
: null),
//Discussions
...(set.plugins.enabled.discussions
? ({
discussions: {
categories: {
stats: { '🙏 Q&A': faker.datatype.number(100), '📣 Announcements': faker.datatype.number(100), '💡 Ideas': faker.datatype.number(100), '💬 General': faker.datatype.number(100) },
favorite: '📣 Announcements'
},
started: faker.datatype.number(1000),
comments: faker.datatype.number(1000),
answers: faker.datatype.number(1000),
},
})
: null),
//Posts
...(set.plugins.enabled.posts
? ({

View File

@@ -0,0 +1,21 @@
### 💬 Discussions
The *discussions* plugin displays your GitHub discussions metrics.
<table>
<td align="center">
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.discussions.svg">
<img width="900" height="1" alt="">
</td>
</table>
#### Examples workflows
[➡️ Available options for this plugin](metadata.yml)
```yaml
- uses: lowlighter/metrics@latest
with:
# ... other options
plugin_discussions: yes
```

View File

@@ -0,0 +1,46 @@
//Setup
export default async function({login, q, imports, graphql, queries, data, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.discussions))
return null
//Load inputs
imports.metadata.plugins.discussions.inputs({data, account, q})
const discussions = {categories:{}}
//Fetch general statistics
const stats = Object.fromEntries(Object.entries((await graphql(queries.discussions.statistics({login}))).user).map(([key, value]) => [key, value.totalCount]))
Object.assign(discussions, stats)
//Load started discussions
{
const fetched = []
const categories = {}
let cursor = null
let pushed = 0
do {
console.debug(`metrics/compute/${login}/discussions > retrieving discussions after ${cursor}`)
const {user:{repositoryDiscussions:{edges = [], nodes = []} = {}}} = await graphql(queries.discussions.categories({login, after:cursor ? `after: "${cursor}"` : ""}))
cursor = edges?.[edges?.length - 1]?.cursor
fetched.push(...nodes)
pushed = nodes.length
console.debug(`metrics/compute/${login}/discussions > retrieved ${pushed} discussions after ${cursor}`)
} while ((pushed) && (cursor))
//Compute favorite category
for (const category of [...fetched.map(({category:{emoji, name}}) => `${imports.emoji.get(emoji)} ${name}`)])
categories[category] = (categories[category] ?? 0) + 1
discussions.categories.stats = categories
discussions.categories.favorite = Object.entries(categories).sort((a, b) => b[1] - a[1]).map(([name]) => name).shift() ?? null
}
//Results
return discussions
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}

View File

@@ -0,0 +1,12 @@
name: "💬 Discussions"
cost: 1 GraphQL request + 1 GraphQL request per 100 discussions started
category: github
supports:
- user
inputs:
# Enable or disable plugin
plugin_discussions:
description: GitHub discussions metrics
type: boolean
default: no

View File

@@ -0,0 +1,15 @@
query DiscussionsCategories {
user(login: "$login") {
repositoryDiscussions($after first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
edges {
cursor
}
nodes {
category {
emoji
name
}
}
}
}
}

View File

@@ -0,0 +1,13 @@
query DiscussionsStatistics {
user(login: "$login") {
started: repositoryDiscussions {
totalCount
}
comments: repositoryDiscussionComments {
totalCount
}
answers: repositoryDiscussionComments(onlyAnswers: true) {
totalCount
}
}
}

View File

@@ -0,0 +1,5 @@
- name: Discussions plugin (default)
uses: lowlighter/metrics@latest
with:
token: MOCKED_TOKEN
plugin_discussions: yes

View File

@@ -25,6 +25,7 @@
"anilist",
"wakatime",
"skyline",
"discussions",
"support",
"stackoverflow",
"stock",

View File

@@ -0,0 +1,48 @@
<% if (plugins.discussions) { %>
<section>
<h2 class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.5 2.75a.25.25 0 01.25-.25h8.5a.25.25 0 01.25.25v5.5a.25.25 0 01-.25.25h-3.5a.75.75 0 00-.53.22L3.5 11.44V9.25a.75.75 0 00-.75-.75h-1a.25.25 0 01-.25-.25v-5.5zM1.75 1A1.75 1.75 0 000 2.75v5.5C0 9.216.784 10 1.75 10H2v1.543a1.457 1.457 0 002.487 1.03L7.061 10h3.189A1.75 1.75 0 0012 8.25v-5.5A1.75 1.75 0 0010.25 1h-8.5zM14.5 4.75a.25.25 0 00-.25-.25h-.5a.75.75 0 110-1.5h.5c.966 0 1.75.784 1.75 1.75v5.5A1.75 1.75 0 0114.25 12H14v1.543a1.457 1.457 0 01-2.487 1.03L9.22 12.28a.75.75 0 111.06-1.06l2.22 2.22v-2.19a.75.75 0 01.75-.75h1a.25.25 0 00.25-.25v-5.5z"></path></svg>
<% if (!plugins.discussions.error) { %>
<%= plugins.discussions.started %> Discussion<%= s(plugins.discussions.started) %> started
<% } else { %>
Discussions
<% } %>
</h2>
<% if (plugins.discussions.error) { %>
<div class="row">
<section>
<div class="field error">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M2.343 13.657A8 8 0 1113.657 2.343 8 8 0 012.343 13.657zM6.03 4.97a.75.75 0 00-1.06 1.06L6.94 8 4.97 9.97a.75.75 0 101.06 1.06L8 9.06l1.97 1.97a.75.75 0 101.06-1.06L9.06 8l1.97-1.97a.75.75 0 10-1.06-1.06L8 6.94 6.03 4.97z"></path></svg>
<%= plugins.discussions.error.message %>
</div>
</section>
</div>
<% } else { %>
<div class="row">
<section class="largeable-column-fields">
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M16 1.25v4.146a.25.25 0 01-.427.177L14.03 4.03l-3.75 3.75a.75.75 0 11-1.06-1.06l3.75-3.75-1.543-1.543A.25.25 0 0111.604 1h4.146a.25.25 0 01.25.25zM2.75 3.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h2a.75.75 0 01.75.75v2.19l2.72-2.72a.75.75 0 01.53-.22h4.5a.25.25 0 00.25-.25v-2.5a.75.75 0 111.5 0v2.5A1.75 1.75 0 0113.25 13H9.06l-2.573 2.573A1.457 1.457 0 014 14.543V13H2.75A1.75 1.75 0 011 11.25v-7.5C1 2.784 1.784 2 2.75 2h5.5a.75.75 0 010 1.5h-5.5z"></path></svg>
<%= plugins.discussions.comments %> Comment<%= s(plugins.discussions.comments) %>
</div>
</section>
<section class="largeable-column-fields">
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.5 8a6.5 6.5 0 1113 0 6.5 6.5 0 01-13 0zM0 8a8 8 0 1116 0A8 8 0 010 8zm11.78-1.72a.75.75 0 00-1.06-1.06L6.75 9.19 5.28 7.72a.75.75 0 00-1.06 1.06l2 2a.75.75 0 001.06 0l4.5-4.5z"></path></svg>
<%= plugins.discussions.answers %> Answer<%= s(plugins.discussions.answers) %>
</div>
</section>
</div>
<% if (Object.keys(plugins.discussions.categories.stats).length) { %>
<div class="row fill-width">
<section>
<div class="field">
<% for (const [category, posts] of Object.entries(plugins.discussions.categories.stats)) { %>
<div class="label label-flex"><%= category %> <div class="label-right"><%= posts %></div> </div>
<% } %>
</div>
</section>
</div>
<% } %>
<% } %>
</section>
<% } %>