Skip to content

Commit f0c32e9

Browse files
committed
Add 'Notify Owners' workflow
1 parent fd4b919 commit f0c32e9

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

.github/owners.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nss": ["@dennisjackson"]
3+
}

.github/scripts/owners.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
};

.github/workflows/owners.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Notify Owners
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
pull-requests: write
9+
contents: read
10+
11+
jobs:
12+
notify-owners:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Notify Owners
19+
uses: actions/github-script@v7
20+
with:
21+
script: |
22+
const script = require('./.github/scripts/owners.js');
23+
await script({ github, context, core });

0 commit comments

Comments
 (0)