Skip to content

Commit

Permalink
Merge pull request #2 from avilaton/template-param
Browse files Browse the repository at this point in the history
Add template parameter
  • Loading branch information
avilaton authored Feb 26, 2021
2 parents 461973c + 54debda commit 051878d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add this to your `.pre-commit-config.yaml`

```yaml
- repo: https://github.com/avilaton/add-msg-issue-prefix-hook
rev: v0.0.1 # Use the ref you want to point at
rev: v0.0.5 # Use the ref you want to point at
hooks:
- id: add-msg-issue-prefix
```
Expand All @@ -21,3 +21,16 @@ and install prepare-commit-msg hooks using
```
pre-commit install --hook-type prepare-commit-msg
```

### Optional template argument
Change how the issue is rendered to the commit message using the `--template` argument.

```yaml
- repo: https://github.com/avilaton/add-msg-issue-prefix-hook
rev: v0.0.5 # Use the ref you want to point at
hooks:
- id: add-msg-issue-prefix
args:
- --template=[{}]

```
14 changes: 12 additions & 2 deletions add_msg_issue_prefix_hook/add_msg_issue_prefix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import argparse
import sys
import re
import subprocess
Expand All @@ -12,7 +13,15 @@ def get_ticket_id_from_branch_name(branch):


def main():
commit_msg_filepath = sys.argv[1]
parser = argparse.ArgumentParser()
parser.add_argument("commit_msg_filepath")
parser.add_argument(
'-t', '--template', default="[{}]",
help='Template to render ticket id into',
)
args = parser.parse_args()
commit_msg_filepath = args.commit_msg_filepath
template = args.template

branch = ""
try:
Expand All @@ -31,7 +40,8 @@ def main():
content_subject = content.split("\n", maxsplit=1)[0].strip()
f.seek(0, 0)
if issue_number and issue_number not in content_subject:
f.write("[{}] {}".format(issue_number, content))
prefix = template.format(issue_number)
f.write("{} {}".format(prefix, content))
else:
f.write(content)

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
name = "y"
name = "add_msg_issue_prefix_hook"
version = "v0.0.4"
description = "Commit hook preparing message using branch name"
authors = ["Gastón Avila <[email protected]>"]
Expand All @@ -10,6 +10,9 @@ python = "^3.5"
[tool.poetry.dev-dependencies]
pytest = "^5.0.0"

[tool.poetry.scripts]
add-msg-issue-prefix = "add_msg_issue_prefix_hook.add_msg_issue_prefix:main"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 051878d

Please sign in to comment.