<commit_before>require 'spaceship'

module WatchBuild
  class Runner
    attr_accessor :spaceship

    # Uses the spaceship to create or download a provisioning profile
    # returns the path the newly created provisioning profile (in /tmp usually)
    def run
      FastlaneCore::PrintTable.print_values(config: WatchBuild.config,
                                            hide_keys: [],
                                            title: "Summary for WatchBuild #{WatchBuild::VERSION}")

      UI.message("Starting login with user '#{WatchBuild.config[:username]}'")

      ENV['FASTLANE_ITC_TEAM_ID'] = WatchBuild.config[:itc_team_id] if WatchBuild.config[:itc_team_id]
      ENV['FASTLANE_ITC_TEAM_NAME'] = WatchBuild.config[:itc_team_name] if WatchBuild.config[:itc_team_name]
      ENV['SLACK_URL'] = WatchBuild.config[:slack_url]

      Spaceship::Tunes.login(WatchBuild.config[:username], nil)
      Spaceship::Tunes.select_team
      UI.message('Successfully logged in')

      start = Time.now
      build = wait_for_build(start)
      minutes = ((Time.now - start) / 60).round
      notification(build, minutes)
    end

    def wait_for_build(start_time)
      UI.user_error!("Could not find app with app identifier #{WatchBuild.config[:app_identifier]}") unless app

      loop do
        begin
          build = find_build
          return build if build.processing == false

          seconds_elapsed = (Time.now - start_time).to_i.abs
          case seconds_elapsed
          when 0..59
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%S seconds'
          when 60..3599
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%M:%S minutes'
          else
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%H:%M:%S hours'
          end

          UI.message("Waiting #{time_elapsed} for App Store Connect to process the build #{build.train_version} (#{build.build_version})... this might take a while...")
        rescue => ex
          UI.error(ex)
          UI.message('Something failed... trying again to recover')
        end
        if WatchBuild.config[:sample_only_once] == false
          sleep 30
        else
          break
        end
      end
      nil
    end

    def notification(build, minutes)
      if build.nil?
        UI.message 'Application build is still processing'
        return
      end

      url = "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/ng/app/#{@app.apple_id}/activity/ios/builds/#{build.train_version}/#{build.build_version}/details"

      if !ENV['SLACK_URL'].empty?
        notify_slack(build, minutes, ENV['SLACK_URL'])
      else
        notify_terminal(build, minutes, url)
      end

      UI.success('Successfully finished processing the build')
      if minutes > 0 # it's 0 minutes if there was no new build uploaded
        UI.message('You can now tweet: ')
        UI.important("App Store Connect #iosprocessingtime #{minutes} minutes")
      end
      UI.message(url)<commit_msg>Added a bit of safety<commit_after>require 'spaceship'

module WatchBuild
  class Runner
    attr_accessor :spaceship

    # Uses the spaceship to create or download a provisioning profile
    # returns the path the newly created provisioning profile (in /tmp usually)
    def run
      FastlaneCore::PrintTable.print_values(config: WatchBuild.config,
                                            hide_keys: [],
                                            title: "Summary for WatchBuild #{WatchBuild::VERSION}")

      UI.message("Starting login with user '#{WatchBuild.config[:username]}'")

      ENV['FASTLANE_ITC_TEAM_ID'] = WatchBuild.config[:itc_team_id] if WatchBuild.config[:itc_team_id]
      ENV['FASTLANE_ITC_TEAM_NAME'] = WatchBuild.config[:itc_team_name] if WatchBuild.config[:itc_team_name]
      ENV['SLACK_URL'] = WatchBuild.config[:slack_url]

      Spaceship::Tunes.login(WatchBuild.config[:username], nil)
      Spaceship::Tunes.select_team
      UI.message('Successfully logged in')

      start = Time.now
      build = wait_for_build(start)
      minutes = ((Time.now - start) / 60).round
      notification(build, minutes)
    end

    def wait_for_build(start_time)
      UI.user_error!("Could not find app with app identifier #{WatchBuild.config[:app_identifier]}") unless app

      loop do
        begin
          build = find_build
          return build if build.processing == false

          seconds_elapsed = (Time.now - start_time).to_i.abs
          case seconds_elapsed
          when 0..59
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%S seconds'
          when 60..3599
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%M:%S minutes'
          else
            time_elapsed = Time.at(seconds_elapsed).utc.strftime '%H:%M:%S hours'
          end

          UI.message("Waiting #{time_elapsed} for App Store Connect to process the build #{build.train_version} (#{build.build_version})... this might take a while...")
        rescue => ex
          UI.error(ex)
          UI.message('Something failed... trying again to recover')
        end
        if WatchBuild.config[:sample_only_once] == false
          sleep 30
        else
          break
        end
      end
      nil
    end

    def notification(build, minutes)
      if build.nil?
        UI.message 'Application build is still processing'
        return
      end

      url = "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/ng/app/#{@app.apple_id}/activity/ios/builds/#{build.train_version}/#{build.build_version}/details"

      slack_url = ENV['SLACK_URL'].to_s
      if !slack_url.empty?
        notify_slack(build, minutes, slack_url)
      else
        notify_terminal(build, minutes, url)
      end

      UI.success('Successfully finished processing the build')
      if minutes > 0 # it's 0 minutes if there was no new build uploaded
        UI.message('You can now tweet: ')
        UI.important("App Store Connect #iosprocessingtime #{minutes} minutes")
      end
      UI.message(url)
{'fraction_non_alphanumeric': 0.1011865524060646, 'fraction_numerical': 0.004943968358602505, 'mean_word_length': 2.469982847341338, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '888671', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1719, 'n_words': 423}