def cancel_build(*, upload_api: UploadAPI) -> bool:
    """
    Cancel an application build

    https://help.veracode.com/reader/LMv_dtSHyb7iIxAQznC~9w/rERUQewXKGx2D_zaoi6wGw
    """
    try:
        endpoint = "deletebuild.do"
        params = {"app_id": upload_api.app_id}

        # If a sandbox_id is specified, add it to the params
        if isinstance(upload_api.sandbox_id, str):
            params["sandbox_id"] = upload_api.sandbox_id

        # https://help.veracode.com/reader/LMv_dtSHyb7iIxAQznC~9w/rERUQewXKGx2D_zaoi6wGw
        response = upload_api.http_get(endpoint=endpoint, params=params)

        if element_contains_error(parsed_xml=response):
            LOG.error("Veracode returned an error when attempting to call %s",
                      endpoint)
            return False

        if isinstance(upload_api.sandbox_id, str):
            LOG.info(
                "Successfully cancelled the build in sandbox id %s of application id %s",
                upload_api.sandbox_id,
                upload_api.app_id,
            )
            return True

        LOG.info("Successfully cancelled the build application id %s",
                 upload_api.app_id)
        return True
    except (
            HTTPError,
            ConnectionError,
            Timeout,
            TooManyRedirects,
            RequestException,
            RuntimeError,
    ):
        return False
Exemple #2
0
    def test_upload_api_http_get(self, mock_http_request):
        """
        Test the UploadAPI http_get method
        """
        upload_api = UploadAPI(app_id=constants.VALID_UPLOAD_API["app_id"])

        # Fail when attempting to call the http_get method with invalid
        # arguments
        self.assertRaises(KeyError,
                          upload_api.http_get,
                          endpoint="getappbuilds.do")

        # Succeed when calling the http_get method with valid arguments
        mock_http_request.return_value = (
            constants.VALID_UPLOAD_API_GETAPPINFO_RESPONSE_XML["Element"])
        self.assertIsInstance(upload_api.http_get(endpoint="getappinfo.do"),
                              InsecureElementTree.Element)

        # Fail when attempting to delete the http_get method, because the
        # deleter is intentionally missing
        self.assertRaises(AttributeError, delattr, upload_api, "http_get")
def build_exists(*, upload_api: UploadAPI) -> bool:
    """
    Return whether a build already exists

    https://help.veracode.com/reader/orRWez4I0tnZNaA_i0zn9g/Yjclv0XIfU1v_yqmkt18zA
    """

    try:
        endpoint = "getbuildlist.do"
        params = {"app_id": upload_api.app_id}

        # If a sandbox_id is specified, add it to the params
        if isinstance(upload_api.sandbox_id, str):
            params["sandbox_id"] = upload_api.sandbox_id

        response = upload_api.http_get(endpoint=endpoint, params=params)

        if element_contains_error(parsed_xml=response):
            LOG.error("Veracode returned an error when attempting to call %s",
                      endpoint)
            raise RuntimeError

        try:
            # If we don't throw, at least one build already exists
            response[0].get("build_id")
            return False

        except IndexError:
            # No existing builds
            return True

    except (
            HTTPError,
            ConnectionError,
            Timeout,
            TooManyRedirects,
            RequestException,
            RuntimeError,
    ) as e:
        raise RuntimeError from e