<commit_before>module Fastlane
  module Actions
    module SharedValues
      LATEST_BUILD_NUMBER = :LATEST_BUILD_NUMBER
    end

    class AppStoreBuildNumberAction < Action
      def self.run(params)
        require 'spaceship'

        build_nr = get_build_number(params)

        # Convert build_nr to int (for legacy use) if no "." in string
        if build_nr.kind_of?(String) && !build_nr.include?(".")
          build_nr = build_nr.to_i
        end

        Actions.lane_context[SharedValues::LATEST_BUILD_NUMBER] = build_nr
      end

      def self.get_build_number(params)
        UI.message("Login to App Store Connect (#{params[:username]})")
        Spaceship::Tunes.login(params[:username])
        Spaceship::Tunes.select_team(team_id: params[:team_id], team_name: params[:team_name])
        UI.message("Login successful")

        app = Spaceship::Tunes::Application.find(params[:app_identifier])
        if params[:live]
          UI.message("Fetching the latest build number for live-version")
          UI.user_error!("Could not find a live-version of #{params[:app_identifier]} on iTC") unless app.live_version
          build_nr = app.live_version.current_build_number

          UI.message("Latest upload for live-version #{app.live_version.version} is build: #{build_nr}")
        else
          version_number = params[:version]

          # Filter on app (and version is specified)
          filter = { app: app.apple_id }
          filter["version"] = version_number if version_number

          # Get version number from latest pre-release if no version number given
          client = Spaceship::ConnectAPI::Base.client
          version = client.get_pre_release_versions(filter: filter, sort: "-version", limit: 1).first
          unless version
            UI.user_error!("Could not find a build number for version #{version_number}")
          end

          # Need pre_release_version_id for filtering build numbers
          pre_release_version_id = version["id"]
          version_number = version["attributes"]["version"]

          UI.message("Fetching the latest build number for version #{version_number}")

          # Get latest build for version number
          build = client.get_builds(filter: { app: app.apple_id, "preReleaseVersion" => pre_release_version_id }, sort: "-uploadedDate", limit: 1).first
          if build
            build_nr = build["attributes"]["version"]
            build_nr
          else
            UI.important("Could not find a build number for version #{version_number} on App Store Connect")
          end

          # Show error and set initial build number
          unless build_nr
            UI.user_error!("Could not find a build on App Store Connect - and 'initial_build_number' option is not set") unless params[:initial_build_number]
            build_nr = params[:initial_build_number]
          end

          UI.message("Latest upload for version #{version_number} is build: #{build_nr}")
        end
<commit_msg>[action] app_store_build_number (and latest_testflight_build_number) don't raise error if using :initial_build_number (#14654)<commit_after>module Fastlane
  module Actions
    module SharedValues
      LATEST_BUILD_NUMBER = :LATEST_BUILD_NUMBER
    end

    class AppStoreBuildNumberAction < Action
      def self.run(params)
        require 'spaceship'

        build_nr = get_build_number(params)

        # Convert build_nr to int (for legacy use) if no "." in string
        if build_nr.kind_of?(String) && !build_nr.include?(".")
          build_nr = build_nr.to_i
        end

        Actions.lane_context[SharedValues::LATEST_BUILD_NUMBER] = build_nr
      end

      def self.get_build_number(params)
        UI.message("Login to App Store Connect (#{params[:username]})")
        Spaceship::Tunes.login(params[:username])
        Spaceship::Tunes.select_team(team_id: params[:team_id], team_name: params[:team_name])
        UI.message("Login successful")

        app = Spaceship::Tunes::Application.find(params[:app_identifier])
        if params[:live]
          UI.message("Fetching the latest build number for live-version")
          UI.user_error!("Could not find a live-version of #{params[:app_identifier]} on iTC") unless app.live_version
          build_nr = app.live_version.current_build_number

          UI.message("Latest upload for live-version #{app.live_version.version} is build: #{build_nr}")
        else
          version_number = params[:version]

          # Filter on app (and version is specified)
          filter = { app: app.apple_id }
          filter["version"] = version_number if version_number

          # Get version number from latest pre-release if no version number given
          client = Spaceship::ConnectAPI::Base.client
          version = client.get_pre_release_versions(filter: filter, sort: "-version", limit: 1).first
          if version
            # Need pre_release_version_id for filtering build numbers
            pre_release_version_id = version["id"]
            version_number = version["attributes"]["version"]

            UI.message("Fetching the latest build number for version #{version_number}")

            # Get latest build for version number
            build = client.get_builds(filter: { app: app.apple_id, "preReleaseVersion" => pre_release_version_id }, sort: "-uploadedDate", limit: 1).first
            if build
              build_nr = build["attributes"]["version"]
              build_nr
            else
              UI.important("Could not find a build number for version #{version_number} on App Store Connect")
            end
          end

          # Show error and set initial build number
          if build_nr
            UI.message("Latest upload for version #{version_number} is build: #{build_nr}")
          elsif params[:initial_build_number].nil?
            UI.user_error!("Could not find a build on App Store Connect - and 'initial_build_number' option is not set")
          else
            build_nr = params[:initial_build_number]
            UI.message("Using initial build number of #{build_nr} for version #{version_number}")
          end

        end

{'fraction_non_alphanumeric': 0.09158696709353217, 'fraction_numerical': 0.0014589074404279462, 'mean_word_length': 2.7079326923076925, 'pattern_counts': {'":': 0, '<': 5, '<?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': '252395', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1643, 'n_words': 463}