|
| 1 | +module.exports = async ({ github, context, core }) => { |
| 2 | + const path = require("path"); |
| 3 | + const owners = require( |
| 4 | + path.join(process.env.GITHUB_WORKSPACE, ".github", "owners.json"), |
| 5 | + ); |
| 6 | + |
| 7 | + const pullNumber = context.payload.pull_request?.number; |
| 8 | + const listFiles = await github.rest.pulls.listFiles({ |
| 9 | + owner: context.repo.owner, |
| 10 | + repo: context.repo.repo, |
| 11 | + pull_number: pullNumber, |
| 12 | + }); |
| 13 | + |
| 14 | + const modifiedModules = new Set(); |
| 15 | + for (const file of listFiles.data) { |
| 16 | + const filename = file.filename; |
| 17 | + if (filename.startsWith("modules/")) { |
| 18 | + modifiedModules.add(filename.split("/")[1]); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + if (modifiedModules.size === 0) { |
| 23 | + core.info("No modules were modified"); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const commentParts = [ |
| 28 | + "<!-- owners-notification-bot -->", |
| 29 | + "A review is required from the following owners:", |
| 30 | + ]; |
| 31 | + for (const modifiedModule of modifiedModules) { |
| 32 | + if (owners[modifiedModule]) { |
| 33 | + commentParts.push( |
| 34 | + `- ${modifiedModule}: ${owners[modifiedModule].join(", ")}`, |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (commentParts.length === 2) { |
| 40 | + core.info("Modules were modified, but no owners were found"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const listComments = await github.rest.issues.listComments({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + issue_number: pullNumber, |
| 48 | + }); |
| 49 | + const comment = listComments.data.find( |
| 50 | + (comment) => |
| 51 | + comment.user.login === "github-actions[bot]" && |
| 52 | + comment.body.startsWith(commentParts[0]), |
| 53 | + ); |
| 54 | + if (comment) { |
| 55 | + core.info("Updating existing comment"); |
| 56 | + await github.rest.issues.updateComment({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + comment_id: comment.id, |
| 60 | + body: commentParts.join("\n"), |
| 61 | + }); |
| 62 | + } else { |
| 63 | + core.info("Creating new comment"); |
| 64 | + await github.rest.issues.createComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: pullNumber, |
| 68 | + body: commentParts.join("\n"), |
| 69 | + }); |
| 70 | + } |
| 71 | +}; |
0 commit comments