Skip to content

Commit 2145592

Browse files
committed
feat: upgrade language support
1 parent ec7b8aa commit 2145592

File tree

7 files changed

+90
-30
lines changed

7 files changed

+90
-30
lines changed

Dockerfile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ RUN apk add --no-cache graphviz curl git
44

55
COPY . /action_internal
66

7-
# python lsif
8-
RUN apk add --no-cache python3 py3-pip && \
9-
pip3 install -r /action_internal/requirements.txt && \
10-
pip3 install --upgrade git+https://github.com/sourcegraph/lsif-py.git
11-
127
# scip converter
138
RUN git clone https://github.com/sourcegraph/scip.git --depth=1 ./scip_repo && \
149
cd scip_repo && \
@@ -26,6 +21,15 @@ RUN apk add openjdk8 gradle maven \
2621
&& ./coursier bootstrap --standalone -o scip-java com.sourcegraph:scip-java_2.13:0.8.18 --main com.sourcegraph.scip_java.ScipJava \
2722
&& ./scip-java --help
2823

24+
# node lsif and python scip
25+
RUN apk add --update nodejs npm \
26+
&& npm install -g lsif \
27+
&& npm install -g @sourcegraph/scip-python
28+
29+
# python runtime for main script
30+
RUN apk add --no-cache python3 py3-pip && \
31+
pip3 install -r /action_internal/requirements.txt
32+
2933
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk
3034

3135
ENTRYPOINT ["python3", "/action_internal/main.py"]

debug.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import os
2+
import pathlib
23

4+
import config
35
from diff import gen_diff
46
from index import gen_index
57
from utils import check_call
68
import shutil
79

810

911
def debug_main():
10-
# pull some repos for test
11-
check_call(["git", "clone", "--depth=2", "https://github.com/gin-gonic/gin.git"])
12-
13-
gen_index("golang", "gin", "")
12+
# clone junit4 repository
13+
check_call(
14+
["git", "clone", "--depth=2", "https://github.com/junit-team/junit4.git"]
15+
)
16+
junit4_dir = pathlib.Path(config.USER_DIR) / "junit4"
17+
gen_index(
18+
"java",
19+
junit4_dir.as_posix(),
20+
"scip-java index -- "
21+
"package -DskipTests "
22+
"--batch-mode "
23+
"--errors "
24+
"--settings .github/workflows/settings.xml",
25+
)
26+
os.chdir(junit4_dir)
27+
gen_diff(junit4_dir.as_posix(), "HEAD~1", "HEAD", "")
28+
os.chdir(config.USER_DIR)
29+
shutil.rmtree(junit4_dir)
1430

15-
os.chdir("gin")
16-
gen_diff("HEAD~1", "HEAD", "")
17-
os.chdir("..")
31+
# clone gin repository
32+
check_call(["git", "clone", "--depth=2", "https://github.com/gin-gonic/gin.git"])
33+
gin_dir = pathlib.Path(config.USER_DIR) / "gin"
34+
os.chdir(gin_dir)
35+
gen_index("golang", gin_dir.as_posix(), "")
36+
gen_diff(gin_dir.as_posix(), "HEAD~1", "HEAD", "")
37+
os.chdir(config.USER_DIR)
38+
shutil.rmtree(gin_dir)
1839

19-
# clean up
20-
shutil.rmtree("gin")
40+
# clone requests repository
41+
check_call(["git", "clone", "--depth=2", "https://github.com/psf/requests.git"])
42+
requests_dir = pathlib.Path(config.USER_DIR) / "requests"
43+
os.chdir(requests_dir)
44+
gen_index("python", requests_dir.as_posix(), "")
45+
gen_diff(requests_dir.as_posix(), "HEAD~1", "HEAD", "")
46+
os.chdir(config.USER_DIR)
47+
shutil.rmtree(requests_dir)

diff.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import pathlib
2+
13
import config
24
from utils import check_call
35

46

5-
def gen_diff(before_sha: str, after_sha: str, lsif_file: str):
6-
# gen diff
7-
set_safe_git_dir()
7+
def gen_diff(src_dir: str, before_sha: str, after_sha: str, lsif_file: str):
8+
set_safe_git_dir(src_dir)
89

910
cmds = [
1011
"srctx",
1112
"diff",
1213
"--src",
13-
config.USER_DIR,
14+
src_dir,
1415
"--before",
1516
before_sha,
1617
"--after",
@@ -20,12 +21,23 @@ def gen_diff(before_sha: str, after_sha: str, lsif_file: str):
2021
"--outputCsv",
2122
config.CSV_RESULT_FILE,
2223
]
23-
if lsif_file:
24+
if not lsif_file:
25+
lsif_file_loc = pathlib.Path(src_dir) / "dump.lsif"
26+
scip_file_loc = pathlib.Path(src_dir) / "index.scip"
27+
28+
if lsif_file_loc.is_file():
29+
cmds += ["--lsif", str(lsif_file_loc.absolute())]
30+
elif scip_file_loc.is_file():
31+
cmds += ["--scip", str(scip_file_loc.absolute())]
32+
else:
33+
raise RuntimeError(f"no index file found in {src_dir}")
34+
else:
2435
cmds += ["--lsif", lsif_file]
36+
2537
check_call(cmds)
2638

2739

28-
def set_safe_git_dir():
40+
def set_safe_git_dir(src_dir: str):
2941
check_call(
30-
["git", "config", "--global", "--add", "safe.directory", config.USER_DIR]
42+
["git", "config", "--global", "--add", "safe.directory", src_dir]
3143
)

index.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def gen_index(lang: str, directory: str, index_command: str):
1313
if index_command:
1414
logger.info(f"custom index command: {index_command}")
1515
check_call(index_command.split(" "))
16+
return
1617

1718
if lang == "golang":
1819
gen_golang_index()
@@ -22,9 +23,10 @@ def gen_index(lang: str, directory: str, index_command: str):
2223
gen_java_and_kotlin_index()
2324
elif lang == "kotlin":
2425
gen_java_and_kotlin_index()
26+
elif lang == "node":
27+
gen_node_index()
2528
else:
26-
logger.error("no index mapping")
27-
return
29+
raise RuntimeError(f"lang {lang} not support")
2830
finally:
2931
os.chdir(current_directory)
3032

@@ -36,9 +38,21 @@ def gen_golang_index():
3638
def gen_java_and_kotlin_index():
3739
# https://sourcegraph.github.io/scip-java/docs/getting-started.html#run-scip-java-index
3840
check_call(["scip-java", "index", "--output", "index.scip"])
39-
# https://github.com/sourcegraph/scip/blob/main/docs/CLI.md
40-
check_call(["scip", "convert", "--from", "index.scip", "--to", "dump.lsif"])
4141

4242

4343
def gen_py_index():
44-
check_call(["lsif-py", ".", "--file", "./dump.lsif"])
44+
check_call(["scip-python", "index", ".", "--project-name", "srctx"])
45+
46+
47+
def gen_node_index():
48+
check_call(
49+
[
50+
"lsif",
51+
"tsc",
52+
"-p",
53+
"./tsconfig.json",
54+
"--package",
55+
"./package.json",
56+
"--stdout",
57+
]
58+
)

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def main():
4848
config.USER_DIR = "."
4949

5050
# data prepare
51-
set_safe_git_dir()
51+
set_safe_git_dir(config.USER_DIR)
5252
files = os.listdir(config.USER_DIR)
5353
logger.info(f"files: {files}")
5454

5555
if not lsif_file:
5656
gen_index(lang, config.USER_DIR, index_command)
57-
gen_diff(before_sha, after_sha, lsif_file)
57+
gen_diff(config.USER_DIR, before_sha, after_sha, lsif_file)
5858

5959
repo_name = os.getenv("GITHUB_REPOSITORY")
6060
file_list = load_index_data()
@@ -160,7 +160,9 @@ def dot_to_svg(dot_file):
160160
return svg_bytes
161161

162162

163-
def sort_files_by_impact(files: typing.Iterable[FileMetrics]) -> typing.List[FileMetrics]:
163+
def sort_files_by_impact(
164+
files: typing.Iterable[FileMetrics],
165+
) -> typing.List[FileMetrics]:
164166
def sort_key(f: FileMetrics):
165167
return f.affectedLineCount
166168

test_index.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from index import gen_index
21
from main import export_csv_table, load_index_data
32

43

utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import subprocess
2+
from loguru import logger
23

34

45
def check_call(commands: list):
6+
logger.info(f"check calling: {commands}")
57
subprocess.check_call(commands)

0 commit comments

Comments
 (0)