Skip to content

Commit 8adcde5

Browse files
Refactor auto-reply workflow for issues and PRs
1 parent 9a128a1 commit 8adcde5

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

.github/workflows/auto-reply.yml

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ name: 🤖 Auto Reply to Issues & PRs
33
on:
44
issues:
55
types: [opened, reopened]
6-
# Use pull_request_target so this job can write comments/labels on PRs from forks.
7-
# SECURITY: Do NOT check out or execute untrusted PR code in this workflow.
86
pull_request_target:
9-
types: [opened, reopened]
7+
types: [opened, reopened, synchronize]
108

119
permissions:
1210
issues: write
@@ -23,82 +21,105 @@ jobs:
2321
script: |
2422
const repo = context.repo;
2523
26-
// Issue opened/reopened
27-
if (context.payload.issue) {
28-
const issue = context.payload.issue;
29-
const user = issue.user?.login || 'contributor';
30-
const issueNumber = issue.number;
31-
const message = `👋 Hi @${user}!\n\nThanks for opening an issue in **MyCMD**.\n\nWe’ll review it soon — please ensure reproduction steps and logs are included.`;
32-
33-
await github.rest.issues.createComment({
24+
async function commentExists(issue_number, messageSnippet) {
25+
const comments = await github.rest.issues.listComments({
3426
owner: repo.owner,
3527
repo: repo.repo,
36-
issue_number: issueNumber,
37-
body: message
28+
issue_number
3829
});
30+
return comments.data.some(c => c.body.includes(messageSnippet));
31+
}
3932
40-
await github.rest.reactions.createForIssue({
33+
async function addReaction(issue_number, reaction) {
34+
const reactions = await github.rest.reactions.listForIssue({
4135
owner: repo.owner,
4236
repo: repo.repo,
43-
issue_number: issueNumber,
44-
content: "tada"
37+
issue_number
4538
});
39+
if (!reactions.data.some(r => r.content === reaction)) {
40+
await github.rest.reactions.createForIssue({
41+
owner: repo.owner,
42+
repo: repo.repo,
43+
issue_number,
44+
content: reaction
45+
});
46+
}
47+
}
4648
49+
async function addLabels(issue_number, labels) {
4750
try {
4851
await github.rest.issues.addLabels({
4952
owner: repo.owner,
5053
repo: repo.repo,
51-
issue_number: issueNumber,
52-
labels: ["triage", "needs-info"]
54+
issue_number,
55+
labels
5356
});
5457
} catch (err) {
55-
core && core.info && core.info("Could not add labels to issue: " + err.message);
58+
core?.info?.("Could not add labels: " + err.message);
5659
}
57-
58-
return;
5960
}
6061
61-
// Pull request opened/reopened (or synchronized)
62+
// Handle Pull Requests
6263
if (context.payload.pull_request) {
6364
const pr = context.payload.pull_request;
6465
const user = pr.user?.login || 'contributor';
6566
const prNumber = pr.number;
6667
67-
const message = `🚀 Hi @${user}!\n\nThank you for contributing to **MyCMD**. A maintainer will review your PR shortly. 🎉`;
68+
const message = `🚀 Hi @${user}!
6869
69-
await github.rest.issues.createComment({
70-
owner: repo.owner,
71-
repo: repo.repo,
72-
issue_number: prNumber,
73-
body: message
74-
});
70+
Thank you for contributing to **MyCMD**. A maintainer will review your PR shortly. 🎉
7571

76-
await github.rest.reactions.createForIssue({
77-
owner: repo.owner,
78-
repo: repo.repo,
79-
issue_number: prNumber,
80-
content: "rocket"
81-
});
72+
### Thank you for raising this issue!
73+
We'll review it as soon as possible. We truly appreciate your contributions! ✨ Meanwhile make sure you've visited the README.md, CONTRIBUTING.md, and CODE_OF_CONDUCT.md before creating a PR for this. Also, please do NOT create a PR until this issue has been assigned to you. 😊`;
74+
75+
const gif = "![TrustTheProcess](https://media0.giphy.com/media/v1.Y2lkPTc5MGI3NjExbWtoOWpsMnozdXd0MmJxejhiNGwwdjltY3dyNW80NHg2Ym01YTdlMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/mPKa6OI5oRsmextwBq/giphy.gif)";
76+
77+
if (!(await commentExists(prNumber, "Thank you for contributing to **MyCMD**"))) {
78+
await github.rest.issues.createComment({
79+
owner: repo.owner,
80+
repo: repo.repo,
81+
issue_number: prNumber,
82+
body: message + "\n\n" + gif
83+
});
84+
}
85+
86+
await addReaction(prNumber, "rocket");
8287

83-
const headRepoFull = pr.head && pr.head.repo && pr.head.repo.full_name ? pr.head.repo.full_name : '';
88+
const headRepoFull = pr.head?.repo?.full_name || '';
8489
const baseFull = `${repo.owner}/${repo.repo}`;
8590
const isFork = headRepoFull.toLowerCase() !== baseFull.toLowerCase();
86-
8791
const labels = ["needs-review"];
8892
if (isFork) labels.push("from-fork");
8993

90-
try {
91-
await github.rest.issues.addLabels({
94+
await addLabels(prNumber, labels);
95+
96+
return;
97+
}
98+
99+
// Handle Issues
100+
if (context.payload.issue) {
101+
const issue = context.payload.issue;
102+
const user = issue.user?.login || 'contributor';
103+
const issueNumber = issue.number;
104+
105+
const message = `👋 Hi @${user}!
106+
107+
Thanks for opening an issue in **MyCMD**. We’ll review it soon — please ensure reproduction steps and logs are included.`;
108+
109+
if (!(await commentExists(issueNumber, "Thanks for opening an issue in **MyCMD**"))) {
110+
await github.rest.issues.createComment({
92111
owner: repo.owner,
93112
repo: repo.repo,
94-
issue_number: prNumber,
95-
labels: labels
113+
issue_number: issueNumber,
114+
body: message
96115
});
97-
} catch (err) {
98-
core && core.info && core.info("Could not add labels to PR: " + err.message);
99116
}
100117

118+
await addReaction(issueNumber, "tada");
119+
120+
await addLabels(issueNumber, ["triage", "needs-info"]);
121+
101122
return;
102123
}
103124

104-
core && core.info && core.info('No issue or pull_request payload found. Nothing to do.');
125+
core?.info?.('No issue or pull_request payload found. Nothing to do.');

0 commit comments

Comments
 (0)