Skip to content

Commit 74dfcae

Browse files
[PR #10880/30894f41 backport][stable-11] github_app_access_token: add support for GitHub Enterprise Server (#10881)
github_app_access_token: add support for GitHub Enterprise Server (#10880) * github_app_access_token: add support for GitHub Enterprise Server (#10879) Add option to specify api endpoint for a GitHub Enterprise Server. If option is not specified, defaults to https://api.github.com. * refactor: apply changes as suggested by felixfontein * docs: fix nox check error and type-o nox check: plugins/lookup/github_app_access_token.py:57:1: DOCUMENTATION: error: too many blank lines (1 > 0) (empty-lines) * refactor: apply changes as suggested by russoz * refactor: apply changes as suggested by felixfontein (cherry picked from commit 30894f4) Co-authored-by: Chris <[email protected]>
1 parent 1e01aea commit 74dfcae

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- "github_app_access_token lookup plugin - add support for GitHub Enterprise Server (https://github.com/ansible-collections/community.general/issues/10879, https://github.com/ansible-collections/community.general/pull/10880)."

plugins/lookup/github_app_access_token.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
- How long the token should last for in seconds.
4848
default: 600
4949
type: int
50+
github_url:
51+
description:
52+
- Base URL for the GitHub API (for GitHub Enterprise Server).
53+
- "Example: C(https://github-enterprise-server.example.com/api/v3)"
54+
default: https://api.github.com
55+
type: str
56+
version_added: 11.4.0
5057
"""
5158

5259
EXAMPLES = r"""
@@ -154,22 +161,24 @@ def encode_jwt(app_id, private_key_obj, exp=600):
154161
raise AnsibleError(f"Error while encoding jwt: {e}")
155162

156163

157-
def post_request(generated_jwt, installation_id):
158-
github_api_url = f'https://api.github.com/app/installations/{installation_id}/access_tokens'
164+
def post_request(generated_jwt, installation_id, api_base):
165+
base = api_base.rstrip('/')
166+
github_url = f"{base}/app/installations/{installation_id}/access_tokens"
167+
159168
headers = {
160169
"Authorization": f'Bearer {generated_jwt}',
161170
"Accept": "application/vnd.github.v3+json",
162171
}
163172
try:
164-
response = open_url(github_api_url, headers=headers, method='POST')
173+
response = open_url(github_url, headers=headers, method='POST')
165174
except HTTPError as e:
166175
try:
167176
error_body = json.loads(e.read().decode())
168177
display.vvv(f"Error returned: {error_body}")
169178
except Exception:
170179
error_body = {}
171180
if e.code == 404:
172-
raise AnsibleError("Github return error. Please confirm your installationd_id value is valid")
181+
raise AnsibleError("Github return error. Please confirm your installation_id value is valid")
173182
elif e.code == 401:
174183
raise AnsibleError("Github return error. Please confirm your private key is valid")
175184
raise AnsibleError(f"Unexpected data returned: {e} -- {error_body}")
@@ -181,10 +190,10 @@ def post_request(generated_jwt, installation_id):
181190
return json_data.get('token')
182191

183192

184-
def get_token(key_path, app_id, installation_id, private_key, expiry=600):
193+
def get_token(key_path, app_id, installation_id, private_key, github_url, expiry=600):
185194
jwk = read_key(key_path, private_key)
186195
generated_jwt = encode_jwt(app_id, jwk, exp=expiry)
187-
return post_request(generated_jwt, installation_id)
196+
return post_request(generated_jwt, installation_id, github_url)
188197

189198

190199
class LookupModule(LookupBase):
@@ -209,6 +218,7 @@ def run(self, terms, variables=None, **kwargs):
209218
self.get_option('app_id'),
210219
self.get_option('installation_id'),
211220
self.get_option('private_key'),
221+
self.get_option('github_url'),
212222
self.get_option('token_expiry'),
213223
)
214224

0 commit comments

Comments
 (0)