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

Handle when jira is behind a proxy #1925

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
19 changes: 19 additions & 0 deletions jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import time
from typing import TYPE_CHECKING, Any, cast
from urllib.parse import ParseResult, urlparse, urlunparse

from requests import Response
from requests.structures import CaseInsensitiveDict
Expand Down Expand Up @@ -292,6 +293,22 @@ def _get_url(self, path: str) -> str:
options.update({"path": path})
return self._base_url.format(**options)

def _validate_self_self_url(self) -> None:
"""In the case of a proxy, use the configured option server URL."""
self_parsed = urlparse(self.self)
server_parsed = urlparse(self._options["server"])
if self_parsed.netloc != server_parsed.netloc:
self.self = urlunparse(
ParseResult(
scheme=server_parsed.scheme,
netloc=server_parsed.netloc,
path=self_parsed.path,
params=self_parsed.params,
query=self_parsed.query,
fragment=self_parsed.fragment,
)
)

def update(
self,
fields: dict[str, Any] | None = None,
Expand Down Expand Up @@ -326,6 +343,7 @@ def update(
else:
querystring = ""

self._validate_self_self_url()
r = self._session.put(self.self + querystring, data=json.dumps(data))
if "autofix" in self._options and r.status_code == 400:
user = None
Expand Down Expand Up @@ -420,6 +438,7 @@ def delete(self, params: dict[str, Any] | None = None) -> Response | None:
Returns:
Optional[Response]: Returns None if async
"""
self._validate_self_self_url()
if self._options["async"]:
# FIXME: mypy doesn't think this should work
if not hasattr(self._session, "_async_jobs"):
Expand Down
26 changes: 26 additions & 0 deletions tests/resources/test_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ def test_update_with_label(self):
issue.update(fields=fields)
self.assertEqual(issue.fields.labels, ["testLabel"])

def test_update_label_with_proxy(self):
issue = self.jira.create_issue(
summary="Test issue for updating labels",
project=self.project_b,
description="Label testing",
issuetype=self.test_manager.CI_JIRA_ISSUE,
)

labelarray = ["testLabel"]
fields = {"labels": labelarray}
issue.self = f"https://foo.bar/jira/rest/api/2/issue/{issue.id}"
skiedude marked this conversation as resolved.
Show resolved Hide resolved
issue.update(fields=fields)
self.assertEqual(issue.fields.labels, ["testLabel"])

def test_update_with_bad_label(self):
issue = self.jira.create_issue(
summary="Test issue for updating bad labels",
Expand Down Expand Up @@ -373,6 +387,18 @@ def test_delete(self):
issue.delete()
self.assertRaises(JIRAError, self.jira.issue, key)

def test_delete_with_proxy(self):
issue = self.jira.create_issue(
summary="Test issue created",
project=self.project_b,
description="Not long for this world",
issuetype=self.test_manager.CI_JIRA_ISSUE,
)
key = issue.key
issue.self = f"https://foo.bar/jira/rest/api/2/issue/{issue.id}"
issue.delete()
self.assertRaises(JIRAError, self.jira.issue, key)

def test_createmeta(self):
meta = self.jira.createmeta()
proj = find_by_key(meta["projects"], self.project_b)
Expand Down
Loading