diff --git a/apps/owidbot/cli.py b/apps/owidbot/cli.py index 3f597b8158f..3db6c9ec83b 100644 --- a/apps/owidbot/cli.py +++ b/apps/owidbot/cli.py @@ -9,7 +9,7 @@ from rich_click.rich_command import RichCommand from apps.owidbot import anomalist, chart_diff, data_diff, grapher -from etl.config import get_container_name +from etl.config import OWIDBOT_ACCESS_TOKEN, get_container_name from . import github_utils as gh_utils @@ -57,7 +57,7 @@ def cli( if repo_name not in get_args(REPOS): raise AssertionError("Invalid repo") - repo = gh_utils.get_repo(repo_name) + repo = gh_utils.get_repo(repo_name, access_token=OWIDBOT_ACCESS_TOKEN) pr = gh_utils.get_pr(repo, branch) if pr is None: log.warning(f"No open PR found for branch {branch}") diff --git a/apps/owidbot/github_utils.py b/apps/owidbot/github_utils.py index 945a362ba36..abd5f138ab1 100644 --- a/apps/owidbot/github_utils.py +++ b/apps/owidbot/github_utils.py @@ -16,16 +16,10 @@ def get_repo(repo_name: str, access_token: Optional[str] = None) -> github.Repository.Repository: - # No access token, try using OWIDBOT_ACCESS_TOKEN if not access_token: - if config.OWIDBOT_ACCESS_TOKEN: - access_token = config.OWIDBOT_ACCESS_TOKEN - auth = Auth.Token(access_token) - else: - # Don't auth, be aware that you won't be able to do write operations. You should - # set up your access token on https://github.com/settings/tokens and set it as - # OWIDBOT_ACCESS_TOKEN in .env - auth = None + # Don't auth, be aware that you won't be able to do write operations. You should + # set up your access token on https://github.com/settings/tokens. + auth = None else: auth = Auth.Token(access_token) @@ -105,10 +99,19 @@ def compute_git_blob_sha1(content: bytes) -> str: return sha1.hexdigest() +def _github_access_token(): + # Use GITHUB_TOKEN if set, otherwise use OWIDBOT_ACCESS_TOKEN + if config.GITHUB_TOKEN: + return config.GITHUB_TOKEN + elif config.OWIDBOT_ACCESS_TOKEN: + return config.OWIDBOT_ACCESS_TOKEN + else: + raise AssertionError("You need to set GITHUB_TOKEN or OWIDBOT_ACCESS_TOKEN in your .env file to commit.") + + def create_branch_if_not_exists(repo_name: str, branch: str, dry_run: bool) -> None: """Create a branch if it doesn't exist.""" - assert config.OWIDBOT_ACCESS_TOKEN, "You need to set OWIDBOT_ACCESS_TOKEN in your .env file to create a branch." - repo = get_repo(repo_name, config.OWIDBOT_ACCESS_TOKEN) + repo = get_repo(repo_name, access_token=_github_access_token()) try: repo.get_branch(branch) except github.GithubException as e: @@ -136,10 +139,8 @@ def commit_file_to_github( dry_run: bool = True, ) -> None: """Commit a table to a GitHub repository using the GitHub API.""" - assert config.OWIDBOT_ACCESS_TOKEN, "You need to set OWIDBOT_ACCESS_TOKEN in your .env file to commit." - # Get the repository object - repo = get_repo(repo_name, config.OWIDBOT_ACCESS_TOKEN) + repo = get_repo(repo_name, access_token=_github_access_token()) new_content_checksum = compute_git_blob_sha1(content.encode("utf-8")) try: