Skip to content
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
10 changes: 9 additions & 1 deletion ogr/abstract/git_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,12 @@ def change_token(self, new_token: str) -> None:
"""
raise NotImplementedError

def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
def get_file_content(
self,
path: str,
ref: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
) -> str:
"""
Get a content of the file in the repo.

Expand All @@ -627,6 +632,9 @@ def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
ref: Branch or commit.

Defaults to repo's default branch.
headers: Additional headers to be sent with the request.

Defaults to `None`, which means no headers.

Returns:
Contents of the file as string.
Expand Down
7 changes: 6 additions & 1 deletion ogr/services/forgejo/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,12 @@ def change_token(self, new_token: str) -> None:
"Not possible; requires recreation of the httpx client",
)

def get_file_content(self, path: str, ref: Optional[str] = None) -> str:
def get_file_content(
self,
path: str,
ref: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
) -> str:
try:
remote_file: types.ContentsResponse = self.partial_api(
self.api.repo_get_contents,
Expand Down
7 changes: 6 additions & 1 deletion ogr/services/github/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ def fork_create(self, namespace: Optional[str] = None) -> "GithubProject":
def change_token(self, new_token: str):
raise OperationNotSupported

def get_file_content(self, path: str, ref=None) -> str:
def get_file_content(
self,
path: str,
ref: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
) -> str:
ref = ref or self.default_branch
try:
return self.github_repo.get_contents(
Expand Down
7 changes: 6 additions & 1 deletion ogr/services/gitlab/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ def get_commits(self, ref: Optional[str] = None) -> list[str]:
for commit in self.gitlab_repo.commits.list(ref_name=ref, all=True)
]

def get_file_content(self, path, ref=None) -> str:
def get_file_content(
self,
path: str,
ref: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
) -> str:
ref = ref or self.default_branch
# GitLab cannot resolve './'
path = os.path.normpath(path)
Expand Down
12 changes: 11 additions & 1 deletion ogr/services/pagure/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def _call_project_api_raw(
method: Optional[str] = None,
params: Optional[dict] = None,
data: Optional[dict] = None,
header: Optional[dict] = None,
) -> RequestResponse:
"""
Call project API endpoint.
Expand All @@ -152,6 +153,7 @@ def _call_project_api_raw(
method: Method of the HTTP request, e.g. `"GET"`, `"POST"`, etc.
params: HTTP(S) query parameters in form of a dictionary.
data: Data to be sent in form of a dictionary.
header: HTTP request header, dict that will update default headers

Returns:
`RequestResponse` object containing response.
Expand All @@ -167,6 +169,7 @@ def _call_project_api_raw(
method=method,
params=params,
data=data,
header=header,
)

def _get_project_url(self, *args, add_fork_part=True, add_api_endpoint_part=True):
Expand Down Expand Up @@ -462,14 +465,21 @@ def add_user_or_group(
def change_token(self, new_token: str) -> None:
self.service.change_token(new_token)

def get_file_content(self, path: str, ref=None) -> str:
def get_file_content(
self,
path: str,
ref: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
) -> str:
ref = ref or self.default_branch
headers = {"Accept": "text/plain", **(headers or {})}
result = self._call_project_api_raw(
"raw",
ref,
"f",
path,
add_api_endpoint_part=False,
header=headers,
)

if not result or result.status_code == HTTPStatus.NOT_FOUND:
Expand Down
18 changes: 17 additions & 1 deletion ogr/services/pagure/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ def __init__(
self.session.mount("https://", adapter)

self.header = {"Authorization": "token " + self._token} if self._token else {}
# By default ogr deals with Pagure API -> json
self.header["Accept"] = "application/json"

if user_agent:
self.header |= {"User-Agent": user_agent}
else:
try:
from ogr import __version__ as ogr_version
except ImportError:
ogr_version = "dev"
self.header |= {"User-Agent": f"ogr/{ogr_version} (hello@packit.dev)"}

if kwargs:
logger.warning(f"Ignored keyword arguments: {kwargs}")
Expand Down Expand Up @@ -193,6 +201,7 @@ def call_api_raw(
url: str,
method: Optional[str] = None,
params: Optional[dict] = None,
header: Optional[dict] = None,
data=None,
):
"""
Expand All @@ -202,6 +211,7 @@ def call_api_raw(
url: URL to be called.
method: Method of the HTTP request, e.g. `"GET"`, `"POST"`, etc.
params: HTTP(S) query parameters in form of a dictionary.
header: HTTP request header, dict that will update default headers
data: Data to be sent in form of a dictionary.

Returns:
Expand All @@ -216,6 +226,7 @@ def call_api_raw(
url=url,
params=params,
data=data,
header=header,
)

except requests.exceptions.ConnectionError as er:
Expand Down Expand Up @@ -257,14 +268,19 @@ def get_raw_request(
ValueError, if JSON cannot be retrieved.
"""

headers = self.header | (header or {})

response = self.session.request(
method=method,
url=url,
params=params,
headers=header or self.header,
headers=headers,
data=data,
verify=not self.insecure,
)
logger.debug(
f"Ogr sent request with following headers: {headers | {'Authorization': '<redacted>'}}",
)
Comment thread
majamassarini marked this conversation as resolved.

json_output = None
try:
Expand Down
Loading