|
| 1 | +name: manage-issue |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, edited] |
| 6 | + |
| 7 | +jobs: |
| 8 | + check-repro: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/github-script@v3 |
| 12 | + with: |
| 13 | + script: | |
| 14 | + const URL_REGEXP = /### Reproduction repository[\r\n]+([^#]+)###/m |
| 15 | + const REPRO_STEPS_REGEXP = /### Steps to reproduce[\r\n]+([^#]+)###/m |
| 16 | + const LABEL_NEEDS_MORE_INFORMATION = 'needs more info' |
| 17 | +
|
| 18 | + function debug(...args) { |
| 19 | + core.info(args.map(JSON.stringify).join(' ')) |
| 20 | + } |
| 21 | +
|
| 22 | + if (context.payload.comment) { |
| 23 | + debug('Ignoring comment update.') |
| 24 | +
|
| 25 | + return |
| 26 | + } |
| 27 | +
|
| 28 | + const user = context.payload.sender.login |
| 29 | + const issue = context.payload.issue |
| 30 | + const body = issue.body |
| 31 | + const urlMatch = body.match(URL_REGEXP) |
| 32 | + const reproStepsMatch = body.match(REPRO_STEPS_REGEXP) |
| 33 | + const url = urlMatch !== null ? urlMatch[1].trim() : null |
| 34 | + const reproSteps = reproStepsMatch !== null ? reproStepsMatch[1].trim() : null |
| 35 | +
|
| 36 | + debug(`Found URL '${url}'`) |
| 37 | + debug(`Found repro steps '${reproSteps}'`) |
| 38 | +
|
| 39 | + async function createComment(comment) { |
| 40 | + comment = comment |
| 41 | + .split('\n') |
| 42 | + .map((line) => line.trim()) |
| 43 | + .join('\n') |
| 44 | + .trim() |
| 45 | +
|
| 46 | + await github.issues.createComment({ |
| 47 | + issue_number: context.issue.number, |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + body: comment, |
| 51 | + }) |
| 52 | + } |
| 53 | +
|
| 54 | + async function getGitHubActionComments() { |
| 55 | + debug(`Loading existing comments...`) |
| 56 | +
|
| 57 | + const comments = await github.issues.listComments({ |
| 58 | + issue_number: context.issue.number, |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + }) |
| 62 | +
|
| 63 | + return comments.data.filter(comment => { |
| 64 | + debug(`comment by user: '${comment.user.login}'`) |
| 65 | + return comment.user.login === 'github-actions[bot]' |
| 66 | + }) |
| 67 | + } |
| 68 | +
|
| 69 | + async function getIssueLabels() { |
| 70 | + const issues = await github.issues.listLabelsOnIssue({ |
| 71 | + issue_number: context.issue.number, |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + }) |
| 75 | +
|
| 76 | + return issues.data |
| 77 | + } |
| 78 | +
|
| 79 | + async function updateIssue(state, state_reason = null) { |
| 80 | + await github.issues.update({ |
| 81 | + owner: context.repo.owner, |
| 82 | + repo: context.repo.repo, |
| 83 | + issue_number: context.issue.number, |
| 84 | + state, |
| 85 | + state_reason, |
| 86 | + }) |
| 87 | + } |
| 88 | +
|
| 89 | + async function closeWithComment(comment) { |
| 90 | + if (issue.state !== 'open') { |
| 91 | + debug(`Issue is not open`) |
| 92 | +
|
| 93 | + return |
| 94 | + } |
| 95 | +
|
| 96 | + const comments = await getGitHubActionComments() |
| 97 | +
|
| 98 | + if (comments.length > 0) { |
| 99 | + debug(`Already commented on issue won't comment again`) |
| 100 | +
|
| 101 | + return |
| 102 | + } |
| 103 | +
|
| 104 | + debug(`Missing required information`) |
| 105 | +
|
| 106 | + await github.issues.addLabels({ |
| 107 | + issue_number: context.issue.number, |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + labels: [LABEL_NEEDS_MORE_INFORMATION], |
| 111 | + }) |
| 112 | +
|
| 113 | + await createComment(comment) |
| 114 | +
|
| 115 | + await updateIssue('closed', 'not_planned') |
| 116 | + } |
| 117 | +
|
| 118 | + async function openWithComment(comment) { |
| 119 | + if (issue.state !== 'closed') { |
| 120 | + debug(`Issue is already open`) |
| 121 | +
|
| 122 | + return |
| 123 | + } |
| 124 | +
|
| 125 | + const labels = await getIssueLabels() |
| 126 | + const label = labels.find(label => label.name === LABEL_NEEDS_MORE_INFORMATION) |
| 127 | +
|
| 128 | + if (! label) { |
| 129 | + debug(`Issue was not tagged as needs information`) |
| 130 | +
|
| 131 | + return |
| 132 | + } |
| 133 | +
|
| 134 | + const comments = await getGitHubActionComments() |
| 135 | +
|
| 136 | + if (comments.length === 0) { |
| 137 | + debug(`Issue was closed by someone else, won't reopen`) |
| 138 | +
|
| 139 | + return |
| 140 | + } |
| 141 | +
|
| 142 | + debug(`Reopening closed issue`) |
| 143 | +
|
| 144 | + await github.issues.removeLabel({ |
| 145 | + issue_number: context.issue.number, |
| 146 | + owner: context.repo.owner, |
| 147 | + repo: context.repo.repo, |
| 148 | + name: LABEL_NEEDS_MORE_INFORMATION, |
| 149 | + }) |
| 150 | +
|
| 151 | + await createComment(comment) |
| 152 | +
|
| 153 | + await updateIssue('open') |
| 154 | + } |
| 155 | +
|
| 156 | + const COMMENT_HEADER = ` |
| 157 | + Hey @${user}! We're sorry to hear that you've hit this issue. 💙 |
| 158 | + `.trim() |
| 159 | +
|
| 160 | + const NO_REPRO_URL = ((! url) || (! url.includes('https://github.com/')) || (url.includes('https://github.com/joelbutcher') && (! url.includes('https://github.com/joelbutcher/laravel-facebook-graph-demo')))) |
| 161 | + const NO_REPRO_STEPS = reproSteps.length < 25 |
| 162 | +
|
| 163 | + if (NO_REPRO_URL || NO_REPRO_STEPS) { |
| 164 | + let comment = ` |
| 165 | + ${COMMENT_HEADER} |
| 166 | +
|
| 167 | + ` |
| 168 | +
|
| 169 | + if (NO_REPRO_URL) { |
| 170 | + comment += ` |
| 171 | + However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue? |
| 172 | +
|
| 173 | + We need a public GitHub repository which contains a Laravel app with the minimal amount of Socialstream code to reproduce the problem. **Please do not link to your actual project**, what we need instead is a _minimal_ reproduction in a fresh project without any unnecessary code. This means it doesn\'t matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly. |
| 174 | + ` |
| 175 | + } |
| 176 | +
|
| 177 | + if (NO_REPRO_URL && NO_REPRO_STEPS) { |
| 178 | + comment += ` |
| 179 | +
|
| 180 | + Also, ` |
| 181 | + } else if (NO_REPRO_STEPS) { |
| 182 | + comment += ` |
| 183 | +
|
| 184 | + However, ` |
| 185 | + } |
| 186 | +
|
| 187 | + if (NO_REPRO_STEPS) { |
| 188 | + comment += `it doesn't look like you've provided much information on how to replicate the issue. Please edit your original post with clear steps we need to take.` |
| 189 | + } |
| 190 | +
|
| 191 | + closeWithComment(comment) |
| 192 | + } else { |
| 193 | + openWithComment(` |
| 194 | + Thank you for providing reproduction steps! Reopening the issue now. |
| 195 | + `) |
| 196 | + } |
0 commit comments