Changeset View
Changeset View
Standalone View
Standalone View
swh/web/save_code_now/origin_save.py
Show First 20 Lines • Show All 398 Lines • ▼ Show 20 Lines | ) -> SaveOriginRequestInfo: | ||||
return save_request.to_dict() | return save_request.to_dict() | ||||
def create_save_origin_request( | def create_save_origin_request( | ||||
visit_type: str, | visit_type: str, | ||||
origin_url: str, | origin_url: str, | ||||
privileged_user: bool = False, | privileged_user: bool = False, | ||||
user_id: Optional[int] = None, | user_id: Optional[int] = None, | ||||
from_webhook: bool = False, | |||||
webhook_origin: Optional[str] = None, | |||||
**kwargs, | **kwargs, | ||||
) -> SaveOriginRequestInfo: | ) -> SaveOriginRequestInfo: | ||||
"""Create a loading task to save a software origin into the archive. | """Create a loading task to save a software origin into the archive. | ||||
This function aims to create a software origin loading task through the use of the | This function aims to create a software origin loading task through the use of the | ||||
swh-scheduler component. | swh-scheduler component. | ||||
First, some checks are performed to see if the visit type and origin url are valid | First, some checks are performed to see if the visit type and origin url are valid | ||||
but also if the the save request can be accepted. For the 'archives' visit type, | but also if the the save request can be accepted. For the 'archives' visit type, | ||||
this also ensures the artifacts actually exists. If those checks passed, the loading | this also ensures the artifacts actually exists. If those checks passed, the loading | ||||
task is then created. Otherwise, the save request is put in pending or rejected | task is then created. Otherwise, the save request is put in pending or rejected | ||||
state. | state. | ||||
All the submitted save requests are logged into the swh-web database to keep track | All the submitted save requests are logged into the swh-web database to keep track | ||||
of them. | of them. | ||||
Args: | Args: | ||||
visit_type: the type of visit to perform (e.g. git, hg, svn, archives, ...) | visit_type: the type of visit to perform (e.g. git, hg, svn, archives, ...) | ||||
origin_url: the url of the origin to save | origin_url: the url of the origin to save | ||||
privileged: Whether the user has some more privilege than other (bypass | privileged: Whether the user has some more privilege than other (bypass | ||||
review, access to privileged other visit types) | review, access to privileged other visit types) | ||||
user_id: User identifier (provided when authenticated) | user_id: User identifier (provided when authenticated) | ||||
olasd: missing `webhook_origin` doc | |||||
from_webhook: Indicates if the save request is created from a webhook receiver | |||||
webhook_origin: Indicates which forge type sent the webhook | |||||
kwargs: Optional parameters (e.g. artifact_url, artifact_filename, | kwargs: Optional parameters (e.g. artifact_url, artifact_filename, | ||||
artifact_version) | artifact_version) | ||||
Raises: | Raises: | ||||
BadInputExc: the visit type or origin url is invalid or inexistent | BadInputExc: the visit type or origin url is invalid or inexistent | ||||
ForbiddenExc: the provided origin url is blacklisted | ForbiddenExc: the provided origin url is blacklisted | ||||
Returns: | Returns: | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | if save_request_status == SAVE_REQUEST_ACCEPTED: | ||||
sor.save() | sor.save() | ||||
else: | else: | ||||
sor = SaveOriginRequest.objects.create( | sor = SaveOriginRequest.objects.create( | ||||
visit_type=visit_type, | visit_type=visit_type, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
status=save_request_status, | status=save_request_status, | ||||
loading_task_id=task["id"], | loading_task_id=task["id"], | ||||
user_ids=f'"{user_id}"' if user_id else None, | user_ids=f'"{user_id}"' if user_id else None, | ||||
from_webhook=from_webhook, | |||||
webhook_origin=webhook_origin, | |||||
) | ) | ||||
# save request must be manually reviewed for acceptation | # save request must be manually reviewed for acceptation | ||||
elif save_request_status == SAVE_REQUEST_PENDING: | elif save_request_status == SAVE_REQUEST_PENDING: | ||||
# check if there is already such a save request already submitted, | # check if there is already such a save request already submitted, | ||||
# no need to add it to the database in that case | # no need to add it to the database in that case | ||||
try: | try: | ||||
sor = SaveOriginRequest.objects.get( | sor = SaveOriginRequest.objects.get( | ||||
visit_type=visit_type, origin_url=origin_url, status=save_request_status | visit_type=visit_type, origin_url=origin_url, status=save_request_status | ||||
) | ) | ||||
user_ids = sor.user_ids if sor.user_ids is not None else "" | user_ids = sor.user_ids if sor.user_ids is not None else "" | ||||
if user_id is not None and f'"{user_id}"' not in user_ids: | if user_id is not None and f'"{user_id}"' not in user_ids: | ||||
# update user ids list | # update user ids list | ||||
sor.user_ids = f'{sor.user_ids},"{user_id}"' | sor.user_ids = f'{sor.user_ids},"{user_id}"' | ||||
sor.save() | sor.save() | ||||
# if not add it to the database | # if not add it to the database | ||||
except ObjectDoesNotExist: | except ObjectDoesNotExist: | ||||
sor = SaveOriginRequest.objects.create( | sor = SaveOriginRequest.objects.create( | ||||
visit_type=visit_type, | visit_type=visit_type, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
status=save_request_status, | status=save_request_status, | ||||
user_ids=f'"{user_id}"' if user_id else None, | user_ids=f'"{user_id}"' if user_id else None, | ||||
from_webhook=from_webhook, | |||||
webhook_origin=webhook_origin, | |||||
) | ) | ||||
# origin can not be saved as its url is blacklisted, | # origin can not be saved as its url is blacklisted, | ||||
# log the request to the database anyway | # log the request to the database anyway | ||||
else: | else: | ||||
sor = SaveOriginRequest.objects.create( | sor = SaveOriginRequest.objects.create( | ||||
visit_type=visit_type, | visit_type=visit_type, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
status=save_request_status, | status=save_request_status, | ||||
user_ids=f'"{user_id}"' if user_id else None, | user_ids=f'"{user_id}"' if user_id else None, | ||||
from_webhook=from_webhook, | |||||
webhook_origin=webhook_origin, | |||||
) | ) | ||||
if save_request_status == SAVE_REQUEST_REJECTED: | if save_request_status == SAVE_REQUEST_REJECTED: | ||||
raise ForbiddenExc( | raise ForbiddenExc( | ||||
( | ( | ||||
'The "save code now" request has been rejected ' | 'The "save code now" request has been rejected ' | ||||
"because the provided origin url is blacklisted." | "because the provided origin url is blacklisted." | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 252 Lines • Show Last 20 Lines |
missing webhook_origin doc