Skip to content

Commit e1d1e7e

Browse files
committed
Add a StrippedLocation interface and pipe it through the CommentEnhancers.
1 parent e226890 commit e1d1e7e

File tree

6 files changed

+52
-34
lines changed

6 files changed

+52
-34
lines changed

browser-extension/src/lib/enhancer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export interface CommentEvent {
1919
draft?: string
2020
}
2121

22+
/**
23+
* Minimal location information that enhancers need for routing decisions.
24+
* Avoids dependency on global window/location objects for better testability.
25+
*/
26+
export interface StrippedLocation {
27+
domain: string
28+
pathname: string
29+
}
30+
2231
/** Wraps the textareas of a given platform with Gitcasso's enhancements. */
2332
export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
2433
/** Guarantees to only return a type within this list. */
@@ -27,7 +36,7 @@ export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
2736
* Whenever a new `textarea` is added to any webpage, this method is called.
2837
* If we return non-null, then we become the handler for that text area.
2938
*/
30-
tryToEnhance(textarea: HTMLTextAreaElement): Spot | null
39+
tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): Spot | null
3140
/** This gets called the first time that `tryToEnhance` returns non-null. */
3241
prepareForFirstEnhancement(): void
3342
/**

browser-extension/src/lib/enhancers/CommentEnhancerMissing.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OverTypeInstance } from 'overtype'
22
import type { ReactNode } from 'react'
3-
import type { CommentEnhancer, CommentSpot } from '../enhancer'
3+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../enhancer'
44

55
/** Used when an entry is in the table which we don't recognize. */
66
export class CommentEnhancerMissing implements CommentEnhancer {
@@ -40,7 +40,7 @@ export class CommentEnhancerMissing implements CommentEnhancer {
4040
forSpotTypes(): string[] {
4141
throw new Error('Method not implemented.')
4242
}
43-
tryToEnhance(_textarea: HTMLTextAreaElement): CommentSpot | null {
43+
tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null {
4444
throw new Error('Method not implemented.')
4545
}
4646
prepareForFirstEnhancement(): void {

browser-extension/src/lib/enhancers/github/githubIssueAddComment.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IssueOpenedIcon } from '@primer/octicons-react'
22
import OverType, { type OverTypeInstance } from 'overtype'
33
import type React from 'react'
4-
import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
4+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
55
import { logger } from '@/lib/logger'
66
import { modifyDOM } from '../modifyDOM'
77
import { commonGithubOptions } from './ghOptions'
@@ -20,19 +20,22 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
2020
return ['GH_ISSUE_ADD_COMMENT']
2121
}
2222

23-
tryToEnhance(textarea: HTMLTextAreaElement): GitHubIssueAddCommentSpot | null {
23+
tryToEnhance(
24+
textarea: HTMLTextAreaElement,
25+
location: StrippedLocation,
26+
): GitHubIssueAddCommentSpot | null {
2427
if (textarea.id === 'feedback') {
2528
return null
2629
}
27-
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
30+
if (location.domain !== 'github.com') {
2831
return null
2932
}
3033

3134
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
32-
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
35+
logger.debug(`${this.constructor.name} examing url`, location.pathname)
3336

34-
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/(\d+))/)
35-
logger.debug(`${this.constructor.name} found match`, window.location.pathname)
37+
const match = location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/(\d+))/)
38+
logger.debug(`${this.constructor.name} found match`, location.pathname)
3639
if (!match) return null
3740

3841
const [, owner, repo, numberStr] = match
@@ -41,7 +44,7 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
4144
const unique_key = `github.com:${slug}:${number}`
4245
const title = 'TODO_TITLE'
4346
return {
44-
domain: 'github.com',
47+
domain: location.domain,
4548
number,
4649
slug,
4750
title,

browser-extension/src/lib/enhancers/github/githubIssueNewComment.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import OverType, { type OverTypeInstance } from 'overtype'
2-
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
2+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
@@ -16,23 +16,26 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
1616
return ['GH_ISSUE_NEW_COMMENT']
1717
}
1818

19-
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubIssueNewCommentSpot | null {
20-
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
19+
tryToEnhance(
20+
_textarea: HTMLTextAreaElement,
21+
location: StrippedLocation,
22+
): GitHubIssueNewCommentSpot | null {
23+
if (location.domain !== 'github.com') {
2124
return null
2225
}
2326

2427
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
25-
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
28+
logger.debug(`${this.constructor.name} examing url`, location.pathname)
2629

27-
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/new)/)
28-
logger.debug(`${this.constructor.name} found match`, window.location.pathname)
30+
const match = location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/new)/)
31+
logger.debug(`${this.constructor.name} found match`, location.pathname)
2932

3033
if (!match) return null
3134
const [, owner, repo] = match
3235
const slug = `${owner}/${repo}`
3336
const unique_key = `github.com:${slug}:new`
3437
return {
35-
domain: 'github.com',
38+
domain: location.domain,
3639
slug,
3740
type: 'GH_ISSUE_NEW_COMMENT',
3841
unique_key,

browser-extension/src/lib/enhancers/github/githubPRAddComment.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import OverType, { type OverTypeInstance } from 'overtype'
22
import type React from 'react'
3-
import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
3+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
@@ -19,28 +19,28 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
1919
return ['GH_PR_ADD_COMMENT']
2020
}
2121

22-
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubPRAddCommentSpot | null {
22+
tryToEnhance(
23+
_textarea: HTMLTextAreaElement,
24+
location: StrippedLocation,
25+
): GitHubPRAddCommentSpot | null {
2326
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
24-
if (
25-
document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com' ||
26-
_textarea.id !== 'new_comment_field'
27-
) {
27+
if (location.domain !== 'github.com' || _textarea.id !== 'new_comment_field') {
2828
return null
2929
}
3030

3131
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
32-
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
32+
logger.debug(`${this.constructor.name} examing url`, location.pathname)
3333

34-
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/)
35-
logger.debug(`${this.constructor.name} found match`, window.location.pathname)
34+
const match = location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/)
35+
logger.debug(`${this.constructor.name} found match`, location.pathname)
3636
if (!match) return null
3737
const [, owner, repo, numberStr] = match
3838
const slug = `${owner}/${repo}`
3939
const number = parseInt(numberStr!, 10)
4040
const unique_key = `github.com:${slug}:${number}`
4141
const title = 'TODO_TITLE'
4242
return {
43-
domain: 'github.com',
43+
domain: location.domain,
4444
number,
4545
slug,
4646
title,

browser-extension/src/lib/enhancers/github/githubPRNewComment.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import OverType, { type OverTypeInstance } from 'overtype'
2-
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
2+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
@@ -16,22 +16,25 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
1616
return ['GH_PR_NEW_COMMENT']
1717
}
1818

19-
tryToEnhance(textarea: HTMLTextAreaElement): GitHubPRNewCommentSpot | null {
19+
tryToEnhance(
20+
textarea: HTMLTextAreaElement,
21+
location: StrippedLocation,
22+
): GitHubPRNewCommentSpot | null {
2023
if (textarea.id === 'feedback') {
2124
return null
2225
}
23-
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
26+
if (location.domain !== 'github.com') {
2427
return null
2528
}
2629

2730
// /owner/repo/compare/feature/more-enhancers?expand=1
2831
// or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1
29-
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
32+
logger.info(`${this.constructor.name} examing url`, location.pathname)
3033

31-
const match = window.location.pathname.match(
34+
const match = location.pathname.match(
3235
/^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/,
3336
)
34-
logger.info(`${this.constructor.name} found match`, window.location.pathname, match)
37+
logger.info(`${this.constructor.name} found match`, location.pathname, match)
3538

3639
if (!match) return null
3740
const [, owner, repo, baseBranch, compareBranch] = match
@@ -40,7 +43,7 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
4043
: `${owner}/${repo}/${compareBranch}`
4144
const unique_key = `github.com:${slug}`
4245
return {
43-
domain: 'github.com',
46+
domain: location.domain,
4447
slug,
4548
type: 'GH_PR_NEW_COMMENT',
4649
unique_key,

0 commit comments

Comments
 (0)