Skip to content

Commit

Permalink
Use ghtoken to fetch GitHub token from more sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Nov 9, 2023
1 parent d97be65 commit ed7be3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,11 @@ alternative to setting them directly in the environment.
GitHub
~~~~~~

In order to retrieve assets from GitHub, a GitHub OAuth token must be specified
either via the ``GITHUB_TOKEN`` environment variable or as the value of the
``hub.oauthtoken`` Git config option.
In order to retrieve assets from GitHub, a GitHub access token with appropriate
permissions must be provided. Specify the token via the ``GH_TOKEN`` or
``GITHUB_TOKEN`` environment variable, by storing a token with the ``gh`` or
``hub`` command, or by setting the ``hub.oauthtoken`` Git config option in your
``~/.gitconfig`` file.

Travis
~~~~~~
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ install_requires =
click >= 7.0
click-loglevel ~= 0.2
in_place ~= 0.4
ghtoken ~= 0.1
pydantic ~= 1.7
python-dateutil ~= 2.7
python-dotenv >= 0.11, < 2.0
Expand Down
24 changes: 9 additions & 15 deletions src/tinuous/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from datetime import datetime, timezone
import email.utils
import logging
import os
from pathlib import Path
import re
from string import Formatter
import subprocess
from time import time
from typing import Any, Optional

from ghtoken import GHTokenNotFound, get_ghtoken

log = logging.getLogger("tinuous")


Expand Down Expand Up @@ -128,20 +128,14 @@ def parse_slice(s: str) -> slice:


def get_github_token() -> str:
token = os.environ.get("GITHUB_TOKEN")
if not token:
r = subprocess.run(
["git", "config", "hub.oauthtoken"],
stdout=subprocess.PIPE,
universal_newlines=True,
try:
# main() already loads the user's dotenv file, so don't load it again
return get_ghtoken(dotenv=False)
except GHTokenNotFound:
raise RuntimeError(
"GitHub token not found. Set via GH_TOKEN, GITHUB_TOKEN, gh, hub,"
" or hub.oauthtoken."
)
if r.returncode != 0 or not r.stdout.strip():
raise RuntimeError(
"GitHub OAuth token not set. Set via GITHUB_TOKEN"
" environment variable or hub.oauthtoken Git config option."
)
token = r.stdout.strip()
return token


def sanitize_pathname(s: str) -> str:
Expand Down

0 comments on commit ed7be3a

Please sign in to comment.