From 61f8a5341c0fbe120ffbed76ebd8c6c376b3cabe Mon Sep 17 00:00:00 2001 From: Jessie Date: Fri, 5 Apr 2024 14:23:10 -0400 Subject: [PATCH] Add pr previews --- .github/upload-preview.js | 97 ++++++++++++++++++++++++++++++++ .github/workflows/pr-preview.yml | 58 +++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 .github/upload-preview.js create mode 100644 .github/workflows/pr-preview.yml diff --git a/.github/upload-preview.js b/.github/upload-preview.js new file mode 100644 index 0000000..a4c7bc1 --- /dev/null +++ b/.github/upload-preview.js @@ -0,0 +1,97 @@ +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 || ''; + +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 uploadFolder = process.argv[2]; +if (!uploadFolder) { + console.log('Usage: upload-preview uploadFolder'); + process.exit(1); +} + +const uploadFolderName = path.basename(uploadFolder); +let uploadURL = `${repo}-${prnum ? `pr-catalog-view-${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; +} + +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; + } + 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 (uploadFolderName === 'public') { + commentBody += tryAddComment(`Preview: https://${uploadURL}`, commentBody); + } + else if (uploadFolderName === 'coverage') { + commentBody += tryAddComment(`A11y report: https://${uploadURL}`, commentBody); + } + + 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 diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 0000000..924dc89 --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -0,0 +1,58 @@ +name: pr-preview +on: pull_request_target +jobs: + build-upload: + runs-on: ubuntu-latest + env: + SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }} + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + GH_PR_TOKEN: ${{ secrets.GH_PR_TOKEN }} + GH_PR_NUM: ${{ github.event.number }} + steps: + - uses: actions/checkout@v2 + # Yes, we really want to checkout the PR + - run: | + git fetch origin pull/$GH_PR_NUM/head:tmp + git checkout tmp + - run: | + git rev-parse origin/main + git rev-parse HEAD + git rev-parse origin/main..HEAD + git log origin/main..HEAD --format="%b" + # Yes, we really want to checkout the PR + # Injected by generate-workflows.js + - uses: actions/setup-node@v1 + with: + node-version: '18' + - uses: actions/cache@v2 + id: yarn-cache + name: Load npm deps from cache + with: + path: '**/node_modules' + key: ${{ runner.os }}-yarn-14-${{ hashFiles('yarn.lock') }} + - run: yarn install --frozen-lockfile + if: steps.yarn-cache.outputs.cache-hit != 'true' + - run: yarn lint:js + name: Lint JS + if: always() + - run: yarn lint:md + name: Lint MD + if: always() + - run: yarn build + name: Build component groups + - uses: actions/cache@v2 + id: docs-cache + name: Load webpack cache + with: + path: '.cache' + key: ${{ runner.os }}-v4-${{ hashFiles('yarn.lock') }} + - run: yarn build:docs + name: Build docs + - run: node .github/upload-preview.js public + name: Upload docs + if: always() + - run: yarn serve:docs & yarn test:a11y + name: a11y tests + - run: node .github/upload-preview.js coverage + name: Upload a11y report + if: always() \ No newline at end of file