Skip to content
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

feat: allow Twitch/StreamLabs to fail without breaking other notifications #109

Merged
merged 1 commit into from
Apr 19, 2020
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
59 changes: 45 additions & 14 deletions src/reactions/github/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,55 @@ export class Ping {
this.twitchChat = twitchChat;
}

public async handle({ payload }: HandleOptions) {
const streamlabsMessage = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`;
await this.streamlabs.alert({
message: streamlabsMessage,
});

const twitchMessage = `🎉 Your repo ${payload.repository.full_name} is configured correctly for ${payload.hook.events} events 🎉`;
await this.twitchChat.send(twitchMessage);
private async notifyStreamlabs({ payload }: HandleOptions) {
try {
const streamlabsMessage = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`;
await this.streamlabs.alert({
message: streamlabsMessage,
});

return {
twitchChat: {
message: twitchMessage,
return {
notified: true,
},
streamlabs: {
message: streamlabsMessage,
};
} catch {
// TODO: add logging

return {
notified: false,
message: '',
};
}
}

private async notifyTwitch({ payload }: HandleOptions) {
try {
const message = `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *${payload.hook.events}* events 🎉`;
await this.twitchChat.send(message);

return {
notified: true,
},
message,
};
} catch {
// TODO: add logging

return {
notified: false,
message: '',
};
}
}

public async handle({ payload }: HandleOptions) {
const [streamlabs, twitchChat] = await Promise.all([
this.notifyStreamlabs({ payload }),
this.notifyTwitch({ payload }),
]);

return {
twitchChat,
streamlabs,
};
}
}
40 changes: 34 additions & 6 deletions test/reactions/github/ping.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Ping } from '../../../src/reactions/github/ping';
import { StreamLabs } from '../../../src/services/StreamLabs';
import { TwitchChat } from '../../../src/services/TwitchChat';

describe('Ping', () => {
describe('#handle', () => {
let streamlabs: any;
let twitchChat: any;
let streamlabs: StreamLabs;
let twitchChat: TwitchChat;
let payload: any;

beforeEach(() => {
streamlabs = { alert: jest.fn() };
twitchChat = { send: jest.fn() };
streamlabs = ({ alert: jest.fn() } as unknown) as StreamLabs;
twitchChat = ({ send: jest.fn() } as unknown) as TwitchChat;
payload = {
hook: {
events: ['fork'],
Expand Down Expand Up @@ -38,7 +40,7 @@ describe('Ping', () => {
await subject.handle({ payload });

expect(twitchChat.send).toHaveBeenCalledWith(
`🎉 Your repo ${payload.repository.full_name} is configured correctly for fork events 🎉`,
`🎉 Your repo *${payload.repository.full_name}* is configured correctly for *fork* events 🎉`,
);
});

Expand All @@ -55,7 +57,7 @@ describe('Ping', () => {
});

expect(response).toEqual({
message: `🎉 Your repo ${payload.repository.full_name} is configured correctly for pull_request events 🎉`,
message: `🎉 Your repo *${payload.repository.full_name}* is configured correctly for *pull_request* events 🎉`,
notified: true,
});
});
Expand All @@ -77,5 +79,31 @@ describe('Ping', () => {
notified: true,
});
});

it("returns 'streamlabs.notified' set to false is something goes wrong with StreamLabs", async () => {
jest.spyOn(streamlabs, 'alert').mockImplementationOnce(async () => {
throw new Error('boom');
});
const subject = new Ping({ streamlabs, twitchChat });

const {
streamlabs: { notified },
} = await subject.handle({ payload });

expect(notified).toEqual(false);
});

it("returns 'twitchChat.notified' set to false is something goes wrong with TwitchChat", async () => {
jest.spyOn(twitchChat, 'send').mockImplementationOnce(async () => {
throw new Error('boom');
});
const subject = new Ping({ streamlabs, twitchChat });

const {
twitchChat: { notified },
} = await subject.handle({ payload });

expect(notified).toEqual(false);
});
});
});