|
| 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