diff --git a/.gitignore b/.gitignore index 7ec2d62..23ca4ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# direnv +.envrc +.direnv/ + # folders .vscode/ venv/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 85efe94..d76c408 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,29 @@ default_install_hook_types: [commit-msg, prepare-commit-msg] repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-ast + - id: check-added-large-files + - id: check-json + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: detect-private-key + - id: end-of-file-fixer + - id: pretty-format-json + args: ["--indent=\t", "--no-sort-keys"] + - id: requirements-txt-fixer + - id: trailing-whitespace + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.4.8 + hooks: + # Run the linter. + - id: ruff + args: ["--fix"] + # Run the formatter. + - id: ruff-format - repo: local hooks: - name: commit-msg diff --git a/git_hooks/prepare_commit_msg.py b/git_hooks/prepare_commit_msg.py index 9e70f73..6b8d356 100644 --- a/git_hooks/prepare_commit_msg.py +++ b/git_hooks/prepare_commit_msg.py @@ -107,47 +107,66 @@ def extract_branch_data(branch: str) -> dict[str, str]: } +def retrieve_linear_data(issue: str) -> dict[str, str]: + # If LINEAR_API_KEY is set and issue number is not empty, fetch issue details + if LINEAR_API_KEY: + try: + linear_issue = retrieve_linear_issue(issue) + return { + "commit_msg_title": linear_issue["title"], + "commit_msg_body": linear_issue["description"], + } + except Exception as exception: + error_details = [ + "# Error while fetching issue details from Linear:", + "#", + ] + if hasattr(exception, "errors") and isinstance(exception.errors, list): + error_details += [f"#\t{e['message']}" for e in exception.errors] + else: + error_details += [f"#\t{str(exception)}"] + error_details += ["#"] + return { + "edit_mode_commit_msg_body": "\n".join(error_details), + } + else: + linear_info = [ + "# NEW FEATURE: Use Linear API key to fetch commit title and description:", + "#", + "#\tTo populate commit message with title and description for an issue number detected", + "#\tin the branch name, ensure that the environment variable LINEAR_API_KEY is set.", + "#\tGet this from Personal API keys section at linear.app/tillit/settings/api.", + "#", + ] + return { + "edit_mode_commit_msg_body": "\n".join(linear_info), + } + + def prepare_commit_msg(raw_commit_msg: str, branch: str) -> str: - commit_msg_lines = raw_commit_msg.strip().splitlines() + commit_msg_lines = raw_commit_msg.splitlines() branch_data = extract_branch_data(branch) + raw_commit_msg_title = commit_msg_lines[0] if len(commit_msg_lines) > 0 else "" + + linear_data = {} + if (issue := branch_data["issue"]) and not raw_commit_msg_title: + linear_data = retrieve_linear_data(issue) + commit_msg_title_data = extract_commit_msg_title_data( - commit_msg_lines[0] if len(commit_msg_lines) > 0 else "" + linear_data.get("commit_msg_title") or raw_commit_msg_title ) - issue = commit_msg_title_data["issue"] or branch_data["issue"] commit_type = commit_msg_title_data["commit_type"] or branch_data["commit_type"] commit_msg_title = ( commit_msg_title_data["commit_msg_title"] or branch_data["commit_msg_title"] ) - commit_msg_body = "\n".join(commit_msg_lines[1:]) if EDITOR_TEXT in raw_commit_msg: - commit_msg_body += f"\n{common.commented_commit_type_doc}" - # If LINEAR_API_KEY is set and issue number is not empty, fetch issue details - if LINEAR_API_KEY and issue: - try: - linear_issue = retrieve_linear_issue(issue) - commit_msg_title = linear_issue["title"] or commit_msg_title - commit_msg_body = linear_issue["description"] + commit_msg_body - except Exception as exception: - error_details = [ - "# Error while fetching issue details from Linear:", - "#", - ] - if hasattr(exception, "errors") and isinstance(exception.errors, list): - error_details += [f"#\t{e['message']}" for e in exception.errors] - else: - error_details += [f"#\t{str(exception)}"] - error_details += ["#"] - commit_msg_body += "\n" + "\n".join(error_details) - else: - linear_info = [ - "# Fetching issue details from Linear using API key:", - "#", - "#\tTo populate commit message with title and description for an issue number detected", - "#\tin the branch name, ensure that the environment variable LINEAR_API_KEY is set.", - "#\tGet this from Personal API keys section at linear.app/tillit/settings/api.", - "#", - ] - commit_msg_body += "\n" + "\n".join(linear_info) + commit_msg_lines.append(common.commented_commit_type_doc) + if edit_mode_commit_msg_body := linear_data.get("edit_mode_commit_msg_body"): + commit_msg_lines.append(edit_mode_commit_msg_body) + linear_commit_msg_lines = ( + [linear_data["commit_msg_body"]] if linear_data.get("commit_msg_body") else [] + ) + commit_msg_body = "\n".join(linear_commit_msg_lines + commit_msg_lines[1:]) # Write to commit message message = commit_msg_title