feat: support reactions

This commit is contained in:
bubkoo 2020-09-28 16:43:10 +08:00
parent 7bb727e28d
commit 9908687d9a
5 changed files with 85 additions and 2 deletions

View File

@ -24,6 +24,9 @@ jobs:
state: all
# If similarity is higher than this threshold([0,1]), issue will be marked as duplicate.
threshold: 0.6
# Reactions to be add to comment when potential duplicates are detected.
# Available reactions: "-1", "+1", "confused", "laugh", "heart", "hooray", "rocket", "eyes"
reactions: 'eyes, confused'
# Comment to post when potential duplicates are detected.
comment: >
Potential duplicates: {{#issues}}

View File

@ -24,6 +24,10 @@ inputs:
- [#{{ number }}] {{ title }} ({{ accuracy }}%)
{{/issues}}
required: false
reactions:
description: Reactions to be add to comment when potential duplicates are detected.
required: false
runs:
using: node12

View File

@ -1,7 +1,7 @@
{
"name": "potential-duplicates",
"description": "A Github Action to search for potential issue duplicates using DamerauLevenshtein algorithm.",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/index.js",
"repository": "https://github.com/bubkoo/potential-duplicates",
"author": "bubkoo <bubkoo.wy@gmail.com>",

View File

@ -3,6 +3,7 @@ import * as github from '@actions/github'
import mustache from 'mustache'
import { Algo } from './algo'
import { Util } from './util'
import { Reaction } from './reaction'
export namespace Action {
export async function run() {
@ -49,16 +50,21 @@ export namespace Action {
}
const comment = core.getInput('comment')
const reactions = core.getInput('reactions')
if (comment) {
const body = mustache.render(comment, {
issues: duplicates,
})
await octokit.issues.createComment({
const { data } = await octokit.issues.createComment({
...context.repo,
body,
issue_number: payload.number,
})
if (reactions) {
await Reaction.add(octokit, data.id, reactions)
}
}
}
}

70
src/reaction.ts Normal file
View File

@ -0,0 +1,70 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
export namespace Reaction {
const presets = [
'+1',
'-1',
'laugh',
'confused',
'heart',
'hooray',
'rocket',
'eyes',
] as const
type ReactionType = typeof presets[number]
function getReactions(inputs: string | string[]) {
const candidates = Array.isArray(inputs)
? inputs
: inputs.split(inputs.indexOf(',') >= 0 ? ',' : /\s+/g)
return candidates
.map((item) => item.trim())
.filter((item: ReactionType) => {
if (item) {
if (presets.includes(item)) {
return true
}
core.debug(`Skipping invalid reaction '${item}'.`)
return false
}
}) as ReactionType[]
}
export async function add(
octokit: ReturnType<typeof github.getOctokit>,
comment_id: number, // tslint:disable-line
reactions: string | string[],
owner: string = github.context.repo.owner,
repo: string = github.context.repo.repo,
) {
const candidates = getReactions(reactions)
if (candidates.length <= 0) {
core.debug(`No valid reactions are contained in '${reactions}'.`)
return
}
core.debug(`Setting '${candidates.join(', ')}' reaction on comment.`)
const deferreds = candidates.map((content) => {
try {
return octokit.reactions.createForIssueComment({
owner,
repo,
comment_id,
content,
})
} catch (e) {
core.debug(
`Adding reaction '${content}' to comment failed with: ${e.message}.`,
)
core.error(e)
}
})
return Promise.all(deferreds)
}
}