diff --git a/mattermost_notify/git.py b/mattermost_notify/git.py index 6ce14b0..e94b5fd 100644 --- a/mattermost_notify/git.py +++ b/mattermost_notify/git.py @@ -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, @@ -92,7 +93,7 @@ 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}" @@ -100,13 +101,15 @@ def fill_template( 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: @@ -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, ) diff --git a/mattermost_notify/parser.py b/mattermost_notify/parser.py index bb4914b..2ae1ed6 100644 --- a/mattermost_notify/parser.py +++ b/mattermost_notify/parser.py @@ -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", diff --git a/tests/test_git.py b/tests/test_git.py index 0d48d22..5dbec75 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -32,7 +32,7 @@ def test_success_no_highlight(self): | Workflow | | | --- | --- | -| Repository (branch) | ([](/tree/None)) | +| Repository (branch) | ([](/tree/)) | | Related commit | [](/commit/) | """ @@ -48,7 +48,7 @@ def test_failure_highlight(self): | Workflow | | | --- | --- | -| Repository (branch) | ([](/tree/None)) | +| Repository (branch) | ([](/tree/)) | | Related commit | [](/commit/) | @user1 @@ -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) @@ -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(), @@ -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) @@ -106,14 +108,13 @@ 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 @@ -121,7 +122,7 @@ def test_template_data_from_github_event(self, mock: MagicMock): | 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) diff --git a/tests/test_parser.py b/tests/test_parser.py index 6af64fb..f4f9da4 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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"]