-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubPrCreateEnhancer.tsx
More file actions
116 lines (104 loc) · 3.1 KB
/
GitHubPrCreateEnhancer.tsx
File metadata and controls
116 lines (104 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { GitPullRequestIcon } from "@primer/octicons-react"
import OverType, { type OverTypeInstance } from "overtype"
import { LinkOutOfPopup } from "@/components/LinkOutOfPopup"
import type {
CommentEnhancer,
CommentSpot,
StrippedLocation,
} from "../../enhancer"
import { logger } from "../../logger"
import { modifyDOM } from "../modifyDOM"
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
const GH_PR_CREATE = "GH_PR_CREATE" as const
export interface GitHubPrCreateSpot extends CommentSpot {
type: typeof GH_PR_CREATE
domain: string
slug: string // owner/repo
title: string
head: string // `user:repo:branch ` where changes are implemented
base: string // branch you want changes pulled into
}
export class GitHubPrCreateEnhancer
implements CommentEnhancer<GitHubPrCreateSpot>
{
forSpotTypes(): string[] {
return [GH_PR_CREATE]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubPrCreateSpot | null {
if (textarea.id === "feedback") {
return null
}
if (location.host !== "github.com") {
return null
}
// /owner/repo/compare/feature/more-enhancers?expand=1
// or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1
logger.debug(
`${this.constructor.name} examing url`,
window.location.pathname
)
const match = location.pathname.match(
/^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/
)
logger.debug(
`${this.constructor.name} found match`,
window.location.pathname,
match
)
if (!match) return null
const [, owner, repo, baseBranch, compareBranch] = match
const slug = `${owner}/${repo}`
const base = baseBranch || "main"
const head = compareBranch!
const unique_key = `github.com:${slug}:${base}...${head}`
const titleInput = document.querySelector(
'input[placeholder="Title"]'
) as HTMLInputElement
const title = titleInput!.value
return {
base,
domain: location.host,
head,
slug,
title,
type: GH_PR_CREATE,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
_spot: GitHubPrCreateSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGitHubOptions,
minHeight: "250px",
placeholder: "Type your description here...",
})[0]!
}
tableUpperDecoration(spot: GitHubPrCreateSpot): React.ReactNode {
return (
<>
<span className="flex h-4 w-4 flex-shrink-0 items-center justify-center">
<GitPullRequestIcon size={16} />
</span>
<span>
<draft>{" "}
<LinkOutOfPopup href={`https://${spot.domain}/${spot.slug}`}>
{spot.slug}
</LinkOutOfPopup>
</span>
</>
)
}
tableTitle(spot: GitHubPrCreateSpot): string {
return spot.title || "<untitled pull request>"
}
buildUrl(spot: GitHubPrCreateSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}