66from osa_tool .core .git .metadata import RepositoryMetadata
77from osa_tool .operations .docs .readme_generation .readme_utils import (
88 find_in_repo_tree ,
9+ resolve_repo_host_and_root_url ,
910 save_sections ,
1011 to_repo_relative_link ,
1112)
1213from osa_tool .tools .repository_analysis .sourcerank import SourceRank
1314from osa_tool .utils .logger import logger
14- from osa_tool .utils .utils import osa_project_root , parse_folder_name
15+ from osa_tool .utils .utils import osa_project_root , resolve_repo_path
1516
1617
1718class CommunityTemplateBuilder :
@@ -25,21 +26,26 @@ def __init__(self, config_manager: ConfigManager, metadata: RepositoryMetadata):
2526 self .sourcerank = SourceRank (self .config_manager )
2627 self .metadata = metadata
2728 self .template_path = os .path .join (osa_project_root (), "docs" , "templates" , "community.toml" )
28- self .url_path = f"https://{ self .config_manager .get_git_settings ().host_domain } /{ self .config_manager .get_git_settings ().full_name } /"
29+ git = self .config_manager .get_git_settings ()
30+ self .host , self .url_path = resolve_repo_host_and_root_url (
31+ repo_url = self .repo_url ,
32+ clone_url_http = self .metadata .clone_url_http ,
33+ host = git .host ,
34+ host_domain = git .host_domain ,
35+ full_name = git .full_name ,
36+ )
2937 self ._template = self .load_template ()
3038
31- self .repo_root = os .path .join (os .getcwd (), parse_folder_name (self .repo_url ))
32- self .repo_path = os .path .join (
33- self .repo_root , "." + self .config_manager .get_git_settings ().host
34- )
39+ self .repo_root = str (resolve_repo_path (self .repo_url ))
40+ self .repo_path = self .repo_root if "sourcecraft" in self .host else os .path .join (self .repo_root , f".{ self .host } " )
3541 self .code_of_conduct_to_save = os .path .join (self .repo_path , "CODE_OF_CONDUCT.md" )
3642 self .security_to_save = os .path .join (self .repo_path , "SECURITY.md" )
3743 self ._setup_paths_depends_on_platform ()
3844
3945 def _setup_paths_depends_on_platform (self ) -> None :
4046 """Configures file save paths depending on the platform."""
4147
42- if "gitlab" in self .config_manager . get_git_settings (). host :
48+ if "gitlab" in self .host :
4349 self .issue_templates_path = os .path .join (self .repo_path , "issue_templates" )
4450 self .merge_request_templates_path = os .path .join (self .repo_path , "merge_request_templates" )
4551 os .makedirs (self .issue_templates_path , exist_ok = True )
@@ -52,26 +58,22 @@ def _setup_paths_depends_on_platform(self) -> None:
5258 self .vulnerability_disclosure_to_save = os .path .join (
5359 self .issue_templates_path , "Vulnerability_Disclosure.md"
5460 )
55- elif "github" in self .config_manager . get_git_settings (). host :
61+ elif "github" in self .host :
5662 self .issue_templates_path = os .path .join (self .repo_path , "ISSUE_TEMPLATE" )
5763 os .makedirs (self .issue_templates_path , exist_ok = True )
5864 self .pr_to_save = os .path .join (self .repo_path , "PULL_REQUEST_TEMPLATE.md" )
5965 self .docs_issue_to_save = os .path .join (self .issue_templates_path , "DOCUMENTATION_ISSUE.md" )
6066 self .feature_issue_to_save = os .path .join (self .issue_templates_path , "FEATURE_ISSUE.md" )
6167 self .bug_issue_to_save = os .path .join (self .issue_templates_path , "BUG_ISSUE.md" )
62- elif "sourcecraft" in self .config_manager .get_git_settings ().host :
63- self .code_of_conduct_to_save = os .path .join (self .repo_root , "CODE_OF_CONDUCT.md" )
64- self .security_to_save = os .path .join (self .repo_root , "SECURITY.md" )
65- self .pr_to_save = os .path .join (self .repo_root , "PULL_REQUEST_TEMPLATE.md" )
66- self .docs_issue_to_save = os .path .join (self .repo_root , "DOCUMENTATION_ISSUE.md" )
67- self .feature_issue_to_save = os .path .join (self .repo_root , "FEATURE_ISSUE.md" )
68- self .bug_issue_to_save = os .path .join (self .repo_root , "BUG_ISSUE.md" )
68+ elif "sourcecraft" in self .host :
69+ self .pr_to_save = os .path .join (self .repo_path , "PULL_REQUEST_TEMPLATE.md" )
70+ self .docs_issue_to_save = os .path .join (self .repo_path , "DOCUMENTATION_ISSUE.md" )
71+ self .feature_issue_to_save = os .path .join (self .repo_path , "FEATURE_ISSUE.md" )
72+ self .bug_issue_to_save = os .path .join (self .repo_path , "BUG_ISSUE.md" )
6973
7074 def _local_repo_link (self , pattern : str , * , from_path : str ) -> str :
7175 rel_path = find_in_repo_tree (self .sourcerank .tree , pattern )
7276 from_dir = os .path .relpath (os .path .dirname (from_path ), self .repo_root ).replace ("\\ " , "/" )
73- if from_dir == "." :
74- from_dir = ""
7577 return to_repo_relative_link (rel_path , from_dir = from_dir )
7678
7779 def load_template (self ) -> dict :
@@ -81,6 +83,12 @@ def load_template(self) -> dict:
8183 with open (self .template_path , "rb" ) as file :
8284 return tomli .load (file )
8385
86+ def _build_security_repo_reference (self ) -> str :
87+ """Return a web repository reference or an editable local placeholder."""
88+ if self .url_path != "." :
89+ return self .url_path .rstrip ("/" )
90+ return f"...{ resolve_repo_path (self .repo_url ).name } "
91+
8492 def build_code_of_conduct (self ) -> bool :
8593 """
8694 Generates and saves the CODE_OF_CONDUCT.md file.
@@ -99,13 +107,12 @@ def build_code_of_conduct(self) -> bool:
99107 def build_pull_request (self ) -> bool :
100108 """Generates and saves the PULL_REQUEST_TEMPLATE.md file."""
101109 try :
110+ contributing_url = ""
102111 if self .sourcerank .contributing_presence ():
103112 pattern = r"\b\w*contribut\w*\.(md|rst|txt)$"
104113 contributing_url = self ._local_repo_link (pattern , from_path = self .pr_to_save )
105- else :
106- contributing_url = "Provide the link"
107114
108- content = self ._template ["pull_request" ].format (contributing_url = contributing_url )
115+ content = self ._template ["pull_request" ].format (contributing_url = contributing_url or "Provide the link" )
109116 save_sections (content , self .pr_to_save )
110117 logger .info (f"PULL_REQUEST_TEMPLATE.md successfully generated in folder { os .path .dirname (self .pr_to_save )} " )
111118 except Exception as e :
@@ -195,9 +202,7 @@ def build_security(self) -> bool:
195202 Has the task been completed successfully
196203 """
197204 try :
198- content = self ._template [f"security_{ self .config_manager .get_git_settings ().host } " ].format (
199- repo_url = self .repo_url
200- )
205+ content = self ._template [f"security_{ self .host } " ].format (repo_url = self ._build_security_repo_reference ())
201206 save_sections (content , self .security_to_save )
202207 logger .info (f"SECURITY.md successfully generated in folder { self .repo_path } " )
203208 except Exception as e :
0 commit comments