-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgithub.mts
More file actions
105 lines (101 loc) · 2.74 KB
/
Copy pathgithub.mts
File metadata and controls
105 lines (101 loc) · 2.74 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
import * as vscode from 'vscode'
import { getStaticParsed, parse as parseToml } from 'local-toml-wasm'
import ini from 'ini'
export function orgOrUserFromString(url: string): string | undefined {
const ghHTTP =
/^(?:git\+)?https?:\/\/(?:www.)?github.com(?::443|:80)?\/(?<target>[^/?#]*)(?=\/|$)/u // socket-hook: allow regex-alternation-order
const ghGit =
/^(?:git(?:\+ssh)?:\/\/)?(?<user>[^@]+@)?github.com[/:](?<target>[^/?#]*)(?=\/|$)/u // socket-hook: allow regex-alternation-order
const match = ghHTTP.exec(url) || ghGit.exec(url)
if (match) {
return match.groups?.['target'] || match.groups?.['user']
}
return undefined
}
/**
* Looks around a workspace folder root for some configuration that would let us
* directly install the github app against rather than asking for too much
* permissions.
*
* @param workspaceRootURI
*/
export async function sniffForGithubOrgOrUser(
workspaceRootURI: vscode.Uri,
): Promise<string | undefined> {
// package.json repository
try {
const pkg = JSON.parse(
Buffer.from(
await vscode.workspace.fs.readFile(
vscode.Uri.joinPath(workspaceRootURI, 'package.json'),
),
).toString(),
)
const repoTopLevel = pkg?.repository
let url: string
if (typeof repoTopLevel === 'string') {
url = repoTopLevel
} else {
url = repoTopLevel?.url
}
if (url) {
const found = orgOrUserFromString(url)
if (found) {
return found
}
}
} catch (e) {}
// poetry in pyproject.toml
try {
const pyproject = getStaticParsed(
parseToml(
Buffer.from(
await vscode.workspace.fs.readFile(
vscode.Uri.joinPath(workspaceRootURI, 'pyproject.toml'),
),
).toString(),
),
) as {
tool?:
| {
poetry?:
| {
repository?: string | undefined
}
| undefined
}
| undefined
}
const url = pyproject.tool?.poetry?.repository
if (url) {
const found = orgOrUserFromString(url)
if (found) {
return found
}
}
} catch (e) {}
// git remotes?
try {
const gitConfig = ini.parse(
Buffer.from(
await vscode.workspace.fs.readFile(
vscode.Uri.joinPath(workspaceRootURI, '.git', 'config'),
),
).toString(),
)
const keys = Object.keys(gitConfig)
for (let i = 0, { length } = keys; i < length; i += 1) {
const key = keys[i]!
if (key.startsWith('remote ')) {
const url = gitConfig[key]?.url
if (url) {
const found = orgOrUserFromString(url)
if (found) {
return found
}
}
}
}
} catch (e) {}
return undefined
}