Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ghtoken to fetch GitHub token from more sources #184

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/tinuous/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def download(self, path: Path) -> list[Path]:
try:
self.client.download_zipfile(self.logs_url, path)
except requests.HTTPError as e:
if e.response.status_code in (404, 410):
if e.response is not None and e.response.status_code in (404, 410):
# 404 can happen when a workflow failed to run due to, say, a
# syntax error. 410 happens when the logs have expired.
log.error(
Expand Down
2 changes: 1 addition & 1 deletion src/tinuous/travis.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_github_commit(self, commit_sha: str) -> Optional[Commit]:
try:
r = self.ghclient.get(f"/repos/{self.repo}/commits/{commit_sha}")
except requests.HTTPError as e:
if e.response.status_code == 404:
if e.response is not None and e.response.status_code == 404:
return None
else:
raise e
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