diff --git a/scripts/deploy-preview-netlify.js b/scripts/deploy-preview-netlify.js index 3b2554bff..b2dd23364 100644 --- a/scripts/deploy-preview-netlify.js +++ b/scripts/deploy-preview-netlify.js @@ -2,12 +2,13 @@ const slugify = require('slugify') const {exec} = require('child_process') const axios = require('axios') +const BOT_USER_ID = 513 const giteaToken = process.env.GITEA_TOKEN const siteId = process.env.NETLIFY_SITE_ID const branchSlug = slugify(process.env.DRONE_SOURCE_BRANCH) const prNumber = process.env.DRONE_PULL_REQUEST -const prIssueUrl = `https://kolaente.dev/api/v1/repos/vikunja/frontend/issues/${prNumber}/comments` +const prIssueCommentsUrl = `https://kolaente.dev/api/v1/repos/vikunja/frontend/issues/${prNumber}/comments` const alias = `${prNumber}-${branchSlug}` const fullPreviewUrl = `https://${alias}--vikunja-frontend-preview.netlify.app` @@ -24,15 +25,25 @@ const promiseExec = cmd => { }) } -(async function() { +(async function () { let stdout = await promiseExec(`./node_modules/.bin/netlify link --id ${siteId}`) console.log(stdout) stdout = await promiseExec(`./node_modules/.bin/netlify deploy --alias ${alias}`) console.log(stdout) - await axios.post(prIssueUrl, { + const {data} = await axios.get(prIssueCommentsUrl) + const hasComment = data.some(c => c.user.id === BOT_USER_ID) + + if (hasComment) { + console.log(`PR #${prNumber} already has a comment with a link, not sending another comment.`) + return + } + + await axios.post(prIssueCommentsUrl, { body: ` -Hi! +Hi ${process.env.DRONE_COMMIT_AUTHOR}! + +Thank you for creating a PR! I've deployed the changes of this PR on a preview environment under this URL: ${fullPreviewUrl} @@ -50,5 +61,6 @@ Have a nice day! 'Authorization': `token ${giteaToken}`, }, }) - .catch(err => console.log('Error sending comment:', err)) + + console.log(`Preview comment sent successfully to PR #${prNumber}!`) })() \ No newline at end of file