Skip to content

Close Test Issues

Close Test Issues #7

name: Close Test Issues
on:
workflow_run:
workflows: ["Tests"]
types:
- completed
permissions:
issues: write
contents: read
jobs:
close-test-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get test results
id: test-results
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11
with:
workflow: tests.yml
run_id: ${{ github.event.workflow_run.id }}
name: pytest-results-3.11
path: test-results-3.11
continue-on-error: true
- name: Get test results (Python 3.12)
id: test-results-3-12
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11
with:
workflow: tests.yml
run_id: ${{ github.event.workflow_run.id }}
name: pytest-results-3.12
path: test-results-3.12
continue-on-error: true
- name: Get test results (Python 3.13)
id: test-results-3-13
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11
with:
workflow: tests.yml
run_id: ${{ github.event.workflow_run.id }}
name: pytest-results-3.13
path: test-results-3.13
continue-on-error: true
- name: Process test results
if: steps.test-results.outcome == 'success' || steps.test-results-3-12.outcome == 'success' || steps.test-results-3-13.outcome == 'success'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
try {
// Process results from all Python versions
const versions = ['3.11', '3.12', '3.13'];
const allTests = new Set();
const testOutcomes = new Map(); // Track outcomes per test across versions
for (const version of versions) {
const resultsPath = path.join(process.env.GITHUB_WORKSPACE, `test-results-${version}`, 'results.json');
if (!fs.existsSync(resultsPath)) {
console.log(`No test results found for Python ${version}`);
continue;
}
// Read test results
const results = JSON.parse(fs.readFileSync(resultsPath, 'utf8'));
if (!results.tests || !Array.isArray(results.tests)) {
console.log(`Invalid test results format for Python ${version}`);
continue;
}
// Process each test
for (const test of results.tests) {
const shortTestName = test.nodeid.split('::').pop();
allTests.add(shortTestName);
// Track test outcome
if (!testOutcomes.has(shortTestName)) {
testOutcomes.set(shortTestName, { passed: false, exists: true });
}
// Update outcome - a test passes if it passes in any version
if (test.outcome === 'passed') {
testOutcomes.get(shortTestName).passed = true;
}
}
}
// Get open test failure issues
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: ['tests']
});
// Close issues for tests that are now passing or no longer exist
for (const issue of issues.data) {
// Extract test name from issue title
const match = issue.title.match(/^Test Failure: (.+)$/);
if (!match) continue;
const shortTestName = match[1];
const testOutcome = testOutcomes.get(shortTestName);
// Close if test is passing in any version or if test no longer exists
if (!testOutcome || testOutcome.passed) {
const closeReason = !testOutcome ? 'Test was removed' : 'Test passed';
// Add comment first
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: `${closeReason} in commit ${context.sha}`
});
// Then close the issue
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
});
}
}
} catch (error) {
console.error('Error processing test results:', error);
core.setFailed(error.message);
}