def test_skip(metadata, config): prepared = PreparedBuild() prepared.metadata = metadata config = config with pytest.raises(SkipRequestException): raise_if_skipped_build(prepared, config)
def test_does_not_skip(): prepared = PreparedBuild() prepared.metadata = { "ref": "ref/heads/master", "commit_info": { "message": "some cool message", }, } config = { "branchtag_regex": "(master)|(heads/master)", } raise_if_skipped_build(prepared, config)
def test_does_not_skip(): prepared = PreparedBuild() prepared.metadata = { 'ref': 'ref/heads/master', 'commit_info': { 'message': 'some cool message', }, } config = { 'branchtag_regex': '(master)|(heads/master)', } raise_if_skipped_build(prepared, config)
def test_start_build_disabled_trigger(app): trigger = model.build.list_build_triggers("devtable", "building")[0] trigger.enabled = False trigger.save() build = PreparedBuild(trigger=trigger) with pytest.raises(BuildTriggerDisabledException): start_build(trigger.repository, build)
def prepare_build(self, metadata, is_manual=False): # Ensure that the metadata meets the scheme. validate(metadata, METADATA_SCHEMA) config = self.config commit_sha = metadata["commit"] # Create the prepared build. prepared = PreparedBuild(self.trigger) prepared.name_from_sha(commit_sha) prepared.subdirectory = config.get("dockerfile_path", None) prepared.context = config.get("context", None) prepared.is_manual = is_manual prepared.metadata = metadata prepared.tags = BuildTriggerHandler._determine_tags(config, metadata) return prepared
def test_maximum_builds(app): # Change the maximum number of builds to 1. user = model.user.create_user("foobar", "password", "*****@*****.**") user.maximum_queued_builds_count = 1 user.save() repo = model.repository.create_repository("foobar", "somerepo", user) # Try to queue a build; should succeed. prepared_build = PreparedBuild() prepared_build.build_name = "foo" prepared_build.is_manual = True prepared_build.dockerfile_id = "foobar" prepared_build.archive_url = "someurl" prepared_build.tags = ["latest"] prepared_build.subdirectory = "/" prepared_build.context = "/" prepared_build.metadata = {} start_build(repo, prepared_build) # Try to queue a second build; should fail. with pytest.raises(MaximumBuildsQueuedException): start_build(repo, prepared_build)
def post(self, namespace, repository): """ Request that a repository be built and pushed from the specified input. """ logger.debug("User requested repository initialization.") request_json = request.get_json() dockerfile_id = request_json.get("file_id", None) archive_url = request_json.get("archive_url", None) if not dockerfile_id and not archive_url: raise InvalidRequest("file_id or archive_url required") if archive_url: archive_match = None try: archive_match = urlparse(archive_url) except ValueError: pass if not archive_match: raise InvalidRequest( "Invalid Archive URL: Must be a valid URI") scheme = archive_match.scheme if scheme != "http" and scheme != "https": raise InvalidRequest( "Invalid Archive URL: Must be http or https") context, subdir = self.get_dockerfile_context(request_json) tags = request_json.get("docker_tags", ["latest"]) pull_robot_name = request_json.get("pull_robot", None) # Verify the security behind the pull robot. if pull_robot_name: result = parse_robot_username(pull_robot_name) if result: try: model.user.lookup_robot(pull_robot_name) except model.InvalidRobotException: raise NotFound() # Make sure the user has administer permissions for the robot's namespace. (robot_namespace, _) = result if not AdministerOrganizationPermission(robot_namespace).can(): raise Unauthorized() else: raise Unauthorized() # Check if the dockerfile resource has already been used. If so, then it # can only be reused if the user has access to the repository in which the # dockerfile was previously built. if dockerfile_id: associated_repository = model.build.get_repository_for_resource( dockerfile_id) if associated_repository: if not ModifyRepositoryPermission( associated_repository.namespace_user.username, associated_repository.name): raise Unauthorized() # Start the build. repo = model.repository.get_repository(namespace, repository) if repo is None: raise NotFound() try: build_name = (user_files.get_file_checksum(dockerfile_id) if dockerfile_id else hashlib.sha224( archive_url.encode("ascii")).hexdigest()[0:7]) except IOError: raise InvalidRequest("File %s could not be found or is invalid" % dockerfile_id) prepared = PreparedBuild() prepared.build_name = build_name prepared.dockerfile_id = dockerfile_id prepared.archive_url = archive_url prepared.tags = tags prepared.subdirectory = subdir prepared.context = context prepared.is_manual = True prepared.metadata = {} try: build_request = start_build(repo, prepared, pull_robot_name=pull_robot_name) except MaximumBuildsQueuedException: abort(429, message="Maximum queued build rate exceeded.") except BuildTriggerDisabledException: abort(400, message="Build trigger is disabled") resp = build_status_view(build_request) repo_string = "%s/%s" % (namespace, repository) headers = { "Location": api.url_for(RepositoryBuildStatus, repository=repo_string, build_uuid=build_request.uuid), } return resp, 201, headers
def test_tags_for_ref(ref, expected_tags): prepared = PreparedBuild() prepared.tags_from_ref(ref, default_branch="master") assert set(prepared._tags) == set(expected_tags)
def prepare_build(self, metadata, is_manual=False): # Ensure that the metadata meets the scheme. validate(metadata, METADATA_SCHEMA) config = self.config ref = metadata.get('ref', None) commit_sha = metadata['commit'] default_branch = metadata.get('default_branch', None) prepared = PreparedBuild(self.trigger) prepared.name_from_sha(commit_sha) prepared.subdirectory = config.get('dockerfile_path', None) prepared.context = config.get('context', None) prepared.is_manual = is_manual prepared.metadata = metadata if ref is not None: prepared.tags_from_ref(ref, default_branch) else: prepared.tags = [commit_sha[:7]] return prepared