From 8b4295eff175df94c06065f6947d2afa9c12c55a Mon Sep 17 00:00:00 2001 From: Dhi Aurrahman Date: Tue, 1 Nov 2022 05:17:16 +0700 Subject: [PATCH 1/2] tools, github: Use authorization prefix based on provided token Signed-off-by: Dhi Aurrahman --- tools/github/write_current_source_version.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/github/write_current_source_version.py b/tools/github/write_current_source_version.py index 9daccac707306..f7839c557299a 100644 --- a/tools/github/write_current_source_version.py +++ b/tools/github/write_current_source_version.py @@ -68,10 +68,12 @@ # Fetch the current version commit information from GitHub. commit_info_request = urllib.request.Request( "https://api.github.com/repos/envoyproxy/envoy/commits/v" + current_version) - github_token = os.environ.get(args.github_api_token_env_name) - if github_token != None: + if github_token := os.environ.get(args.github_api_token_env_name): + # Reference: https://github.com/octokit/auth-token.js/blob/902a172693d08de998250bf4d8acb1fdb22377a4/src/with-authorization-prefix.ts#L6-L12. + authorization_header_prefix = "bearer" if len(github_token.split(".")) is 3 else "token" # To avoid rate-limited API calls. - commit_info_request.add_header("Authorization", f"Bearer {github_token}") + commit_info_request.add_header( + "Authorization", f"{authorization_header_prefix} {github_token}") with urllib.request.urlopen(commit_info_request) as response: commit_info = json.loads(response.read()) source_version_file = project_root_dir.joinpath("SOURCE_VERSION") From 328595fc875b4a8d06d4a7564ccfd1d9808b3e4d Mon Sep 17 00:00:00 2001 From: Dhi Aurrahman Date: Tue, 1 Nov 2022 17:07:35 +0700 Subject: [PATCH 2/2] Update tools/github/write_current_source_version.py Co-authored-by: phlax Signed-off-by: Dhi Aurrahman --- tools/github/write_current_source_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/github/write_current_source_version.py b/tools/github/write_current_source_version.py index f7839c557299a..684245b98c6f9 100644 --- a/tools/github/write_current_source_version.py +++ b/tools/github/write_current_source_version.py @@ -70,7 +70,7 @@ "https://api.github.com/repos/envoyproxy/envoy/commits/v" + current_version) if github_token := os.environ.get(args.github_api_token_env_name): # Reference: https://github.com/octokit/auth-token.js/blob/902a172693d08de998250bf4d8acb1fdb22377a4/src/with-authorization-prefix.ts#L6-L12. - authorization_header_prefix = "bearer" if len(github_token.split(".")) is 3 else "token" + authorization_header_prefix = "bearer" if len(github_token.split(".")) == 3 else "token" # To avoid rate-limited API calls. commit_info_request.add_header( "Authorization", f"{authorization_header_prefix} {github_token}")