-
Notifications
You must be signed in to change notification settings - Fork 3
feat: new 'MergeRequestOpened' reaction #156
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b8b5783
chore: creates a new 'MergeRequestOpened' reaction
SantiMA10 b9ceca8
chore: returns true if the event is 'Merge Request Hook' and 'object_…
SantiMA10 c1d5170
chore: returns false if the event is 'Fork' and 'object_attributes.st…
SantiMA10 3e938ff
chore: returns false if the event is 'Merge Request Hook' and 'object…
SantiMA10 f944f8c
chore: returns false if the event is 'Merge Request Hook', 'object_at…
SantiMA10 7c1bc47
chore: returns true if the event is 'Merge Request Hook', 'object_att…
SantiMA10 bb8fee1
refactor: returns true if the event is 'Merge Request Hook', 'object_…
SantiMA10 0a30c34
refactor: returns true if the event is 'Merge Request Hook', 'object_…
SantiMA10 b0ebefa
chore: sends the expected message to TwitchChat
SantiMA10 3d881f9
chore: sends the expected message to StreamLabs
SantiMA10 4026ade
refactor: sends the expected message to StreamLabs
SantiMA10 4cf8a8a
refactor: sends the expected message to StreamLabs
SantiMA10 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,29 @@ | ||
| import { Config } from '../../config'; | ||
| import { Reaction, ReactionCanHandleOptions, ReactionHandleOptions } from '../github/reaction'; | ||
| import { MergeRequestPayload } from '../../schemas/gitlab/merge-request-payload'; | ||
|
|
||
| export class MergeRequestOpened extends Reaction<MergeRequestPayload> { | ||
| getStreamLabsMessage({ payload }: ReactionHandleOptions<MergeRequestPayload>): string { | ||
| return `*${payload.user.username}* just opened a merge request in *${payload.repository.name}*`; | ||
| } | ||
|
|
||
| getTwitchChatMessage({ payload }: ReactionHandleOptions<MergeRequestPayload>): string { | ||
| return `${payload.user.username} just opened a merge request in ${payload.repository.homepage}`; | ||
| } | ||
|
|
||
| canHandle({ payload, event, config }: ReactionCanHandleOptions<MergeRequestPayload>): boolean { | ||
| return ( | ||
| event === 'Merge Request Hook' && | ||
| payload.object_attributes.state === 'opened' && | ||
| this.isAllowedByConfig(config, payload) | ||
| ); | ||
| } | ||
|
|
||
| private isAllowedByConfig(config: Config | undefined, payload: MergeRequestPayload): boolean { | ||
| return ( | ||
| !config || | ||
| !config.IGNORE_PR_OPENED_BY.includes(payload.user.username) || | ||
| config.IGNORE_PR_OPENED_BY.length === 0 | ||
| ); | ||
| } | ||
| } |
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,18 @@ | ||
| type url = string; | ||
|
|
||
| export interface MergeRequestPayload { | ||
| user: { | ||
| name: string; | ||
| username: string; | ||
| avatar_url: url; | ||
| }; | ||
| repository: { | ||
| name: string; | ||
| url: url; | ||
| description: string; | ||
| homepage: url; | ||
| }; | ||
| object_attributes: { | ||
| state: 'merged' | 'created' | 'updated' | 'closed' | 'opened'; | ||
| }; | ||
| } |
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,114 @@ | ||
| import { StreamLabsMock } from '../../__mocks__/StreamLabs'; | ||
| import { TwitchChatMock } from '../../__mocks__/TwitchChat'; | ||
| import { MergeRequestOpened } from '../../../src/reactions/gitlab/merge-request-opened'; | ||
| import { Config } from '../../../src/config'; | ||
| import { MergeRequestPayload } from '../../../src/schemas/gitlab/merge-request-payload'; | ||
|
|
||
| describe('MergeRequestOpened', () => { | ||
| const twitchChat = new TwitchChatMock(); | ||
| const streamlabs = new StreamLabsMock(); | ||
|
|
||
| describe('#canHandle', () => { | ||
| it("creates a new 'MergeRequestOpened' reaction", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| expect(subject).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("returns true if the event is 'Merge Request Hook' and 'object_attributes.state' is 'opened'", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| const result = subject.canHandle({ | ||
| event: 'Merge Request Hook', | ||
| payload: { object_attributes: { state: 'opened' } } as MergeRequestPayload, | ||
| }); | ||
|
|
||
| expect(result).toEqual(true); | ||
| }); | ||
|
|
||
| it("returns false if the event is 'Fork' and 'object_attributes.state' is 'opened'", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| const result = subject.canHandle({ | ||
| event: 'Fork', | ||
| payload: { object_attributes: { state: 'opened' } } as MergeRequestPayload, | ||
| }); | ||
|
|
||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| it("returns false if the event is 'Merge Request Hook' and 'object_attributes.state' is 'merged'", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| const result = subject.canHandle({ | ||
| event: 'Merge Request Hook', | ||
| payload: { object_attributes: { state: 'merged' } } as MergeRequestPayload, | ||
| }); | ||
|
|
||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| it("returns false if the event is 'Merge Request Hook', 'object_attributes.state' is 'opened' but the opener is in the ignore list", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| const result = subject.canHandle({ | ||
| event: 'Merge Request Hook', | ||
| payload: { | ||
| object_attributes: { state: 'opened' }, | ||
| user: { username: 'SantiMA10' }, | ||
| } as MergeRequestPayload, | ||
| config: { IGNORE_PR_OPENED_BY: ['SantiMA10'] } as Config, | ||
| }); | ||
|
|
||
| expect(result).toEqual(false); | ||
| }); | ||
|
|
||
| it("returns true if the event is 'Merge Request Hook', 'object_attributes.state' is 'opened' and the 'IGNORE_PR_OPENED_BY' is empty", () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
|
|
||
| const result = subject.canHandle({ | ||
| event: 'Merge Request Hook', | ||
| payload: { | ||
| object_attributes: { state: 'opened' }, | ||
| user: { username: 'SantiMA10' }, | ||
| } as MergeRequestPayload, | ||
| config: { IGNORE_PR_OPENED_BY: [] as string[] } as Config, | ||
| }); | ||
|
|
||
| expect(result).toEqual(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#handle', () => { | ||
| it('sends the expected message to TwitchChat', async () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
| const payload = { | ||
| object_attributes: { state: 'opened' }, | ||
| user: { username: 'SantiMA10' }, | ||
| repository: { homepage: 'https://gitlab.com/streamlabs/webhook' }, | ||
| } as MergeRequestPayload; | ||
|
|
||
| const { twitchChat: response } = await subject.handle({ payload }); | ||
|
|
||
| expect(response).toEqual({ | ||
| notified: true, | ||
| message: `${payload.user.username} just opened a merge request in ${payload.repository.homepage}`, | ||
| }); | ||
| }); | ||
|
|
||
| it('sends the expected message to StreamLabs', async () => { | ||
| const subject = new MergeRequestOpened(twitchChat, streamlabs); | ||
| const payload = { | ||
| user: { username: 'SantiMA10' }, | ||
| repository: { name: 'streamdevs/webhook' }, | ||
| } as MergeRequestPayload; | ||
|
|
||
| const { streamlabs: response } = await subject.handle({ payload }); | ||
|
|
||
| expect(response).toEqual({ | ||
| notified: true, | ||
| message: `*${payload.user.username}* just opened a merge request in *${payload.repository.name}*`, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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 test doesn't belong on this describe