Skip to content

Commit

Permalink
Merge pull request #94 from github/report-query
Browse files Browse the repository at this point in the history
Add search query to the markdown and json reports
  • Loading branch information
zkoppert authored Aug 2, 2023
2 parents f209436 + f870625 commit c3d29d0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ lint:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --exit-zero --max-complexity=15 --max-line-length=127 --statistics
pylint --rcfile=.pylintrc --fail-under=9.0 *.py
15 changes: 11 additions & 4 deletions issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Searches for issues in a GitHub repository that match the given search query.
auth_to_github() -> github3.GitHub: Connect to GitHub API with token authentication.
get_per_issue_metrics(issues: Union[List[dict], List[github3.issues.Issue]],
discussions: bool = False), labels: Union[List[str], None] = None, ignore_users: List[str] = [] -> tuple[List, int, int]:
discussions: bool = False), labels: Union[List[str], None] = None,
ignore_users: List[str] = [] -> tuple[List, int, int]:
Calculate the metrics for each issue in a list of GitHub issues.
get_owner(search_query: str) -> Union[str, None]]:
Get the owner from the search query.
Expand Down Expand Up @@ -123,10 +124,12 @@ def auth_to_github() -> github3.GitHub:
if token := os.getenv("GH_TOKEN"):
if not os.getenv("GITHUB_SERVER_URL"):
github_connection = github3.login(token=token)
elif os.getenv("GITHUB_SERVER_URL") == 'https://github.com':
elif os.getenv("GITHUB_SERVER_URL") == "https://github.com":
github_connection = github3.login(token=token)
else:
github_connection = github3.GitHubEnterprise(os.getenv("GITHUB_SERVER_URL"),token=token)
github_connection = github3.GitHubEnterprise(
os.getenv("GITHUB_SERVER_URL"), token=token
)
else:
raise ValueError("GH_TOKEN environment variable not set")

Expand All @@ -137,7 +140,7 @@ def get_per_issue_metrics(
issues: Union[List[dict], List[github3.search.IssueSearchResult]], # type: ignore
discussions: bool = False,
labels: Union[List[str], None] = None,
ignore_users: List[str] = [],
ignore_users: List[str] = None,
) -> tuple[List, int, int]:
"""
Calculate the metrics for each issue/pr/discussion in a list provided.
Expand All @@ -159,6 +162,8 @@ def get_per_issue_metrics(
issues_with_metrics = []
num_issues_open = 0
num_issues_closed = 0
if ignore_users is None:
ignore_users = []

for issue in issues:
if discussions:
Expand Down Expand Up @@ -320,6 +325,7 @@ def main():
average_time_in_labels,
num_issues_open,
num_issues_closed,
search_query,
)
write_to_markdown(
issues_with_metrics,
Expand All @@ -330,6 +336,7 @@ def main():
num_issues_open,
num_issues_closed,
labels,
search_query,
)


Expand Down
6 changes: 6 additions & 0 deletions json_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
average_time_to_answer: timedelta,
num_issues_opened: int,
num_issues_closed: int,
search_query: str,
) -> str:
Write the issues with metrics to a json file.
Expand All @@ -30,6 +31,7 @@ def write_to_json(
average_time_in_labels: Union[dict, None],
num_issues_opened: Union[int, None],
num_issues_closed: Union[int, None],
search_query: str,
) -> str:
"""
Write the issues with metrics to a JSON file called issue_metrics.json.
Expand Down Expand Up @@ -63,6 +65,7 @@ def write_to_json(
}
},
],
"search_query": "is:issue is:open repo:owner/repo"
}
"""
Expand Down Expand Up @@ -107,6 +110,9 @@ def write_to_json(
# Add the issues to the metrics dictionary
metrics["issues"] = issues

# Add the search query to the metrics dictionary
metrics["search_query"] = search_query

# add output to github action output
# pylint: disable=unspecified-encoding
metrics_json = json.dumps(metrics)
Expand Down
2 changes: 1 addition & 1 deletion labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_label_events(
return label_events


def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: # type: ignore
def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
"""
Calculate the time spent with the given labels on a given issue.
Expand Down
4 changes: 4 additions & 0 deletions markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def write_to_markdown(
num_issues_opened: Union[int, None],
num_issues_closed: Union[int, None],
labels=None,
search_query=None,
) -> None:
"""Write the issues with metrics to a markdown file.
Expand All @@ -88,6 +89,7 @@ def write_to_markdown(
num_issues_opened (int): The Number of items that remain opened.
num_issues_closed (int): The number of issues that were closed.
labels (List[str]): A list of the labels that are used in the issues.
search_query (str): The search query used to find the issues.
Returns:
None.
Expand Down Expand Up @@ -154,6 +156,8 @@ def write_to_markdown(
file.write(
"\n_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
)
if search_query:
file.write(f"Search query used to find these items: `{search_query}`\n")

print("Wrote issue metrics to issue_metrics.md")

Expand Down
2 changes: 2 additions & 0 deletions test_json_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_write_to_json(self):
"label_metrics": {},
},
],
"search_query": "is:issue repo:owner/repo",
}

# Call the function and check the output
Expand All @@ -78,6 +79,7 @@ def test_write_to_json(self):
},
num_issues_opened=num_issues_opened,
num_issues_closed=num_issues_closed,
search_query="is:issue repo:owner/repo",
),
json.dumps(expected_output),
)
Expand Down
12 changes: 8 additions & 4 deletions test_markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_write_to_markdown(self):
num_issues_opened=num_issues_opened,
num_issues_closed=num_issues_closed,
labels=["bug"],
search_query="is:issue is:open label:bug",
)

# Check that the function writes the correct markdown file
Expand All @@ -87,17 +88,18 @@ def test_write_to_markdown(self):
"| Issue 2 | https://github.com/user/repo/issues/2 | 3 days, 0:00:00 | "
"4 days, 0:00:00 | 5 days, 0:00:00 | 2 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"
)
self.assertEqual(content, expected_content)
os.remove("issue_metrics.md")

def test_write_to_markdown_with_vertical_bar_in_title(self):
"""Test that write_to_markdown writes the correct markdown file when the title contains a vertical bar.
This test creates a list of mock GitHub issues (one of which contains a vertical bar in the title) with time to first response
attributes, calls write_to_markdown with the list and the average time to
first response, time to close and checks that the function writes the correct
markdown file.
This test creates a list of mock GitHub issues (one of which contains a vertical
bar in the title) with time to first response attributes, calls write_to_markdown
with the list and the average time to first response, time to close and checks
that the function writes the correct markdown file.
"""
# Create mock data
Expand Down Expand Up @@ -243,6 +245,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
num_issues_opened=num_issues_opened,
num_issues_closed=num_issues_closed,
labels=["label1"],
search_query="repo:user/repo is:issue",
)

# Check that the function writes the correct markdown file
Expand All @@ -260,6 +263,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
"| Issue 1 | https://github.com/user/repo/issues/1 |\n"
"| Issue 2 | https://github.com/user/repo/issues/2 |\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"
)
self.assertEqual(content, expected_content)
os.remove("issue_metrics.md")
4 changes: 3 additions & 1 deletion time_to_first_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
def measure_time_to_first_response(
issue: Union[github3.issues.Issue, None], # type: ignore
discussion: Union[dict, None],
ignore_users: List[str] = [],
ignore_users: List[str] = None,
) -> Union[timedelta, None]:
"""Measure the time to first response for a single issue or a discussion.
Expand All @@ -44,6 +44,8 @@ def measure_time_to_first_response(
first_comment_time = None
earliest_response = None
issue_time = None
if ignore_users is None:
ignore_users = []

# Get the first comment time
if issue:
Expand Down

0 comments on commit c3d29d0

Please sign in to comment.