33
44import jwt
55import requests
6+ import logging
67
78from src .config import get_env , normalize_private_key
89from src .config import get_secret
910
1011GITHUB_API = "https://api.github.com"
12+ logger = logging .getLogger ("github-app" )
13+ GITHUB_API_VERSION = "2022-11-28"
1114
1215
1316def create_app_jwt () -> str :
@@ -29,8 +32,16 @@ def get_installation_token(installation_id: int) -> Dict[str, str]:
2932 headers = {
3033 "Authorization" : f"Bearer { token } " ,
3134 "Accept" : "application/vnd.github+json" ,
35+ "User-Agent" : "ons-github-app" ,
36+ "X-GitHub-Api-Version" : GITHUB_API_VERSION ,
3237 }
3338 response = requests .post (url , headers = headers , timeout = 10 )
39+ if not response .ok :
40+ logger .error (
41+ "GitHub installation token request failed status=%s body=%s" ,
42+ response .status_code ,
43+ response .text ,
44+ )
3445 response .raise_for_status ()
3546 data = response .json ()
3647 return {"token" : data ["token" ], "expires_at" : data ["expires_at" ]}
@@ -42,8 +53,18 @@ def post_pr_comment(*, installation_id: int, repo_full_name: str, pr_number: int
4253 token = get_installation_token (installation_id )["token" ]
4354 url = f"{ GITHUB_API } /repos/{ repo_full_name } /issues/{ pr_number } /comments"
4455 headers = {
45- "Authorization" : f"token { token } " ,
56+ "Authorization" : f"Bearer { token } " ,
4657 "Accept" : "application/vnd.github+json" ,
58+ "User-Agent" : "ons-github-app" ,
59+ "X-GitHub-Api-Version" : GITHUB_API_VERSION ,
4760 }
4861 response = requests .post (url , headers = headers , json = {"body" : body }, timeout = 10 )
62+ if not response .ok :
63+ logger .error (
64+ "GitHub post comment failed repo=%s pr=%s status=%s body=%s" ,
65+ repo_full_name ,
66+ pr_number ,
67+ response .status_code ,
68+ response .text ,
69+ )
4970 response .raise_for_status ()
0 commit comments