<commit_before># Utility functions

require 'git'

ENV['HOCKEY_API_TOKEN'] = '<KEY>'

# Returns true if the `NO_RESET` env var is set to 1
def reset_disabled?
  ENV['NO_RESET'] == '1'
end

# Returns true if the `NO_DEPLOY` env var is set to 1
def deploy_disabled?
  ENV['NO_DEPLOY'] == '1'
end

# Returns true if the `NO_TEST` env var is set to 1
def test_disabled?
  ENV['NO_TEST'] == '1'
end
# Runs goals from the project's Makefile, this requires going up to the project directory.
# :args: Additional arguments to be passed to `make`.
# Returns The result of the `make` command
def make(args)
  # Maybe we should write an "uncrustify" fastlane action?...
  Dir.chdir '..' do
    sh 'make ' + args
  end
end

def run_unit_tests

  unless test_disabled?
    xctest({
      scheme: 'Wikipedia',
      destination: "platform=iOS Simulator,name=iPhone 6,OS=8.3",
      reports: [{
        report: "html",
        output: "build/reports/report.html"
      },
      {
        report: "junit",
        output: "build/reports/report.xml"
        }],
        clean: nil
        })

      end
end

# Generate a list of commit subjects from `rev` to `HEAD`
# :rev: The git SHA to start the log from, defaults to `ENV[LAST_SUCCESS_REV']`
def generate_git_commit_log(rev=ENV['LAST_SUCCESS_REV'])
  g = Git.open(Dir.getwd)
  change_log = g.log.between(rev).map { |c| "- " + c.message.lines.first.chomp }.join "\n"
  "Commit Log:\n\n#{change_log}\n"
end

# Memoized version of `generate_git_commit_log` which stores the result in `ENV['GIT_COMMIT_LOG']`.
def git_commit_log
  ENV['GIT_COMMIT_LOG'] || ENV['GIT_COMMIT_LOG'] = generate_git_commit_log
end

def deploy_testflight_build
  unless deploy_disabled?
    # Upload the DSYM to Hockey
    hockey({
      api_token: ENV['HOCKEY_API_TOKEN'],
      notes: git_commit_log,
      notify: 0,
      status: 1, #Means do not make available for download
    })

    #Set "What To Test" in iTunes Connect for Testflight builds, in the future, reference tickets instead of git commits
    DELIVER_WHAT_TO_TEST.replace = git_commit_log
    #Set "App Description" in iTunes Connect for Testflight builds, in the future set a better description
    DELIVER_BETA_DESCRIPTION.replace = git_commit_log
    #Set "Feedback email" in iTunes Connect for Testflight builds
    DELIVER_BETA_FEEDBACK_EMAIL.replace = '<EMAIL>'

    # Upload the IPA and DSYM to iTunes Connect<commit_msg>Fix change logs - git not using the proper path to the repo.<commit_after># Utility functions

require 'git'

ENV['HOCKEY_API_TOKEN'] = '<KEY>'

# Returns true if the `NO_RESET` env var is set to 1
def reset_disabled?
  ENV['NO_RESET'] == '1'
end

# Returns true if the `NO_DEPLOY` env var is set to 1
def deploy_disabled?
  ENV['NO_DEPLOY'] == '1'
end

# Returns true if the `NO_TEST` env var is set to 1
def test_disabled?
  ENV['NO_TEST'] == '1'
end
# Runs goals from the project's Makefile, this requires going up to the project directory.
# :args: Additional arguments to be passed to `make`.
# Returns The result of the `make` command
def make(args)
  # Maybe we should write an "uncrustify" fastlane action?...
  Dir.chdir '..' do
    sh 'make ' + args
  end
end

def run_unit_tests

  unless test_disabled?
    xctest({
      scheme: 'Wikipedia',
      destination: "platform=iOS Simulator,name=iPhone 6,OS=8.3",
      reports: [{
        report: "html",
        output: "build/reports/report.html"
      },
      {
        report: "junit",
        output: "build/reports/report.xml"
        }],
        clean: nil
        })

      end
end

# Generate a list of commit subjects from `rev` to `HEAD`
# :rev: The git SHA to start the log from, defaults to `ENV[LAST_SUCCESS_REV']`
def generate_git_commit_log(rev=ENV['GIT_PREVIOUS_SUCCESSFUL_COMMIT'])
  g = Git.open(ENV['PWD'], :log => Logger.new(STDOUT))
  change_log = g.log.between(rev).map { |c| "- " + c.message.lines.first.chomp }.join "\n"
  "Commit Log:\n\n#{change_log}\n"
  p change_log
end

# Memoized version of `generate_git_commit_log` which stores the result in `ENV['GIT_COMMIT_LOG']`.
def git_commit_log
  ENV['GIT_COMMIT_LOG'] || ENV['GIT_COMMIT_LOG'] = generate_git_commit_log
end

def deploy_testflight_build
  unless deploy_disabled?
    # Upload the DSYM to Hockey
    hockey({
      api_token: ENV['HOCKEY_API_TOKEN'],
      notes: '',
      notify: 0,
      status: 1, #Means do not make available for download
    })

    #Set "Feedback email" in iTunes Connect for Testflight builds
    self.class.const_set("DELIVER_BETA_FEEDBACK_EMAIL", '<EMAIL>')
    #Set "What To Test" in iTunes Connect for Testflight builds, in the future, reference tickets instead of git commits
    self.class.const_set("DELIVER_WHAT_TO_TEST", git_commit_log)
    #Set "App Description" in iTunes Connect for Testflight builds, in the future set a better description
    self.class.const_set("DELIVER_BETA_DESCRIPTION", git_commit_log)

    # Upload the IPA and DSYM to iTunes Connect
{'fraction_non_alphanumeric': 0.10686868686868686, 'fraction_numerical': 0.0044444444444444444, 'mean_word_length': 3.5842592592592593, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3788092', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1649, 'n_words': 531}