-
Notifications
You must be signed in to change notification settings - Fork 287
fix: refetch current issue state before assignment bots assign users #2237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chaitanyamedidar
wants to merge
8
commits into
hiero-ledger:main
Choose a base branch
from
chaitanyamedidar:fix/assignment-bots-fresh-issue-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+324
−7
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c71e516
fix: refetch issue state before assignment
chaitanyamedidar b7e4bef
Added assignment issue test
chaitanyamedidar ae81c81
test: cover assignment bot fresh issue defensive paths
chaitanyamedidar 2ee160b
test: cover closed issue assignment guard
chaitanyamedidar bbc3e40
Merge branch 'main' into fix/assignment-bots-fresh-issue-state
chaitanyamedidar 4522767
Merge branch 'main' into fix/assignment-bots-fresh-issue-state
chaitanyamedidar e07ae11
Merge branch 'main' into fix/assignment-bots-fresh-issue-state
chaitanyamedidar 4b92214
Merge branch 'main' into fix/assignment-bots-fresh-issue-state
chaitanyamedidar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Regression coverage for stale issue_comment payloads: the bots must refetch issue state before assigning. | ||
|
|
||
| const { describe, it } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
|
|
||
| const runGfiAssignBot = require('./bot-gfi-assign-on-comment.js'); | ||
| const runBeginnerAssignBot = require('./bot-beginner-assign-on-comment.js'); | ||
|
|
||
| function createContext({ labelName, payloadAssignees = [], freshLabels, freshAssignees = [], freshState = 'open' }) { | ||
| const labels = [{ name: labelName }]; | ||
|
|
||
| return { | ||
| repo: { | ||
| owner: 'hiero-ledger', | ||
| repo: 'hiero-sdk-python', | ||
| }, | ||
| payload: { | ||
| repository: { | ||
| owner: { login: 'hiero-ledger' }, | ||
| name: 'hiero-sdk-python', | ||
| }, | ||
| issue: { | ||
| number: 123, | ||
| labels, | ||
| assignees: payloadAssignees, | ||
| }, | ||
| comment: { | ||
| body: '/assign', | ||
| user: { | ||
| login: 'new-contributor', | ||
| type: 'User', | ||
| }, | ||
| }, | ||
| }, | ||
| freshIssue: { | ||
| number: 123, | ||
| title: 'Example issue', | ||
| state: freshState, | ||
| labels: freshLabels ?? labels, | ||
| assignees: freshAssignees, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createGithubMock(context, { freshIssueError } = {}) { | ||
| const calls = { | ||
| comments: [], | ||
| assignees: [], | ||
| }; | ||
|
|
||
| const github = { | ||
| rest: { | ||
| issues: { | ||
| get: async () => { | ||
| if (freshIssueError) { | ||
| throw freshIssueError; | ||
| } | ||
| return { data: context.freshIssue }; | ||
| }, | ||
| createComment: async (params) => { | ||
| calls.comments.push(params); | ||
| return { data: {} }; | ||
| }, | ||
| addAssignees: async (params) => { | ||
| calls.assignees.push(params); | ||
| return { data: {} }; | ||
| }, | ||
| }, | ||
| }, | ||
| paginate: async () => [], | ||
| graphql: async () => ({ search: { issueCount: 1 } }), | ||
| }; | ||
|
|
||
| return { github, calls }; | ||
| } | ||
|
|
||
| describe('assignment bots use fresh issue state before assigning', () => { | ||
| it('GFI bot does not assign when webhook payload is stale but issue is now assigned', async () => { | ||
| const context = createContext({ | ||
| labelName: 'Good First Issue', | ||
| payloadAssignees: [], | ||
| freshAssignees: [{ login: 'current-assignee' }], | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runGfiAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 1); | ||
| assert.match(calls.comments[0].body, /already assigned/i); | ||
| assert.match(calls.comments[0].body, /@current-assignee/); | ||
| }); | ||
|
|
||
| it('GFI bot exits safely when fresh issue fetch fails before assignment', async () => { | ||
| const context = createContext({ | ||
| labelName: 'Good First Issue', | ||
| payloadAssignees: [], | ||
| }); | ||
| const { github, calls } = createGithubMock(context, { | ||
| freshIssueError: Object.assign(new Error('GitHub API unavailable'), { status: 503 }), | ||
| }); | ||
|
|
||
| await runGfiAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
|
|
||
| it('GFI bot exits when the fresh issue is no longer a Good First Issue', async () => { | ||
| const context = createContext({ | ||
| labelName: 'Good First Issue', | ||
| payloadAssignees: [], | ||
| freshLabels: [{ name: 'help wanted' }], | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runGfiAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
|
|
||
| it('GFI bot exits when the fresh issue is closed before assignment', async () => { | ||
| const context = createContext({ | ||
| labelName: 'Good First Issue', | ||
| payloadAssignees: [], | ||
| freshState: 'closed', | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runGfiAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
|
|
||
| it('beginner bot does not assign when webhook payload is stale but issue is now assigned', async () => { | ||
| const context = createContext({ | ||
| labelName: 'skill: beginner', | ||
| payloadAssignees: [], | ||
| freshAssignees: [{ login: 'current-assignee' }], | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runBeginnerAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 1); | ||
| assert.match(calls.comments[0].body, /already assigned/i); | ||
| assert.match(calls.comments[0].body, /@current-assignee/); | ||
| }); | ||
|
|
||
| it('beginner bot exits safely when fresh issue fetch fails before assignment', async () => { | ||
| const context = createContext({ | ||
| labelName: 'skill: beginner', | ||
| payloadAssignees: [], | ||
| }); | ||
| const { github, calls } = createGithubMock(context, { | ||
| freshIssueError: Object.assign(new Error('GitHub API unavailable'), { status: 503 }), | ||
| }); | ||
|
|
||
| await runBeginnerAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
|
|
||
| it('beginner bot exits when the fresh issue no longer has the beginner label', async () => { | ||
| const context = createContext({ | ||
| labelName: 'skill: beginner', | ||
| payloadAssignees: [], | ||
| freshLabels: [{ name: 'help wanted' }], | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runBeginnerAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
|
|
||
| it('beginner bot exits when the fresh issue is closed before assignment', async () => { | ||
| const context = createContext({ | ||
| labelName: 'skill: beginner', | ||
| payloadAssignees: [], | ||
| freshState: 'closed', | ||
| }); | ||
| const { github, calls } = createGithubMock(context); | ||
|
|
||
| await runBeginnerAssignBot({ github, context }); | ||
|
|
||
| assert.equal(calls.assignees.length, 0); | ||
| assert.equal(calls.comments.length, 0); | ||
| }); | ||
| }); | ||
|
chaitanyamedidar marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,16 @@ function getCurrentAssigneeMention(issue) { | |
| return login ? `@${login}` : 'someone'; | ||
| } | ||
|
|
||
| async function getFreshIssue({ github, owner, repo, issueNumber }) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have two getFreshIssue functions, we can move them to /shared |
||
| const { data } = await github.rest.issues.get({ | ||
| owner, | ||
| repo, | ||
| issue_number: issueNumber, | ||
| }); | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a comment explaining that the issue is already assigned. | ||
| * Requester username is passed from main | ||
|
|
@@ -361,7 +371,54 @@ module.exports = async ({ github, context }) => { | |
| return; | ||
| } | ||
|
|
||
| console.log('[gfi-assign] Assigning issue to requester'); | ||
| console.log('[gfi-assign] Checking current issue state before assignment'); | ||
|
|
||
| let currentIssue; | ||
| try { | ||
| currentIssue = await getFreshIssue({ | ||
| github, | ||
| owner, | ||
| repo, | ||
| issueNumber, | ||
| }); | ||
|
chaitanyamedidar marked this conversation as resolved.
|
||
| } catch (error) { | ||
| console.error('[gfi-assign] Failed to fetch current issue state before assignment:', { | ||
| message: error.message, | ||
| status: error.status, | ||
| owner, | ||
| repo, | ||
| issueNumber, | ||
| requesterUsername, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (!issueIsGoodFirstIssue(currentIssue)) { | ||
| console.log('[gfi-assign] Exit: current issue state is no longer a Good First Issue'); | ||
| return; | ||
| } | ||
|
|
||
| if (currentIssue.state !== 'open') { | ||
| console.log('[gfi-assign] Exit: current issue state is not open', { | ||
| issueNumber, | ||
| state: currentIssue.state, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (currentIssue.assignees?.length > 0) { | ||
| console.log('[gfi-assign] Exit: current issue state is already assigned'); | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: issueNumber, | ||
| body: commentAlreadyAssigned(requesterUsername, currentIssue), | ||
| }); | ||
|
|
||
| console.log('[gfi-assign] Posted already-assigned comment from current issue state'); | ||
| return; | ||
| } | ||
|
exploreriii marked this conversation as resolved.
|
||
|
|
||
| // All validations passed and user has requested assignment on a GFI | ||
| // Assign the issue to the requester | ||
|
|
@@ -409,7 +466,7 @@ module.exports = async ({ github, context }) => { | |
| if (planExists) { | ||
| console.log('[gfi-assign] CodeRabbit plan already exists, skipping'); | ||
| } else { | ||
| await triggerCodeRabbitPlan(github, owner, repo, issue); | ||
| await triggerCodeRabbitPlan(github, owner, repo, currentIssue); | ||
| console.log('[gfi-assign] CodeRabbit plan chained successfully'); | ||
| } | ||
| } catch (error) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be rewritten as Jest under .github/scripts/tests/jest/ — then test-scripts.yml runs it automatically.
(new change since, sorry)