-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring your own token for commits (#10)
- Loading branch information
1 parent
5b3ee12
commit 67a8424
Showing
4 changed files
with
36 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,19 @@ def __init__(self, repo_name, pr_number): | |
repo_name (str): Repository name. | ||
pr_number (int): Pull request number. | ||
""" | ||
self.github_obj = Github(os.getenv('GITHUB_TOKEN')) | ||
token_var_name = os.getenv('ALTTEXTER_GITHUB_TOKEN_OVERRIDE') | ||
if token_var_name: | ||
github_token = os.getenv(token_var_name) | ||
if github_token: | ||
logging.info(f"Using custom token provided by the environment variable: {token_var_name}") | ||
else: | ||
logging.error(f"The environment variable {token_var_name} does not exist or is not set. Falling back to GITHUB_TOKEN.") | ||
github_token = os.getenv('GITHUB_TOKEN') | ||
else: | ||
logging.debug("No custom token override provided; using GITHUB_TOKEN.") | ||
github_token = os.getenv('GITHUB_TOKEN') | ||
|
||
self.github_obj = Github(github_token) | ||
self.repo = self.github_obj.get_repo(repo_name) | ||
self.pr = self.repo.get_pull(pr_number) | ||
|
||
|
@@ -72,12 +84,25 @@ def commit_and_push(self, updated_files, commit_message): | |
file_paths_str = "[" + ", ".join(updated_files) + "]" | ||
logging.info(f"{file_paths_str} Initiating commit and push process") | ||
|
||
git_username = os.getenv('ALTTEXTER_GITHUB_USERNAME') or 'github-actions' | ||
git_email = os.getenv('ALTTEXTER_GITHUB_EMAIL') or '[email protected]' | ||
|
||
if os.getenv('ALTTEXTER_GITHUB_USERNAME'): | ||
logging.info(f"Using custom Git username: {git_username}") | ||
else: | ||
logging.info("Using default Git username: 'github-actions'") | ||
|
||
if os.getenv('ALTTEXTER_GITHUB_EMAIL'): | ||
logging.info(f"Using custom Git email: {git_email}") | ||
else: | ||
logging.info("Using default Git email: '[email protected]'") | ||
|
||
try: | ||
# Configure Git to allow operations in the current directory | ||
current_directory = os.getcwd() | ||
subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', current_directory], check=True) | ||
subprocess.run(['git', 'config', 'user.name', 'github-actions'], check=True) | ||
subprocess.run(['git', 'config', 'user.email', '[email protected]'], check=True) | ||
subprocess.run(['git', 'config', '--global', 'user.name', git_username], check=True) | ||
subprocess.run(['git', 'config', '--global', 'user.email', git_email], check=True) | ||
|
||
if subprocess.run(['git', 'status', '--porcelain'], capture_output=True, text=True).stdout: | ||
subprocess.run(['git', 'add'] + updated_files, check=True) | ||
|