Skip to content

Commit c3d29d0

Browse files
authored
Merge pull request #94 from github/report-query
Add search query to the markdown and json reports
2 parents f209436 + f870625 commit c3d29d0

8 files changed

+36
-11
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ lint:
1111
# stop the build if there are Python syntax errors or undefined names
1212
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
1313
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
14-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
14+
flake8 . --count --exit-zero --max-complexity=15 --max-line-length=127 --statistics
1515
pylint --rcfile=.pylintrc --fail-under=9.0 *.py

issue_metrics.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
Searches for issues in a GitHub repository that match the given search query.
1414
auth_to_github() -> github3.GitHub: Connect to GitHub API with token authentication.
1515
get_per_issue_metrics(issues: Union[List[dict], List[github3.issues.Issue]],
16-
discussions: bool = False), labels: Union[List[str], None] = None, ignore_users: List[str] = [] -> tuple[List, int, int]:
16+
discussions: bool = False), labels: Union[List[str], None] = None,
17+
ignore_users: List[str] = [] -> tuple[List, int, int]:
1718
Calculate the metrics for each issue in a list of GitHub issues.
1819
get_owner(search_query: str) -> Union[str, None]]:
1920
Get the owner from the search query.
@@ -123,10 +124,12 @@ def auth_to_github() -> github3.GitHub:
123124
if token := os.getenv("GH_TOKEN"):
124125
if not os.getenv("GITHUB_SERVER_URL"):
125126
github_connection = github3.login(token=token)
126-
elif os.getenv("GITHUB_SERVER_URL") == 'https://github.com':
127+
elif os.getenv("GITHUB_SERVER_URL") == "https://github.com":
127128
github_connection = github3.login(token=token)
128129
else:
129-
github_connection = github3.GitHubEnterprise(os.getenv("GITHUB_SERVER_URL"),token=token)
130+
github_connection = github3.GitHubEnterprise(
131+
os.getenv("GITHUB_SERVER_URL"), token=token
132+
)
130133
else:
131134
raise ValueError("GH_TOKEN environment variable not set")
132135

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

163168
for issue in issues:
164169
if discussions:
@@ -320,6 +325,7 @@ def main():
320325
average_time_in_labels,
321326
num_issues_open,
322327
num_issues_closed,
328+
search_query,
323329
)
324330
write_to_markdown(
325331
issues_with_metrics,
@@ -330,6 +336,7 @@ def main():
330336
num_issues_open,
331337
num_issues_closed,
332338
labels,
339+
search_query,
333340
)
334341

335342

json_writer.py

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
average_time_to_answer: timedelta,
99
num_issues_opened: int,
1010
num_issues_closed: int,
11+
search_query: str,
1112
) -> str:
1213
Write the issues with metrics to a json file.
1314
@@ -30,6 +31,7 @@ def write_to_json(
3031
average_time_in_labels: Union[dict, None],
3132
num_issues_opened: Union[int, None],
3233
num_issues_closed: Union[int, None],
34+
search_query: str,
3335
) -> str:
3436
"""
3537
Write the issues with metrics to a JSON file called issue_metrics.json.
@@ -63,6 +65,7 @@ def write_to_json(
6365
}
6466
},
6567
],
68+
"search_query": "is:issue is:open repo:owner/repo"
6669
}
6770
6871
"""
@@ -107,6 +110,9 @@ def write_to_json(
107110
# Add the issues to the metrics dictionary
108111
metrics["issues"] = issues
109112

113+
# Add the search query to the metrics dictionary
114+
metrics["search_query"] = search_query
115+
110116
# add output to github action output
111117
# pylint: disable=unspecified-encoding
112118
metrics_json = json.dumps(metrics)

labels.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_label_events(
2929
return label_events
3030

3131

32-
def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: # type: ignore
32+
def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
3333
"""
3434
Calculate the time spent with the given labels on a given issue.
3535

markdown_writer.py

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def write_to_markdown(
7373
num_issues_opened: Union[int, None],
7474
num_issues_closed: Union[int, None],
7575
labels=None,
76+
search_query=None,
7677
) -> None:
7778
"""Write the issues with metrics to a markdown file.
7879
@@ -88,6 +89,7 @@ def write_to_markdown(
8889
num_issues_opened (int): The Number of items that remain opened.
8990
num_issues_closed (int): The number of issues that were closed.
9091
labels (List[str]): A list of the labels that are used in the issues.
92+
search_query (str): The search query used to find the issues.
9193
9294
Returns:
9395
None.
@@ -154,6 +156,8 @@ def write_to_markdown(
154156
file.write(
155157
"\n_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
156158
)
159+
if search_query:
160+
file.write(f"Search query used to find these items: `{search_query}`\n")
157161

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

test_json_writer.py

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_write_to_json(self):
6464
"label_metrics": {},
6565
},
6666
],
67+
"search_query": "is:issue repo:owner/repo",
6768
}
6869

6970
# Call the function and check the output
@@ -78,6 +79,7 @@ def test_write_to_json(self):
7879
},
7980
num_issues_opened=num_issues_opened,
8081
num_issues_closed=num_issues_closed,
82+
search_query="is:issue repo:owner/repo",
8183
),
8284
json.dumps(expected_output),
8385
)

test_markdown_writer.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def test_write_to_markdown(self):
6363
num_issues_opened=num_issues_opened,
6464
num_issues_closed=num_issues_closed,
6565
labels=["bug"],
66+
search_query="is:issue is:open label:bug",
6667
)
6768

6869
# Check that the function writes the correct markdown file
@@ -87,17 +88,18 @@ def test_write_to_markdown(self):
8788
"| Issue 2 | https://github.com/user/repo/issues/2 | 3 days, 0:00:00 | "
8889
"4 days, 0:00:00 | 5 days, 0:00:00 | 2 days, 0:00:00 |\n\n"
8990
"_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
91+
"Search query used to find these items: `is:issue is:open label:bug`\n"
9092
)
9193
self.assertEqual(content, expected_content)
9294
os.remove("issue_metrics.md")
9395

9496
def test_write_to_markdown_with_vertical_bar_in_title(self):
9597
"""Test that write_to_markdown writes the correct markdown file when the title contains a vertical bar.
9698
97-
This test creates a list of mock GitHub issues (one of which contains a vertical bar in the title) with time to first response
98-
attributes, calls write_to_markdown with the list and the average time to
99-
first response, time to close and checks that the function writes the correct
100-
markdown file.
99+
This test creates a list of mock GitHub issues (one of which contains a vertical
100+
bar in the title) with time to first response attributes, calls write_to_markdown
101+
with the list and the average time to first response, time to close and checks
102+
that the function writes the correct markdown file.
101103
102104
"""
103105
# Create mock data
@@ -243,6 +245,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
243245
num_issues_opened=num_issues_opened,
244246
num_issues_closed=num_issues_closed,
245247
labels=["label1"],
248+
search_query="repo:user/repo is:issue",
246249
)
247250

248251
# Check that the function writes the correct markdown file
@@ -260,6 +263,7 @@ def test_writes_markdown_file_with_non_hidden_columns_only(self):
260263
"| Issue 1 | https://github.com/user/repo/issues/1 |\n"
261264
"| Issue 2 | https://github.com/user/repo/issues/2 |\n\n"
262265
"_This report was generated with the [Issue Metrics Action](https://github.com/github/issue-metrics)_\n"
266+
"Search query used to find these items: `repo:user/repo is:issue`\n"
263267
)
264268
self.assertEqual(content, expected_content)
265269
os.remove("issue_metrics.md")

time_to_first_response.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
def measure_time_to_first_response(
2828
issue: Union[github3.issues.Issue, None], # type: ignore
2929
discussion: Union[dict, None],
30-
ignore_users: List[str] = [],
30+
ignore_users: List[str] = None,
3131
) -> Union[timedelta, None]:
3232
"""Measure the time to first response for a single issue or a discussion.
3333
@@ -44,6 +44,8 @@ def measure_time_to_first_response(
4444
first_comment_time = None
4545
earliest_response = None
4646
issue_time = None
47+
if ignore_users is None:
48+
ignore_users = []
4749

4850
# Get the first comment time
4951
if issue:

0 commit comments

Comments
 (0)