Skip to content

fix: handle created_at with diff types #491

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
Mar 14, 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
5 changes: 4 additions & 1 deletion issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def get_per_issue_metrics(
elif issue.state == "open": # type: ignore
num_issues_open += 1
if not env_vars.hide_created_at:
issue_with_metrics.created_at = issue["created_at"]
if isinstance(issue, github3.search.IssueSearchResult): # type: ignore
issue_with_metrics.created_at = issue.issue.created_at # type: ignore
elif isinstance(issue, dict): # type: ignore
issue_with_metrics.created_at = issue["createdAt"] # type: ignore
issues_with_metrics.append(issue_with_metrics)

return issues_with_metrics, num_issues_open, num_issues_closed
Expand Down
1 change: 1 addition & 0 deletions test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def setUp(self):
"GH_TOKEN",
"GHE",
"HIDE_AUTHOR",
"HIDE_CREATED_AT",
"HIDE_ITEMS_CLOSED_COUNT",
"HIDE_LABEL_METRICS",
"HIDE_TIME_TO_ANSWER",
Expand Down
9 changes: 3 additions & 6 deletions test_issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ def setUp(self):
self.issue1 = {
"title": "Issue 1",
"url": "github.com/user/repo/issues/1",
"user": {
"login": "alice",
},
"user": {"login": "alice"},
"createdAt": "2023-01-01T00:00:00Z",
"comments": {
"nodes": [
Expand All @@ -392,9 +390,7 @@ def setUp(self):
self.issue2 = {
"title": "Issue 2",
"url": "github.com/user/repo/issues/2",
"user": {
"login": "bob",
},
"user": {"login": "bob"},
"createdAt": "2023-01-01T00:00:00Z",
"comments": {"nodes": [{"createdAt": "2023-01-03T00:00:00Z"}]},
"answerChosenAt": "2023-01-05T00:00:00Z",
Expand Down Expand Up @@ -441,6 +437,7 @@ def test_get_per_issue_metrics_with_discussion(self):
"GH_TOKEN": "test_token",
"SEARCH_QUERY": "is:issue is:open repo:user/repo",
"HIDE_AUTHOR": "true",
"HIDE_CREATED_AT": "false",
"HIDE_LABEL_METRICS": "true",
"HIDE_TIME_TO_ANSWER": "true",
"HIDE_TIME_TO_CLOSE": "true",
Expand Down
33 changes: 21 additions & 12 deletions test_markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"SEARCH_QUERY": "is:open repo:user/repo",
"GH_TOKEN": "test_token",
"DRAFT_PR_TRACKING": "True",
"HIDE_CREATED_AT": "False",
},
)
class TestWriteToMarkdown(unittest.TestCase):
Expand All @@ -44,6 +45,7 @@ def test_write_to_markdown(self):
title="Issue 1",
html_url="https://github.com/user/repo/issues/1",
author="alice",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(days=1),
time_to_close=timedelta(days=2),
time_to_answer=timedelta(days=3),
Expand All @@ -54,6 +56,7 @@ def test_write_to_markdown(self):
title="Issue 2\r",
html_url="https://github.com/user/repo/issues/2",
author="bob",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(days=3),
time_to_close=timedelta(days=4),
time_to_answer=timedelta(days=5),
Expand Down Expand Up @@ -129,12 +132,12 @@ def test_write_to_markdown(self):
"| Number of most active mentors | 5 |\n"
"| Total number of items created | 2 |\n\n"
"| Title | URL | Author | Time to first response | Time to close |"
" Time to answer | Time in draft | Time spent in bug |\n"
"| --- | --- | --- | --- | --- | --- | --- | --- |\n"
" Time to answer | Time in draft | Time spent in bug | Created At |\n"
"| --- | --- | --- | --- | --- | --- | --- | --- | --- |\n"
"| Issue 1 | https://github.com/user/repo/issues/1 | [alice](https://github.com/alice) | 1 day, 0:00:00 | "
"2 days, 0:00:00 | 3 days, 0:00:00 | 1 day, 0:00:00 | 4 days, 0:00:00 |\n"
"2 days, 0:00:00 | 3 days, 0:00:00 | 1 day, 0:00:00 | 4 days, 0:00:00 | -5 days, 0:00:00 |\n"
"| Issue 2 | https://github.com/user/repo/issues/2 | [bob](https://github.com/bob) | 3 days, 0:00:00 | "
"4 days, 0:00:00 | 5 days, 0:00:00 | 1 day, 0:00:00 | 2 days, 0:00:00 |\n\n"
"4 days, 0:00:00 | 5 days, 0:00:00 | 1 day, 0:00:00 | 2 days, 0:00:00 | -5 days, 0:00:00 |\n\n"
"_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
"Search query used to find these items: `is:issue is:open label:bug`\n"
)
Expand All @@ -156,6 +159,7 @@ def test_write_to_markdown_with_vertical_bar_in_title(self):
title="Issue 1",
html_url="https://github.com/user/repo/issues/1",
author="alice",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(days=1),
time_to_close=timedelta(days=2),
time_to_answer=timedelta(days=3),
Expand All @@ -166,6 +170,7 @@ def test_write_to_markdown_with_vertical_bar_in_title(self):
title="feat| Issue 2", # title contains a vertical bar
html_url="https://github.com/user/repo/issues/2",
author="bob",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(days=3),
time_to_close=timedelta(days=4),
time_to_answer=timedelta(days=5),
Expand Down Expand Up @@ -238,12 +243,12 @@ def test_write_to_markdown_with_vertical_bar_in_title(self):
"| Number of most active mentors | 5 |\n"
"| Total number of items created | 2 |\n\n"
"| Title | URL | Author | Time to first response | Time to close |"
" Time to answer | Time in draft | Time spent in bug |\n"
"| --- | --- | --- | --- | --- | --- | --- | --- |\n"
" Time to answer | Time in draft | Time spent in bug | Created At |\n"
"| --- | --- | --- | --- | --- | --- | --- | --- | --- |\n"
"| Issue 1 | https://github.com/user/repo/issues/1 | [alice](https://github.com/alice) | 1 day, 0:00:00 | "
"2 days, 0:00:00 | 3 days, 0:00:00 | 1 day, 0:00:00 | 1 day, 0:00:00 |\n"
"2 days, 0:00:00 | 3 days, 0:00:00 | 1 day, 0:00:00 | 1 day, 0:00:00 | -5 days, 0:00:00 |\n"
"| feat| Issue 2 | https://github.com/user/repo/issues/2 | [bob](https://github.com/bob) | 3 days, 0:00:00 | "
"4 days, 0:00:00 | 5 days, 0:00:00 | None | 2 days, 0:00:00 |\n\n"
"4 days, 0:00:00 | 5 days, 0:00:00 | None | 2 days, 0:00:00 | -5 days, 0:00:00 |\n\n"
"_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
)
self.assertEqual(content, expected_content)
Expand Down Expand Up @@ -287,6 +292,7 @@ def test_write_to_markdown_no_issues(self):
{
"SEARCH_QUERY": "is:open repo:user/repo",
"GH_TOKEN": "test_token",
"HIDE_CREATED_AT": "False",
"HIDE_TIME_TO_FIRST_RESPONSE": "True",
"HIDE_TIME_TO_CLOSE": "True",
"HIDE_TIME_TO_ANSWER": "True",
Expand All @@ -309,6 +315,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
title="Issue 1",
html_url="https://github.com/user/repo/issues/1",
author="alice",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(minutes=10),
time_to_close=timedelta(days=1),
time_to_answer=timedelta(hours=2),
Expand All @@ -321,6 +328,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
title="Issue 2",
html_url="https://github.com/user/repo/issues/2",
author="bob",
created_at=timedelta(days=-5),
time_to_first_response=timedelta(minutes=20),
time_to_close=timedelta(days=2),
time_to_answer=timedelta(hours=4),
Expand Down Expand Up @@ -363,17 +371,18 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
# Check that the function writes the correct markdown file
with open("issue_metrics.md", "r", encoding="utf-8") as file:
content = file.read()

expected_content = (
"# Issue Metrics\n\n"
"| Metric | Count |\n"
"| --- | ---: |\n"
"| Number of items that remain open | 2 |\n"
"| Number of most active mentors | 5 |\n"
"| Total number of items created | 2 |\n\n"
"| Title | URL | Author |\n"
"| --- | --- | --- |\n"
"| Issue 1 | https://www.github.com/user/repo/issues/1 | [alice](https://github.com/alice) |\n"
"| Issue 2 | https://www.github.com/user/repo/issues/2 | [bob](https://github.com/bob) |\n\n"
"| Title | URL | Author | Created At |\n"
"| --- | --- | --- | --- |\n"
"| Issue 1 | https://www.github.com/user/repo/issues/1 | [alice](https://github.com/alice) | -5 days, 0:00:00 |\n"
"| Issue 2 | https://www.github.com/user/repo/issues/2 | [bob](https://github.com/bob) | -5 days, 0:00:00 |\n\n"
"_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
"Search query used to find these items: `repo:user/repo is:issue`\n"
)
Expand Down
Loading