<commit_before>require 'fileutils'
require 'retryable'
require 'tempfile'
require 'time'

module Stove
  class Cookbook
    require_relative 'cookbook/metadata'

    include Stove::Git

    # The path to this cookbook.
    #
    # @return [String]
    attr_reader :path

    # The name of the cookbook (must correspond to the name of the
    # cookbook on the community site).
    #
    # @return [String]
    attr_reader :name

    # The version of this cookbook (originally).
    #
    # @return [String]
    attr_reader :version

    # The new version of the cookbook.
    #
    # @return [String]
    attr_reader :new_version

    # The metadata for this cookbook.
    #
    # @return [Stove::Cookbook::Metadata]
    attr_reader :metadata

    # The list of options passed to the cookbook.
    #
    # @return [Hash]
    attr_reader :options

    # Create a new wrapper around the cookbook object.
    #
    # @param [Hash] options
    #   the list of options
    def initialize(options = {})
      @path = options[:path] || Dir.pwd
      @new_version = options[:new_version]
      @options = options

      load_metadata!
    end

    # The category for this cookbook on the community site.
    #
    # @return [String]
    def category
      @category ||= options[:category] || Stove::CommunitySite.cookbook(name)['category']
    rescue
      raise Stove::CookbookCategoryNotFound
    end

    # The URL for the cookbook on the Community Site.
    #
    # @return [String]
    def url
      "#{Stove::CommunitySite.http_uri}/cookbooks/#{name}"
    end

    # Deterine if this cookbook version is released on the community site
    def released?
      @_released ||= begin
        Stove::CommunitySite.cookbook(name, version)
        true
      rescue Stove::BadResponse
        false
      end
    end

    # The unreleased JIRA tickets for this cookbook.
    #
    # @return [Hashie::Dash, Array]
    def unreleased_tickets
      @unreleased_tickets ||= Stove::JIRA.unreleased_tickets_for(name)
    end

    #
    def release!
      if options[:git]
        Stove::Logger.info "Running validations"
        validate_git_repo!
        validate_git_clean!
        validate_remote_updated!
      end

      Stove::Logger.info "Bumping version"
      version_bump

      if options[:changelog]
        Stove::Logger.info "Updating changelog"
        update_changelog
      end

      if options[:git]
        Dir.chdir(path) do
          Stove::Logger.info "Committing git changes in '#{path}'"

          git "add metadata.rb"
          git "add CHANGELOG.md"
          git "commit -m 'Version bump to #{tag_version}'"
          git "push #{options[:remote]} #{options[:branch]}"

          if options[:github]
            Stove::Logger.info "Pushing release to GitHub"
            Stove::GitHub.new(self).publish_release!
          else
            Stove::Logger.info "Tagging a release"
            git "tag #{tag_version}"
            git "push #{options[:remote]} #{tag_version}"
          end
        end
      end

      if options[:upload]
        Stove::Logger.info "Uploading cookbook"
        retryable(tries: 3) do
          upload
        end
      end

      if options[:jira]
        Stove::Logger.info "Resolving JIRA issues"
        resolve_jira_issues
      end

      if options[:devodd]
        Stove::Logger.info "Bumping devodd release"
        split = version.split('.').map(&:to_i)
        split[2] += 1
        devodd = split.join('.')

        version_bump(devodd)

        if options[:git]
          Dir.chdir(path) do
            git "add metadata.rb"
            git "commit -m 'Version bump to #{tag_version}'"
            git "push #{options[:remote]} #{options[:branch]}"
          end
        end
      end
    end

    def tag_version
      "v#{version}"
    end

    # So there's this really really crazy bug that the tmp directory could
    # be deleted mid-request...<commit_msg>Fix single quotes on windows (#6)<commit_after>require 'fileutils'
require 'retryable'
require 'tempfile'
require 'time'

module Stove
  class Cookbook
    require_relative 'cookbook/metadata'

    include Stove::Git

    # The path to this cookbook.
    #
    # @return [String]
    attr_reader :path

    # The name of the cookbook (must correspond to the name of the
    # cookbook on the community site).
    #
    # @return [String]
    attr_reader :name

    # The version of this cookbook (originally).
    #
    # @return [String]
    attr_reader :version

    # The new version of the cookbook.
    #
    # @return [String]
    attr_reader :new_version

    # The metadata for this cookbook.
    #
    # @return [Stove::Cookbook::Metadata]
    attr_reader :metadata

    # The list of options passed to the cookbook.
    #
    # @return [Hash]
    attr_reader :options

    # Create a new wrapper around the cookbook object.
    #
    # @param [Hash] options
    #   the list of options
    def initialize(options = {})
      @path = options[:path] || Dir.pwd
      @new_version = options[:new_version]
      @options = options

      load_metadata!
    end

    # The category for this cookbook on the community site.
    #
    # @return [String]
    def category
      @category ||= options[:category] || Stove::CommunitySite.cookbook(name)['category']
    rescue
      raise Stove::CookbookCategoryNotFound
    end

    # The URL for the cookbook on the Community Site.
    #
    # @return [String]
    def url
      "#{Stove::CommunitySite.http_uri}/cookbooks/#{name}"
    end

    # Deterine if this cookbook version is released on the community site
    def released?
      @_released ||= begin
        Stove::CommunitySite.cookbook(name, version)
        true
      rescue Stove::BadResponse
        false
      end
    end

    # The unreleased JIRA tickets for this cookbook.
    #
    # @return [Hashie::Dash, Array]
    def unreleased_tickets
      @unreleased_tickets ||= Stove::JIRA.unreleased_tickets_for(name)
    end

    #
    def release!
      if options[:git]
        Stove::Logger.info "Running validations"
        validate_git_repo!
        validate_git_clean!
        validate_remote_updated!
      end

      Stove::Logger.info "Bumping version"
      version_bump

      if options[:changelog]
        Stove::Logger.info "Updating changelog"
        update_changelog
      end

      if options[:git]
        Dir.chdir(path) do
          Stove::Logger.info "Committing git changes in '#{path}'"

          git "add metadata.rb"
          git "add CHANGELOG.md"
          git "commit -m \"Version bump to #{tag_version}\""
          git "push #{options[:remote]} #{options[:branch]}"

          if options[:github]
            Stove::Logger.info "Pushing release to GitHub"
            Stove::GitHub.new(self).publish_release!
          else
            Stove::Logger.info "Tagging a release"
            git "tag #{tag_version}"
            git "push #{options[:remote]} #{tag_version}"
          end
        end
      end

      if options[:upload]
        Stove::Logger.info "Uploading cookbook"
        retryable(tries: 3) do
          upload
        end
      end

      if options[:jira]
        Stove::Logger.info "Resolving JIRA issues"
        resolve_jira_issues
      end

      if options[:devodd]
        Stove::Logger.info "Bumping devodd release"
        split = version.split('.').map(&:to_i)
        split[2] += 1
        devodd = split.join('.')

        version_bump(devodd)

        if options[:git]
          Dir.chdir(path) do
            git "add metadata.rb"
            git "commit -m \"Version bump to #{tag_version}\""
            git "push #{options[:remote]} #{options[:branch]}"
          end
        end
      end
    end

    def tag_version
      "v#{version}"
    end

    # So there's this really really crazy bug that the tmp directory could
    # be deleted mid-request...
{'fraction_non_alphanumeric': 0.09832103790384127, 'fraction_numerical': 0.0008903586873569067, 'mean_word_length': 2.1654589371980677, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6305145', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2304, 'n_words': 666}