Skip to content

Commit

Permalink
T-17808/feat: Grab linear title and body from linear ref if API key i…
Browse files Browse the repository at this point in the history
…s present
  • Loading branch information
brtkwr committed Jun 24, 2024
1 parent 0685a51 commit fb4ae82
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# direnv
.envrc
.direnv/

# folders
.vscode/
venv/
Expand Down
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
83 changes: 51 additions & 32 deletions git_hooks/prepare_commit_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb4ae82

Please sign in to comment.