-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathproviders.ts
More file actions
321 lines (300 loc) · 8.15 KB
/
providers.ts
File metadata and controls
321 lines (300 loc) · 8.15 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import * as path from "path"
import * as url from "url"
import { IProviderConfig } from "./extension"
import { cleanHostname } from "./utils"
import { flatten } from "lodash"
import gitUrlParse from "git-url-parse"
interface ISelection {
start: { line: number; character: number }
end: { line: number; character: number }
}
interface IBaseGetUrls {
readonly selection: ISelection
readonly head: Head
readonly relativeFilePath: string | null
}
export interface IUrlInfo {
readonly blobUrl: string | null
readonly repoUrl: string | null
readonly blameUrl: string | null
readonly historyUrl: string | null
readonly prUrl: string | null
readonly compareUrl: string | null
}
interface IOrgInfo {
readonly org: string
readonly repo: string
readonly hostname: string
}
interface IBranch {
readonly kind: "branch"
readonly value: string
}
export function createBranch(value: string): IBranch {
return { kind: "branch", value }
}
interface ISHA {
readonly kind: "sha"
readonly value: string
}
type Head = IBranch | ISHA
export function createSha(value: string): ISHA {
return { kind: "sha", value }
}
abstract class BaseProvider {
abstract readonly DEFAULT_HOSTNAMES: string[]
abstract readonly PROVIDER_NAME: string
private CONFIG: { [key: string]: IProviderConfig | undefined }
private GLOBAL_DEFAULT_REMOTE: string
private FIND_REMOTE: (x: string) => Promise<string | null>
constructor(
config: { [key: string]: IProviderConfig | undefined },
global_default_remote: string,
findRemote: (x: string) => Promise<string | null>,
) {
this.CONFIG = config
this.GLOBAL_DEFAULT_REMOTE = global_default_remote
this.FIND_REMOTE = findRemote.bind(this)
}
private getConfig() {
return this.CONFIG[this.PROVIDER_NAME]
}
private getRemoteName(): string {
const conf = this.getConfig()
return conf && conf.remote ? conf.remote : this.GLOBAL_DEFAULT_REMOTE
}
private getHostnames() {
const conf = this.getConfig()
return this.DEFAULT_HOSTNAMES.concat((conf && conf.hostnames) || []).map(
cleanHostname,
)
}
async findOrgInfo(): Promise<IOrgInfo | null> {
const origin = await this.FIND_REMOTE(this.getRemoteName())
if (origin == null) {
return null
}
let parsed: gitUrlParse.GitUrl
try {
parsed = gitUrlParse(origin)
} catch {
return null
}
if (!this.getHostnames().some((x) => parsed.resource.includes(x))) {
return null
}
return { org: parsed.owner, repo: parsed.name, hostname: parsed.resource }
}
abstract getUrls(params: IBaseGetUrls): Promise<IUrlInfo | null>
}
export function pathJoin(...args: string[]): string {
return path.join(
...flatten(args.map((x) => x.split("/"))).map(encodeURIComponent),
)
}
export class Github extends BaseProvider {
DEFAULT_HOSTNAMES = ["github.com"]
PROVIDER_NAME = "github"
buildLines({ start, end }: ISelection): string {
if (start.line === end.line && start.character === end.character) {
return ""
}
let line = `L${start.line + 1}`
if (start.character !== 0) {
line += `C${start.character + 1}`
} else {
line += `-L${end.line + 1}`
return line
}
line += `-L${end.line + 1}`
if (end.character !== 0) {
line += `C${end.character + 1}`
}
return line
}
async getUrls({
selection,
head,
relativeFilePath,
}: IBaseGetUrls): Promise<IUrlInfo | null> {
const repoInfo = await this.findOrgInfo()
if (repoInfo == null) {
return null
}
const rootUrl = `https://${repoInfo.hostname}/`
const lines = this.buildLines(selection)
const repoUrl = new url.URL(
path.join(repoInfo.org, repoInfo.repo),
rootUrl,
).toString()
const createUrl = (mode: string, hash = true) => {
if (relativeFilePath == null) {
return null
}
const u = new url.URL(
pathJoin(
repoInfo.org,
repoInfo.repo,
mode,
head.value,
relativeFilePath,
),
rootUrl,
)
if (hash) {
u.hash = lines
}
return u.toString()
}
const blobUrl = createUrl("blob")
const blameUrl = createUrl("blame")
const historyUrl = createUrl("commits", false)
const compareUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo, "compare", head.value),
rootUrl,
).toString()
const prUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo, "pull", "new", head.value),
rootUrl,
).toString()
return {
blobUrl,
blameUrl,
compareUrl,
historyUrl,
prUrl,
repoUrl,
}
}
}
export class Gitlab extends BaseProvider {
DEFAULT_HOSTNAMES = ["gitlab.com"]
PROVIDER_NAME = "gitlab"
async getUrls({
selection,
relativeFilePath,
head,
}: IBaseGetUrls): Promise<IUrlInfo | null> {
const repoInfo = await this.findOrgInfo()
if (repoInfo == null) {
return null
}
const rootUrl = `https://${repoInfo.hostname}/`
const { start, end } = selection
// The format is L34-56 (this is one character off from Github)
const lines = `L${start.line + 1}-${end.line + 1}`
const repoUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo),
rootUrl,
).toString()
const createUrl = (mode: string, hash = true) => {
if (relativeFilePath == null) {
return null
}
const u = new url.URL(
pathJoin(
repoInfo.org,
repoInfo.repo,
mode,
head.value,
relativeFilePath,
),
rootUrl,
)
if (hash && lines) {
u.hash = lines
}
return u.toString()
}
const blobUrl = createUrl("blob")
const blameUrl = createUrl("blame")
const historyUrl = createUrl("commits", false)
const compareUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo, "compare", head.value),
rootUrl,
).toString()
// https://gitlab.com/recipeyak/recipeyak/merge_requests/new?merge_request%5Bsource_branch%5D=master
const prUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo, "merge_requests", "new"),
rootUrl,
)
prUrl.search = `merge_request%5Bsource_branch%5D=${head.value}`
return {
blobUrl,
blameUrl,
compareUrl,
historyUrl,
prUrl: prUrl.toString(),
repoUrl,
}
}
}
export class Bitbucket extends BaseProvider {
DEFAULT_HOSTNAMES = ["bitbucket.org"]
PROVIDER_NAME = "bitbucket"
async getUrls({
selection,
relativeFilePath,
head,
}: IBaseGetUrls): Promise<IUrlInfo | null> {
const repoInfo = await this.findOrgInfo()
if (repoInfo == null) {
return null
}
// https://bitbucket.org/recipeyak/recipeyak/src/master/app/main.py#lines-12:15
const rootUrl = `https://${repoInfo.hostname}/`
const { start, end } = selection
const lines = `lines-${start.line + 1}:${end.line + 1}`
const repoUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo),
rootUrl,
).toString()
const createUrl = (mode: string, hash = true) => {
if (relativeFilePath == null) {
return null
}
const u = new url.URL(
pathJoin(
repoInfo.org,
repoInfo.repo,
mode,
head.value,
relativeFilePath,
),
rootUrl,
)
if (hash && lines) {
u.hash = lines
}
return u.toString()
}
const blobUrl = createUrl("blob")
const blameUrl = createUrl("annotate")
const compareUrl = new url.URL(
pathJoin(
repoInfo.org,
repoInfo.repo,
"branches",
"compare",
head.value + "..",
),
rootUrl,
).toString()
const historyUrl = createUrl("history-node", false)
// "https://bitbucket.org/recipeyak/recipeyak/pull-requests/new?source=db99a912f5c4bffe11d91e163cd78ed96589611b"
const prUrl = new url.URL(
pathJoin(repoInfo.org, repoInfo.repo, "pull-requests", "new"),
rootUrl,
)
prUrl.search = `source=${head.value}`
return {
blobUrl,
blameUrl,
compareUrl,
historyUrl,
prUrl: prUrl.toString(),
repoUrl,
}
}
}
export const providers = [Bitbucket, Gitlab, Github]