Skip to content

Commit

Permalink
#13 Clean up configuration yaml and environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsken committed Nov 1, 2024
1 parent 65f2a82 commit 29dd249
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
9 changes: 5 additions & 4 deletions check_done/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import requests

from check_done.common import (
CHECK_DONE_GITHUB_APP_ID,
CHECK_DONE_GITHUB_APP_PRIVATE_KEY,
AuthenticationError,
HttpBearerAuth,
config_info,
)

_CHECK_DONE_GITHUB_APP_ID = config_info().check_done_github_app_id
_CHECK_DONE_GITHUB_APP_PRIVATE_KEY = config_info().check_done_github_app_private_key
_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code"
_SECONDS_PER_MINUTE = 60
_ISSUED_AT = int(time.time())
Expand Down Expand Up @@ -50,9 +51,9 @@ def _generated_jwt_token() -> str:
payload = {
"exp": _EXPIRES_AT,
"iat": _ISSUED_AT,
"iss": CHECK_DONE_GITHUB_APP_ID,
"iss": _CHECK_DONE_GITHUB_APP_ID,
}
return jwt.encode(payload, CHECK_DONE_GITHUB_APP_PRIVATE_KEY, algorithm="RS256")
return jwt.encode(payload, _CHECK_DONE_GITHUB_APP_PRIVATE_KEY, algorithm="RS256")
except Exception as error:
raise AuthenticationError(f"Cannot generate JWT token: {error}") from error

Expand Down
27 changes: 12 additions & 15 deletions check_done/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from requests.auth import AuthBase

load_dotenv()
_ENVVAR_CHECK_DONE_GITHUB_APP_ID = "CHECK_DONE_GITHUB_APP_ID"
_ENVVAR_CHECK_DONE_GITHUB_APP_PRIVATE_KEY = "CHECK_DONE_GITHUB_APP_PRIVATE_KEY"
CHECK_DONE_GITHUB_APP_ID = os.environ.get(_ENVVAR_CHECK_DONE_GITHUB_APP_ID)
CHECK_DONE_GITHUB_APP_PRIVATE_KEY = os.environ.get(_ENVVAR_CHECK_DONE_GITHUB_APP_PRIVATE_KEY)

_GITHUB_ORGANIZATION_NAME_AND_PROJECT_NUMBER_URL_REGEX = re.compile(
r"https://github\.com/orgs/(?P<organization_name>[a-zA-Z0-9\-]+)/projects/(?P<project_number>[0-9]+).*"
Expand All @@ -22,20 +18,21 @@


class _ConfigInfo(BaseModel):
board_url: str
api_key: str

@field_validator("api_key", mode="before")
def api_key_from_env(cls, api_key: Any | None):
if isinstance(api_key, str):
stripped_api_key = api_key.strip()
project_board_url: str
check_done_github_app_id: str
check_done_github_app_private_key: str

@field_validator("check_done_github_app_id", "check_done_github_app_private_key", mode="before")
def value_from_env(cls, value: Any | None):
if isinstance(value, str):
stripped_value = value.strip()
result = (
resolved_environment_variables(api_key)
if stripped_api_key.startswith("${") and stripped_api_key.endswith("}")
else stripped_api_key
resolved_environment_variables(value)
if stripped_value.startswith("${") and stripped_value.endswith("}")
else stripped_value
)
else:
result = api_key
result = value
return result


Expand Down
4 changes: 2 additions & 2 deletions check_done/done_project_items_info/done_project_items_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
)

_GRAPHQL_ENDPOINT = "https://api.github.com/graphql"
_BOARD_URL = config_info().board_url
ORGANIZATION_NAME, PROJECT_NUMBER = github_organization_name_and_project_number_from_url_if_matches(_BOARD_URL)
_PROJECT_BOARD_URL = config_info().project_board_url
ORGANIZATION_NAME, PROJECT_NUMBER = github_organization_name_and_project_number_from_url_if_matches(_PROJECT_BOARD_URL)
_ACCESS_TOKEN = github_app_access_token(ORGANIZATION_NAME)
_PATH_TO_QUERIES = Path(__file__).parent.parent / "done_project_items_info" / "queries"
_MAX_ENTRIES_PER_PAGE = 100
Expand Down
5 changes: 3 additions & 2 deletions data/.check_done.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
board_url: "https://github.com/orgs/siisurit/projects/2"
api_key: ${GITHUB_API_KEY}
project_board_url: "https://github.com/orgs/siisurit/projects/2"
check_done_github_app_id: ${CHECK_DONE_GITHUB_APP_ID}
check_done_github_app_private_key: ${CHECK_DONE_GITHUB_APP_PRIVATE_KEY}

0 comments on commit 29dd249

Please sign in to comment.