Skip to content

Commit

Permalink
Merge pull request #18 from rajivshah3/fix/api-and-spaceship-updates
Browse files Browse the repository at this point in the history
fix: Various fixes and updates
  • Loading branch information
Josh Holtz authored Mar 22, 2021
2 parents a7700fa + 84ee43d commit 9b2e168
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions lib/watchbuild/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def run
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
Spaceship::ConnectAPI.login(WatchBuild.config[:username], nil, use_portal: false, use_tunes: true)
UI.message('Successfully logged in')

start = Time.now
Expand All @@ -30,22 +29,31 @@ def run
def wait_for_build(start_time)
UI.user_error!("Could not find app with app identifier #{WatchBuild.config[:app_identifier]}") unless app

build = nil
showed_info = false

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'
build = find_build(build)

if build.nil?
UI.important("Read more information on why this build isn't showing up yet - https://github.com/fastlane/fastlane/issues/14997") unless showed_info
showed_info = true
else
time_elapsed = Time.at(seconds_elapsed).utc.strftime '%H:%M:%S hours'
return build if build.processed?

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.app_version} (#{build.version})... this might take a while...")
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')
Expand All @@ -65,7 +73,9 @@ def notification(build, minutes)
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"
platform = build.pre_release_version.platform.downcase.gsub('_', '')

url = "https://appstoreconnect.apple.com/apps/#{app.id}/testflight/#{platform}/#{build.id}/metadata"

slack_url = ENV['SLACK_URL'].to_s
if !slack_url.empty?
Expand All @@ -85,15 +95,15 @@ def notification(build, minutes)
private

def app
@app ||= Spaceship::Application.find(WatchBuild.config[:app_identifier])
@app ||= Spaceship::ConnectAPI::App.find(WatchBuild.config[:app_identifier])
end

def notify_slack(build, minutes, url)
require 'net/http'
require 'uri'
require 'json'

message = "App Store build #{build.train_version} (#{build.build_version}) has finished processing in #{minutes} minutes"
message = "App Store build #{build.app_version} (#{build.version}) has finished processing in #{minutes} minutes"
slack_url = URI.parse(url)
slack_message = {
"text": message
Expand All @@ -118,19 +128,24 @@ def notify_terminal(build, minutes, url)
require 'terminal-notifier'

TerminalNotifier.notify('Build finished processing',
title: build.app_name,
subtitle: "#{build.train_version} (#{build.build_version})",
title: app.name,
subtitle: "#{build.app_version} (#{build.version})",
execute: "open '#{url}'")
end

def find_build
build = nil
app.latest_version.candidate_builds.each do |b|
build = b if !build || b.upload_date > build.upload_date
# Finds a build if none given
# Otherwise fetches a build (to get updated state)
def find_build(build)
if build.nil?
build = app.get_builds(includes: Spaceship::ConnectAPI::Build::ESSENTIAL_INCLUDES).select do |build|
build.processing_state == Spaceship::ConnectAPI::Build::ProcessingState::PROCESSING
end.sort_by(&:uploaded_date).last
else
build = Spaceship::ConnectAPI::Build.get(build_id: build.id, includes: Spaceship::ConnectAPI::Build::ESSENTIAL_INCLUDES)
end

unless build
UI.user_error!("No processing builds available for app #{WatchBuild.config[:app_identifier]}")
UI.error("No processing builds available for app #{WatchBuild.config[:app_identifier]} - this may take a few minutes (check your email for processing issues if this continues)")
end

build
Expand Down

0 comments on commit 9b2e168

Please sign in to comment.