|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +const checkForReproducer = require('../checkForReproducer'); |
| 11 | + |
| 12 | +const AUTHOR = 'issue-author'; |
| 13 | +const BOT = 'react-native-bot'; |
| 14 | +const MAINTAINER = 'some-maintainer'; |
| 15 | + |
| 16 | +function labelEvent(event, login, type, name = 'Needs: Repro') { |
| 17 | + return {event, label: {name}, actor: {login, type}}; |
| 18 | +} |
| 19 | + |
| 20 | +function buildGithub({body, comments = [], timeline = []}) { |
| 21 | + return { |
| 22 | + rest: { |
| 23 | + issues: { |
| 24 | + get: jest.fn().mockResolvedValue({ |
| 25 | + data: { |
| 26 | + user: {login: AUTHOR}, |
| 27 | + created_at: '2026-01-01T00:00:00Z', |
| 28 | + body, |
| 29 | + }, |
| 30 | + }), |
| 31 | + listComments: jest.fn().mockResolvedValue({data: comments}), |
| 32 | + listEventsForTimeline: jest.fn().mockResolvedValue({data: timeline}), |
| 33 | + removeLabel: jest.fn().mockResolvedValue({}), |
| 34 | + addLabels: jest.fn().mockResolvedValue({}), |
| 35 | + }, |
| 36 | + }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +const context = { |
| 41 | + payload: {issue: {number: 1}}, |
| 42 | + repo: {owner: 'react', repo: 'react-native'}, |
| 43 | +}; |
| 44 | + |
| 45 | +const REPRO_LINK = `Repro: https://github.com/${AUTHOR}/rn-repro`; |
| 46 | + |
| 47 | +describe('checkForReproducer', () => { |
| 48 | + beforeEach(() => { |
| 49 | + jest.clearAllMocks(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('adds "Needs: Repro" and "Needs: Author Feedback" when no reproducer is present', async () => { |
| 53 | + const github = buildGithub({body: 'It crashes.'}); |
| 54 | + |
| 55 | + await checkForReproducer(github, context); |
| 56 | + |
| 57 | + expect(github.rest.issues.addLabels).toHaveBeenCalledWith( |
| 58 | + expect.objectContaining({ |
| 59 | + labels: ['Needs: Repro', 'Needs: Author Feedback'], |
| 60 | + }), |
| 61 | + ); |
| 62 | + expect(github.rest.issues.removeLabel).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('removes "Needs: Repro" when the author links a repository they own', async () => { |
| 66 | + const github = buildGithub({body: REPRO_LINK}); |
| 67 | + |
| 68 | + await checkForReproducer(github, context); |
| 69 | + |
| 70 | + expect(github.rest.issues.removeLabel).toHaveBeenCalledWith( |
| 71 | + expect.objectContaining({name: 'Needs: Repro'}), |
| 72 | + ); |
| 73 | + expect(github.rest.issues.addLabels).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('removes "Needs: Repro" after the author edits in a reproducer, even though react-native-bot applied the label', async () => { |
| 77 | + const github = buildGithub({ |
| 78 | + body: REPRO_LINK, |
| 79 | + timeline: [ |
| 80 | + labelEvent('labeled', BOT, 'User', 'Needs: Author Feedback'), |
| 81 | + labelEvent('labeled', BOT, 'User'), |
| 82 | + labelEvent( |
| 83 | + 'unlabeled', |
| 84 | + 'github-actions[bot]', |
| 85 | + 'Bot', |
| 86 | + 'Needs: Author Feedback', |
| 87 | + ), |
| 88 | + ], |
| 89 | + }); |
| 90 | + |
| 91 | + await checkForReproducer(github, context); |
| 92 | + |
| 93 | + expect(github.rest.issues.removeLabel).toHaveBeenCalledWith( |
| 94 | + expect.objectContaining({name: 'Needs: Repro'}), |
| 95 | + ); |
| 96 | + expect(github.rest.issues.addLabels).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('does nothing when a maintainer has changed the "Needs: Repro" label', async () => { |
| 100 | + const github = buildGithub({ |
| 101 | + body: REPRO_LINK, |
| 102 | + timeline: [ |
| 103 | + labelEvent('labeled', BOT, 'User'), |
| 104 | + labelEvent('unlabeled', MAINTAINER, 'User'), |
| 105 | + labelEvent('labeled', MAINTAINER, 'User'), |
| 106 | + ], |
| 107 | + }); |
| 108 | + |
| 109 | + await checkForReproducer(github, context); |
| 110 | + |
| 111 | + expect(github.rest.issues.removeLabel).not.toHaveBeenCalled(); |
| 112 | + expect(github.rest.issues.addLabels).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('ignores label changes on other labels when deciding whether a maintainer intervened', async () => { |
| 116 | + const github = buildGithub({ |
| 117 | + body: REPRO_LINK, |
| 118 | + timeline: [ |
| 119 | + labelEvent('labeled', BOT, 'User'), |
| 120 | + labelEvent('labeled', MAINTAINER, 'User', 'Platform: iOS'), |
| 121 | + ], |
| 122 | + }); |
| 123 | + |
| 124 | + await checkForReproducer(github, context); |
| 125 | + |
| 126 | + expect(github.rest.issues.removeLabel).toHaveBeenCalledWith( |
| 127 | + expect.objectContaining({name: 'Needs: Repro'}), |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('accepts a reproducer link posted in a comment by its own author', async () => { |
| 132 | + const github = buildGithub({ |
| 133 | + body: 'It crashes.', |
| 134 | + comments: [ |
| 135 | + { |
| 136 | + user: {login: AUTHOR}, |
| 137 | + body: `Here you go: https://github.com/${AUTHOR}/rn-repro`, |
| 138 | + }, |
| 139 | + ], |
| 140 | + timeline: [labelEvent('labeled', BOT, 'User')], |
| 141 | + }); |
| 142 | + |
| 143 | + await checkForReproducer(github, context); |
| 144 | + |
| 145 | + expect(github.rest.issues.removeLabel).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({name: 'Needs: Repro'}), |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments