Skip to content
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
2 changes: 1 addition & 1 deletion .github/linters/.mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
disable_error_code = attr-defined, import-not-found

[mypy-github3.*]
[mypy-github.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion .github/linters/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Don't suggest [ -n "$VAR" ] over [ ! -z "$VAR" ]
# Don't suggest [ -n "$VAR" ] over [ ! -z "$VAR" ]
disable=SC2129
40 changes: 18 additions & 22 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This is the module that contains functions related to authenticating to GitHub with a personal access token."""

import github3
import requests
from github import Auth, Github, GithubException, GithubIntegration


def auth_to_github(
Expand All @@ -11,7 +11,7 @@ def auth_to_github(
gh_app_private_key_bytes: bytes,
ghe: str,
gh_app_enterprise_only: bool,
) -> github3.GitHub:
) -> Github:
"""
Connect to GitHub.com or GitHub Enterprise, depending on env variables.

Expand All @@ -24,29 +24,25 @@ def auth_to_github(
gh_app_enterprise_only (bool): Set this to true if the GH APP is created on GHE and needs to communicate with GHE api only

Returns:
github3.GitHub: the GitHub connection object
Github: the GitHub connection object
"""
if gh_app_id and gh_app_private_key_bytes and gh_app_installation_id:
app_auth = Auth.AppAuth(int(gh_app_id), gh_app_private_key_bytes.decode())
installation_auth = app_auth.get_installation_auth(int(gh_app_installation_id))
if ghe and gh_app_enterprise_only:
gh = github3.github.GitHubEnterprise(url=ghe)
github_connection = Github(base_url=f"{ghe}/api/v3", auth=installation_auth)
else:
gh = github3.github.GitHub()
gh.login_as_app_installation(
gh_app_private_key_bytes, str(gh_app_id), gh_app_installation_id
)
github_connection = gh
github_connection = Github(auth=installation_auth)
elif ghe and token:
github_connection = github3.github.GitHubEnterprise(url=ghe, token=token)
github_connection = Github(base_url=f"{ghe}/api/v3", auth=Auth.Token(token))
elif token:
github_connection = github3.login(token=token)
github_connection = Github(auth=Auth.Token(token))
else:
raise ValueError(
"GH_TOKEN or the set of [GH_APP_ID, GH_APP_INSTALLATION_ID, GH_APP_PRIVATE_KEY] environment variables are not set"
)

if not github_connection:
raise ValueError("Unable to authenticate to GitHub")
return github_connection # type: ignore
return github_connection


def get_github_app_installation_token(
Expand All @@ -68,14 +64,14 @@ def get_github_app_installation_token(
Returns:
str: the GitHub App token
"""
jwt_headers = github3.apps.create_jwt_headers(gh_app_private_key_bytes, gh_app_id)
api_endpoint = f"{ghe}/api/v3" if ghe else "https://api.github.com"
url = f"{api_endpoint}/app/installations/{gh_app_installation_id}/access_tokens"

app_auth = Auth.AppAuth(int(gh_app_id), gh_app_private_key_bytes.decode())
if ghe:
gi = GithubIntegration(base_url=f"{ghe}/api/v3", auth=app_auth)
else:
gi = GithubIntegration(auth=app_auth)
try:
response = requests.post(url, headers=jwt_headers, json=None, timeout=5)
response.raise_for_status()
except requests.exceptions.RequestException as e:
installation_token = gi.get_access_token(int(gh_app_installation_id))
except (GithubException, requests.exceptions.RequestException) as e:
print(f"Request to get GitHub App Installation Token failed: {e}")
return None
return response.json().get("token")
return installation_token.token
17 changes: 9 additions & 8 deletions contributors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=broad-exception-caught
"""This file contains the main() and other functions needed to get contributor information from the organization or repository"""

from datetime import datetime
from typing import List

import auth
Expand Down Expand Up @@ -122,12 +123,11 @@ def get_all_contributors(
"""
repos = []
if organization:
repos = github_connection.organization(organization).repositories()
repos = github_connection.get_organization(organization).get_repos()
else:
repos = []
for repo in repository_list:
owner, repo_name = repo.split("/")
repository_obj = github_connection.repository(owner, repo_name)
repository_obj = github_connection.get_repo(repo)
repos.append(repository_obj)

all_contributors = []
Expand Down Expand Up @@ -165,9 +165,10 @@ def get_contributors(repo: object, start_date: str, end_date: str, ghe: str):
# on large repositories.
avatars: dict[str, str] = {}
counts: dict[str, int] = {}
for commit in repo.commits(since=start_date, until=end_date):
# github3.py leaves commit.author as a raw dict (not a ShortUser)
# when the API returns an author payload without a matching GitHub user.
for commit in repo.get_commits(
since=datetime.fromisoformat(start_date),
until=datetime.fromisoformat(end_date),
):
login = getattr(commit.author, "login", None)
if not login or "[bot]" in login:
continue
Expand All @@ -187,15 +188,15 @@ def get_contributors(repo: object, start_date: str, end_date: str, ghe: str):
)
contributors.append(contributor)
else:
for user in repo.contributors():
for user in repo.get_contributors():
login = getattr(user, "login", None)
if not login or "[bot]" in login:
continue
commit_url = f"{endpoint}/{repo.full_name}/commits?author={login}"
contributor = contributor_stats.ContributorStats(
login,
getattr(user, "avatar_url", "") or "",
getattr(user, "contributions_count", 0) or 0,
getattr(user, "contributions", 0) or 0,
commit_url,
"",
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "2.0.0"
description = "GitHub Action that produces information about contributors over a specified time period."
requires-python = ">=3.11"
dependencies = [
"github3-py==4.0.1",
"PyGithub==2.9.1",
"python-dotenv==1.2.2",
"requests==2.34.2",
]
Expand Down
Loading
Loading