Skip to content

Commit fdfd414

Browse files
Copilotzkoppert
andcommitted
Split markdown functions into separate module as requested
Co-authored-by: zkoppert <[email protected]>
1 parent 0de76ac commit fdfd414

File tree

4 files changed

+319
-296
lines changed

4 files changed

+319
-296
lines changed

markdown.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Markdown utilities for stale repository reporting."""
2+
3+
import os
4+
5+
6+
def write_to_markdown(
7+
inactive_repos,
8+
inactive_days_threshold,
9+
additional_metrics=None,
10+
workflow_summary_enabled=False,
11+
file=None,
12+
):
13+
"""Write the list of inactive repos to a markdown file.
14+
15+
Args:
16+
inactive_repos: A list of dictionaries containing the repo, days inactive,
17+
the date of the last push, repository visibility (public/private),
18+
days since the last release, and days since the last pr
19+
inactive_days_threshold: The threshold (in days) for considering a repo as inactive.
20+
additional_metrics: A list of additional metrics to include in the report.
21+
workflow_summary_enabled: If True, adds the report to GitHub Actions workflow summary.
22+
file: A file object to write to. If None, a new file will be created.
23+
24+
"""
25+
inactive_repos = sorted(
26+
inactive_repos, key=lambda x: x["days_inactive"], reverse=True
27+
)
28+
29+
# Generate markdown content
30+
content = generate_markdown_content(
31+
inactive_repos, inactive_days_threshold, additional_metrics
32+
)
33+
34+
# Write to file
35+
with file or open("stale_repos.md", "w", encoding="utf-8") as markdown_file:
36+
markdown_file.write(content)
37+
print("Wrote stale repos to stale_repos.md")
38+
39+
# Write to GitHub step summary if enabled
40+
if workflow_summary_enabled and os.environ.get("GITHUB_STEP_SUMMARY"):
41+
with open(
42+
os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8"
43+
) as summary_file:
44+
summary_file.write(content)
45+
print("Added stale repos to workflow summary")
46+
47+
48+
def generate_markdown_content(
49+
inactive_repos, inactive_days_threshold, additional_metrics=None
50+
):
51+
"""Generate markdown content for the inactive repos report.
52+
53+
Args:
54+
inactive_repos: A list of dictionaries containing the repo, days inactive,
55+
the date of the last push, repository visibility (public/private),
56+
days since the last release, and days since the last pr
57+
inactive_days_threshold: The threshold (in days) for considering a repo as inactive.
58+
additional_metrics: A list of additional metrics to include in the report.
59+
60+
Returns:
61+
str: The generated markdown content.
62+
"""
63+
content = "# Inactive Repositories\n\n"
64+
content += (
65+
f"The following repos have not had a push event for more than "
66+
f"{inactive_days_threshold} days:\n\n"
67+
)
68+
content += "| Repository URL | Days Inactive | Last Push Date | Visibility |"
69+
70+
# Include additional metrics columns if configured
71+
if additional_metrics:
72+
if "release" in additional_metrics:
73+
content += " Days Since Last Release |"
74+
if "pr" in additional_metrics:
75+
content += " Days Since Last PR |"
76+
content += "\n| --- | --- | --- | --- |"
77+
if additional_metrics:
78+
if "release" in additional_metrics:
79+
content += " --- |"
80+
if "pr" in additional_metrics:
81+
content += " --- |"
82+
content += "\n"
83+
84+
for repo_data in inactive_repos:
85+
content += (
86+
f"| {repo_data['url']} "
87+
f"| {repo_data['days_inactive']} "
88+
f"| {repo_data['last_push_date']} "
89+
f"| {repo_data['visibility']} |"
90+
)
91+
if additional_metrics:
92+
if "release" in additional_metrics:
93+
content += f" {repo_data['days_since_last_release']} |"
94+
if "pr" in additional_metrics:
95+
content += f" {repo_data['days_since_last_pr']} |"
96+
content += "\n"
97+
98+
return content

stale_repos.py

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from env import get_env_vars
1212

1313
import auth
14+
from markdown import write_to_markdown
1415

1516

1617
def main(): # pragma: no cover
@@ -241,101 +242,6 @@ def get_active_date(repo):
241242
return active_date
242243

243244

244-
def write_to_markdown(
245-
inactive_repos,
246-
inactive_days_threshold,
247-
additional_metrics=None,
248-
workflow_summary_enabled=False,
249-
file=None,
250-
):
251-
"""Write the list of inactive repos to a markdown file.
252-
253-
Args:
254-
inactive_repos: A list of dictionaries containing the repo, days inactive,
255-
the date of the last push, repository visibility (public/private),
256-
days since the last release, and days since the last pr
257-
inactive_days_threshold: The threshold (in days) for considering a repo as inactive.
258-
additional_metrics: A list of additional metrics to include in the report.
259-
workflow_summary_enabled: If True, adds the report to GitHub Actions workflow summary.
260-
file: A file object to write to. If None, a new file will be created.
261-
262-
"""
263-
inactive_repos = sorted(
264-
inactive_repos, key=lambda x: x["days_inactive"], reverse=True
265-
)
266-
267-
# Generate markdown content
268-
content = generate_markdown_content(
269-
inactive_repos, inactive_days_threshold, additional_metrics
270-
)
271-
272-
# Write to file
273-
with file or open("stale_repos.md", "w", encoding="utf-8") as markdown_file:
274-
markdown_file.write(content)
275-
print("Wrote stale repos to stale_repos.md")
276-
277-
# Write to GitHub step summary if enabled
278-
if workflow_summary_enabled and os.environ.get("GITHUB_STEP_SUMMARY"):
279-
with open(
280-
os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8"
281-
) as summary_file:
282-
summary_file.write(content)
283-
print("Added stale repos to workflow summary")
284-
285-
286-
def generate_markdown_content(
287-
inactive_repos, inactive_days_threshold, additional_metrics=None
288-
):
289-
"""Generate markdown content for the inactive repos report.
290-
291-
Args:
292-
inactive_repos: A list of dictionaries containing the repo, days inactive,
293-
the date of the last push, repository visibility (public/private),
294-
days since the last release, and days since the last pr
295-
inactive_days_threshold: The threshold (in days) for considering a repo as inactive.
296-
additional_metrics: A list of additional metrics to include in the report.
297-
298-
Returns:
299-
str: The generated markdown content.
300-
"""
301-
content = "# Inactive Repositories\n\n"
302-
content += (
303-
f"The following repos have not had a push event for more than "
304-
f"{inactive_days_threshold} days:\n\n"
305-
)
306-
content += "| Repository URL | Days Inactive | Last Push Date | Visibility |"
307-
308-
# Include additional metrics columns if configured
309-
if additional_metrics:
310-
if "release" in additional_metrics:
311-
content += " Days Since Last Release |"
312-
if "pr" in additional_metrics:
313-
content += " Days Since Last PR |"
314-
content += "\n| --- | --- | --- | --- |"
315-
if additional_metrics:
316-
if "release" in additional_metrics:
317-
content += " --- |"
318-
if "pr" in additional_metrics:
319-
content += " --- |"
320-
content += "\n"
321-
322-
for repo_data in inactive_repos:
323-
content += (
324-
f"| {repo_data['url']} "
325-
f"| {repo_data['days_inactive']} "
326-
f"| {repo_data['last_push_date']} "
327-
f"| {repo_data['visibility']} |"
328-
)
329-
if additional_metrics:
330-
if "release" in additional_metrics:
331-
content += f" {repo_data['days_since_last_release']} |"
332-
if "pr" in additional_metrics:
333-
content += f" {repo_data['days_since_last_pr']} |"
334-
content += "\n"
335-
336-
return content
337-
338-
339245
def output_to_json(inactive_repos, file=None):
340246
"""Convert the list of inactive repos to a json string.
341247

0 commit comments

Comments
 (0)