-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Build: pass api_client down to environment/builders/etc #10390
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
from requests.exceptions import ConnectionError, ReadTimeout | ||
from requests_toolbelt.multipart.encoder import MultipartEncoder | ||
|
||
from readthedocs.api.v2.client import api as api_v2 | ||
from readthedocs.builds.models import BuildCommandResultMixin | ||
from readthedocs.core.utils import slugify | ||
from readthedocs.projects.models import Feature | ||
|
@@ -227,7 +226,7 @@ def get_command(self): | |
return ' '.join(self.command) | ||
return self.command | ||
|
||
def save(self): | ||
def save(self, api_client): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume it's easier to pass it in here, than to put the client on the BuildCommand when it's created? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it touches less code, and the API client is scoped to where is needed (just this method), since commands can be optionally saved. |
||
"""Save this command and result via the API.""" | ||
# Force record this command as success to avoid Build reporting errors | ||
# on commands that are just for checking purposes and do not interferes | ||
|
@@ -251,7 +250,7 @@ def save(self): | |
encoder = MultipartEncoder( | ||
{key: str(value) for key, value in data.items()} | ||
) | ||
resource = api_v2.command | ||
resource = api_client.command | ||
resp = resource._store["session"].post( | ||
resource._store["base_url"] + "/", | ||
data=encoder, | ||
|
@@ -261,7 +260,7 @@ def save(self): | |
) | ||
log.debug('Post response via multipart form.', response=resp) | ||
else: | ||
resp = api_v2.command.post(data) | ||
resp = api_client.command.post(data) | ||
log.debug('Post response via JSON encoded data.', response=resp) | ||
|
||
|
||
|
@@ -416,9 +415,12 @@ class BaseBuildEnvironment: | |
:param build: Build instance | ||
:param environment: shell environment variables | ||
:param record: whether or not record a build commands in the databse via | ||
the API. The only case where we want this to be `False` is when | ||
instantiating this class from `sync_repository_task` because it's a | ||
background task that does not expose commands to the user. | ||
the API. The only case where we want this to be `False` is when | ||
instantiating this class from `sync_repository_task` because it's a | ||
background task that does not expose commands to the user. | ||
:param api_client: API v2 client instance (readthedocs.v2.client). | ||
This is used to record commands in the database, if `record=True` | ||
this argument is required. | ||
""" | ||
|
||
def __init__( | ||
|
@@ -429,6 +431,7 @@ def __init__( | |
config=None, | ||
environment=None, | ||
record=True, | ||
api_client=None, | ||
**kwargs, | ||
): | ||
self.project = project | ||
|
@@ -438,6 +441,10 @@ def __init__( | |
self.build = build | ||
self.config = config | ||
self.record = record | ||
self.api_client = api_client | ||
|
||
if self.record and not self.api_client: | ||
raise ValueError("api_client is required when record=True") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solid defensive programming 👍 |
||
|
||
# TODO: remove these methods, we are not using LocalEnvironment anymore. We | ||
# need to find a way for tests to not require this anymore | ||
|
@@ -449,7 +456,7 @@ def __exit__(self, exc_type, exc_value, tb): | |
|
||
def record_command(self, command): | ||
if self.record: | ||
command.save() | ||
command.save(self.api_client) | ||
|
||
def run(self, *cmd, **kwargs): | ||
"""Shortcut to run command from environment.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have DONT_HIT_API and DONT_HIT_DB settings, which seem like inverses of the same value 🙃
Does it make sense to try to remove the DONT_HIT_API & DONT_HIT_DB settings as a later refactor here, or are they still needed in other places in the codebase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think we should just always assume that builders hit our API, and the rest of our code the DB.
Looks like we can just remove
DONT_HIT_API
, I can try to removeDONT_HIT_DB
in another PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I think these variables where only useful when we didn't have Docker development setup.