Skip to content

Commit

Permalink
added getting authentication token from defined env vars from github …
Browse files Browse the repository at this point in the history
…for authenticating GitHub CLI from workflow systems instead of users shell
  • Loading branch information
Terry McGuinness committed Feb 27, 2025
1 parent 02486bc commit 45fb69b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions ci/scripts/utils/githubpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import re

from github import Github, GithubException, InputFileContent, UnknownObjectException
from wxflow import which
from wxflow import which, Logger

# Initialize logger with environment variable for logging level
logger = Logger(level=os.environ.get("LOGGING_LEVEL", "DEBUG"), colored_log=False)


class GitHubDBError(Exception):
Expand Down Expand Up @@ -54,14 +57,22 @@ def __init__(self, repo_url=None, TOKEN=None):
environment variable when repo_url is not provided.
"""
if TOKEN is None:
gh_cli = which('gh')
gh_cli.add_default_arg(['auth', 'status', '--show-token'])
gh_output = gh_cli(output=str, error=str)
token_match = re.search(r"Token:\s*([a-zA-Z0-9_]+)", gh_output)
if token_match:
TOKEN = token_match.group(1)
TOKEN = os.environ.get('GH_TOKEN') or os.environ.get('GITHUB_TOKEN')
if TOKEN:
logger.info("Using TOKEN from environment variable.")
else:
raise ValueError("Token not found in gh CLI output.")
gh_cli = which('gh')
gh_cli.add_default_arg(['auth', 'status', '--show-token'])
gh_output = gh_cli(output=str, error=str)
token_match = re.search(r"Token:\s*([a-zA-Z0-9_]+)", gh_output)
if token_match:
TOKEN = token_match.group(1)
logger.info("Using TOKEN from gh CLI tool.")
else:
raise ValueError("Token not found in gh CLI output.")
else:
logger.info("Using provided TOKEN.")

super().__init__(TOKEN)

self.repo = self.get_repo_url(repo_url)
Expand Down

0 comments on commit 45fb69b

Please sign in to comment.