From e8eb71c9395dd3fa5ab204b40893b50da2bc64b3 Mon Sep 17 00:00:00 2001 From: Filip Hlavac Date: Mon, 1 Jul 2024 13:32:05 +0200 Subject: [PATCH] Update upload-preview.js --- .github/upload-preview.js | 163 +++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 81 deletions(-) diff --git a/.github/upload-preview.js b/.github/upload-preview.js index d560bc84..8efdea16 100644 --- a/.github/upload-preview.js +++ b/.github/upload-preview.js @@ -1,97 +1,98 @@ const fs = require('fs'); const path = require('path'); -const { Octokit } = require('@octokit/rest'); -const octokit = new Octokit({ auth: process.env.GH_PR_TOKEN }); const surge = require('surge'); const publishFn = surge().publish(); -// From github actions -const ghrepo = process.env.GITHUB_REPOSITORY || ''; +(async () => { + const { Octokit } = await import('@octokit/rest'); + const octokit = new Octokit({ auth: process.env.GH_PR_TOKEN }); -const owner = process.env.CIRCLE_PROJECT_USERNAME || ghrepo.split('/')[0]; // patternfly -const repo = process.env.CIRCLE_PROJECT_REPONAME || ghrepo.split('/')[1]; -const prnum = process.env.CIRCLE_PR_NUMBER || process.env.GH_PR_NUM; -const prbranch = process.env.CIRCLE_BRANCH || process.env.GITHUB_REF.split('/').pop(); + // From github actions + const ghrepo = process.env.GITHUB_REPOSITORY || ''; -const uploadFolder = process.argv[2]; -if (!uploadFolder) { - console.log('Usage: upload-preview uploadFolder'); - process.exit(1); -} + const owner = process.env.CIRCLE_PROJECT_USERNAME || ghrepo.split('/')[0]; // patternfly + const repo = process.env.CIRCLE_PROJECT_REPONAME || ghrepo.split('/')[1]; + const prnum = process.env.CIRCLE_PR_NUMBER || process.env.GH_PR_NUM; + const prbranch = process.env.CIRCLE_BRANCH || process.env.GITHUB_REF.split('/').pop(); -const uploadFolderName = path.basename(uploadFolder); -let uploadURL = `${repo}-${prnum ? `pr-component-groups-${prnum}` : prbranch}`.replace(/[\/|\.]/g, '-'); + const uploadFolder = process.argv[2]; + if (!uploadFolder) { + console.log('Usage: upload-preview uploadFolder'); + process.exit(1); + } -switch(uploadFolderName) { - case 'coverage': - uploadURL += '-a11y.surge.sh'; - break; - case 'public': - if (!prnum && prbranch === 'main') { - uploadURL = 'https://pf-extensions.surge.sh/'; - } - else { + const uploadFolderName = path.basename(uploadFolder); + let uploadURL = `${repo}-${prnum ? `pr-component-groups-${prnum}` : prbranch}`.replace(/[\/|\.]/g, '-'); + + switch(uploadFolderName) { + case 'coverage': + uploadURL += '-a11y.surge.sh'; + break; + case 'public': + if (!prnum && prbranch === 'main') { + uploadURL = 'https://pf-extensions.surge.sh/'; + } else { + uploadURL += '.surge.sh'; + } + break; + default: + uploadURL += `-${uploadFolderName}`; uploadURL += '.surge.sh'; - } - break; - default: - uploadURL += `-${uploadFolderName}`; - uploadURL += '.surge.sh'; - break; -} + break; + } -publishFn({ - project: uploadFolder, - p: uploadFolder, - domain: uploadURL, - d: uploadURL, - e: 'https://surge.surge.sh', - endpoint: 'https://surge.surge.sh' -}); + publishFn({ + project: uploadFolder, + p: uploadFolder, + domain: uploadURL, + d: uploadURL, + e: 'https://surge.surge.sh', + endpoint: 'https://surge.surge.sh' + }); -function tryAddComment(comment, commentBody) { - if (!commentBody.includes(comment)) { - return comment; + function tryAddComment(comment, commentBody) { + if (!commentBody.includes(comment)) { + return comment; + } + return ''; } - return ''; -} -if (prnum) { - octokit.issues.listComments({ - owner, - repo, - issue_number: prnum - }) - .then(res => res.data) - .then(comments => { - let commentBody = ''; - const existingComment = comments.find(comment => comment.user.login === 'patternfly-build'); - if (existingComment) { - commentBody += existingComment.body.trim(); - commentBody += '\n\n'; - } + if (prnum) { + const comments = await octokit.issues.listComments({ + owner, + repo, + issue_number: prnum + }).then(res => res.data); - if (uploadFolderName === 'public') { - commentBody += tryAddComment(`Preview: https://${uploadURL}`, commentBody); - } - else if (uploadFolderName === 'coverage') { - commentBody += tryAddComment(`A11y report: https://${uploadURL}`, commentBody); - } + let commentBody = ''; + const existingComment = comments.find(comment => comment.user.login === 'patternfly-build'); + if (existingComment) { + commentBody += existingComment.body.trim(); + commentBody += '\n\n'; + } - if (existingComment) { - octokit.issues.updateComment({ - owner, - repo, - comment_id: existingComment.id, - body: commentBody - }).then(() => console.log('Updated comment!')); - } else { - octokit.issues.createComment({ - owner, - repo, - issue_number: prnum, - body: commentBody - }).then(() => console.log('Created comment!')); - } - }); -} \ No newline at end of file + if (uploadFolderName === 'public') { + commentBody += tryAddComment(`Preview: https://${uploadURL}`, commentBody); + } else if (uploadFolderName === 'coverage') { + commentBody += tryAddComment(`A11y report: https://${uploadURL}`, commentBody); + } + + if (existingComment) { + await octokit.issues.updateComment({ + owner, + repo, + comment_id: existingComment.id, + body: commentBody + }); + console.log('Updated comment!'); + } else { + await octokit.issues.createComment({ + owner, + repo, + issue_number: prnum, + body: commentBody + }); + console.log('Created comment!'); + } + } +})();