Skip to content

Commit

Permalink
Add PEM string formatting fix to enhance private key handling
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaele-oplabs committed Dec 12, 2024
1 parent bea9429 commit 47bafe9
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion github_utility/github_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typer
import re # For regular expressions
from github import Github, GithubIntegration, GithubException
from typing import Optional # Import Optional for type hinting
from pathlib import Path # For file operations
Expand All @@ -12,7 +13,24 @@
get_pr_base_sha,
create_issue_from_string,
)
app = typer.Typer(help="CLI tool for GitHub operations.")
app = typer.Typer(help="CLI tool for GitHub operations.",
pretty_exceptions_show_locals=False)


def fix_pem_string(content: str) -> str:
"""
Fix the formatting of a PEM string by replacing spaces in the body with newlines.
"""
content = re.sub(r"(-----BEGIN [A-Z ]+-----)\s+",
r"\1\n", content) # Fix BEGIN line
content = re.sub(r"\s+(-----END [A-Z ]+-----)",
r"\n\1", content) # Fix END line

# Replace spaces in the body with newlines
fixed_content = re.sub(r"(?<=-----\n)(.+?)(?=\n-----)",
lambda match: match.group(0).replace(" ", "\n"),
content, flags=re.S)
return fixed_content


def get_github_client(
Expand Down Expand Up @@ -65,6 +83,7 @@ def get_github_acces_token(
raise ValueError(
"A valid private key must be provided as a file or string.")

private_key = fix_pem_string(private_key)
# Authenticate using GitHub App
integration = GithubIntegration(app_id, private_key)

Expand Down

0 comments on commit 47bafe9

Please sign in to comment.