Skip to content

Commit 980e529

Browse files
authored
KTOR-927 Add git hook to check commit message (#2103)
* KTOR-927 Add git hook to check commit message
1 parent 9a839b7 commit 980e529

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ if there is already an open ticket and open a new one if not yet opened.
99

1010
Contributions are made using Github [pull requests](https://help.github.com/en/articles/about-pull-requests).
1111

12-
1. [Create](https://github.com/ktorio/ktor/compare) a new PR, your PR should request to merge to the **master** branch.
13-
2. Ensure that the description is clear, refer to an existing ticket/bug if applicable.
14-
3. When contributing a new feature, provide motivation and use-cases describing why
12+
1. Open (or find) the corresponding [YT](https://youtrack.jetbrains.com/issues/KTOR) issue.
13+
2. Please make sure that commit messages start with issue number (like `KTOR-1001`). You can copy `githook/prepare-commit-msg` to `.git/hooks` to enable automatic commit message check.
14+
3. [Create](https://github.com/ktorio/ktor/compare) a new PR, your PR should request to merge to the **master** branch.
15+
4. Ensure that the description is clear, refer to an existing ticket/bug if applicable.
16+
5. When contributing a new feature, provide motivation and use-cases describing why
1517
the feature is important enough to be delivered with ktor to everyone.
16-
4. Adding and updating features may require to update the [documentation](https://github.com/ktorio/ktorio.github.io).
18+
6. Adding and updating features may require to update the [documentation](https://github.com/ktorio/ktorio.github.io).
1719
Create a documentation PR and link both pull requests.
18-
5. Make sure you have adequate tests added and no existing tests were broken.
20+
7. Make sure you have adequate tests added and no existing tests were broken.
1921

2022
# Styleguides
2123

githook/prepare-commit-msg

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
ALLOW = 0
6+
RESTRICT = 1
7+
8+
9+
def is_integer(value: str) -> bool:
10+
try:
11+
int(value)
12+
return True
13+
except ValueError as _:
14+
return False
15+
16+
17+
def is_correct_commit_message(message: str) -> bool:
18+
issue_name = first_line.split(" ")[0]
19+
20+
# Allow fixup commits
21+
if issue_name in ["fixup!", "WIP"]:
22+
return True
23+
24+
if issue_name.startswith("~"):
25+
return True
26+
27+
prefix = "KTOR-"
28+
if issue_name.startswith(prefix) and is_integer(issue_name[len(prefix):]):
29+
return True
30+
31+
print(f"Commit name should start with the issue number(like KTOR-1001). Current prefix is '{issue_name}'")
32+
return False
33+
34+
35+
if __name__ == "__main__":
36+
commit_message_filename: str = sys.argv[1]
37+
with open(commit_message_filename) as file:
38+
first_line: str = file.readline()
39+
40+
exit_code = ALLOW if is_correct_commit_message(first_line) else RESTRICT
41+
exit(exit_code)

0 commit comments

Comments
 (0)