Merge pull request #2 from jetbrains-eval-lab/add-workflow-pr-label-m… #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Run Tests | |
on: | |
push: | |
branches: [ "main", "scenario/*", "eval/*", "feature/*" ] | |
pull_request: | |
branches: [ "main", "scenario/*", "eval/*", "feature/*" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
issues: write # so we can create & edit comments | |
contents: read | |
steps: | |
# ──────────── 1. checkout ──────────── | |
- uses: actions/checkout@v4 | |
# ──────────── 2. post placeholder comment (EARLY) ──────────── | |
- name: Create placeholder issue comment | |
id: create_comment | |
uses: actions/github-script@v7 | |
env: | |
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
result-encoding: string | |
script: | | |
const issuePat = /#(\d+)/g; | |
let issueNum = null, m; | |
// • PR context | |
if (context.payload.pull_request) { | |
const whole = `${context.payload.pull_request.title}\n${context.payload.pull_request.body}`; | |
if ((m = issuePat.exec(whole)) !== null) issueNum = +m[1]; | |
} | |
// • Push context | |
if (!issueNum && context.payload.commits) { | |
for (const c of context.payload.commits) { | |
if ((m = issuePat.exec(c.message)) !== null) { issueNum = +m[1]; break; } | |
} | |
} | |
if (!issueNum) { core.info('No #issue reference found.'); return; } | |
const body = `⏳ **[${process.env.GITHUB_WORKFLOW}](${process.env.RUN_URL})** has **started**…`; | |
const { data: comment } = await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issueNum, | |
body | |
}); | |
core.setOutput('comment_id', comment.id.toString()); | |
# ──────────── 3. Java / Maven setup ──────────── | |
- name: Set up Java | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 24 | |
distribution: temurin | |
cache: maven | |
# ──────────── 4. compile ──────────── | |
- run: mvn -B compile --file pom.xml | |
# ──────────── 5. extract FAIL_TO_PASS / PASS_TO_PASS ──────────── | |
- name: Extract test names | |
id: extract_tests | |
uses: actions/github-script@v7 | |
with: | |
result-encoding: string | |
script: | | |
const grab = (txt, re) => [...txt.matchAll(re)].flatMap(m => m[1].split(/[ ,]+/)); | |
const uniq = a => [...new Set(a.filter(Boolean))]; | |
let blocks = []; | |
if (context.eventName === 'pull_request') { | |
blocks = [`${context.payload.pull_request.title}\n${context.payload.pull_request.body}`]; | |
} else if (context.eventName === 'push') { | |
blocks = context.payload.commits.map(c => c.message); | |
} | |
const fail = blocks.flatMap(b => grab(b, /FAIL_TO_PASS:\s*([^\n]+)/gi)); | |
const pass = blocks.flatMap(b => grab(b, /PASS_TO_PASS:\s*([^\n]+)/gi)); | |
const tests = uniq([...fail, ...pass]).join(','); | |
core.setOutput('tests', tests); | |
# ──────────── 6. run tests ──────────── | |
- name: Run selected tests | |
if: ${{ steps.extract_tests.outputs.tests }} | |
run: mvn -B -Dtest="${{ steps.extract_tests.outputs.tests }}" test | |
- name: Run all tests | |
if: ${{ steps.extract_tests.outputs.tests == '' }} | |
run: mvn -B test --file pom.xml | |
# ──────────── 7. update the same comment (FINAL) ──────────── | |
- name: Update issue comment with final status | |
if: always() | |
uses: actions/github-script@v7 | |
env: | |
COMMENT_ID: ${{ steps.create_comment.outputs.comment_id }} | |
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
JOB_STATUS: ${{ job.status }} | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
if (!process.env.COMMENT_ID) { | |
core.info('No comment to update.'); return; | |
} | |
const statusEmoji = { | |
success: '✅', | |
failure: '❌', | |
cancelled: '🟡' | |
}[process.env.JOB_STATUS] || '🟡'; | |
const body = `${statusEmoji} **[${process.env.GITHUB_WORKFLOW}](${process.env.RUN_URL})** finished with status **${process.env.JOB_STATUS.toUpperCase()}**.`; | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: Number(process.env.COMMENT_ID), | |
body | |
}); |