Skip to content

Commit 7b2af3f

Browse files
committed
chore: Add DEFAULT_COMMIT_TYPE env variable
1 parent 9aedd78 commit 7b2af3f

2 files changed

Lines changed: 128 additions & 2 deletions

File tree

git_hooks/prepare_commit_msg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""This hook prepares a commit message containing a reference to a Linear issue as well as a conventional commit type.
22
It uses the branch name to determine the issue number and the commit message title as well as the conventional commit type.
33
If LINEAR_API_KEY is set, it fetches the issue title and description from Linear and populates the commit message with it.
4+
If DEFAULT_COMMIT_TYPE is set, it uses that as the default commit type when none is detected (defaults to "feat").
45
See https://www.conventionalcommits.org for examples of conventional commit types.
56
"""
67

@@ -16,6 +17,7 @@
1617
EDITOR_TEXT = "# Please enter the commit message for your changes."
1718

1819
LINEAR_API_KEY = os.environ.get("LINEAR_API_KEY")
20+
DEFAULT_COMMIT_TYPE = os.environ.get("DEFAULT_COMMIT_TYPE", "feat")
1921

2022

2123
# Convert branch name to commit message
@@ -27,7 +29,7 @@ def branch_to_commit_msg(branch: str) -> str:
2729
def prefix_to_commit_type(prefix: str) -> str:
2830
if prefix_match := re.match(common.prefix_regex, prefix):
2931
return prefix_match.group(1)
30-
return "fix"
32+
return DEFAULT_COMMIT_TYPE
3133

3234

3335
def get_branch_name() -> str | None:
@@ -166,7 +168,7 @@ def prepare_commit_msg(raw_commit_msg: str, branch: str) -> str:
166168
commit_msg_title_data = extract_commit_msg_title_data(
167169
linear_data.get("commit_msg_title") or raw_commit_msg_title
168170
)
169-
commit_type = commit_msg_title_data["commit_type"] or branch_data["commit_type"]
171+
commit_type = commit_msg_title_data["commit_type"] or branch_data["commit_type"] or DEFAULT_COMMIT_TYPE
170172
commit_msg_title = (
171173
commit_msg_title_data["commit_msg_title"] or branch_data["commit_msg_title"]
172174
)

git_hooks/test_prepare_commit_msg.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from prepare_commit_msg import extract_commit_msg_title_data
66
from prepare_commit_msg import get_branch_name
77
from prepare_commit_msg import retrieve_linear_issue
8+
from prepare_commit_msg import prepare_commit_msg
9+
from prepare_commit_msg import prefix_to_commit_type
810
from prepare_commit_msg import main
911

1012
from prepare_commit_msg import EDITOR_TEXT
@@ -98,6 +100,38 @@ def test_retrieve_linear_issue(mock_linear_client):
98100
"commit_msg_title": "Branch title",
99101
},
100102
),
103+
(
104+
"dae-75-design-spec-for-configurable-oidc-provider-for-merchants",
105+
{
106+
"issue": "DAE-75",
107+
"commit_type": "",
108+
"commit_msg_title": "Design spec for configurable oidc provider for merchants",
109+
},
110+
),
111+
(
112+
"feat/dae-75-design-spec-for-configurable-oidc-provider-for-merchants",
113+
{
114+
"issue": "DAE-75",
115+
"commit_type": "feat",
116+
"commit_msg_title": "Design spec for configurable oidc provider for merchants",
117+
},
118+
),
119+
(
120+
"fix/abc-123-very-long-branch-name-with-many-hyphens-and-words",
121+
{
122+
"issue": "ABC-123",
123+
"commit_type": "fix",
124+
"commit_msg_title": "Very long branch name with many hyphens and words",
125+
},
126+
),
127+
(
128+
"hotfix/xyz-456-update-critical-security-vulnerability-in-auth-module",
129+
{
130+
"issue": "XYZ-456",
131+
"commit_type": "hotfix",
132+
"commit_msg_title": "Update critical security vulnerability in auth module",
133+
},
134+
),
101135
],
102136
)
103137
def test_extract_branch_data(branch, expected):
@@ -143,3 +177,93 @@ def test_extract_branch_data(branch, expected):
143177
)
144178
def test_extract_commit_msg_title_data(commit_msg_title, expected):
145179
assert extract_commit_msg_title_data(commit_msg_title) == expected
180+
181+
182+
@mock.patch("prepare_commit_msg.LINEAR_API_KEY", None)
183+
@pytest.mark.parametrize(
184+
"raw_commit_msg, branch, expected_commit_msg",
185+
[
186+
(
187+
"",
188+
"dae-75-design-spec-for-configurable-oidc-provider-for-merchants",
189+
"DAE-75/feat: Design spec for configurable oidc provider for merchants",
190+
),
191+
(
192+
"",
193+
"feat/dae-75-design-spec-for-configurable-oidc-provider-for-merchants",
194+
"DAE-75/feat: Design spec for configurable oidc provider for merchants",
195+
),
196+
(
197+
"",
198+
"fix/abc-123-very-long-branch-name-with-many-hyphens-and-words",
199+
"ABC-123/fix: Very long branch name with many hyphens and words",
200+
),
201+
(
202+
"",
203+
"branch-without-issue",
204+
"feat: Branch without issue",
205+
),
206+
],
207+
)
208+
def test_prepare_commit_msg_with_default_commit_type(raw_commit_msg, branch, expected_commit_msg):
209+
result = prepare_commit_msg(raw_commit_msg, branch)
210+
assert result == expected_commit_msg
211+
212+
213+
@mock.patch("prepare_commit_msg.LINEAR_API_KEY", None)
214+
@mock.patch("prepare_commit_msg.DEFAULT_COMMIT_TYPE", "fix")
215+
@pytest.mark.parametrize(
216+
"raw_commit_msg, branch, expected_commit_msg",
217+
[
218+
(
219+
"",
220+
"dae-75-design-spec-for-configurable-oidc-provider-for-merchants",
221+
"DAE-75/fix: Design spec for configurable oidc provider for merchants",
222+
),
223+
(
224+
"",
225+
"abc-123-branch-without-prefix",
226+
"ABC-123/fix: Branch without prefix",
227+
),
228+
(
229+
"",
230+
"branch-without-issue",
231+
"fix: Branch without issue",
232+
),
233+
],
234+
)
235+
def test_prepare_commit_msg_with_custom_default_commit_type(raw_commit_msg, branch, expected_commit_msg):
236+
result = prepare_commit_msg(raw_commit_msg, branch)
237+
assert result == expected_commit_msg
238+
239+
240+
@mock.patch("prepare_commit_msg.DEFAULT_COMMIT_TYPE", "feat")
241+
@pytest.mark.parametrize(
242+
"prefix, expected",
243+
[
244+
("feat", "feat"),
245+
("fix", "fix"),
246+
("chore", "chore"),
247+
("unknown", "feat"), # Should fall back to DEFAULT_COMMIT_TYPE
248+
("invalid", "feat"), # Should fall back to DEFAULT_COMMIT_TYPE
249+
],
250+
)
251+
def test_prefix_to_commit_type_with_default_feat(prefix, expected):
252+
result = prefix_to_commit_type(prefix)
253+
assert result == expected
254+
255+
256+
@mock.patch("prepare_commit_msg.DEFAULT_COMMIT_TYPE", "chore")
257+
@pytest.mark.parametrize(
258+
"prefix, expected",
259+
[
260+
("feat", "feat"),
261+
("fix", "fix"),
262+
("chore", "chore"),
263+
("unknown", "chore"), # Should fall back to DEFAULT_COMMIT_TYPE
264+
("invalid", "chore"), # Should fall back to DEFAULT_COMMIT_TYPE
265+
],
266+
)
267+
def test_prefix_to_commit_type_with_default_chore(prefix, expected):
268+
result = prefix_to_commit_type(prefix)
269+
assert result == expected

0 commit comments

Comments
 (0)