/
sg.py
72 lines (61 loc) · 2.06 KB
/
sg.py
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
import os
import re
import requests
from typing import Dict, Any
SOURCEGRAPH_API_URL = "https://sourcegraph.com/.api/graphql"
SOURCEGRAPH_API_TOKEN = os.environ["SOURCEGRAPH_API_TOKEN"]
SOURCEGRAPH_API_HEADERS = {
"Content-Type": "application/json",
"Authorization": f"token {SOURCEGRAPH_API_TOKEN}",
}
SOURCEGRAPH_GRAPHQL_HIGHLIGHTED_FILE_RANGE_QUERY = """
query HighlightedFile(
$repoName: String!
$commitID: String!
$filePath: String!
$startLine: Int!
$endLine: Int!
) {
repository(name: $repoName) {
commit(rev: $commitID) {
file(path: $filePath) {
content
highlight(disableTimeout: true, format: HTML_HIGHLIGHT) {
lineRanges(ranges: [{startLine: $startLine, endLine: $endLine}])
}
}
}
}
}
"""
def query_highlighted_file_line_range(
repo_name: str, commit_id: str, file_path: str, start_line: int, end_line: int
):
data = {
"query": SOURCEGRAPH_GRAPHQL_HIGHLIGHTED_FILE_RANGE_QUERY,
"variables": {
"repoName": repo_name,
"commitID": commit_id,
"filePath": file_path,
"startLine": start_line,
"endLine": end_line,
},
}
response = requests.post(
SOURCEGRAPH_API_URL, headers=SOURCEGRAPH_API_HEADERS, json=data
)
file = response.json()["data"]["repository"]["commit"]["file"]
content = file["content"]
highlighted_range = "\n".join(file["highlight"]["lineRanges"][0])
return content, highlighted_range
url_regexp = re.compile(r"https:\/\/sourcegraph\.com\/(.*?)(@.*)?\/-\/blob\/(.*)")
def parse_url(url: str) -> Dict[str, Any]:
groups = url_regexp.match(url)
repo_name, commit_id, file_path = groups.group(1), groups.group(2), groups.group(3)
if commit_id:
commit_id = commit_id[1:]
else:
commit_id = "HEAD"
if "?" in file_path:
file_path = file_path.split("?")[0]
return repo_name, commit_id, file_path