Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/reactions/gitlab/merge-request-opened.ts
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
);
}
}
18 changes: 18 additions & 0 deletions src/schemas/gitlab/merge-request-payload.ts
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';
};
}
114 changes: 114 additions & 0 deletions test/reactions/gitlab/merge-request-opened.spec.ts
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", () => {
Copy link
Member

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

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}*`,
});
});
});
});