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

feat: add netrc support #252

Merged
merged 3 commits into from
Mar 8, 2024
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
12 changes: 9 additions & 3 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ branch
path
Only commits containing this file path will be returned.

host
Hostname for self-hosted GitHub instance.

use_latest_release
Set this to ``true`` to check for the latest release on GitHub.

Expand Down Expand Up @@ -415,8 +418,9 @@ An authorization token may be needed in order to use ``use_latest_tag``,

To set an authorization token, you can set:

- a key named ``github`` in the keyfile
- the token option
- an entry in the keyfile for the host (e.g. ``github.com``)
- an entry in your ``netrc`` file for the host

This source supports :ref:`list options` when ``use_max_tag`` is set.

Expand Down Expand Up @@ -447,8 +451,9 @@ token

To set an authorization token, you can set:

- a key named ``gitea_{host}`` in the keyfile, where ``host`` is all-lowercased host name
- the token option
- an entry in the keyfile for the host (e.g. ``gitea.com``)
- an entry in your ``netrc`` file for the host

This source supports :ref:`list options` when ``use_max_tag`` is set.

Expand Down Expand Up @@ -520,8 +525,9 @@ token

To set an authorization token, you can set:

- a key named ``gitlab_{host}`` in the keyfile, where ``host`` is all-lowercased host name
- the token option
- an entry in the keyfile for the host (e.g. ``gitlab.com``)
- an entry in your ``netrc`` file for the host

This source supports :ref:`list options` when ``use_max_tag`` is set.

Expand Down
14 changes: 12 additions & 2 deletions nvchecker/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pathlib import Path
import contextvars
import abc
import netrc
from dataclasses import dataclass

if TYPE_CHECKING:
Expand Down Expand Up @@ -90,10 +91,19 @@ def __init__(
else:
keys = {}
self.keys = keys
try:
netrc_file = netrc.netrc()
netrc_hosts = netrc_file.hosts
except (FileNotFoundError, netrc.NetrcParseError) as e:
netrc_hosts = {}
self.netrc = netrc_hosts

def get_key(self, name: str) -> Optional[str]:
def get_key(self, name: str, legacy_name: Optional[str] = None) -> Optional[str]:
'''Get the named key (token) in the keyfile.'''
return self.keys.get(name)
keyfile_token = self.keys.get(name) or self.keys.get(legacy_name)
netrc_entry: Optional[Tuple[str, str, str]]
netrc_entry = self.netrc.get(name)
return keyfile_token or (netrc_entry and netrc_entry[2])

class EntryWaiter:
def __init__(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions nvchecker_source/gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ async def get_version(
token = conf.get('token')
# Load token from keyman
if token is None:
key_name = 'gitea_' + host.lower()
token = keymanager.get_key(key_name)
token = keymanager.get_key(host.lower(), 'gitea_' + host.lower())

# Set private token if token exists.
headers = {}
Expand Down
33 changes: 17 additions & 16 deletions nvchecker_source/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
ALLOW_REQUEST = None
RATE_LIMITED_ERROR = False

GITHUB_URL = 'https://api.github.com/repos/%s/commits'
GITHUB_LATEST_RELEASE = 'https://api.github.com/repos/%s/releases/latest'
GITHUB_URL = 'https://api.%s/repos/%s/commits'
GITHUB_LATEST_RELEASE = 'https://api.%s/repos/%s/releases/latest'
# https://developer.github.com/v3/git/refs/#get-all-references
GITHUB_MAX_TAG = 'https://api.github.com/repos/%s/git/refs/tags'
GITHUB_GRAPHQL_URL = 'https://api.github.com/graphql'
GITHUB_MAX_TAG = 'https://api.%s/repos/%s/git/refs/tags'
GITHUB_GRAPHQL_URL = 'https://api.%s/graphql'

async def get_version(name, conf, **kwargs):
global RATE_LIMITED_ERROR, ALLOW_REQUEST
Expand Down Expand Up @@ -78,8 +78,8 @@ async def get_version(name, conf, **kwargs):
}}
'''

async def get_latest_tag(key: Tuple[str, str, str]) -> RichResult:
repo, query, token = key
async def get_latest_tag(key: Tuple[str, str, str, str]) -> RichResult:
host, repo, query, token = key
owner, reponame = repo.split('/')
headers = {
'Authorization': f'bearer {token}',
Expand All @@ -92,7 +92,7 @@ async def get_latest_tag(key: Tuple[str, str, str]) -> RichResult:
)

res = await session.post(
GITHUB_GRAPHQL_URL,
GITHUB_GRAPHQL_URL % host,
headers = headers,
json = {'query': q},
)
Expand All @@ -108,8 +108,8 @@ async def get_latest_tag(key: Tuple[str, str, str]) -> RichResult:
url = f'https://github.com/{repo}/releases/tag/{version}',
)

async def get_latest_release_with_prereleases(key: Tuple[str, str]) -> RichResult:
repo, token = key
async def get_latest_release_with_prereleases(key: Tuple[str, str, str]) -> RichResult:
host, repo, token = key
owner, reponame = repo.split('/')
headers = {
'Authorization': f'bearer {token}',
Expand All @@ -121,7 +121,7 @@ async def get_latest_release_with_prereleases(key: Tuple[str, str]) -> RichResul
)

res = await session.post(
GITHUB_GRAPHQL_URL,
GITHUB_GRAPHQL_URL % host,
headers = headers,
json = {'query': q},
)
Expand All @@ -142,38 +142,39 @@ async def get_version_real(
**kwargs,
) -> VersionResult:
repo = conf['github']
host = conf.get('host', "github.com")

# Load token from config
token = conf.get('token')
# Load token from keyman
if token is None:
token = keymanager.get_key('github')
token = keymanager.get_key(host.lower(), 'github')

use_latest_tag = conf.get('use_latest_tag', False)
if use_latest_tag:
if not token:
raise GetVersionError('token not given but it is required')

query = conf.get('query', '')
return await cache.get((repo, query, token), get_latest_tag) # type: ignore
return await cache.get((host, repo, query, token), get_latest_tag) # type: ignore

use_latest_release = conf.get('use_latest_release', False)
include_prereleases = conf.get('include_prereleases', False)
if use_latest_release and include_prereleases:
if not token:
raise GetVersionError('token not given but it is required')

return await cache.get((repo, token), get_latest_release_with_prereleases) # type: ignore
return await cache.get((host, repo, token), get_latest_release_with_prereleases) # type: ignore

br = conf.get('branch')
path = conf.get('path')
use_max_tag = conf.get('use_max_tag', False)
if use_latest_release:
url = GITHUB_LATEST_RELEASE % repo
url = GITHUB_LATEST_RELEASE % (host, repo)
elif use_max_tag:
url = GITHUB_MAX_TAG % repo
url = GITHUB_MAX_TAG % (host, repo)
else:
url = GITHUB_URL % repo
url = GITHUB_URL % (host, repo)
parameters = {}
if br:
parameters['sha'] = br
Expand Down
3 changes: 1 addition & 2 deletions nvchecker_source/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ async def get_version_real(
token = conf.get('token')
# Load token from keyman
if token is None:
key_name = 'gitlab_' + host.lower()
token = keymanager.get_key(key_name)
token = keymanager.get_key(host.lower(), 'gitlab_' + host.lower())

# Set private token if token exists.
headers = {}
Expand Down
Loading