skip_docs
default_platform(:ios)

ENV['ITMSTRANSPORTER_FORCE_ITMS_PACKAGE_UPLOAD'] = 'true'

platform :ios do


######################### PUBLIC


desc "Submit a new Beta Build to Apple TestFlight"
lane :test_flight do
  private_testflight()
end


######################### PRIVATE


private_lane :private_testflight do
  api_key = app_store_connect_api_key(
    key_id: ENV["APPLE_KEY_ID"],
    issuer_id: ENV["APPLE_ISSUER_ID"],
    key_filepath: "./AuthKey_7837A2V482.p8",
    in_house: false,
  )

  app_id = "ru.lingualeo"

  previous_build_number = latest_testflight_build_number(
    app_identifier: app_id,
    api_key: api_key,
  )

  increment_build_number(
    xcodeproj: "LinguaLeo.xcodeproj",
    skip_info_plist: true,
    build_number: previous_build_number + 1
  )

  match(
    type: "appstore",
    app_identifier:["ru.lingualeo.NotificationServiceExtension", "ru.lingualeo", "ru.lingualeo.StickerPackExtension", "ru.lingualeo.PhraseOfTheDayWidget"]
  )

  gym(
    scheme: "LinguaLeo",
    export_method: 'app-store',
    include_symbols: true,
    include_bitcode: false,
    output_directory: "./artefacts",
    clean: true
  )

  notes = changelog_from_git_commits(
    merge_commit_filtering: "exclude_merges",
    pretty: "- %s",
    between: ["origin/master", "HEAD"]
  )

  upload_to_testflight(
    api_key: api_key,
    app_identifier: "ru.lingualeo",
    skip_waiting_for_build_processing: true,
    changelog: notes
  )

  upload_symbols_to_crashlytics(
    gsp_path: "./LinguaLeo/Plant\ Lists/GoogleService-Info.plist", 
    dsym_path: "./artefacts/LinguaLeo.app.dSYM.zip",
    debug: true
  )

  version     = get_version_number(xcodeproj: "LinguaLeo.xcodeproj", target: "LinguaLeo")
  build       = get_build_number(xcodeproj: "LinguaLeo.xcodeproj")

  git_commit(
    path: ["./LinguaLeo.xcodeproj", "./artefacts/"],
    message: "Bump build #{version}(#{build})"
  )

  push_to_git_remote(
    tags: false
  )

  post_to_slack(scheme: "LinguaLeo", destination: "Test Flight")
end

private_lane :post_to_slack do |options|

    scheme      = options[:scheme]
    version     = get_version_number(xcodeproj: "LinguaLeo.xcodeproj", target: "LinguaLeo")
    build       = get_build_number(xcodeproj: "LinguaLeo.xcodeproj")

    notes = changelog_from_git_commits(
      merge_commit_filtering: "exclude_merges",
      pretty: "- %s",
      commits_count: 3,
    )

    slack(message: ":rocket: Successfully uploaded a new build! :rocket:",
        slack_url: ENV["SLACK_URL_IOS"],
        channel: "#ci_ios_releases",
        payload: {
            "Version" => version,
            "Build" => build,
            "Release notes" => notes
      },
        default_payloads: [:git_branch]
      )
end

end
