Skip to content

Commit 24c69d5

Browse files
authored
Merge pull request #103 from learningequality/community-pr-linked-issue-handling
Act on the issue a community pull request closes
2 parents 324bbc4 + 90f0bd1 commit 24c69d5

10 files changed

Lines changed: 503 additions & 9 deletions

‎.github/workflows/contributor-pr-reply.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
const script = require('./scripts/contributor-pr-reply.js');
5555
return await script({ github, context, core });
5656
- name: Send Slack notification
57-
if: ${{ steps.script.outputs.slack_notification != ''}}
57+
if: ${{ !cancelled() && steps.script.outputs.slack_notification != '' }}
5858
uses: slackapi/slack-github-action@v4.0.0
5959
with:
6060
webhook-type: incoming-webhook

‎docs/community-automations.md‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,22 @@ In `scripts/constants.js` set:
6161

6262
# `contributor-pr-reply`
6363

64-
Sends reply to a community pull requests.
64+
Sends reply to a community pull request, and acts on the issue that pull request closes.
6565

66-
In `scripts/contants.js` set:
66+
What happens depends on who the linked issue is assigned to:
67+
68+
- assigned to someone else: posts a closing message instead of the reply, closes the pull request, and notifies Slack
69+
- assigned to the author: sends the reply, then requests `rtibblesbot` for review and adds the `community-review` label
70+
- not assigned, or no issue linked: sends the reply
71+
72+
An issue counts as linked only when the description closes it, for example `Fixes #123`. Assignments on any other issue are ignored. A pull request with no linked issue is asked to add one under `## References`.
73+
74+
If the author is assigned to any linked issue, the pull request is not closed.
75+
76+
In `scripts/constants.js` set:
6777
- `BOT_MESSAGE_PULL_REQUEST`: Message text
78+
- `BOT_MESSAGE_LINK_ISSUE`: Text added when no issue is linked
79+
- `BOT_MESSAGE_PULL_REQUEST_CLOSED`: Closing message text
6880

6981
# `holiday-message`
7082

‎scripts/constants.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ const BOT_MESSAGE_KEYWORD_GOOD_FIRST_ISSUE =
141141
` to learn about the contributing process. 😊${GSOC_NOTE}`;
142142

143143
const BOT_MESSAGE_PULL_REQUEST = author =>
144-
`👋 Hi @${author}, thanks for contributing! \n\n **For the review process to begin, please verify that the following is satisfied:**\n\n- [ ] **Contribution is aligned with our [contributing guidelines](https://learningequality.org/contributing-to-our-open-code-base)**\n- [ ] **Pull request description has correctly filled _AI usage_ section & follows our AI guidance:**\n\n <details>\n <summary><b><i>AI guidance</i></b></summary>\n\n <br>\n\n **State explicitly whether you didn't use or used AI & how.**\n\n If you used it, ensure that the PR is aligned with [Using AI](https://learningequality.org/contributing-to-our-open-code-base/#using-generative-ai) as well as our DEEP framework. DEEP asks you:\n\n - **Disclose** — Be open about when you've used AI for support.\n - **Engage critically** — Question what is generated. Review code for correctness and unnecessary complexity.\n - **Edit** — Review and refine AI output. Remove unnecessary code and verify it still works after your edits.\n - **Process sharing** — Explain how you used the AI so others can learn.\n\n <br>\n\n Examples of good disclosures:\n\n > "I used Claude Code to implement the component, prompting it to follow the pattern in ComponentX. I reviewed the generated code, removed unnecessary error handling, and verified the tests pass."\n\n > "I brainstormed the approach with Gemini, then had it write failing tests for the feature. After reviewing the tests, I used Claude Code to generate the implementation. I refactored the output to reduce verbosity and ran the full test suite."\n\n </details>\n\nAlso check that issue requirements are satisfied & you ran \`pre-commit\` locally. \n\n**Pull requests that don't follow the guidelines will be closed.**\n\n**Reviewer assignment can take up to 2 weeks.**`;
144+
`👋 Hi @${author}, thanks for contributing! \n\n **For the review process to begin, please verify that the following is satisfied:**\n\n- [ ] **Contribution is aligned with our [contributing guidelines](https://learningequality.org/contributing-to-our-open-code-base)**\n- [ ] **Pull request description has correctly filled _AI usage_ section & follows our AI guidance:**\n\n <details>\n <summary><b><i>AI guidance</i></b></summary>\n\n <br>\n\n **State explicitly whether you didn't use or used AI & how.**\n\n If you used it, ensure that the PR is aligned with [Using AI](https://learningequality.org/contributing-to-our-open-code-base/#using-generative-ai) as well as our DEEP framework. DEEP asks you:\n\n - **Disclose** — Be open about when you've used AI for support.\n - **Engage critically** — Question what is generated. Review code for correctness and unnecessary complexity.\n - **Edit** — Review and refine AI output. Remove unnecessary code and verify it still works after your edits.\n - **Process sharing** — Explain how you used the AI so others can learn.\n\n <br>\n\n Examples of good disclosures:\n\n > "I used Claude Code to implement the component, prompting it to follow the pattern in ComponentX. I reviewed the generated code, removed unnecessary error handling, and verified the tests pass."\n\n > "I brainstormed the approach with Gemini, then had it write failing tests for the feature. After reviewing the tests, I used Claude Code to generate the implementation. I refactored the output to reduce verbosity and ran the full test suite."\n\n </details>\n\n**📢✨ Before we assign a reviewer:**\n\n- **\`@rtibblesbot\` will pre-review this pull request.** Its comments are generated by an LLM, and should be evaluated accordingly.\n- **We'll also invite community pre-review.** See the [community review guidance](https://github.com/learningequality/.github/blob/main/docs/community-review.md) for both authors and reviewers.\n\nAlso check that issue requirements are satisfied & you ran \`pre-commit\` locally.\n\n**Pull requests that don't follow the guidelines will be closed.**\n\n**Reviewer assignment can take up to 2 weeks.**`;
145+
146+
const BOT_MESSAGE_LINK_ISSUE = `**📎 We couldn't find an issue linked to this pull request.** Please edit the description and link one under **References**, for example \`Fixes #123\` or \`Closes #123\`. A plain \`#123\` mention doesn't link the PR to the issue.`;
147+
148+
const BOT_MESSAGE_PULL_REQUEST_CLOSED = (author, issueUrl) =>
149+
`👋 Hi @${author}, thanks for your interest! \n\n **We're closing this pull request because ${issueUrl} is assigned to someone else.** Visit [Contributing guidelines](https://learningequality.org/contributing-to-our-open-code-base) to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added. \n\n We really appreciate your willingness to help. 😊${GSOC_NOTE}`;
145150

146151
const HOLIDAY_MESSAGE = `Season's greetings! 👋 \n\n We'd like to thank everyone for another year of fruitful collaborations, engaging discussions, and for the continued support of our work. **Learning Equality will be on holidays from December 22 to January 5.** We look forward to much more in the new year and wish you a very happy holiday season!${GSOC_NOTE}`;
147152

@@ -180,6 +185,8 @@ module.exports = {
180185
BOT_MESSAGE_ASSIGN_NOT_GOOD_FIRST_ISSUE,
181186
BOT_MESSAGE_KEYWORD_GOOD_FIRST_ISSUE,
182187
BOT_MESSAGE_PULL_REQUEST,
188+
BOT_MESSAGE_LINK_ISSUE,
189+
BOT_MESSAGE_PULL_REQUEST_CLOSED,
183190
BOT_MESSAGE_RTIBBLESBOT_REVIEW,
184191
RTIBBLESBOT_USERNAME,
185192
LABEL_COMMUNITY_REVIEW,

‎scripts/contributor-pr-reply.js‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
1-
const { BOT_MESSAGE_PULL_REQUEST } = require('./constants');
2-
const { sendBotMessage } = require('./utils');
1+
const {
2+
BOT_MESSAGE_PULL_REQUEST,
3+
BOT_MESSAGE_LINK_ISSUE,
4+
BOT_MESSAGE_PULL_REQUEST_CLOSED,
5+
LABEL_COMMUNITY_REVIEW,
6+
RTIBBLESBOT_USERNAME,
7+
} = require('./constants');
8+
const { sendBotMessage, getLinkedIssues } = require('./utils');
39

410
module.exports = async ({ github, context, core }) => {
511
try {
12+
const owner = context.repo.owner;
613
const repo = context.repo.repo;
714
const number = context.payload.pull_request.number;
815
const url = context.payload.pull_request.html_url;
916
const title = context.payload.pull_request.title;
1017
const author = context.payload.pull_request.user.login;
1118

12-
const botMessageUrl = await sendBotMessage(number, BOT_MESSAGE_PULL_REQUEST(author), {
19+
const lookup = await getLinkedIssues(number, { github, context, core });
20+
const linkedIssues = lookup || [];
21+
const authorAssigned = linkedIssues.some(issue => issue.assignees.includes(author));
22+
const assignedElsewhere = linkedIssues.find(
23+
issue => issue.assignees.length > 0 && !issue.assignees.includes(author),
24+
);
25+
26+
if (!authorAssigned && assignedElsewhere) {
27+
const botMessageUrl = await sendBotMessage(
28+
number,
29+
BOT_MESSAGE_PULL_REQUEST_CLOSED(author, assignedElsewhere.url),
30+
{ github, context, core },
31+
);
32+
try {
33+
await github.rest.pulls.update({
34+
owner,
35+
repo,
36+
pull_number: number,
37+
state: 'closed',
38+
});
39+
core.setOutput(
40+
'slack_notification',
41+
`*[${repo}] <${botMessageUrl}|Closed> pull request on assigned issue <${assignedElsewhere.url}|#${assignedElsewhere.number}>: <${url}|${title}>*`,
42+
);
43+
} catch (error) {
44+
core.setOutput(
45+
'slack_notification',
46+
`*[${repo}] Failed to close pull request <${url}|${title}>, but the closing message was posted*`,
47+
);
48+
core.setFailed(`Failed to close the pull request: ${error.message}`);
49+
}
50+
return;
51+
}
52+
53+
const reply =
54+
lookup !== null && lookup.length === 0
55+
? `${BOT_MESSAGE_PULL_REQUEST(author)}\n\n${BOT_MESSAGE_LINK_ISSUE}`
56+
: BOT_MESSAGE_PULL_REQUEST(author);
57+
const botMessageUrl = await sendBotMessage(number, reply, {
1358
github,
1459
context,
1560
core,
@@ -21,6 +66,25 @@ module.exports = async ({ github, context, core }) => {
2166
} else {
2267
core.setOutput('slack_notification', '');
2368
}
69+
70+
if (authorAssigned) {
71+
try {
72+
await github.rest.pulls.requestReviewers({
73+
owner,
74+
repo,
75+
pull_number: number,
76+
reviewers: [RTIBBLESBOT_USERNAME],
77+
});
78+
await github.rest.issues.addLabels({
79+
owner,
80+
repo,
81+
issue_number: number,
82+
labels: [LABEL_COMMUNITY_REVIEW],
83+
});
84+
} catch (error) {
85+
core.setFailed(`Failed to request review or add the label: ${error.message}`);
86+
}
87+
}
2488
} catch (error) {
2589
core.setOutput('slack_notification', '');
2690
core.setFailed(`Action failed with error: ${error.message}`);
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert/strict');
3+
4+
const contributorPrReply = require('./contributor-pr-reply');
5+
const { LABEL_COMMUNITY_REVIEW, RTIBBLESBOT_USERNAME } = require('./constants');
6+
7+
const AUTHOR = 'outsider';
8+
9+
function fakeContext(author = AUTHOR) {
10+
return {
11+
repo: { owner: 'learningequality', repo: 'kolibri' },
12+
payload: {
13+
pull_request: {
14+
number: 42,
15+
html_url: 'https://github.com/learningequality/kolibri/pull/42',
16+
title: 'Fix it',
17+
user: { login: author },
18+
},
19+
},
20+
};
21+
}
22+
23+
function fakeCore() {
24+
const outputs = {};
25+
const warnings = [];
26+
const failures = [];
27+
return {
28+
outputs,
29+
warnings,
30+
failures,
31+
info() {},
32+
warning(message) {
33+
warnings.push(message);
34+
},
35+
setFailed(message) {
36+
failures.push(message);
37+
},
38+
setOutput(name, value) {
39+
outputs[name] = value;
40+
},
41+
};
42+
}
43+
44+
function fakeGithub(linkedIssues = [], { graphqlError } = {}) {
45+
const calls = { comments: [], updates: [], reviewers: [], labels: [], order: [] };
46+
return {
47+
calls,
48+
graphql: async () => {
49+
if (graphqlError) {
50+
throw new Error(graphqlError);
51+
}
52+
return {
53+
repository: {
54+
pullRequest: {
55+
closingIssuesReferences: {
56+
nodes: linkedIssues.map(issue => ({
57+
number: issue.number,
58+
title: `Issue ${issue.number}`,
59+
url: `https://github.com/learningequality/kolibri/issues/${issue.number}`,
60+
assignees: { nodes: issue.assignees.map(login => ({ login })) },
61+
})),
62+
},
63+
},
64+
},
65+
};
66+
},
67+
rest: {
68+
issues: {
69+
createComment: async ({ issue_number, body }) => {
70+
calls.comments.push({ issue_number, body });
71+
calls.order.push('comment');
72+
return { data: { html_url: 'https://github.com/learningequality/kolibri/pull/42#c1' } };
73+
},
74+
addLabels: async ({ labels }) => {
75+
calls.labels.push(...labels);
76+
calls.order.push('label');
77+
},
78+
},
79+
pulls: {
80+
update: async ({ pull_number, state }) => {
81+
calls.updates.push({ pull_number, state });
82+
calls.order.push('close');
83+
},
84+
requestReviewers: async ({ reviewers }) => {
85+
calls.reviewers.push(...reviewers);
86+
calls.order.push('review');
87+
},
88+
},
89+
},
90+
};
91+
}
92+
93+
async function run(github, context = fakeContext()) {
94+
const core = fakeCore();
95+
await contributorPrReply({ github, context, core });
96+
return core;
97+
}
98+
99+
test('a linked issue assigned to someone else closes the pull request', async () => {
100+
const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]);
101+
const core = await run(github);
102+
103+
assert.deepEqual(github.calls.updates, [{ pull_number: 42, state: 'closed' }]);
104+
assert.equal(github.calls.comments.length, 1);
105+
assert.match(github.calls.comments[0].body, /issues\/7 is assigned to someone else/);
106+
assert.match(core.outputs.slack_notification, /Closed/);
107+
assert.deepEqual(github.calls.reviewers, []);
108+
assert.deepEqual(github.calls.labels, []);
109+
assert.deepEqual(core.failures, []);
110+
});
111+
112+
test('the closing message replaces the standard reply', async () => {
113+
const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]);
114+
await run(github);
115+
116+
assert.doesNotMatch(github.calls.comments[0].body, /For the review process to begin/);
117+
});
118+
119+
test('a failed close still reports to Slack', async () => {
120+
const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]);
121+
github.rest.pulls.update = async () => {
122+
throw new Error('Resource not accessible');
123+
};
124+
const core = await run(github);
125+
126+
assert.equal(github.calls.comments.length, 1);
127+
assert.match(core.outputs.slack_notification, /Failed to close/);
128+
assert.equal(core.failures.length, 1);
129+
});
130+
131+
test('a cross-repo linked issue is named by its url', async () => {
132+
const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]);
133+
const core = await run(github);
134+
135+
assert.match(github.calls.comments[0].body, /https:\/\/github\.com\/.+\/issues\/7/);
136+
assert.match(core.outputs.slack_notification, /issues\/7/);
137+
});
138+
139+
test('a linked issue assigned to the author requests review and adds the label', async () => {
140+
const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]);
141+
const core = await run(github);
142+
143+
assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]);
144+
assert.deepEqual(github.calls.labels, [LABEL_COMMUNITY_REVIEW]);
145+
assert.deepEqual(github.calls.updates, []);
146+
assert.match(github.calls.comments[0].body, /For the review process to begin/);
147+
assert.match(core.outputs.slack_notification, /Reply sent/);
148+
});
149+
150+
test('the reply is sent before the review request and the label', async () => {
151+
const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]);
152+
await run(github);
153+
154+
assert.deepEqual(github.calls.order, ['comment', 'review', 'label']);
155+
});
156+
157+
test('a failed review request still leaves the reply in place', async () => {
158+
const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]);
159+
github.rest.pulls.requestReviewers = async () => {
160+
throw new Error('Reviewer cannot be requested');
161+
};
162+
const core = await run(github);
163+
164+
assert.equal(github.calls.comments.length, 1);
165+
assert.equal(core.failures.length, 1);
166+
assert.match(core.outputs.slack_notification, /Reply sent/);
167+
});
168+
169+
test('the author counts as assigned alongside other assignees', async () => {
170+
const github = fakeGithub([{ number: 7, assignees: ['someone-else', AUTHOR] }]);
171+
await run(github);
172+
173+
assert.deepEqual(github.calls.updates, []);
174+
assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]);
175+
});
176+
177+
test('an issue assigned to the author outweighs another assigned elsewhere', async () => {
178+
const github = fakeGithub([
179+
{ number: 7, assignees: ['someone-else'] },
180+
{ number: 8, assignees: [AUTHOR] },
181+
]);
182+
await run(github);
183+
184+
assert.deepEqual(github.calls.updates, []);
185+
assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]);
186+
});
187+
188+
test('no linked issue leaves the pull request alone', async () => {
189+
const github = fakeGithub([]);
190+
const core = await run(github);
191+
192+
assert.deepEqual(github.calls.updates, []);
193+
assert.deepEqual(github.calls.reviewers, []);
194+
assert.deepEqual(github.calls.labels, []);
195+
assert.equal(github.calls.comments.length, 1);
196+
assert.match(core.outputs.slack_notification, /Reply sent/);
197+
});
198+
199+
test('a linked issue with no assignee leaves the pull request alone', async () => {
200+
const github = fakeGithub([{ number: 7, assignees: [] }]);
201+
await run(github);
202+
203+
assert.deepEqual(github.calls.updates, []);
204+
assert.deepEqual(github.calls.reviewers, []);
205+
assert.deepEqual(github.calls.labels, []);
206+
});
207+
208+
test('an unlinked pull request is asked to link an issue', async () => {
209+
const github = fakeGithub([]);
210+
await run(github);
211+
212+
assert.match(github.calls.comments[0].body, /link one under \*\*References\*\*/);
213+
});
214+
215+
test('a linked pull request is not asked to link an issue', async () => {
216+
const github = fakeGithub([{ number: 7, assignees: [] }]);
217+
await run(github);
218+
219+
assert.doesNotMatch(github.calls.comments[0].body, /link one under/);
220+
});
221+
222+
test('a failed lookup falls back to the standard reply', async () => {
223+
const github = fakeGithub([], { graphqlError: 'API down' });
224+
const core = await run(github);
225+
226+
assert.deepEqual(github.calls.updates, []);
227+
assert.deepEqual(github.calls.reviewers, []);
228+
assert.equal(github.calls.comments.length, 1);
229+
assert.equal(core.warnings.length, 1);
230+
assert.match(core.warnings[0], /API down/);
231+
assert.deepEqual(core.failures, []);
232+
});
233+
234+
test('a failed lookup does not ask the author to link an issue', async () => {
235+
const github = fakeGithub([], { graphqlError: 'API down' });
236+
await run(github);
237+
238+
assert.doesNotMatch(github.calls.comments[0].body, /link one under/);
239+
});

0 commit comments

Comments
 (0)