Skip to content

Commit 9954dc2

Browse files
feat: add env variable for ghe apps only + add tests
1 parent 1408143 commit 9954dc2

File tree

7 files changed

+188
-12
lines changed

7 files changed

+188
-12
lines changed

.github/linters/.markdown-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ MD024: false
88
# MD033/no-inline-html - Inline HTML
99
MD033:
1010
# Allowed elements
11-
allowed_elements: [br, li, ul]
11+
allowed_elements: [br, li, ul]

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ This action can be configured to authenticate with GitHub App Installation or Pe
7979

8080
##### GitHub App Installation
8181

82-
| field | required | default | description |
83-
| ------------------------ | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
84-
| `GH_APP_ID` | True | `""` | GitHub Application ID. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
85-
| `GH_APP_INSTALLATION_ID` | True | `""` | GitHub Application Installation ID. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
86-
| `GH_APP_PRIVATE_KEY` | True | `""` | GitHub Application Private Key. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
82+
| field | required | default | description |
83+
| ---------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
84+
| `GH_APP_ID` | True | `""` | GitHub Application ID. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
85+
| `GH_APP_INSTALLATION_ID` | True | `""` | GitHub Application Installation ID. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
86+
| `GH_APP_PRIVATE_KEY` | True | `""` | GitHub Application Private Key. See [documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/about-authentication-with-a-github-app) for more details. |
87+
| `GITHUB_APP_ENTERPRISE_ONLY` | False | false | Set this input to `true` if your app is created in GHE and communicates with GHE. |
8788

8889
The needed GitHub app permissions are the following:
8990

@@ -221,8 +222,9 @@ jobs:
221222
GH_APP_ID: ${{ secrets.GH_APP_ID }}
222223
GH_APP_INSTALLATION_ID: ${{ secrets.GH_APP_INSTALLATION_ID }}
223224
GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }}
225+
# GITHUB_APP_ENTERPRISE_ONLY: True --> Set to true when created GHE App needs to communicate with GHE api
224226
GH_ENTERPRISE_URL: ${{ github.server_url }}
225-
# GH_TOKEN: ${{ steps.app-token.outputs.token }} the token input is not used if the github app inputs are set
227+
# GH_TOKEN: ${{ steps.app-token.outputs.token }} --> the token input is not used if the github app inputs are set
226228
ORGANIZATION: your_organization
227229
UPDATE_EXISTING: True
228230
GROUP_DEPENDENCIES: True

auth.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
"""This is the module that contains functions related to authenticating to GitHub with a personal access token."""
22

3+
import logging
4+
import logging.config
5+
36
import github3
47
import requests
58

9+
logging.basicConfig(level=logging.DEBUG)
10+
611

712
def auth_to_github(
813
token: str,
914
gh_app_id: int | None,
1015
gh_app_installation_id: int | None,
1116
gh_app_private_key_bytes: bytes,
1217
ghe: str,
18+
gh_app_enterprise_only: bool,
1319
) -> github3.GitHub:
1420
"""
1521
Connect to GitHub.com or GitHub Enterprise, depending on env variables.
@@ -20,12 +26,13 @@ def auth_to_github(
2026
gh_app_installation_id (int | None): the GitHub App Installation ID
2127
gh_app_private_key_bytes (bytes): the GitHub App Private Key
2228
ghe (str): the GitHub Enterprise URL
29+
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
2330
2431
Returns:
2532
github3.GitHub: the GitHub connection object
2633
"""
2734
if gh_app_id and gh_app_private_key_bytes and gh_app_installation_id:
28-
if ghe:
35+
if ghe and gh_app_enterprise_only:
2936
gh = github3.github.GitHubEnterprise(url=ghe)
3037
else:
3138
gh = github3.github.GitHub()

env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def get_env_vars(
9898
int | None,
9999
int | None,
100100
bytes,
101+
bool,
101102
str,
102103
str,
103104
list[str],
@@ -132,6 +133,7 @@ def get_env_vars(
132133
gh_app_id (int | None): The GitHub App ID to use for authentication
133134
gh_app_installation_id (int | None): The GitHub App Installation ID to use for authentication
134135
gh_app_private_key_bytes (bytes): The GitHub App Private Key as bytes to use for authentication
136+
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
135137
token (str): The GitHub token to use for authentication
136138
ghe (str): The GitHub Enterprise URL to use for authentication
137139
exempt_repositories_list (list[str]): A list of repositories to exempt from the action
@@ -183,6 +185,7 @@ def get_env_vars(
183185
gh_app_id = get_int_env_var("GH_APP_ID")
184186
gh_app_private_key_bytes = os.environ.get("GH_APP_PRIVATE_KEY", "").encode("utf8")
185187
gh_app_installation_id = get_int_env_var("GH_APP_INSTALLATION_ID")
188+
gh_app_enterprise_only = get_bool_env_var("GITHUB_APP_ENTERPRISE_ONLY")
186189

187190
if gh_app_id and (not gh_app_private_key_bytes or not gh_app_installation_id):
188191
raise ValueError(
@@ -340,6 +343,7 @@ def get_env_vars(
340343
gh_app_id,
341344
gh_app_installation_id,
342345
gh_app_private_key_bytes,
346+
gh_app_enterprise_only,
343347
token,
344348
ghe,
345349
exempt_repositories_list,

evergreen.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def main(): # pragma: no cover
2121
gh_app_id,
2222
gh_app_installation_id,
2323
gh_app_private_key,
24+
gh_app_enterprise_only,
2425
token,
2526
ghe,
2627
exempt_repositories_list,
@@ -46,7 +47,12 @@ def main(): # pragma: no cover
4647

4748
# Auth to GitHub.com or GHE
4849
github_connection = auth.auth_to_github(
49-
token, gh_app_id, gh_app_installation_id, gh_app_private_key, ghe
50+
token,
51+
gh_app_id,
52+
gh_app_installation_id,
53+
gh_app_private_key,
54+
ghe,
55+
gh_app_enterprise_only,
5056
)
5157

5258
if not token and gh_app_id and gh_app_installation_id and gh_app_private_key:

test_auth.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_auth_to_github_with_token(self, mock_login):
1818
"""
1919
mock_login.return_value = "Authenticated to GitHub.com"
2020

21-
result = auth.auth_to_github("token", "", "", b"", "")
21+
result = auth.auth_to_github("token", "", "", b"", "", False)
2222

2323
self.assertEqual(result, "Authenticated to GitHub.com")
2424

@@ -28,7 +28,7 @@ def test_auth_to_github_without_token(self):
2828
Expect a ValueError to be raised.
2929
"""
3030
with self.assertRaises(ValueError) as context_manager:
31-
auth.auth_to_github("", "", "", b"", "")
31+
auth.auth_to_github("", "", "", b"", "", False)
3232
the_exception = context_manager.exception
3333
self.assertEqual(
3434
str(the_exception),
@@ -41,10 +41,38 @@ def test_auth_to_github_with_ghe(self, mock_ghe):
4141
Test the auth_to_github function when the GitHub Enterprise URL is provided.
4242
"""
4343
mock_ghe.return_value = "Authenticated to GitHub Enterprise"
44-
result = auth.auth_to_github("token", "", "", b"", "https://github.example.com")
44+
result = auth.auth_to_github(
45+
"token", "", "", b"", "https://github.example.com", False
46+
)
4547

4648
self.assertEqual(result, "Authenticated to GitHub Enterprise")
4749

50+
@patch("github3.github.GitHubEnterprise")
51+
def test_auth_to_github_with_ghe_and_ghe_app(self, mock_ghe):
52+
"""
53+
Test the auth_to_github function when the GitHub Enterprise URL is provided and the app was created in GitHub Enterprise URL.
54+
"""
55+
mock = mock_ghe.return_value
56+
mock.login_as_app_installation = MagicMock(return_value=True)
57+
result = auth.auth_to_github(
58+
"", "123", "123", b"123", "https://github.example.com", True
59+
)
60+
mock.login_as_app_installation.assert_called_once()
61+
self.assertEqual(result, mock)
62+
63+
@patch("github3.github.GitHub")
64+
def test_auth_to_github_with_app(self, mock_gh):
65+
"""
66+
Test the auth_to_github function when app credentials are provided
67+
"""
68+
mock = mock_gh.return_value
69+
mock.login_as_app_installation = MagicMock(return_value=True)
70+
result = auth.auth_to_github(
71+
"", "123", "123", b"123", "https://github.example.com", False
72+
)
73+
mock.login_as_app_installation.assert_called_once()
74+
self.assertEqual(result, mock)
75+
4876
@patch("github3.apps.create_jwt_headers", MagicMock(return_value="gh_token"))
4977
@patch("requests.post")
5078
def test_get_github_app_installation_token(self, mock_post):

0 commit comments

Comments
 (0)