|
| 1 | +name: Mirror EssentialsX dumps to Gist |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, edited, closed] |
| 6 | + issue_comment: |
| 7 | + types: [created] |
| 8 | + |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + contents: read |
| 12 | + |
| 13 | +env: |
| 14 | + DUMP_REGEX: 'https?://essentialsx\.net/dump\?bytebin=([A-Za-z0-9_-]+)' |
| 15 | + GIST_LINK_FMT: 'https://essentialsx.net/dump?gist=' |
| 16 | + |
| 17 | +jobs: |
| 18 | + handle-dump: |
| 19 | + if: github.event.action != 'closed' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Mirror dump to a private Gist |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + github-token: ${{ secrets.GIST_TOKEN }} |
| 27 | + script: | |
| 28 | + const issue = context.payload.issue ?? context.payload.comment.issue; |
| 29 | + const bodySrc = context.payload.comment ? context.payload.comment.body : issue.body || ''; |
| 30 | +
|
| 31 | + const regex = new RegExp(process.env.DUMP_REGEX); |
| 32 | + const m = bodySrc.match(regex); |
| 33 | + if (!m) { |
| 34 | + core.info('No EssentialsX dump link found – abort.'); |
| 35 | + return; |
| 36 | + } |
| 37 | +
|
| 38 | + const key = m[1]; |
| 39 | + const dump = await fetch(`https://api.pastes.dev/${key}`); |
| 40 | + if (!dump.ok) throw new Error(`pastes.dev fetch failed (${dump.status})`); |
| 41 | + const text = await dump.text(); |
| 42 | +
|
| 43 | + const { data: gist } = await github.rest.gists.create({ |
| 44 | + files: { [`${key}.txt`]: { content: text } }, |
| 45 | + description: `EssentialsX dump for issue #${issue.number}`, |
| 46 | + public: false |
| 47 | + }); |
| 48 | +
|
| 49 | + const link = `${process.env.GIST_LINK_FMT}${gist.id}`; |
| 50 | +
|
| 51 | + await github.rest.issues.createComment({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + issue_number: issue.number, |
| 55 | + body: `📋 Essentials Dump Backup → ${link}` |
| 56 | + }); |
| 57 | +
|
| 58 | + cleanup-gist: |
| 59 | + if: github.event_name == 'issues' && github.event.action == 'closed' |
| 60 | + runs-on: ubuntu-latest |
| 61 | + |
| 62 | + steps: |
| 63 | + - name: Delete gist and hide bot comment |
| 64 | + uses: actions/github-script@v7 |
| 65 | + with: |
| 66 | + github-token: ${{ secrets.GIST_TOKEN }} |
| 67 | + script: | |
| 68 | + const issue = context.payload.issue; |
| 69 | +
|
| 70 | + const comments = await github.paginate( |
| 71 | + github.rest.issues.listComments, |
| 72 | + { owner: context.repo.owner, repo: context.repo.repo, issue_number: issue.number, per_page: 100 } |
| 73 | + ); |
| 74 | + |
| 75 | + const re = /https?:\/\/essentialsx\.net\/dump\?gist=([A-Za-z0-9]+)/; |
| 76 | + const hit = comments.reverse().find(c => re.test(c.body || '')); |
| 77 | + |
| 78 | + if (!hit) { |
| 79 | + core.info('No gist link comment found – nothing to clean up.'); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const gistId = (hit.body.match(re))[1]; |
| 84 | + const commentNodeId = hit.node_id; |
| 85 | +
|
| 86 | + try { |
| 87 | + await github.request('DELETE /gists/{gist_id}', { gist_id: gistId }); |
| 88 | + core.info(`Deleted Gist ${gistId}`); |
| 89 | + } catch (err) { |
| 90 | + core.warning(`Could not delete Gist ${gistId}: ${err.message}`); |
| 91 | + } |
| 92 | +
|
| 93 | + try { |
| 94 | + await github.graphql( |
| 95 | + `mutation($id:ID!){ |
| 96 | + minimizeComment(input:{subjectId:$id, classifier:OUTDATED}) { |
| 97 | + minimizedComment { id } |
| 98 | + } |
| 99 | + }`, |
| 100 | + { id: commentNodeId } |
| 101 | + ); |
| 102 | + core.info(`Minimised comment ${hit.id} as OUTDATED`); |
| 103 | + } catch (err) { |
| 104 | + core.warning(`Could not minimise comment: ${err.message}`); |
| 105 | + } |
0 commit comments