-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from ricardojdsilva87/feat/support-github-ent…
…erprise-api feat: support github enterprise api
- Loading branch information
Showing
8 changed files
with
160 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
[settings] | ||
profile = black | ||
known_third_party = github3,dateutil,dotenv | ||
known_first_party = auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""This is the module that contains functions related to authenticating to GitHub with a personal access token.""" | ||
|
||
import github3 | ||
|
||
|
||
def auth_to_github( | ||
token: str, | ||
gh_app_id: int | None, | ||
gh_app_installation_id: int | None, | ||
gh_app_private_key_bytes: bytes, | ||
ghe: str, | ||
gh_app_enterprise_only: bool, | ||
) -> github3.GitHub: | ||
""" | ||
Connect to GitHub.com or GitHub Enterprise, depending on env variables. | ||
Args: | ||
token (str): the GitHub personal access token | ||
gh_app_id (int | None): the GitHub App ID | ||
gh_app_installation_id (int | None): the GitHub App Installation ID | ||
gh_app_private_key_bytes (bytes): the GitHub App Private Key | ||
ghe (str): the GitHub Enterprise URL | ||
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 | ||
""" | ||
if gh_app_id and gh_app_private_key_bytes and gh_app_installation_id: | ||
if ghe and gh_app_enterprise_only: | ||
gh = github3.github.GitHubEnterprise(url=ghe) | ||
else: | ||
gh = github3.github.GitHub() | ||
gh.login_as_app_installation( | ||
gh_app_private_key_bytes, gh_app_id, gh_app_installation_id | ||
) | ||
github_connection = gh | ||
elif ghe and token: | ||
github_connection = github3.github.GitHubEnterprise(url=ghe, token=token) | ||
elif token: | ||
github_connection = github3.login(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Test cases for the auth module.""" | ||
|
||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
import auth | ||
|
||
|
||
class TestAuth(unittest.TestCase): | ||
""" | ||
Test case for the auth module. | ||
""" | ||
|
||
@patch("github3.login") | ||
def test_auth_to_github_with_token(self, mock_login): | ||
""" | ||
Test the auth_to_github function when the token is provided. | ||
""" | ||
mock_login.return_value = "Authenticated to GitHub.com" | ||
|
||
result = auth.auth_to_github("token", "", "", b"", "", False) | ||
|
||
self.assertEqual(result, "Authenticated to GitHub.com") | ||
|
||
def test_auth_to_github_without_token(self): | ||
""" | ||
Test the auth_to_github function when the token is not provided. | ||
Expect a ValueError to be raised. | ||
""" | ||
with self.assertRaises(ValueError) as context_manager: | ||
auth.auth_to_github("", "", "", b"", "", False) | ||
the_exception = context_manager.exception | ||
self.assertEqual( | ||
str(the_exception), | ||
"GH_TOKEN or the set of [GH_APP_ID, GH_APP_INSTALLATION_ID, GH_APP_PRIVATE_KEY] environment variables are not set", | ||
) | ||
|
||
@patch("github3.github.GitHubEnterprise") | ||
def test_auth_to_github_with_ghe(self, mock_ghe): | ||
""" | ||
Test the auth_to_github function when the GitHub Enterprise URL is provided. | ||
""" | ||
mock_ghe.return_value = "Authenticated to GitHub Enterprise" | ||
result = auth.auth_to_github( | ||
"token", "", "", b"", "https://github.example.com", False | ||
) | ||
|
||
self.assertEqual(result, "Authenticated to GitHub Enterprise") | ||
|
||
@patch("github3.github.GitHubEnterprise") | ||
def test_auth_to_github_with_ghe_and_ghe_app(self, mock_ghe): | ||
""" | ||
Test the auth_to_github function when the GitHub Enterprise URL is provided and the app was created in GitHub Enterprise URL. | ||
""" | ||
mock = mock_ghe.return_value | ||
mock.login_as_app_installation = MagicMock(return_value=True) | ||
result = auth.auth_to_github( | ||
"", "123", "123", b"123", "https://github.example.com", True | ||
) | ||
mock.login_as_app_installation.assert_called_once() | ||
self.assertEqual(result, mock) | ||
|
||
@patch("github3.github.GitHub") | ||
def test_auth_to_github_with_app(self, mock_gh): | ||
""" | ||
Test the auth_to_github function when app credentials are provided | ||
""" | ||
mock = mock_gh.return_value | ||
mock.login_as_app_installation = MagicMock(return_value=True) | ||
result = auth.auth_to_github( | ||
"", "123", "123", b"123", "https://github.example.com", False | ||
) | ||
mock.login_as_app_installation.assert_called_once() | ||
self.assertEqual(result, mock) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.