Skip to content

Commit

Permalink
test: Fix condition to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Sep 20, 2024
1 parent 0c1df57 commit 0b9ea94
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 40 deletions.
18 changes: 18 additions & 0 deletions .github/actions/update-check-run/action.yml
Original file line number Diff line number Diff line change
@@ -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'
85 changes: 85 additions & 0 deletions .github/actions/update-check-run/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
65 changes: 25 additions & 40 deletions .github/workflows/run-cctp-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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}).`
}
});
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 }}

0 comments on commit 0b9ea94

Please sign in to comment.