Skip to content

Commit

Permalink
add parameter for custom_repository url
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Grzybowski committed Apr 7, 2022
1 parent 5b10b2d commit 8a6118a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ python setup.py install
```yaml
repos:
- repo: git://github.com/nokaut/redmine_hook
rev: v0.9.5
rev: v0.9.6
hooks:
- id: redmine-commit-check
name: NKT redmine-commit-check validator
Expand Down
22 changes: 14 additions & 8 deletions redmine_commit_msg/redmine_commit_msg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import getopt
import os
import re
import subprocess
Expand All @@ -8,11 +9,10 @@

MESSAGE_REGEX = "(RM[#-]?)([0-9]{4,7})"
BRANCHNAME_REGEX = "(RM[#-]?)([0-9]{4,7})-"
GIT_REPOSITORY = "(gitlab)|(github)"
GIT_REPOSITORY_REGEX = "(gitlab)|(github)"
REDMINE_URL = os.getenv("REDMINE_URL")
REDMINE_TOKEN = os.getenv("REDMINE_TOKEN")


def git_addr_trim(addr):
after_trim = addr.replace("http://", "")
after_trim = after_trim.replace("https://", "")
Expand All @@ -21,9 +21,9 @@ def git_addr_trim(addr):
return after_trim


def git_addr_repo(addr, commit_hash):
def git_addr_repo(git_repository, addr, commit_hash):
trim = git_addr_trim(addr)
if re.findall(GIT_REPOSITORY, trim):
if re.findall(git_repository, trim):
REPO_URL_SCHEMA = os.getenv("REPO_URL_SCHEMA", "http")
git_addr = trim.split(":", 1)
url_syntax = "{}://{}/{}/commit/{}".format(
Expand Down Expand Up @@ -73,7 +73,7 @@ def get_redmine_issue_id(message):
return False


def update_redmine_task(message):
def update_redmine_task(message, git_repository):
issue_id = get_redmine_issue_id(message)
if issue_id:
try:
Expand All @@ -83,14 +83,20 @@ def update_redmine_task(message):
except:
print("Issue with connection to Redmine. Did you export REDMINE_TOKEN & REDMINE_URL envs?")
sys.exit(1)
git_addr_to_repo = git_addr_repo(str(current_origin()), str(current_commithash()))
git_addr_to_repo = git_addr_repo(git_repository, str(current_origin()), str(current_commithash()))
note = "*Branch*: __{}__ | {}\n {}".format(str(current_branch_name()), git_addr_to_repo, message)
redmine.issue.update(issue_id, notes=note)
return


def main():
message_file = sys.argv[1:][0] if sys.argv[1:] else ".git/COMMIT_EDITMSG"
GIT_REPOSITORY = GIT_REPOSITORY_REGEX
options, remainder = getopt.getopt(sys.argv[1:], 'r', ['custom_repo=', ])
for opt, arg in options:
if opt in ('r', '--custom_repo'):
GIT_REPOSITORY = arg

message_file = remainder[1:][0] if remainder[1:] else ".git/COMMIT_EDITMSG"

try:
txt_file = open(message_file, "r")
Expand All @@ -99,7 +105,7 @@ def main():
txt_file.close()

if REDMINE_URL and REDMINE_TOKEN:
update_redmine_task(commit_message)
update_redmine_task(commit_message, GIT_REPOSITORY)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = redmine-commit-msg
version = 0.9.4
version = 0.9.6
description = Sending info about commits to redmine
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 8a6118a

Please sign in to comment.