Skip to content

Commit 0280baf

Browse files
authored
Merge pull request #2055 from timbrel/env-vars
2 parents 6b2382f + 44dae32 commit 0280baf

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

GitSavvy.sublime-settings

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@
4242
"users_home": "~",
4343

4444
/*
45-
Enter your github API key in the field below. To interact with
46-
a GitHub Enterprise instance, add a field with the FQDN as the key
47-
and the API key as the value.
45+
Enter your GitHub/GitLab API key(s) in the field below.
46+
To interact with enterprise/self-hosted instances, add a field with
47+
the FQDN as the key and the API key as the value.
48+
49+
Alternatively, you can set environment variables as fallback:
50+
- GITHUB_TOKEN (for GitHub)
51+
- GITLAB_TOKEN (for GitLab)
52+
53+
Per-host keys in `api_tokens` take precedence over env vars.
4854
*/
4955
"api_tokens": {
5056
// "github.com": "ADD YOUR GITHUB TOKEN HERE"

docs/github.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ When interacting with a public repository, no configuration is required. Howeve
1111
2. After submitting, copy the generated API key.
1212
3. In the Sublime Menu, open `Preferences > Package Settings > GitSavvy > Settings - User`.
1313
4. Add your key to the `api_tokens` object (you can find an example in `Preferences > Package Settings > GitSavvy > Settings`).
14+
5. Alternatively, set the `GITHUB_TOKEN` environment variable. GitSavvy uses it as a fallback when no token is configured for the remote in `api_tokens`.
1415

1516

1617
## Choosing a remote

docs/gitlab.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ When interacting with a public repository, no configuration is required. Howeve
1111
2. After submitting, copy the generated API key.
1212
3. In the Sublime Menu, open `Preferences > Package Settings > GitSavvy > Settings - User`.
1313
4. Add your key to the `api_tokens` object (you can find an example in `Preferences > Package Settings > GitSavvy > Settings`).
14+
5. Alternatively, set the `GITLAB_TOKEN` environment variable. GitSavvy uses it as a fallback when no token is configured for the remote in `api_tokens`.
1415

1516

1617
## Choosing a remote

github/commands/create_repo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ def on_done(name: str) -> None:
3232

3333
def get_github_user_token(cmd: GsCommand, args: Args, done: Kont) -> None:
3434
fqdn = "github.com"
35-
token = cmd.savvy_settings.get("api_tokens", {}).get(fqdn)
35+
token = cmd.savvy_settings.get("api_tokens", {}).get(fqdn) or os.environ.get("GITHUB_TOKEN")
3636
if not token:
37-
cmd.window.status_message(f"Abort, no API token found in the settings for {fqdn}.")
37+
cmd.window.status_message(
38+
f"Abort, no API token found in settings for {fqdn} or env var GITHUB_TOKEN."
39+
)
3840
return
3941
done(token)
4042

github/github.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from __future__ import annotations
6+
import os
67
import re
78
from webbrowser import open as open_in_browser
89
from functools import partial
@@ -29,7 +30,7 @@ class GitHubRepo(NamedTuple):
2930
fqdn: str
3031
owner: str
3132
repo: str
32-
token: str
33+
token: str | None
3334

3435

3536
def remote_to_url(remote_url: str) -> str:
@@ -72,7 +73,7 @@ def parse_remote(remote_url: str) -> GitHubRepo:
7273
raise ValueError("Invalid github url: {}".format(url))
7374

7475
fqdn, owner, repo = match.groups()
75-
token = GitSavvySettings().get("api_tokens", {}).get(fqdn)
76+
token = GitSavvySettings().get("api_tokens", {}).get(fqdn) or os.environ.get("GITHUB_TOKEN")
7677
return GitHubRepo(url, fqdn, owner, repo, token)
7778

7879

gitlab/gitlab.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
GitLab methods that are functionally separate from anything Sublime-related.
33
"""
44

5+
import os
56
import re
67
from collections import namedtuple
78
from functools import partial, lru_cache
@@ -72,7 +73,7 @@ def parse_remote(remote):
7273
return None
7374

7475
fqdn, owner, repo = match.groups()
75-
token = GitSavvySettings().get("api_tokens", {}).get(fqdn)
76+
token = GitSavvySettings().get("api_tokens", {}).get(fqdn) or os.environ.get("GITLAB_TOKEN")
7677
return GitLabRepo(url, fqdn, owner, repo, token)
7778

7879

0 commit comments

Comments
 (0)