From 4a6dc86a60608e507a7df0385801ea768321189e Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Tue, 18 Jun 2024 09:39:50 +0530 Subject: [PATCH] ci: Update PR description with GitHub Script (#34292) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR switches the steps updating PR description to a local Javascript script, run using `actions/github-script`, instead of depending on an external third-party action. Why? Besides reducing our dependency on third-party actions, which have been found to break with surprises, we need this to fix a bug with the `/test` command. The `/test` command, when is written with invalid tags, reports the error in the workflow run, but doesn't update the PR description. Because of how it's written in Javascript, we need a Javascript way to update the description to fix that. This will provide for that. Further PRs will replace other uses of PR description update steps with this. **/test sanity** > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: > Commit: 00d06858f6f7d14c6393cb16c894ddfd808b7cf8 > Cypress dashboard. > Tags: `@tag.Sanity` ## Summary by CodeRabbit - **Chores** - Updated GitHub Actions workflow to enhance pull request automation with Cypress test results. - Improved PR body updates with detailed test outcomes. --- .github/workflows/pr-automation.yml | 27 ++++++------- .../scripts/write-cypress-comment.js | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/scripts/write-cypress-comment.js diff --git a/.github/workflows/pr-automation.yml b/.github/workflows/pr-automation.yml index aaacb5c7a1..1475cf7a9e 100644 --- a/.github/workflows/pr-automation.yml +++ b/.github/workflows/pr-automation.yml @@ -171,22 +171,19 @@ jobs: # In case of a runnable command, update the PR with run details - name: Add test response with link to workflow run - uses: nefrob/pr-description@v1.1.2 + uses: actions/github-script@v7 + env: + NODE_PATH: "${{ github.workspace }}/.github/workflows/scripts" + BODY: | + > [!IMPORTANT] + > 🟣 🟣 🟣 Your tests are running. + > Tests running at: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}> + > Commit: ${{ github.event.pull_request.head.sha }} + > Workflow: `${{ github.workflow }}` + > Tags: `${{ steps.checkAll.outputs.tags }}` with: - content: | - - > [!IMPORTANT] - > 🟣 🟣 🟣 Your tests are running. - > Tests running at: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}> - > Commit: ${{ github.event.pull_request.head.sha }} - > Workflow: `${{ github.workflow }}` - > Tags: `${{ steps.checkAll.outputs.tags }}` - - - - regex: ".*?" - regexFlags: ims - token: ${{ secrets.GITHUB_TOKEN }} + script: | + require("write-cypress-comment.js")({core, context, github}, process.env.BODY) # Call the workflow to run Cypress tests perform-test: diff --git a/.github/workflows/scripts/write-cypress-comment.js b/.github/workflows/scripts/write-cypress-comment.js new file mode 100644 index 0000000000..cf27fc895f --- /dev/null +++ b/.github/workflows/scripts/write-cypress-comment.js @@ -0,0 +1,39 @@ +const HEADER = ''; +const FOOTER = ''; +const PATTERN = new RegExp(HEADER + ".*?" + FOOTER, "ims"); + +module.exports = async function({core, context, github}, note) { + const prNumber = context.payload.pull_request?.number; + + if (!prNumber) { + core.setFailed( + `No open pull request found for ${context.eventName}, ${context.sha}`, + ); + return; + } + + const response = await github.rest.pulls.get({ + ...context.repo, + pull_number: prNumber, + }); + + let body = response?.data?.body; + if (!body) { + core.setFailed(JSON.stringify(response, null, 2)); + return; + } + + note = [HEADER, note, FOOTER].join("\n"); + + if (body.match(PATTERN)) { + body = body.replace(PATTERN, note); + } else { + body += "\n\n" + note + "\n"; + } + + await github.rest.pulls.update({ + ...context.repo, + pull_number: prNumber, + body, + }); +}