Repository: bridgecrewio/checkov

Issue:
broken links in policy index
https://www.checkov.io/5.Policy%20Index/all.html

782 links to files directly in [`main/checkov/terraform/checks/graph_checks/`](https://github.com/bridgecrewio/checkov/tree/main/checkov/terraform/checks/graph_checks) (missing the provider subdirectories before the direct file name)

---

unrelated, it'd also be nice if all policy index links were clickable as well


Discussion:
hey @PatMyron thanks for reaching out.
Nice catch 🥇 should be no problem to add the provider to the link. For the link I need to check, where exactly the docs are generated 🧐 

Resolving patch:
diff --git a/checkov/common/checks_infra/registry.py b/checkov/common/checks_infra/registry.py
--- a/checkov/common/checks_infra/registry.py
+++ b/checkov/common/checks_infra/registry.py
@@ -49,7 +49,7 @@ def _load_checks_from_dir(self, directory: str, external_check: bool) -> None:
                             self.logger.error(f"Loaded data from JSON is not Dict. Skipping. Data: {check_json}.")
                             continue
                         check = self.parser.parse_raw_check(
-                            check_json, resources_types=self._get_resource_types(check_json), check_path=f'{dir}/{file}'
+                            check_json, resources_types=self._get_resource_types(check_json), check_path=f'{root}/{file}'
                         )
                         if not any(c for c in self.checks if check.id == c.id):
                             if external_check:
diff --git a/checkov/common/util/docs_generator.py b/checkov/common/util/docs_generator.py
--- a/checkov/common/util/docs_generator.py
+++ b/checkov/common/util/docs_generator.py
@@ -2,6 +2,7 @@
 
 from __future__ import annotations
 
+import os
 import re
 import inspect
 from typing import List, Optional, Tuple, Union
@@ -20,6 +21,7 @@
 from checkov.cloudformation.checks.resource.registry import cfn_registry as cfn_registry
 from checkov.common.checks.base_check_registry import BaseCheckRegistry
 from checkov.common.checks_infra.registry import BaseRegistry as BaseGraphRegistry, get_graph_checks_registry
+from checkov.common.runners.base_runner import strtobool
 from checkov.dockerfile.registry import registry as dockerfile_registry
 from checkov.github.registry import registry as github_configuration_registry
 from checkov.github_actions.checks.registry import registry as github_actions_jobs_registry
@@ -38,6 +40,7 @@
 
 ID_PARTS_PATTERN = re.compile(r'([^_]*)_([^_]*)_(\d+)')
 CODE_LINK_BASE = 'https://github.com/bridgecrewio/checkov/blob/main/checkov'
+CREATE_MARKDOWN_HYPERLINKS = strtobool(os.getenv("CHECKOV_CREATE_MARKDOWN_HYPERLINKS", "FALSE"))
 SKIP_CHECK_IDS = {
     "CKV_SECRET_10",  # this is an intermediate step, which is needed for another check
 }
@@ -72,6 +75,10 @@ def get_check_link(absolute_path: str) -> str:
     temp = absolute_path.split("checkov")
     # this will even work in the likely event that you're running checkov from a folder called checkov
     link = f'{CODE_LINK_BASE}{temp[len(temp)-1]}'
+
+    if CREATE_MARKDOWN_HYPERLINKS:
+        return f"[{absolute_path.rsplit('/', maxsplit=1)[1]}]({link})"
+
     return link
 
 


Test patch:
diff --git a/tests/common/utils/test_docs_generator.py b/tests/common/utils/test_docs_generator.py
--- a/tests/common/utils/test_docs_generator.py
+++ b/tests/common/utils/test_docs_generator.py
@@ -1,6 +1,8 @@
 import unittest
 
-from checkov.common.util.docs_generator import get_compare_key
+from pytest_mock import MockerFixture
+
+from checkov.common.util.docs_generator import get_compare_key, get_check_link
 
 
 class TestOutputSorting(unittest.TestCase):
@@ -66,5 +68,30 @@ def test_sorting_by_resource_id(self):
         self.assertEqual(sorted_list[2], ['CKV_AWS_1', '', 'aws_ebs_volume', '', ''])
         self.assertEqual(sorted_list[3], ['CKV_AWS_1', '', 'aws_s3_bucket', '', ''])
 
+
+def test_get_check_link():
+    # given
+    abs_path = "/path/to/checkov/checkov/terraform/checks/resource/aws/LambdaXrayEnabled.py"
+
+    # when
+    link = get_check_link(absolute_path=abs_path)
+
+    # then
+    assert link == "https://github.com/bridgecrewio/checkov/blob/main/checkov/terraform/checks/resource/aws/LambdaXrayEnabled.py"
+
+
+def test_get_check_link_for_markdown(mocker: MockerFixture):
+    # given
+    abs_path = "/path/to/checkov/checkov/terraform/checks/resource/aws/LambdaXrayEnabled.py"
+
+    mocker.patch("checkov.common.util.docs_generator.CREATE_MARKDOWN_HYPERLINKS", return_value=True)
+
+    # when
+    link = get_check_link(absolute_path=abs_path)
+
+    # then
+    assert link == "[LambdaXrayEnabled.py](https://github.com/bridgecrewio/checkov/blob/main/checkov/terraform/checks/resource/aws/LambdaXrayEnabled.py)"
+
+
 if __name__ == '__main__':
     unittest.main()