Skip to content

Options: Add --format=mrkdwn option, improving Slack messages #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- GitHub/Backup: Added wrapper around `github-backup`
- Options: Use `aika` for parsing time intervals.
Also, rename command-line option `--timerange` to `--when`.
- Options: Added `--format=mrkdwn` option, improving Slack messages

## v0.1.0, 2025-02-20
- Started using `GH_TOKEN` environment variable instead of `GITHUB_TOKEN`,
Expand Down
11 changes: 11 additions & 0 deletions docs/guide/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ or relative human-readable notations.
- this month


## Output options

### Slack flavored Markdown

The program can output two flavors of Markdown. Standard Markdown is default,
while the [Slack `mrkdwn` format] can be produced using the `--format=mrkdwn`
command-line option. Rapporto uses the [markdown-to-mrkdwn] package here.


[aika]: https://pypi.org/project/aika/
[markdown-to-mrkdwn]: https://pypi.org/project/markdown-to-mrkdwn/
[Slack `mrkdwn` format]: https://api.slack.com/reference/surfaces/formatting#basic-formatting
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dependencies = [
"dataclasses-json<1",
"github-backup<1",
"importlib-metadata; python_version<'3.8'",
"markdown-to-mrkdwn<0.2",
"munch<5",
"pueblo<1",
"python-dateutil<3",
Expand Down
3 changes: 0 additions & 3 deletions src/rapporto/github/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ def markdown(self):
{mdc.render()}
""".strip() # noqa: E501

def print(self):
print(self.markdown)


class GitHubActionsRequest:
"""
Expand Down
20 changes: 13 additions & 7 deletions src/rapporto/github/activity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import dataclasses
import io
import logging
import typing as t
from contextlib import redirect_stdout
from operator import attrgetter
from textwrap import dedent

Expand Down Expand Up @@ -112,14 +114,18 @@ def format_pr(item: "PullRequestMetadata"):
f"comments: {item.comments_total}, files: {item.changed_files}, size: {item.code_size}"
)

def print(self):
@property
def markdown(self) -> str:
timerange = self.inquiry.created and f"for {self.inquiry.created}" or ""
print(f"# PPP report {timerange}")
# print("## Overview")
print(self.markdown_overview)
print()
print("*Top changes:*")
print(self.markdown_significant)
with redirect_stdout(io.StringIO()) as buffer:
print(f"# PPP report {timerange}")
# print("## Overview")
print(self.markdown_overview)
print()
print("*Top changes:*")
print(self.markdown_significant)
buffer.seek(0)
return buffer.read()


@dataclass_json(undefined=Undefined.INCLUDE)
Expand Down
3 changes: 0 additions & 3 deletions src/rapporto/github/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,3 @@ def markdown(self):
Time range: {self.search.query_builder.timerange or "n/a"}
{mdc.render()}
""".strip() # noqa: E501

def print(self):
print(self.markdown)
29 changes: 24 additions & 5 deletions src/rapporto/github/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from rapporto.github.attention import GitHubAttentionReport
from rapporto.github.backup import GitHubBackup
from rapporto.github.model import GitHubInquiry
from rapporto.util import to_mrkdwn

organization_option = click.option("--organization", "--org", type=str, required=False)
author_option = click.option("--author", type=str, required=False)
when_option = click.option("--when", type=str, required=False)
format_option = click.option("--format", "format_", type=str, required=False, default="markdown")
repository_option = click.option("--repository", type=str, required=False)
repositories_file_option = click.option("--repositories-file", type=Path, required=False)

Expand All @@ -30,23 +32,35 @@
@organization_option
@author_option
@when_option
@format_option
def activity(
organization: t.Optional[str] = None,
author: t.Optional[str] = None,
when: t.Optional[str] = None,
format_: t.Optional[str] = None,
):
"""
Activities of individual authors.
"""
inquiry = GitHubInquiry(organization=organization, author=author, created=when)
report = GitHubActivityReport(inquiry=inquiry)
report.print()
print_output(report, format_)


def print_output(report, format_):
if format_ == "markdown":
print(report.markdown)
elif format_ == "mrkdwn":
print(to_mrkdwn(report.markdown))

Check warning on line 54 in src/rapporto/github/cli.py

View check run for this annotation

Codecov / codecov/patch

src/rapporto/github/cli.py#L53-L54

Added lines #L53 - L54 were not covered by tests
else:
raise NotImplementedError(f"Unknown output format: {format_}")

Check warning on line 56 in src/rapporto/github/cli.py

View check run for this annotation

Codecov / codecov/patch

src/rapporto/github/cli.py#L56

Added line #L56 was not covered by tests


@cli.command(aliases=["ci"])
@repository_option
@repositories_file_option
def actions(repository: str, repositories_file: Path = None):
@format_option
def actions(repository: str, repositories_file: Path = None, format_: t.Optional[str] = None):
"""
CI/GHA failures.
"""
Expand All @@ -60,19 +74,24 @@
)
raise SystemExit(1) from ex
report = GitHubActionsReport(inquiry=inquiry)
report.print()
print_output(report, format_)


@cli.command(aliases=["att"])
@organization_option
@when_option
def attention(organization: t.Optional[str] = None, when: t.Optional[str] = None):
@format_option
def attention(
organization: t.Optional[str] = None,
when: t.Optional[str] = None,
format_: t.Optional[str] = None,
):
"""
Important items that deserve attention.
"""
inquiry = GitHubInquiry(organization=organization, created=when)
report = GitHubAttentionReport(inquiry=inquiry)
report.print()
print_output(report, format_)


@cli.command(
Expand Down
10 changes: 10 additions & 0 deletions src/rapporto/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from markdown_to_mrkdwn import SlackMarkdownConverter


def sanitize_title(title: str) -> str:
"""
Strip characters that are unfortunate in Markdown link titles.
Expand All @@ -12,3 +15,10 @@
if " " in text:
return f'"{text}"'
return text


mrkdwn_converter = SlackMarkdownConverter()


def to_mrkdwn(markdown: str) -> str:
return mrkdwn_converter.convert(markdown)

Check warning on line 24 in src/rapporto/util.py

View check run for this annotation

Codecov / codecov/patch

src/rapporto/util.py#L24

Added line #L24 was not covered by tests