<commit_before>module Projects
  class UpdateService < BaseService
    def execute
      # check that user is allowed to set specified visibility_level
      new_visibility = params[:visibility_level]
      if new_visibility
        if new_visibility.to_i != project.visibility_level
          unless can?(current_user, :change_visibility_level, project) &&
            Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
            deny_visibility_level(project, new_visibility)
            return project
          end
        end

        return false unless visibility_level_allowed?(new_visibility)
        update_forks_visibility_level(new_visibility)
      end

      new_branch = params[:default_branch]

      if project.repository.exists? && new_branch && new_branch != project.default_branch
        project.change_head(new_branch)
      end

      if project.update_attributes(params.except(:default_branch))
        if project.previous_changes.include?('path')
          project.rename_repo
        end
      end
    end

    private

    def visibility_level_allowed?(level)
      return true if project.visibility_level_allowed?(level)

      level_name = Gitlab::VisibilityLevel.level_name(level)
      project.errors.add(
        :visibility_level,
        "#{level_name} could not be set as visibility level of this project - parent project settings are more restrictive"
      )

      false
    end

    def update_forks_visibility_level(new_level)
      project.forks.each do |forked_project|
        fork_level = forked_project.visibility_level

        if fork_level > new_level.to_i
          forked_project.visibility_level = new_level.to_i
          forked_project.save!
        end
      end
    end
  end
end<commit_msg>Prevent unnecessary forks iteration at parent update<commit_after>module Projects
  class UpdateService < BaseService
    def execute
      # check that user is allowed to set specified visibility_level
      new_visibility = params[:visibility_level]
      if new_visibility
        if new_visibility.to_i != project.visibility_level
          unless can?(current_user, :change_visibility_level, project) &&
            Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
            deny_visibility_level(project, new_visibility)
            return project
          end
        end

        return false unless visibility_level_allowed?(new_visibility)
        update_forks_visibility_level(new_visibility.to_i)
      end

      new_branch = params[:default_branch]

      if project.repository.exists? && new_branch && new_branch != project.default_branch
        project.change_head(new_branch)
      end

      if project.update_attributes(params.except(:default_branch))
        if project.previous_changes.include?('path')
          project.rename_repo
        end
      end
    end

    private

    def visibility_level_allowed?(level)
      return true if project.visibility_level_allowed?(level)

      level_name = Gitlab::VisibilityLevel.level_name(level)
      project.errors.add(
        :visibility_level,
        "#{level_name} could not be set as visibility level of this project - parent project settings are more restrictive"
      )

      false
    end

    def update_forks_visibility_level(new_level)
      return unless new_level < project.visibility_level

      project.forks.each do |forked_project|
        fork_level = forked_project.visibility_level

        if fork_level > new_level
          forked_project.visibility_level = new_level
          forked_project.save!
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.059944751381215466, 'fraction_numerical': 0.0, 'mean_word_length': 2.8236536430834214, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5614602', 'n_tokens_mistral': 1006, 'n_tokens_neox': 953, 'n_words': 221}