From 09ac6716eee3c15e441ab1be152a60c0a8089fb0 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Tue, 15 Nov 2022 20:39:55 +0100 Subject: [PATCH] feat: use fetch instead of axios for deploy preview --- scripts/deploy-preview-netlify.js | 37 +++++++++++++++--------- scripts/deploy-preview-netlify.js.sha384 | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/scripts/deploy-preview-netlify.js b/scripts/deploy-preview-netlify.js index 11eac4e3a..65ae1585a 100644 --- a/scripts/deploy-preview-netlify.js +++ b/scripts/deploy-preview-netlify.js @@ -1,5 +1,4 @@ -const {exec} = require('child_process') -const axios = require('axios') +const { exec } = require('child_process') const BOT_USER_ID = 513 const giteaToken = process.env.GITEA_TOKEN @@ -35,7 +34,7 @@ const promiseExec = cmd => { stdout = await promiseExec(`./node_modules/.bin/netlify deploy --alias ${alias}`) console.log(stdout) - const {data} = await axios.get(prIssueCommentsUrl) + const data = await fetch(prIssueCommentsUrl).then(response => response.json()) const hasComment = data.some(c => c.user.id === BOT_USER_ID) if (hasComment) { @@ -43,8 +42,7 @@ const promiseExec = cmd => { return } - await axios.post(prIssueCommentsUrl, { - body: ` + const message = ` Hi ${process.env.DRONE_COMMIT_AUTHOR}! Thank you for creating a PR! @@ -57,14 +55,25 @@ You will need to manually connect this to an api running somehwere. The easiest Have a nice day! > Beep boop, I'm a bot. -`, - }, { - headers: { - 'Content-Type': 'application/json', - 'accept': 'application/json', - 'Authorization': `token ${giteaToken}`, - }, - }) +` - console.log(`Preview comment sent successfully to PR #${prNumber}!`) + try { + const response = await fetch(prIssueCommentsUrl, { + method: 'POST', + body: JSON.stringify({ + body: message, + }), + headers: { + 'Content-Type': 'application/json', + 'accept': 'application/json', + 'Authorization': `token ${giteaToken}`, + }, + }) + if (!response.ok) { + throw new Error(`HTTP error, status = ${response.status}`) + } + console.log(`Preview comment sent successfully to PR #${prNumber}!`) + } catch (e) { + console.log(`Could not send preview comment to PR #${prNumber}! ${e.message}`) + } })() \ No newline at end of file diff --git a/scripts/deploy-preview-netlify.js.sha384 b/scripts/deploy-preview-netlify.js.sha384 index 03ac06468..98bd091e0 100644 --- a/scripts/deploy-preview-netlify.js.sha384 +++ b/scripts/deploy-preview-netlify.js.sha384 @@ -1 +1 @@ -bb46342a0a08105b340ba7976cff9d80ef89901120ec0639669caa70bb7d2dbc43e78b1f635a7654ab2456e8358c98a4 ./scripts/deploy-preview-netlify.js +05c69e5323a4d4bac041ade830735becd52c230277396d1f72be8fde83683a75dc095f6678804083b2ca66f27cc7995f ./scripts/deploy-preview-netlify.js