Skip to content

Commit

Permalink
feat: update minimized comment with explainer (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 authored Oct 2, 2023
1 parent b5a123e commit f5f1579
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Nissuer
uses: balazsorban44/nissuer@1.5.0
uses: balazsorban44/nissuer@1.7.0
with:
label-area-section: 'Which area\(s\) are affected\?(.*)'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Nissuer
uses: balazsorban44/nissuer@1.5.0
uses: balazsorban44/nissuer@1.7.0
```

Add a comment file (by default we look for `.github/invalid-reproduction.md`):
Expand Down
16 changes: 10 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ branding:
color: orange

inputs:
comment-add-explainer:
description: 'Add an explainer to a comment that was marked as off-topic. Example: "true"'
default: 'true'
comment-unhelpful-weight:
description: 'If an issue comment is below this rate, it will be marked as off-topic and gets hidden. Example: 0.3'
default: '0.3'
Expand Down Expand Up @@ -53,15 +56,16 @@ runs:
shell: sh
# https://github.com/actions/runner/issues/665#issuecomment-676581170
env:
"INPUT_COMMENT_ADD_EXPLAINER": ${{comment-add-explainer}}
"INPUT_COMMENT_UNHELPFUL_WEIGHT": ${{inputs.comment-unhelpful-weight}}
"INPUT_DELETE_VULNERABILITY_REPORT": ${{inputs.delete-vulnerability-report}}
"INPUT_LABEL_AREA_PREFIX": ${{inputs.label-area-prefix}}
"INPUT_LABEL_AREA_SECTION": ${{inputs.label-area-section}}
"INPUT_LABEL_COMMENTS": ${{inputs.label-comments}}
"INPUT_REPRODUCTION_COMMENT": ${{inputs.reproduction-comment}}
"INPUT_REPRODUCTION_HOSTS": ${{inputs.reproduction-hosts}}
"INPUT_REPRODUCTION_INVALID_LABEL": ${{inputs.reproduction-invalid-label}}
"INPUT_REPRODUCTION_ISSUE_LABELS": ${{inputs.reproduction-issue-labels}}
"INPUT_REPRODUCTION_LINK_SECTION": ${{inputs.reproduction-link-section}}
"INPUT_LABEL_COMMENTS": ${{inputs.label-comments}}
"INPUT_COMMENT_UNHELPFUL_WEIGHT": ${{inputs.comment-unhelpful-weight}}
"INPUT_LABEL_AREA_PREFIX": ${{inputs.label-area-prefix}}
"INPUT_LABEL_AREA_SECTION": ${{inputs.label-area-section}}
"INPUT_WEBHOOK_URL": ${{inputs.webhook-url}}
"INPUT_WEBHOOK_SECRET": ${{inputs.webhook-secret}}
"INPUT_DELETE_VULNERABILITY_REPORT": ${{inputs.delete-vulnerability-report}}
"INPUT_WEBHOOK_URL": ${{inputs.webhook-url}}
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

29 changes: 25 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function tryParse(json) {
}
}

/** @param {string} value */
function getBooleanOrUndefined(value) {
const variable = process.env[`INPUT_${value.toUpperCase()}`]
if (variable === undefined || variable === "") return undefined
return getBooleanInput(value)
}

const config = {
invalidLink: {
comment:
Expand All @@ -48,13 +55,14 @@ const config = {
},
comments: {
unhelpfulWeight: Number(getInput("comment_unhelpful_weight")) || 0.3,
addExplainer: getBooleanOrUndefined("comment_add_explainer") ?? true,
},
webhook: {
url: getInput("webhook_url"),
secret: getInput("webhook_secret"),
},
vuln: {
shouldDelete: getBooleanInput("delete_vulnerability_report"),
shouldDelete: getBooleanOrUndefined("delete_vulnerability_report"),
},
token: process.env.GITHUB_TOKEN,
workspace: process.env.GITHUB_WORKSPACE,
Expand Down Expand Up @@ -226,6 +234,8 @@ function isUnhelpfulComment(text) {
return restText.length / textLength < UNHELPFUL_LIMIT
}

const updatedComment = `_Edit by maintainers: Comment was **automatically** minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!_`

async function hideUnhelpfulComments() {
const { comment, action, issue } = context.payload
if (action !== "created" || !comment || !issue) return
Expand All @@ -234,10 +244,21 @@ async function hideUnhelpfulComments() {

if (!isUnhelpfulComment(body) && !isStillHappeningWithoutLink(body)) return

debug(
info(
`Comment (${body}) on issue #${issue.number} is unhelpful, minimizing...`
)
const { graphql } = getOctokit(config.token)
const { graphql, rest: client } = getOctokit(config.token)

if (config.comments.addExplainer) {
debug(`Adding explainer to comment #${subjectId}...`)
await client.issues.updateComment({
...context.repo,
comment_id: subjectId,
body: `${body}\n\n${updatedComment}`,
})
info(`Explainer added to comment #${subjectId}`)
}

/** @see https://docs.github.com/en/graphql/reference/mutations#minimizecomment */
await graphql(
`
Expand Down Expand Up @@ -346,7 +367,7 @@ async function notifyOnPubliclyDisclosedVulnerability() {
info(`Deleted issue #${issue_number}`)
debug(`Deleted issue #${issue_number}`)
} catch (error) {
error(`Couldn't delete issue #${issue_number}: ${e}`)
error(`Couldn't delete issue #${issue_number}: ${error}`)
}
} else debug(`Not deleting issue #${issue_number}`)

Expand Down

0 comments on commit f5f1579

Please sign in to comment.