Skip to content

Commit 10fb34d

Browse files
committed
Option to auth with GITHUB_TOKEN env var, closes dogsheep#33
1 parent 4a0c4ef commit 10fb34d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Run this command and paste in your new token:
2525

2626
This will create a file called `auth.json` in your current directory containing the required value. To save the file at a different path or filename, use the `--auth=myauth.json` option.
2727

28+
As an alternative to using an `auth.json` file you can add your access token to an environment variable called `GITHUB_TOKEN`.
29+
2830
## Fetching issues for a repository
2931

3032
The `issues` command retrieves all of the issues belonging to a specified repository.

github_to_sqlite/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,7 @@ def load_token(auth):
335335
token = json.load(open(auth))["github_personal_token"]
336336
except (KeyError, FileNotFoundError):
337337
token = None
338+
if token is None:
339+
# Fallback to GITHUB_TOKEN environment variable
340+
token = os.environ.get("GITHUB_TOKEN") or None
338341
return token

tests/test_auth.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from click.testing import CliRunner
2+
from github_to_sqlite import cli
3+
import json
4+
import os
5+
import pytest
6+
7+
8+
@pytest.fixture
9+
def mocked_starred(requests_mock):
10+
requests_mock.get("https://api.github.com/user", json={"id": 1, "login": "test"})
11+
m = requests_mock.get("https://api.github.com/user/starred", json=[])
12+
return m
13+
14+
15+
def test_auth_command():
16+
runner = CliRunner()
17+
with runner.isolated_filesystem():
18+
assert [] == os.listdir(".")
19+
result = runner.invoke(cli.cli, ["auth"], input="zzz")
20+
assert 0 == result.exit_code
21+
assert ["auth.json"] == os.listdir(".")
22+
assert {"github_personal_token": "zzz"} == json.load(open("auth.json"))
23+
24+
25+
def test_auth_file(mocked_starred):
26+
runner = CliRunner()
27+
with runner.isolated_filesystem():
28+
open("auth.json", "w").write(json.dumps({"github_personal_token": "xxx"}))
29+
result = runner.invoke(
30+
cli.cli, ["starred", "starred.db"], catch_exceptions=False
31+
)
32+
assert 0 == result.exit_code
33+
assert mocked_starred.called
34+
assert "token xxx" == mocked_starred.last_request.headers["authorization"]
35+
36+
37+
def test_auth_environment_variable(mocked_starred, monkeypatch):
38+
monkeypatch.setenv("GITHUB_TOKEN", "xyz")
39+
runner = CliRunner()
40+
with runner.isolated_filesystem():
41+
result = runner.invoke(
42+
cli.cli, ["starred", "starred.db"], catch_exceptions=False
43+
)
44+
assert 0 == result.exit_code
45+
assert mocked_starred.called
46+
assert "token xyz" == mocked_starred.last_request.headers["authorization"]

0 commit comments

Comments
 (0)