Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix condition to run tests #1919

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
81 changes: 81 additions & 0 deletions .github/actions/update-check-run/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const core = require("@actions/core");
const github = require("@actions/github");

async function run() {
try {
const token = core.getInput("github-token", { required: true });
const status = core.getInput("status", { required: true });
const conclusion = core.getInput("conclusion");
const detailsUrl = core.getInput("details-url");
const checkName = "CCTP E2E Tests";

const octokit = github.getOctokit(token);
const context = github.context;
const { owner, repo } = context.repo;

let pull_number, head_sha;

if (context.eventName === "issue_comment") {
pull_number = context.issue.number;
const { data: pr } = await octokit.rest.pulls.get({
owner,
repo,
pull_number,
});
head_sha = pr.head.sha;
} else if (context.eventName === "pull_request_review") {
pull_number = context.payload.pull_request.number;
head_sha = context.payload.pull_request.head.sha;
} else {
core.setFailed("Unexpected event type");
return;
}

// Get all check runs for the current SHA
const { data: allCheckRuns } = 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}).`,
},
details_url: detailsUrl,
};

if (allCheckRuns[0]) {
// Update existing check run
await octokit.rest.checks.update({
check_run_id: allCheckRuns[0].id,
...checkRunData,
});
} else {
// Create new check run
await octokit.rest.checks.create(checkRunData);
}
} catch (error) {
core.setFailed(error.message);
}
}

run();
65 changes: 26 additions & 39 deletions .github/workflows/run-cctp-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
initialize-check:
name: Initialize Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set check 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}, {
token: process.env.GITHUB_TOKEN,
status: 'in_progress',
'details-url': 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
})

check-files:
name: Check files
runs-on: ubuntu-latest
Expand All @@ -43,7 +61,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,7 +78,6 @@ jobs:
echo "should_run=false" >> $GITHUB_OUTPUT
fi


build:
name: "Build"
runs-on: ubuntu-latest
Expand Down Expand Up @@ -94,49 +111,19 @@ jobs:

update-pr-status:
name: "Update PR Status"
needs: [cctp-e2e-tests]
needs: [cctp-e2e-tests, initialize-check]
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,
const script = require('./.github/actions/update-check-run/index.js')
await script({github, context, core}, {
token: process.env.GITHUB_TOKEN,
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}).`
}
});
'details-url': 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
})
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@
],
"devDependencies": {
"audit-ci": "^6.3.0"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
}
}
Loading
Loading