Skip to content

Commit 0edad55

Browse files
Fixing copilot & other findings
1 parent 9a9ebab commit 0edad55

7 files changed

Lines changed: 57 additions & 54 deletions

File tree

docs.bzl

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -183,88 +183,79 @@ def docs(source_dir = "docs", data = [], deps = [], scan_code = [], known_good =
183183
data_with_docs_sources = _rewrite_needs_json_to_docs_sources(data)
184184
additional_combo_sourcelinks = _rewrite_needs_json_to_sourcelinks(data)
185185
_merge_sourcelinks(name = "merged_sourcelinks", sourcelinks = [":sourcelinks_json"] + additional_combo_sourcelinks, known_good = known_good)
186+
docs_data = data + [":sourcelinks_json"]
187+
combo_data = data_with_docs_sources + [":merged_sourcelinks"]
188+
189+
docs_env = {
190+
"SOURCE_DIRECTORY": source_dir,
191+
"DATA": str(data),
192+
"ACTION": "incremental",
193+
"SCORE_SOURCELINKS": "$(location :sourcelinks_json)",
194+
}
195+
docs_sources_env = {
196+
"SOURCE_DIRECTORY": source_dir,
197+
"DATA": str(data_with_docs_sources),
198+
"ACTION": "incremental",
199+
"SCORE_SOURCELINKS": "$(location :merged_sourcelinks)",
200+
}
201+
if known_good:
202+
docs_env["KNOWN_GOOD_JSON"] = "$(location "+ known_good + ")"
203+
docs_sources_env["KNOWN_GOOD_JSON"] = "$(location "+ known_good + ")"
204+
docs_data.append(known_good)
205+
combo_data.append(known_good)
186206

187207
py_binary(
188208
name = "docs",
189209
tags = ["cli_help=Build documentation:\nbazel run //:docs"],
190210
srcs = ["@score_docs_as_code//src:incremental.py"],
191-
data = data + [":sourcelinks_json"],
211+
data = docs_data,
192212
deps = deps,
193-
env = {
194-
"SOURCE_DIRECTORY": source_dir,
195-
"DATA": str(data),
196-
"ACTION": "incremental",
197-
"SCORE_SOURCELINKS": "$(location :sourcelinks_json)",
198-
},
213+
env = docs_env,
199214
)
200215

201216
py_binary(
202217
name = "docs_combo_experimental",
203218
tags = ["cli_help=Build full documentation with all dependencies:\nbazel run //:docs_combo_experimental"],
204219
srcs = ["@score_docs_as_code//src:incremental.py"],
205-
data = data_with_docs_sources + [":merged_sourcelinks"],
220+
data = combo_data,
206221
deps = deps,
207-
env = {
208-
"SOURCE_DIRECTORY": source_dir,
209-
"DATA": str(data_with_docs_sources),
210-
"ACTION": "incremental",
211-
"SCORE_SOURCELINKS": "$(location :merged_sourcelinks)",
212-
},
222+
env = docs_sources_env
213223
)
214224

215225
py_binary(
216226
name = "docs_link_check",
217227
tags = ["cli_help=Verify Links inside Documentation:\nbazel run //:link_check\n (Note: this could take a long time)"],
218228
srcs = ["@score_docs_as_code//src:incremental.py"],
219-
data = data,
229+
data = docs_data,
220230
deps = deps,
221-
env = {
222-
"SOURCE_DIRECTORY": source_dir,
223-
"DATA": str(data),
224-
"ACTION": "linkcheck",
225-
},
231+
env = docs_env
226232
)
227233

228234
py_binary(
229235
name = "docs_check",
230236
tags = ["cli_help=Verify documentation:\nbazel run //:docs_check"],
231237
srcs = ["@score_docs_as_code//src:incremental.py"],
232-
data = data + [":sourcelinks_json"],
238+
data = docs_data,
233239
deps = deps,
234-
env = {
235-
"SOURCE_DIRECTORY": source_dir,
236-
"DATA": str(data),
237-
"ACTION": "check",
238-
"SCORE_SOURCELINKS": "$(location :sourcelinks_json)",
239-
},
240+
env = docs_env,
240241
)
241242

242243
py_binary(
243244
name = "live_preview",
244245
tags = ["cli_help=Live preview documentation in the browser:\nbazel run //:live_preview"],
245246
srcs = ["@score_docs_as_code//src:incremental.py"],
246-
data = data + [":sourcelinks_json"],
247+
data = docs_data,
247248
deps = deps,
248-
env = {
249-
"SOURCE_DIRECTORY": source_dir,
250-
"DATA": str(data),
251-
"ACTION": "live_preview",
252-
"SCORE_SOURCELINKS": "$(location :sourcelinks_json)",
253-
},
249+
env = docs_env,
254250
)
255251

256252
py_binary(
257253
name = "live_preview_combo_experimental",
258254
tags = ["cli_help=Live preview full documentation with all dependencies in the browser:\nbazel run //:live_preview_combo_experimental"],
259255
srcs = ["@score_docs_as_code//src:incremental.py"],
260-
data = data_with_docs_sources + [":merged_sourcelinks"],
256+
data = combo_data,
261257
deps = deps,
262-
env = {
263-
"SOURCE_DIRECTORY": source_dir,
264-
"DATA": str(data_with_docs_sources),
265-
"ACTION": "live_preview",
266-
"SCORE_SOURCELINKS": "$(location :merged_sourcelinks)",
267-
},
258+
env = docs_sources_env
268259
)
269260

270261
score_virtualenv(

scripts_bazel/merge_sourcelinks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
if external => parse thing for module_name => look up known_good json for hash & url
3535
"""
3636

37+
3738
def main():
3839
parser = argparse.ArgumentParser(
3940
description="Merge multiple sourcelinks JSON files into one"
@@ -63,7 +64,18 @@ def main():
6364
for json_file in all_files:
6465
with open(json_file) as f:
6566
data = json.load(f)
67+
# If the file is empty e.g. '[]' there is nothing to parse, we continue
68+
if not data:
69+
continue
6670
metadata = data[0]
71+
if not isinstance(metadata, dict) or "module_name" not in metadata:
72+
logger.warning(
73+
f"Unexpected schema in sourcelinks file '{json_file}': "
74+
"expected first element to be a metadata dict "
75+
"with a 'module_name' key. "
76+
)
77+
# As we can't deal with bad JSON structure we just skip it
78+
continue
6779
if metadata["module_name"] and metadata["module_name"] != "local_module":
6880
hash, repo = parse_info_from_known_good(
6981
known_good_json=args.known_good, module_name=metadata["module_name"]

src/extensions/score_source_code_linker/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ py_library(
5555
"testlink.py",
5656
"xml_parser.py",
5757
"helpers.py",
58+
"module_source_links.py",
5859
],
5960
imports = ["."],
6061
visibility = ["//visibility:public"],

src/extensions/score_source_code_linker/helpers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_github_link_from_git(
5757
def get_github_link_from_json(
5858
metadata: ModuleInfo,
5959
link: NeedLink | DataForTestLink | DataOfTestCase | None = None,
60-
):
60+
) -> str:
6161
if link is None:
6262
link = DefaultNeedLink()
6363
base_url = metadata.url
@@ -67,24 +67,24 @@ def get_github_link_from_json(
6767

6868
def parse_module_name_from_path(path: Path) -> str:
6969
"""
70-
Parse out the Module-Name from the filename gotten
71-
/home/user/.cache/bazel/aksj37981712/external/score_docs_as_code+/src/tests/testfile.py
72-
=> score_docs_as_code
70+
Parse out the Module-Name from the filename:
71+
Combo Example:
72+
Path: external/score_docs_as_code+/src/helper_lib/test_helper_lib.py
73+
=> score_docs_as_code
74+
Local:
75+
Path: src/helper_lib/test_helper_lib.py
76+
=> local_module
77+
7378
"""
7479

7580
# COMBO BUILD
76-
# If external is in the filepath that gets parsed =>
77-
# file is in an external module => combo build
78-
# Example Path:
79-
# PosixPath('external/score_docs_as_code+/src/helper_lib/test_helper_lib.py'
8081

8182
if str(path).startswith("external/"):
8283
# This allows for files / folders etc. to have `external` in their name too.
8384
module_raw = str(path).removeprefix("external/")
8485
filepath_split = str(module_raw).split("/", maxsplit=1)
8586
return str(filepath_split[0].removesuffix("+"))
8687
# We return this when we are in a local build `//:docs` the rest of DaC knows
87-
# What to do then if it encounters this module_name
8888
return "local_module"
8989

9090

src/extensions/score_source_code_linker/module_source_links.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def store_module_source_links_json(
7878
# After `rm -rf _build` or on clean builds the directory does not exist, so we need
7979
# to create it
8080
file.parent.mkdir(exist_ok=True)
81-
with open(file, "w") as f:
81+
with open(file, "w", encoding="utf-8") as f:
8282
json.dump(
8383
source_code_links,
8484
f,

src/extensions/score_source_code_linker/need_source_links.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ def group_by_need(
123123
124124
{
125125
"need": "<need_id>",
126-
"module_name": <module_name>,
127-
"hash": <git hash>,
128-
"url": <github base url>,
129126
"links": {
130127
"CodeLinks": [NeedLink, NeedLink, ...],
131128
"TestLinks": [testlink, testlink, ...]

src/incremental.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def get_env(name: str) -> str:
9090
base_arguments.append(f"-A=github_repo={args.github_repo}")
9191
base_arguments.append("-A=github_version=main")
9292
base_arguments.append(f"-A=doc_path={get_env('SOURCE_DIRECTORY')}")
93+
if os.getenv("KNOWN_GOOD_JSON"):
94+
base_arguments.append(f"--define=KNOWN_GOOD_JSON={get_env('KNOWN_GOOD_JSON')}")
9395

9496
action = get_env("ACTION")
9597
if action == "live_preview":

0 commit comments

Comments
 (0)