From a05664cd95bd995133291409f7936909bb43f64c Mon Sep 17 00:00:00 2001 From: Emilien Devos Date: Mon, 8 May 2023 13:08:04 +0200 Subject: [PATCH] add instance automatically to uptime robot --- .github/ISSUE_TEMPLATE/new_instance.yaml | 2 + .../workflows/add-instance-uptimerobot.yml | 112 ++++++++++++++++++ .github/workflows/remove-30days-label.yml | 23 ++++ 3 files changed, 137 insertions(+) create mode 100644 .github/workflows/add-instance-uptimerobot.yml create mode 100644 .github/workflows/remove-30days-label.yml diff --git a/.github/ISSUE_TEMPLATE/new_instance.yaml b/.github/ISSUE_TEMPLATE/new_instance.yaml index a5e0e5c..de1e042 100644 --- a/.github/ISSUE_TEMPLATE/new_instance.yaml +++ b/.github/ISSUE_TEMPLATE/new_instance.yaml @@ -4,6 +4,8 @@ title: "[New instance] " assignees: - unixfox - samantazfox +labels: + - instance-add body: - type: markdown diff --git a/.github/workflows/add-instance-uptimerobot.yml b/.github/workflows/add-instance-uptimerobot.yml new file mode 100644 index 0000000..38aef62 --- /dev/null +++ b/.github/workflows/add-instance-uptimerobot.yml @@ -0,0 +1,112 @@ +name: Add instance to uptimerobot + +on: + issues: + types: [opened, reopened] + +jobs: + + replycomment: + + runs-on: ubuntu-latest + permissions: write-all + steps: + - uses: actions/checkout@v3 + if: contains(github.event.issue.labels.*.name, 'instance-add') + - uses: actions/setup-node@v3 + if: contains(github.event.issue.labels.*.name, 'instance-add') + with: + node-version: 16 + - run: npm install request linkifyjs + if: contains(github.event.issue.labels.*.name, 'instance-add') + - uses: actions/github-script@v6 + if: contains(github.event.issue.labels.*.name, 'instance-add') + with: + script: | + var issueInfo = (await github.rest.issues.get({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + })).data; + var linkify = require("linkifyjs"); + var issueTitleParseUrl = linkify.find(issueInfo.title); + if (issueTitleParseUrl.length !== 0) { + if (issueInfo.title.includes(".onion")) { + var replyComment = + ['Hello! I\'ve detected that you are requesting to add an onion URL.', + 'Please create a pull request instead for adding your onion url as an alternative to your clearnet URL: https://github.com/iv-org/documentation/edit/master/docs/instances.md' + ].join('\n'); + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: replyComment + }); + await github.rest.issues.update({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + state: 'closed' + }); + } + else { + var instanceHostname = (new URL(issueTitleParseUrl[0].href)).hostname; + var request = require("request"); + var options = { method: 'POST', + url: 'https://api.uptimerobot.com/v2/newMonitor', + json:true, + headers: + { 'content-type': 'application/x-www-form-urlencoded', + 'cache-control': 'no-cache' }, + form: + { api_key: '${{ secrets.UPTIMEROBOT_API_KEY }}', + format: 'json', + type: '1', + url: 'https://' + instanceHostname, + friendly_name: instanceHostname } }; + + request(options, async function (error, response, body) { + if (error) throw new Error(error); + console.log(body); + if (body.stat == "ok") { + var replyComment = + ['Hello! Your instance has been added to our monitoring system: https://stats.uptimerobot.com/89VnzSKAn/' + body.monitor.id, + 'You need to wait 30 days before we add your instance.', + '', + 'Make sure you double checked all the mandatory checks or this will slow down the process of adding your instance!' + ].join('\n'); + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: replyComment + }) + await github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['wait-30-days'] + }) + } + }); + } + } + else { + var replyComment = + ['Domain not detected in the title, please edit the title by correcting it like this and reopen the issue:', + 'Issue title example: `[New instance] https://myinstance.com`' + ].join('\n'); + + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: replyComment + }) + await github.rest.issues.update({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + state: 'closed' + }) + } \ No newline at end of file diff --git a/.github/workflows/remove-30days-label.yml b/.github/workflows/remove-30days-label.yml new file mode 100644 index 0000000..6e235ad --- /dev/null +++ b/.github/workflows/remove-30days-label.yml @@ -0,0 +1,23 @@ +name: Remove instance label when closing + +on: + issues: + types: [closed] + +jobs: + + replycomment: + + runs-on: ubuntu-latest + permissions: write-all + steps: + - uses: actions/github-script@v6 + if: contains(github.event.issue.labels.*.name, 'wait-30-days') + with: + script: | + await github.rest.issues.removeLabel({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + name: 'wait-30-days' + }); \ No newline at end of file