Exemplo n.º 1
0
    def test_as_hash(self):
        builder = BasePackageZipBuilder()
        builder.zf.writestr("1", "1")
        hash1 = builder.as_hash()

        builder = BasePackageZipBuilder()
        builder.zf.writestr("1", "1")
        hash2 = builder.as_hash()

        assert hash2 == hash1

        builder.zf.writestr("2", "2")
        hash3 = builder.as_hash()
        assert hash3 != hash2
Exemplo n.º 2
0
    def test_create_version_request__existing_package_version(self, task):
        responses.add(
            "GET",
            f"{self.devhub_base_url}/tooling/query/",
            json={
                "size": 1,
                "records": [{
                    "Id": "08c000000000001AAA"
                }]
            },
        )

        builder = BasePackageZipBuilder()
        result = task._create_version_request("0Ho6g000000fy4ZCAQ",
                                              task.package_config, builder)
        assert result == "08c000000000001AAA"
Exemplo n.º 3
0
    def _create_version_request(
        self,
        package_id: str,
        package_config: PackageConfig,
        package_zip_builder: BasePackageZipBuilder,
        skip_validation: bool = False,
        dependencies: list = None,
    ):
        # Prepare the VersionInfo file
        version_bytes = io.BytesIO()
        version_info = zipfile.ZipFile(version_bytes, "w",
                                       zipfile.ZIP_DEFLATED)
        try:

            # Add the package.zip
            package_hash = package_zip_builder.as_hash()
            version_info.writestr("package.zip",
                                  package_zip_builder.as_bytes())

            if not self.options["force_upload"]:
                # Check for an existing package with the same contents
                res = self.tooling.query(
                    "SELECT Id "
                    "FROM Package2VersionCreateRequest "
                    f"WHERE Package2Id = '{package_id}' "
                    "AND Status != 'Error' "
                    f"AND SkipValidation = {str(skip_validation)} "
                    f"AND Tag = 'hash:{package_hash}' "
                    "ORDER BY CreatedDate DESC")
                if res["size"] > 0:
                    self.logger.info(
                        "Found existing request for package with the same metadata.  Using existing package."
                    )
                    return res["records"][0]["Id"]

            # Create the package descriptor
            # @@@ we should support releasing a successor to an older version by specifying a base version
            last_version_parts = self._get_highest_version_parts(package_id)
            version_number = self._get_next_version_number(
                last_version_parts, package_config.version_type)
            package_descriptor = {
                "ancestorId": "",  # @@@ need to add this for Managed 2gp
                "id": package_id,
                "path": "",
                "versionName": package_config.version_name,
                "versionNumber": version_number,
            }

            # Add org shape
            with open(self.org_config.config_file, "r") as f:
                scratch_org_def = json.load(f)
            for key in (
                    "country",
                    "edition",
                    "language",
                    "features",
                    "snapshot",
            ):
                if key in scratch_org_def:
                    package_descriptor[key] = scratch_org_def[key]

            # Add settings
            if "settings" in scratch_org_def:
                with build_settings_package(scratch_org_def["settings"],
                                            self.api_version) as path:
                    settings_zip_builder = MetadataPackageZipBuilder(path=path)
                    version_info.writestr("settings.zip",
                                          settings_zip_builder.as_bytes())

            # Add the dependencies for the package
            is_dependency = package_config is not self.package_config
            if (not (package_config.org_dependent or skip_validation)
                    and not is_dependency):
                self.logger.info("Determining dependencies for package")
                dependencies = self._get_dependencies()
            if dependencies:
                package_descriptor["dependencies"] = dependencies

            # Add package descriptor to version info
            version_info.writestr("package2-descriptor.json",
                                  json.dumps(package_descriptor))
        finally:
            version_info.close()
        version_info = base64.b64encode(
            version_bytes.getvalue()).decode("utf-8")
        Package2CreateVersionRequest = self._get_tooling_object(
            "Package2VersionCreateRequest")
        request = {
            "Package2Id": package_id,
            "SkipValidation": skip_validation,
            "Tag": f"hash:{package_hash}",
            "VersionInfo": version_info,
        }
        self.logger.info(
            f"Requesting creation of package version {version_number} "
            f"for package {package_config.package_name} ({package_id})")
        response = Package2CreateVersionRequest.create(request)
        self.logger.info(
            f"Package2VersionCreateRequest created with id {response['id']}")
        return response["id"]
Exemplo n.º 4
0
 def test_populate_zip(self):
     builder = BasePackageZipBuilder()
     with self.assertRaises(NotImplementedError):
         builder._populate_zip()