Skip to content

Commit 321abff

Browse files
authored
config(toolkits): check for presence of test files in PRs (aws#5398)
## Problem Tests aren't being written ## Solution Add an action that checks for the presence of test files in a PR. If there are no tests then create a comment on the PR
1 parent 0987b1d commit 321abff

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

.github/workflows/node.js.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ jobs:
3232
- uses: actions/setup-node@v4
3333
with:
3434
node-version: '20'
35+
- name: Check for tests
36+
uses: actions/github-script@v7
37+
with:
38+
script: |
39+
const notify = require('.github/workflows/notify.js')
40+
await notify({github, context})
3541
- name: Check PR title
3642
run: |
3743
node "$GITHUB_WORKSPACE/.github/workflows/lintcommit.js"

.github/workflows/notify.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
const needsTestFiles =
7+
'This pull request modifies files in src/ but no tests were added/updated. Confirm whether tests should be added or ensure the PR description explains why tests are not required.'
8+
9+
/**
10+
* Remind partner teams that tests are required. We don't need to remind them if:
11+
* 1. They did not change anything in a src directory
12+
* 2. They already have test files in the PR
13+
* 3. We've already told them in a previous PR comment
14+
*/
15+
module.exports = async ({ github, context }) => {
16+
const owner = context.repo.owner
17+
const repo = context.repo.repo
18+
19+
const response = await github.rest.repos.compareCommitsWithBasehead({
20+
owner,
21+
repo,
22+
basehead: `${context.payload.pull_request.base.ref}...${context.payload.pull_request.head.ref}`,
23+
})
24+
25+
const filenames = response.data.files.map((file) => file.filename)
26+
27+
// Check if src directory changed
28+
const srcFiles = filenames.filter((file) => file.includes('src/'))
29+
if (srcFiles.length === 0) {
30+
console.log('Did not find src files in the code changes')
31+
return
32+
}
33+
34+
// Check if test files were added or modified
35+
const testFiles = filenames.filter((file) => file.endsWith('.test.ts'))
36+
if (testFiles.length > 0) {
37+
console.log('Found test files in the code changes')
38+
return
39+
}
40+
41+
// Check for prior comments on the PR
42+
const comments = await github.rest.issues.listComments({
43+
owner,
44+
repo,
45+
issue_number: context.payload.pull_request.number,
46+
})
47+
48+
if (comments.data.some((comment) => comment.body.includes(needsTestFiles))) {
49+
console.log('Found prior comment indicating tests are needed')
50+
return
51+
}
52+
53+
await github.rest.issues.createComment({
54+
issue_number: context.issue.number,
55+
owner,
56+
repo,
57+
body: needsTestFiles,
58+
})
59+
}

0 commit comments

Comments
 (0)