Skip to content

Commit

Permalink
Bring your own token for commits (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanalgar authored Feb 20, 2024
1 parent 5b3ee12 commit 67a8424
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.11-slim as base
FROM python:3.11-slim AS base

RUN apt-get update && apt-get install -y git
RUN apt-get update && apt-get install -y git && apt clean

COPY requirements.txt /
RUN pip install --no-cache-dir -r requirements.txt
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ jobs:
run: python /app/alttexter-ghclient.py
```
You'll need to set the following repo secrets:
You'll need to [add the following repo secrets](https://docs.github.com/en/codespaces/managing-codespaces-for-your-organization/managing-development-environment-secrets-for-your-repository-or-organization#adding-secrets-for-a-repository):
* `ALTTEXTER_ENDPOINT`: Endpoint URL of the running `alttexter` (eg. `https://alttexter-prod.westeurope.cloudapp.azure.com:9100/alttexter`)
* `ALTTEXTER_TOKEN`: Single token for service.
* `ALTTEXTER_RATEMINUTE`: Maximum number of calls to `alttexter` service per minute. Consider [OpenAI rate limits](https://platform.openai.com/docs/guides/rate-limits) for your tier. I'd reccomend no more than 3.

Optionally you can [add repo environment variables](https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository) `ALTTEXTER_GITHUB_TOKEN_OVERRIDE` (text of a repo secret name, for example `CR_TOKEN`—if using pass the secret in `alttexter.yml`), `ALTTEXTER_GITHUB_USERNAME` & `ALTTEXTER_GITHUB_EMAIL` to override the default GitHub token, username and email used for commits.

Once that's done you can open a PR with files that need alt text and/or title attributes.

## Features
Expand Down
6 changes: 3 additions & 3 deletions alttexter-ghclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async def get_image_metadata(self, session, markdown_content, file_path, encoded

return response_structure

def update_image_metadata(self, markdown_content, file_path, image_metadata, base_dir, repo_root, is_ipynb):
def update_image_metadata(self, markdown_content, image_metadata, base_dir, repo_root, is_ipynb):
"""
Updates markdown content with new alt-text and title attributes.
Expand All @@ -234,7 +234,7 @@ def update_image_metadata(self, markdown_content, file_path, image_metadata, bas

def replacement(match):
groups = match.groups()
image_alt, image_path = groups[:2]
_, image_path = groups[:2]
title_double, title_single = groups[2:4] if len(groups) == 4 else ("", "")
image_title = title_double or title_single or ""

Expand Down Expand Up @@ -311,7 +311,7 @@ async def process_file(session, file, alttexter_endpoint, github_handler, metada

image_metadata = response["data"].get('images', [])
updated_content, images_not_updated = metadata_updater.update_image_metadata(
markdown_content, file_path, image_metadata, base_dir, repo_root, is_ipynb
markdown_content, image_metadata, base_dir, repo_root, is_ipynb
)

if updated_content != markdown_content:
Expand Down
31 changes: 28 additions & 3 deletions ghutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 67a8424

Please sign in to comment.