-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add pr close banner #9
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
Changes from 4 commits
c8a35e0
ad7979e
914117d
5f8ed57
d707a2e
611551c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'elden-ring-github': minor | ||
| --- | ||
|
|
||
| Add a PR close banner option with settings toggle, popup control, and reusable banner rendering helper. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { renderBanner, type BannerType } from './banner'; | ||
|
|
||
| describe('renderBanner', () => { | ||
| const soundUrl = 'chrome-extension://mock/sound.mp3'; | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = ''; | ||
| vi.useFakeTimers(); | ||
|
|
||
| const chromeGlobal = globalThis as unknown as { | ||
| chrome?: { runtime?: { getURL?: (path: string) => string } }; | ||
| }; | ||
| chromeGlobal.chrome = chromeGlobal.chrome || {}; | ||
| chromeGlobal.chrome.runtime = chromeGlobal.chrome.runtime || {}; | ||
| chromeGlobal.chrome.runtime.getURL = vi.fn((path: string) => `chrome-extension://mock/${path}`); | ||
|
|
||
| global.Audio = vi.fn().mockImplementation(() => { | ||
| return { | ||
| play: vi.fn().mockResolvedValue(undefined), | ||
| volume: 0, | ||
| } as unknown as HTMLAudioElement; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('should render banners for each type', () => { | ||
| const types: BannerType[] = ['merged', 'created', 'approved', 'closed']; | ||
|
|
||
| types.forEach((type) => { | ||
| const onHide = vi.fn(); | ||
| renderBanner({ | ||
| type, | ||
| soundUrl, | ||
| soundEnabled: true, | ||
| onHide, | ||
| }); | ||
|
|
||
| const banner = document.getElementById('elden-ring-banner'); | ||
| expect(banner).toBeTruthy(); | ||
| expect(banner?.innerHTML).toContain('.png'); | ||
| const chromeRuntime = (globalThis as any).chrome.runtime; | ||
| expect(chromeRuntime.getURL).toHaveBeenCalled(); | ||
|
|
||
| vi.runAllTimers(); | ||
| expect(onHide).toHaveBeenCalled(); | ||
|
|
||
| document.body.innerHTML = ''; | ||
| }); | ||
| }); | ||
|
|
||
| it('should skip audio when sound is disabled', () => { | ||
| const onHide = vi.fn(); | ||
| renderBanner({ | ||
| type: 'merged', | ||
| soundUrl, | ||
| soundEnabled: false, | ||
| onHide, | ||
| }); | ||
|
|
||
| expect(global.Audio).not.toHaveBeenCalled(); | ||
| vi.runAllTimers(); | ||
| expect(onHide).toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| export type BannerType = 'merged' | 'created' | 'approved' | 'closed'; | ||
|
|
||
| interface RenderBannerOptions { | ||
| type: BannerType; | ||
| soundUrl: string; | ||
| soundEnabled: boolean; | ||
| onHide: () => void; | ||
| } | ||
|
|
||
| const bannerAssetMap: Record<BannerType, { image: string; alt: string }> = { | ||
| merged: { | ||
| image: 'pull-request-merged.png', | ||
| alt: 'Pull Request Merged', | ||
| }, | ||
| created: { | ||
| image: 'pull-request-created.png', | ||
| alt: 'Pull Request Created', | ||
| }, | ||
| approved: { | ||
| image: 'approve-pull-request.png', | ||
| alt: 'Pull Request Approved', | ||
| }, | ||
| closed: { | ||
| image: 'close-pull-request.png', | ||
| alt: 'Pull Request Closed', | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Renders the Elden Ring banner with correct imagery and optional audio with safe cleanup. | ||
| */ | ||
| export const renderBanner = ({ | ||
| type, | ||
| soundUrl, | ||
| soundEnabled, | ||
| onHide, | ||
| }: RenderBannerOptions): boolean => { | ||
| try { | ||
| const banner = document.createElement('div'); | ||
| banner.id = 'elden-ring-banner'; | ||
|
|
||
| const asset = bannerAssetMap[type]; | ||
| const imgPath = chrome.runtime.getURL(`assets/${asset.image}`); | ||
| banner.innerHTML = `<img src="${imgPath}" alt="${asset.alt}">`; | ||
| document.body.appendChild(banner); | ||
|
|
||
| if (soundEnabled) { | ||
| const audio = new Audio(soundUrl); | ||
| audio.volume = 1.0; | ||
| audio.play().catch((err) => console.log('Sound playback failed:', err)); | ||
| } | ||
|
|
||
| setTimeout(() => banner.classList.add('show'), 50); | ||
| setTimeout(() => { | ||
| banner.classList.remove('show'); | ||
| setTimeout(() => { | ||
| if (banner.parentNode) { | ||
| banner.remove(); | ||
| } | ||
| onHide(); | ||
| }, 500); | ||
| }, 3000); | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| console.log('Image banner failed, using text banner:', error); | ||
| onHide(); | ||
| return false; | ||
| } | ||
|
Comment on lines
67
to
72
Owner
Author
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. Maintainability: Inconsistent Error Handling The function catches all errors and logs them, but returns different boolean values. However, the error case calls Current: } catch (error) {
console.log('Image banner failed, using text banner:', error);
onHide();
return false;
}Suggestion: } catch (error) {
console.error('Banner rendering failed:', error);
// Don't call onHide() here - let the caller handle the failure
return false;
}Or alternatively, ensure the caller properly handles the false return value. The current pattern is confusing because |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { waitForCloseComplete } from './closeWatcher'; | ||
|
|
||
| describe('waitForCloseComplete', () => { | ||
| beforeEach(() => { | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('should trigger callback immediately if closed state already exists', () => { | ||
| document.body.innerHTML = ` | ||
| <span class="State State--closed">Closed</span> | ||
| `; | ||
|
|
||
| const onClose = vi.fn(); | ||
| waitForCloseComplete(onClose); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should trigger callback when closed state is added later', async () => { | ||
| const onClose = vi.fn(); | ||
| waitForCloseComplete(onClose); | ||
|
|
||
| const closedElement = document.createElement('span'); | ||
| closedElement.className = 'State State--closed'; | ||
| closedElement.textContent = 'Closed'; | ||
| document.body.appendChild(closedElement); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 150)); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| let closeHandled = false; | ||
|
|
||
| const isClosedState = (element: Element | null): boolean => { | ||
| if (!element) return false; | ||
| if (!element.matches('.State.State--closed')) { | ||
| return false; | ||
| } | ||
| const text = element.textContent?.toLowerCase().trim() || ''; | ||
| return text.includes('closed'); | ||
| }; | ||
|
|
||
| const handleClose = (observer: MutationObserver, onClose: () => void): void => { | ||
| if (closeHandled) return; | ||
| closeHandled = true; | ||
| console.log('β οΈ Pull request closed!'); | ||
| onClose(); | ||
| observer.disconnect(); | ||
| }; | ||
|
|
||
| const checkExistingClosedState = (observer: MutationObserver, onClose: () => void): boolean => { | ||
| const closedElement = document.querySelector('.State.State--closed'); | ||
| if (isClosedState(closedElement)) { | ||
| handleClose(observer, onClose); | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| export const waitForCloseComplete = (onClose: () => void, timeoutMs: number = 10000): void => { | ||
| closeHandled = false; | ||
|
|
||
| const observer = new MutationObserver((mutations) => { | ||
| if (closeHandled) return; | ||
|
|
||
| mutations.forEach((mutation) => { | ||
| mutation.addedNodes.forEach((node) => { | ||
| if (node.nodeType !== Node.ELEMENT_NODE || closeHandled) { | ||
| return; | ||
| } | ||
|
|
||
| const element = node as Element; | ||
| if (isClosedState(element)) { | ||
| handleClose(observer, onClose); | ||
| return; | ||
| } | ||
|
|
||
| const closedElement = element.querySelector('.State.State--closed'); | ||
| if (isClosedState(closedElement)) { | ||
| handleClose(observer, onClose); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| observer.observe(document.body, { | ||
| childList: true, | ||
| subtree: true, | ||
| }); | ||
|
|
||
| if (checkExistingClosedState(observer, onClose)) { | ||
| return; | ||
| } | ||
|
|
||
| setTimeout(() => { | ||
| checkExistingClosedState(observer, onClose); | ||
| }, 100); | ||
|
||
|
|
||
| setTimeout(() => { | ||
| observer.disconnect(); | ||
| console.log('β° Close detection timeout'); | ||
| }, timeoutMs); | ||
| }; | ||
|
Comment on lines
40
to
95
Owner
Author
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. Performance Issue: MutationObserver Memory Leak If Current: setTimeout(() => {
checkExistingClosedState(observer, onClose);
}, 100);
setTimeout(() => {
observer.disconnect();
console.log('β° Close detection timeout');
}, timeoutMs);Fix: const checkTimeout = setTimeout(() => {
if (!closeHandled) {
checkExistingClosedState(observer, onClose);
}
}, 100);
const cleanupTimeout = setTimeout(() => {
if (!closeHandled) {
observer.disconnect();
console.log('β° Close detection timeout');
}
}, timeoutMs);
// Also clear these timeouts in handleClose:
const handleClose = (observer: MutationObserver, onClose: () => void): void => {
if (closeHandled) return;
closeHandled = true;
console.log('β οΈ Pull request closed!');
clearTimeout(checkTimeout);
clearTimeout(cleanupTimeout);
onClose();
observer.disconnect();
};This ensures proper cleanup and prevents memory leaks. |
||
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.
Security Issue: Potential XSS Vulnerability
While
chrome.runtime.getURL()is safe, usinginnerHTMLto inject content could be risky if the asset mapping logic is ever modified. It's better to use DOM APIs for security best practices.Current:
Fix:
This eliminates any potential XSS attack surface.