Skip to content

Commit

Permalink
Merge pull request #1 from Advik-B/sourcery/main
Browse files Browse the repository at this point in the history
Sourcery AI refactored this branch
  • Loading branch information
Advik-B authored Jun 14, 2023
2 parents 5de46e3 + 9246937 commit f589da2
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ def search_issues(
return issues


def auth_to_github() -> github3.GitHub | None:

def auth_to_github():
"""Connect to GitHub.com or GitHub Enterprise, depending on env variables."""

"""
Connect to GitHub.com or GitHub Enterprise, depending on env variables.
Returns:
github3.GitHub: A github api connection.
"""
token = os.getenv("GH_TOKEN")
if token:
if token := os.getenv("GH_TOKEN"):
github_connection = github3.login(token=token)
else:
raise ValueError("GH_TOKEN environment variable not set")
Expand All @@ -97,7 +99,7 @@ def measure_time_to_first_response(issue: github3.issues.Issue) -> timedelta:
# Get the first comment
if issue.comments <= 0:
first_comment_time = None
time_to_first_response = None
return None
else:
comments = issue.issue.comments(
number=1, sort="created", direction="asc"
Expand All @@ -110,9 +112,7 @@ def measure_time_to_first_response(issue: github3.issues.Issue) -> timedelta:
issue_time = datetime.fromisoformat(issue.created_at) # type: ignore

# Calculate the time between the issue and the first comment
time_to_first_response = first_comment_time - issue_time # type: ignore

return time_to_first_response
return first_comment_time - issue_time


def measure_time_to_close(issue: github3.issues.Issue) -> timedelta:
Expand All @@ -131,9 +131,7 @@ def measure_time_to_close(issue: github3.issues.Issue) -> timedelta:
closed_at = datetime.fromisoformat(issue.closed_at)
created_at = datetime.fromisoformat(issue.created_at)

time_to_close = closed_at - created_at

return time_to_close
return closed_at - created_at


def get_average_time_to_first_response(issues: List[IssueWithMetrics]) -> timedelta:
Expand Down Expand Up @@ -249,7 +247,8 @@ def get_average_time_to_close(issues_with_metrics: List[IssueWithMetrics]) -> ti

# Calculate the total time to close for all issues
total_time_to_close = sum(
[issue.time_to_close for issue in issues_with_time_to_close], timedelta()
(issue.time_to_close for issue in issues_with_time_to_close),
timedelta(),
)

# Calculate the average time to close
Expand Down Expand Up @@ -313,10 +312,10 @@ def main():
)
if issue.state == "closed": # type: ignore
issue_with_metrics.time_to_close = measure_time_to_close(issue) # type: ignore
if issue.state == "open": # type: ignore
num_issues_open += 1
if issue.state == "closed": # type: ignore
if issue.state == "closed":
num_issues_closed += 1
elif issue.state == "open":
num_issues_open += 1
issues_with_metrics.append(issue_with_metrics)

average_time_to_first_response = get_average_time_to_first_response(
Expand Down Expand Up @@ -348,10 +347,10 @@ def get_env_vars() -> tuple[str, str]:
if not search_query:
raise ValueError("SEARCH_QUERY environment variable not set")

repo_url = os.getenv("REPOSITORY_URL")
if not repo_url:
if repo_url := os.getenv("REPOSITORY_URL"):
return search_query, repo_url
else:
raise ValueError("REPOSITORY_URL environment variable not set")
return search_query, repo_url


class IssueWithMetrics:
Expand Down

0 comments on commit f589da2

Please sign in to comment.