Skip to content

Commit

Permalink
Send own User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Nov 10, 2023
1 parent 8349a4c commit 1db47d8
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 11 deletions.
4 changes: 1 addition & 3 deletions bugme.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
from typing import Any

from scantags import scan_tags
from services import get_urltag, Issue
from services import get_urltag, Issue, VERSION
from services.guess import guess_service
from utils import dateit, html_tag


VERSION = "2.4.0"

DEFAULT_CREDENTIALS_FILE = os.path.expanduser("~/creds.json")


Expand Down
3 changes: 2 additions & 1 deletion gitblame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from requests import RequestException

from services import debugme
from services import debugme, VERSION
from utils import utc_date


Expand Down Expand Up @@ -54,6 +54,7 @@ def __init__(self, repo: str, branch: str, access_token: str) -> None:
self.api_url = "https://api.github.com/graphql"
self.session = requests.Session()
self.session.headers["Authorization"] = f"Bearer {access_token}"
self.session.headers["User-Agent"] = f"bugme/{VERSION}"
self.timeout = 30
if os.getenv("DEBUG"):
self.session.hooks["response"].append(debugme)
Expand Down
3 changes: 3 additions & 0 deletions services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from requests_toolbelt.utils import dump # type: ignore


VERSION = "2.4.1"

TAG_REGEX = "|".join(
[
r"(?:bnc|bsc|boo|poo|lp)#[0-9]+",
Expand Down Expand Up @@ -230,6 +232,7 @@ def __init__(self, url: str, token: str | None):
if token is not None:
self.session.headers["Authorization"] = f"token {token}"
self.session.headers["Accept"] = "application/json"
self.session.headers["User-Agent"] = f"bugme/{VERSION}"
if os.getenv("DEBUG"):
self.session.hooks["response"].append(debugme)
self.timeout = 10
Expand Down
3 changes: 2 additions & 1 deletion services/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from requests.exceptions import RequestException

from utils import utc_date
from . import Service, Issue, debugme, status
from . import Service, Issue, debugme, status, VERSION


# Reference: https://bugzilla.readthedocs.io/en/latest/api/index.html#apis
Expand All @@ -31,6 +31,7 @@ def __init__(self, url: str, creds: dict):
self.client = Bugzilla(self.url, **options)
except (BugzillaError, RequestException) as exc:
logging.error("Bugzilla: %s: %s", self.url, exc)
self.client._session._session.headers["User-Agent"] = f"bugme/{VERSION}"
if os.getenv("DEBUG"):
self.client._session._session.hooks["response"].append(debugme)

Expand Down
4 changes: 2 additions & 2 deletions services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from requests.exceptions import RequestException

from utils import utc_date
from . import Service, Issue, status
from . import Service, Issue, status, VERSION


# Reference:
Expand All @@ -26,7 +26,7 @@ def __init__(self, url: str, creds: dict):
# auth = Auth.Token(**creds)
# self.client = Github(auth=auth)
self.tag = "gh"
self.client = Github(**creds)
self.client = Github(user_agent=f"bugme/{VERSION}", **creds)
if os.getenv("DEBUG"):
logging.getLogger("github").setLevel(logging.DEBUG)

Expand Down
3 changes: 2 additions & 1 deletion services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from requests.exceptions import RequestException

from utils import utc_date
from . import Service, Issue, debugme, status
from . import Service, Issue, debugme, status, VERSION


# References:
Expand All @@ -27,6 +27,7 @@ def __init__(self, url: str, creds: dict):
super().__init__(url)
options: dict[str, Any] = {
"ssl_verify": os.environ.get("REQUESTS_CA_BUNDLE", True),
"user_agent": f"bugme/{VERSION}",
}
options |= creds
hostname = str(urlparse(self.url).hostname)
Expand Down
3 changes: 2 additions & 1 deletion services/guess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests
from requests.exceptions import RequestException

from . import debugme
from . import debugme, VERSION
from .bugzilla import MyBugzilla
from .gitea import MyGitea
from .github import MyGithub
Expand Down Expand Up @@ -72,6 +72,7 @@ def guess_service2(server: str) -> Any | None:

with requests.Session() as session:
session.headers["Accept"] = "application/json"
session.headers["User-Agent"] = f"bugme/{VERSION}"
session.verify = os.environ.get("REQUESTS_CA_BUNDLE", True)
if os.getenv("DEBUG"):
session.hooks["response"].append(debugme)
Expand Down
3 changes: 2 additions & 1 deletion services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from requests.exceptions import RequestException

from utils import utc_date
from . import Service, Issue, debugme, status
from . import Service, Issue, debugme, status, VERSION


# References:
Expand All @@ -24,6 +24,7 @@ class MyJira(Service):
def __init__(self, url: str, creds: dict):
super().__init__(url)
self.client = Jira(url=self.url, **creds)
self.client.session.headers["User-Agent"] = f"bugme/{VERSION}"
if os.getenv("DEBUG"):
self.client.session.hooks["response"].append(debugme)

Expand Down
3 changes: 2 additions & 1 deletion services/redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from requests.exceptions import RequestException

from utils import utc_date
from . import Service, Issue, debugme, status
from . import Service, Issue, debugme, status, VERSION


# Reference: https://www.redmine.org/projects/redmine/wiki/Rest_api
Expand All @@ -27,6 +27,7 @@ def __init__(self, url: str, creds: dict):
}
options |= creds
self.client = Redmine(url=self.url, **options)
self.client.engine.session.headers["User-Agent"] = f"bugme/{VERSION}"
if os.getenv("DEBUG"):
self.client.engine.session.hooks["response"].append(debugme)

Expand Down

0 comments on commit 1db47d8

Please sign in to comment.