From 0b9ea94f891512984652f864331f9c8032be77c9 Mon Sep 17 00:00:00 2001 From: Christophe Date: Fri, 20 Sep 2024 15:39:01 +0000 Subject: [PATCH] test: Fix condition to run tests --- .github/actions/update-check-run/action.yml | 18 +++++ .github/actions/update-check-run/index.js | 85 +++++++++++++++++++++ .github/workflows/run-cctp-tests.yml | 65 ++++++---------- 3 files changed, 128 insertions(+), 40 deletions(-) create mode 100644 .github/actions/update-check-run/action.yml create mode 100644 .github/actions/update-check-run/index.js diff --git a/.github/actions/update-check-run/action.yml b/.github/actions/update-check-run/action.yml new file mode 100644 index 0000000000..409e309fe7 --- /dev/null +++ b/.github/actions/update-check-run/action.yml @@ -0,0 +1,18 @@ +name: 'Update Check Run' +description: 'Create or update a check run for CCTP E2E Tests' +inputs: + github-token: + description: 'GitHub token' + required: true + status: + description: 'Status of the check run (in_progress or completed)' + required: true + conclusion: + description: 'Conclusion of the check run (success, failure, etc.)' + required: false + details-url: + description: 'URL for more details' + required: false +runs: + using: 'node20' + main: 'index.js' \ No newline at end of file diff --git a/.github/actions/update-check-run/index.js b/.github/actions/update-check-run/index.js new file mode 100644 index 0000000000..74412ffe62 --- /dev/null +++ b/.github/actions/update-check-run/index.js @@ -0,0 +1,85 @@ +const core = require("@actions/core"); +const github = require("@actions/github"); + +module.exports = async ({ github, context, core }, inputs) => { + try { + const token = inputs.token; + const status = inputs.status; + const conclusion = inputs.conclusion; + const detailsUrl = inputs["details-url"]; + + const octokit = github.getOctokit(token); + const { owner, repo } = context.repo; + + let pull_number, head_sha; + + if (context.eventName === "issue_comment") { + pull_number = context.issue.number; + } else if (context.eventName === "pull_request_review") { + pull_number = context.payload.pull_request.number; + } else if ( + context.eventName === "push" || + context.eventName === "merge_group" + ) { + head_sha = context.sha; + } else { + core.setFailed("Unexpected event type"); + return; + } + + if (!head_sha) { + const { data: pr } = await octokit.rest.pulls.get({ + owner, + repo, + pull_number, + }); + head_sha = pr.head.sha; + } + + const checkName = "CCTP E2E Tests"; + + // Check if a check run already exists + const { data: existingChecks } = await octokit.rest.checks.listForRef({ + owner, + repo, + ref: head_sha, + check_name: checkName, + }); + + const checkRunData = { + owner, + repo, + head_sha, + name: checkName, + status, + conclusion: status === "completed" ? conclusion : undefined, + output: { + title: + status === "in_progress" + ? "CCTP E2E Tests In Progress" + : "CCTP E2E Tests Result", + summary: + status === "in_progress" + ? "The CCTP E2E tests have been triggered and are now running." + : `The CCTP E2E tests have completed with status: ${conclusion}.`, + text: + status === "in_progress" + ? "Tests are currently in progress. Results will be updated upon completion." + : `For detailed information, please check the [workflow run](${detailsUrl}).`, + }, + }; + + if (existingChecks.check_runs.length > 0) { + // Update existing check run + await octokit.rest.checks.update({ + check_run_id: existingChecks.check_runs[0].id, + ...checkRunData, + }); + } else { + // Create new check run + await octokit.rest.checks.create(checkRunData); + } + } catch (error) { + core.setFailed(error.message); + } +}; diff --git a/.github/workflows/run-cctp-tests.yml b/.github/workflows/run-cctp-tests.yml index 587280a5b1..b5c7a2b958 100644 --- a/.github/workflows/run-cctp-tests.yml +++ b/.github/workflows/run-cctp-tests.yml @@ -43,7 +43,7 @@ jobs: id: should-run-tests run: | if [[ "${{ github.event_name }}" == "issue_comment" ]]; then - if [[ "${{ contains(fromJson('["OWNER", "MEMBER"]'), github.event.comment.author_association) }}" == "false" ]]; then + if [[ "${{ contains(fromJson('["CONTRIBUTOR", "OWNER", "MEMBER"]'), github.event.comment.author_association) }}" == "false" ]]; then echo "should_run=false" >> $GITHUB_OUTPUT elif [[ "${{ github.event.comment.body }}" == "/run-cctp-tests" && "${{ github.event.issue.pull_request }}" != "" ]]; then echo "should_run=true" >> $GITHUB_OUTPUT @@ -60,6 +60,22 @@ jobs: echo "should_run=false" >> $GITHUB_OUTPUT fi + set-check-in-progress: + runs-on: ubuntu-latest + if: >- + github.event_name == 'issue_comment' && + github.event.comment.body == '/run-cctp-tests' && + contains(fromJson('["CONTRIBUTOR", "OWNER", "MEMBER"]'), github.event.comment.author_association) + steps: + - name: Set check run to in-progress + uses: actions/github-script@v7 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const script = require('./.github/actions/update-check-run/index.js') + await script({github, context, core}) + result-encoding: string + status: 'in_progress' build: name: "Build" @@ -94,49 +110,18 @@ jobs: update-pr-status: name: "Update PR Status" - needs: [cctp-e2e-tests] + needs: [cctp-e2e-tests, set-check-in-progress] runs-on: ubuntu-latest if: always() steps: - - name: Create check run + - name: Update check run with final status uses: actions/github-script@v7 with: github-token: ${{secrets.GITHUB_TOKEN}} script: | - const { owner, repo } = context.repo; - let pull_number; - - if ('${{ github.event_name }}' === 'issue_comment') { - pull_number = context.issue.number; - } else if ('${{ github.event_name }}' === 'pull_request_review') { - pull_number = context.payload.pull_request.number; - } else { - console.log('Unexpected event type'); - return; - } - - // Fetch the PR data to get the latest SHA - const { data: pr } = await github.rest.pulls.get({ - owner, - repo, - pull_number: pull_number, - }); - - const head_sha = pr.head.sha; - - // Construct the URL for this workflow run - const workflowUrl = `https://github.com/${owner}/${repo}/actions/runs/${context.runId}`; - - await github.rest.checks.create({ - owner, - repo, - name: 'CCTP Tests', - head_sha: head_sha, - status: 'completed', - conclusion: '${{ needs.cctp-e2e-tests.result }}', - output: { - title: 'CCTP Tests Result', - summary: `The CCTP tests have completed with status: ${{ needs.cctp-e2e-tests.result }}.`, - text: `For detailed information, please check the [workflow run](${workflowUrl}).` - } - }); \ No newline at end of file + const script = require('./.github/actions/update-check-run/index.js') + await script({github, context, core}) + result-encoding: string + status: 'completed' + conclusion: ${{ needs.cctp-e2e-tests.result }} + details-url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \ No newline at end of file