Skip to content

Commit

Permalink
Add: Allow to set an explicit commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Jul 18, 2023
1 parent 90b5f6f commit f9c2199
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
13 changes: 8 additions & 5 deletions mattermost_notify/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def fill_template(
short: bool = False,
highlight: Optional[list[str]] = None,
commit: Optional[str] = None,
commit_message: Optional[str] = None,
branch: Optional[str] = None,
repository: Optional[str] = None,
status: Optional[str] = None,
Expand Down Expand Up @@ -92,21 +93,23 @@ def fill_template(
used_branch: str = (
branch if branch else workflow_info.get("head_branch", "")
)
branch_url = f"{repository_url}/tree/{branch}"
branch_url = f"{repository_url}/tree/{used_branch}"

workflow_url = (
f"{repository_url}/actions/runs/{used_workflow_id}"
if repository
else workflow_info.get("html_url", "")
)

head_commit = workflow_info.get("head_commit", {})

if commit:
commit_url = f"{repository_url}/commit/{commit}"
commit_message = commit
else:
head_commit = workflow_info.get("head_commit", {})
commit_url = f'{repository_url}/commit/{head_commit.get("id", "")}'
commit_message: str = head_commit.get("message", "").split("\n", 1)[0] # type: ignore[no-redef] # noqa: E501

if not commit_message:
commit_message = head_commit.get("message", "").split("\n", 1)[0]

highlight_str = ""
if highlight and workflow_status is not Status.SUCCESS:
Expand All @@ -117,7 +120,7 @@ def fill_template(
workflow=linker(used_workflow_name, workflow_url),
repository=linker(repository, repository_url),
branch=linker(used_branch, branch_url),
commit=linker(commit_message, commit_url),
commit=linker(commit_message, commit_url), # type: ignore[arg-type]
highlight=highlight_str,
)

Expand Down
3 changes: 2 additions & 1 deletion mattermost_notify/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def parse_args(args=None) -> Namespace:
"-n", "--workflow_name", type=str, help="name of the workflow"
)

parser.add_argument("--commit", help="Commit to use")
parser.add_argument("--commit", help="Commit ID to use")
parser.add_argument("--commit-message", help="Commit Message to use")

parser.add_argument(
"--free",
Expand Down
15 changes: 8 additions & 7 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_success_no_highlight(self):
| Workflow | |
| --- | --- |
| Repository (branch) | ([](/tree/None)) |
| Repository (branch) | ([](/tree/)) |
| Related commit | [](/commit/) |
"""
Expand All @@ -48,7 +48,7 @@ def test_failure_highlight(self):
| Workflow | |
| --- | --- |
| Repository (branch) | ([](/tree/None)) |
| Repository (branch) | ([](/tree/)) |
| Related commit | [](/commit/) |
@user1
Expand All @@ -63,13 +63,14 @@ def test_short_template(self):
workflow_name="SomeWorkflow",
workflow_id="w1",
commit="12345",
commit_message="Add foo",
repository="foo/bar",
branch="main",
terminal=MagicMock(),
)
expected = (
":white_check_mark: success: [SomeWorkflow](https://github.com/foo/bar/actions/runs/w1) "
"([12345](https://github.com/foo/bar/commit/12345)) in "
"([Add foo](https://github.com/foo/bar/commit/12345)) in "
"[foo/bar](https://github.com/foo/bar) ([main](https://github.com/foo/bar/tree/main))"
)
self.assertEqual(expected, actual)
Expand All @@ -81,6 +82,7 @@ def test_template(self):
workflow_name="SomeWorkflow",
workflow_id="w1",
commit="12345",
commit_message="Add foo",
repository="foo/bar",
branch="main",
terminal=MagicMock(),
Expand All @@ -90,7 +92,7 @@ def test_template(self):
| Workflow | [SomeWorkflow](https://github.com/foo/bar/actions/runs/w1) |
| --- | --- |
| Repository (branch) | [foo/bar](https://github.com/foo/bar) ([main](https://github.com/foo/bar/tree/main)) |
| Related commit | [12345](https://github.com/foo/bar/commit/12345) |
| Related commit | [Add foo](https://github.com/foo/bar/commit/12345) |
"""
self.assertEqual(expected, actual)
Expand All @@ -106,22 +108,21 @@ def test_template_data_from_github_event(self, mock: MagicMock):
"html_url": "https://github.com/foo/bar",
},
"head_branch": "main",
"head_commit": {"id": "12345"},
"head_commit": {"id": "12345", "message": "Add foo"},
"workflow_id": "w1",
}
}
mock.return_value = event

actual = fill_template(
short=False,
terminal=MagicMock(),
)
expected = """#### Status: :white_check_mark: success
| Workflow | [SomeWorkflow](https://github.com/foo/bar/actions/runs/w1) |
| --- | --- |
| Repository (branch) | [foo/bar](https://github.com/foo/bar) ([main](https://github.com/foo/bar/tree/main)) |
| Related commit | [12345](https://github.com/foo/bar/commit/12345) |
| Related commit | [Add foo](https://github.com/foo/bar/commit/12345) |
"""
self.assertEqual(expected, actual)
6 changes: 6 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def test_parse_commit(self):
parsed_args = parse_args(["www.url.de", "channel", "--commit", "1234"])
self.assertEqual(parsed_args.commit, "1234")

def test_parse_commit_message(self):
parsed_args = parse_args(
["www.url.de", "channel", "--commit-message", "foo bar"]
)
self.assertEqual(parsed_args.commit_message, "foo bar")

def test_parse_free(self):
parsed_args = parse_args(
["www.url.de", "channel", "--free", "lorem ipsum"]
Expand Down

0 comments on commit f9c2199

Please sign in to comment.