Add support for reactions plugin (#180)

This commit is contained in:
Simon Lecoq
2021-03-09 19:58:53 +01:00
committed by GitHub
parent a839b0ae2d
commit 732d2a44b0
11 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
### 🎭 Comment reactions
The *reactions* plugin displays overall reactions on your recent issues and issue comments.
<table>
<td align="center">
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.reactions.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_reactions: yes
plugin_reactions_limit: 200 # Compute reactions over last 200 issue comments
plugin_reactions_days: 14 # Compute reactions on issue comments posted less than 14 days ago
plugin_reactions_details: percentage # Display reactions percentage
```

View File

@@ -0,0 +1,53 @@
//Setup
export default async function({login, q, imports, data, graphql, queries, account}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.reactions))
return null
//Load inputs
let {limit, days, details} = imports.metadata.plugins.reactions.inputs({data, account, q})
//Load issue comments
let cursor = null, pushed = 0
const comments = []
for (const type of ["issues", "issueComments"]) {
do {
//Load issue comments
console.debug(`metrics/compute/${login}/plugins > reactions > retrieving ${type} after ${cursor}`)
const {user:{[type]:{edges}}} = await graphql(queries.reactions({login, type, after:cursor ? `after: "${cursor}"` : ""}))
cursor = edges?.[edges?.length-1]?.cursor
//Save issue comments
const filtered = edges.flatMap(({node:{createdAt:created, reactions:{nodes:reactions}}}) => ({created:new Date(created), reactions:reactions.map(({content}) => content)})).filter(comment => Number.isFinite(days) ? comment.created < new Date(Date.now()-days*24*60*60*1000) : true)
pushed = filtered.length
comments.push(...filtered)
console.debug(`metrics/compute/${login}/plugins > reactions > currently at ${comments.length} comments`)
//Early break
if ((comments.length >= limit)||(filtered.length < edges.length))
break
} while ((cursor)&&(pushed)&&(comments.length < limit))
}
//Applying limit
if (limit) {
comments.splice(limit)
console.debug(`metrics/compute/${login}/plugins > reactions > keeping only ${comments.length} comments`)
}
//Format reactions list
const list = {}
const reactions = comments.flatMap(({reactions}) => reactions)
for (const reaction of reactions)
list[reaction] = (list[reaction] ?? 0) + 1
for (const [key, value] of Object.entries(list))
list[key] = {value, score:value/reactions.length}
//Results
return {list, comments:comments.length, details, days}
}
//Handle errors
catch (error) {
throw {error:{message:"An error occured", instance:error}}
}
}

View File

@@ -0,0 +1,39 @@
name: "🎭 Comment reactions"
cost: 1 GraphQL request per 100 issues and issues comments fetched
categorie: github
supports:
- user
inputs:
# Enable or disable plugin
plugin_reactions:
description: Display average issue comments reactions
type: boolean
default: no
# Maximum number of issue comments to parse
# Issues will be fetched before issues comments
plugin_reactions_limit:
description: Maximum number of issue comments to parse
type: number
default: 200
min: 1
max: 1000
# Filter reactions by issue comments age
# Set to 0 to disable age filtering
plugin_reactions_days:
description: Maximum issue comments age
type: number
default: 0
min: 0
# Additional details
plugin_reactions_details:
description: Additional details
type: string
default: none
values:
- none
- count
- percentage

View File

@@ -0,0 +1,18 @@
query ReactionsDefault {
user(login: "$login") {
login
$type($after first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
edges {
cursor
node {
createdAt
reactions(last: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
content
}
}
}
}
}
}
}

View File

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