Skip to content
Draft
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
18 changes: 13 additions & 5 deletions src/github_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from __future__ import annotations

import contextlib
import logging
import time
from typing import Generator

import jwt
import requests

logger = logging.getLogger(__name__)


class GithubAppToken:
def __init__(self, private_key, app_id) -> None:
Expand All @@ -25,14 +28,19 @@ def get_token(self, installation_id: int) -> Generator[str, None, None]:
)
req.raise_for_status()
resp = req.json()
token = resp["token"]
try:
# This token expires in an hour
yield resp["token"]
yield token
finally:
requests.delete(
"https://api.github.com/installation/token",
headers={"Authorization": f"token {resp['token']}"},
)
try:
requests.delete(
"https://api.github.com/installation/token",
headers={"Authorization": f"token {token}"},
)
except requests.RequestException:
# Best-effort cleanup should not mask the actual workflow failure.
logger.warning("Failed to revoke GitHub app installation token", exc_info=True)

def get_jwt_token(self, private_key, app_id):
payload = {
Expand Down
51 changes: 51 additions & 0 deletions tests/test_github_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations

from unittest.mock import Mock
from unittest.mock import patch

import pytest
import requests

from src.github_app import GithubAppToken


def _new_token_manager():
manager = GithubAppToken.__new__(GithubAppToken)
manager.headers = {"Authorization": "Bearer fake-jwt"}
return manager


@patch("src.github_app.requests.delete")
@patch("src.github_app.requests.post")
def test_get_token_returns_token_when_cleanup_succeeds(mock_post, mock_delete):
response = Mock()
response.raise_for_status.return_value = None
response.json.return_value = {"token": "installation-token"}
mock_post.return_value = response

manager = _new_token_manager()
with manager.get_token(123) as token:
assert token == "installation-token"

mock_delete.assert_called_once_with(
"https://api.github.com/installation/token",
headers={"Authorization": "token installation-token"},
)


@patch("src.github_app.requests.delete")
@patch("src.github_app.requests.post")
def test_get_token_does_not_mask_original_error_when_cleanup_fails(
mock_post,
mock_delete,
):
response = Mock()
response.raise_for_status.return_value = None
response.json.return_value = {"token": "installation-token"}
mock_post.return_value = response
mock_delete.side_effect = requests.ConnectionError("cleanup network failure")

manager = _new_token_manager()
with pytest.raises(RuntimeError, match="original failure"):
with manager.get_token(123):
raise RuntimeError("original failure")
Loading