Last active
December 21, 2015 12:28
-
-
Save zaki/6305554 to your computer and use it in GitHub Desktop.
Mayday - Delete old TestFlight builds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "mechanize" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
require "base64" | |
require 'json' | |
USERNAME = "" | |
PASSWORD = "" | |
# github | |
AUTH = "" | |
REPO = "" | |
APPLICATIONS = { | |
"000000" => 0, # PR iOS | |
"111111" => 1, # PR Android | |
"222222" => 1, # master iOS | |
"333333" => 1, # master Android | |
} | |
class Mayday | |
TESTFLIGHT_URL = "https://testflightapp.com/" | |
TF_LOGIN_PAGE_URL = "#{TESTFLIGHT_URL}/login/" | |
def initialize(application_code, pages_to_keep) | |
@application_code = application_code | |
@pages_to_keep = pages_to_keep | |
@agent = Mechanize.new | |
@agent.verify_mode=OpenSSL::SSL::VERIFY_NONE # yes, bad form, but just need a quick script rather than fixing my env for now | |
puts "Starting cleanup of application #{application_code}" | |
end | |
def testflight_builds_url(page=1) | |
"#{TESTFLIGHT_URL}/dashboard/applications/#{@application_code}/builds/?page=#{page}" | |
end | |
def login | |
page = @agent.get TF_LOGIN_PAGE_URL | |
login_form = page.form | |
login_form.username = USERNAME | |
login_form.password = Base64::decode64(PASSWORD) | |
@agent.submit(login_form, login_form.buttons.first) | |
end | |
def build_page_count | |
page = @agent.get testflight_builds_url | |
pagination = page.search(".pagination .center ul li") | |
return 0 unless pagination.length > 2 | |
pagination[-2].text.to_i | |
end | |
def builds_on_last_page(page_no) | |
page = @agent.get testflight_builds_url(page_no) | |
page.search(".goversion") | |
end | |
def delete_build(build_id) | |
@agent.post("#{TESTFLIGHT_URL}/dashboard/builds/delete/#{build_id}/") | |
end | |
def cleanup(prs) | |
login | |
page_count = build_page_count | |
page_count.downto(@pages_to_keep+1) do |page_no| | |
puts "Processing page #{page_no}" | |
builds = builds_on_last_page(page_no) | |
builds.each do |build| | |
build_id = build.attribute("id").value | |
build_no = build.search("td:first-child").text | |
id, build = nil, nil | |
if build_id =~ /\/dashboard\/builds\/report\/(\d+)\// | |
id = $1 | |
end | |
if build_no.to_s =~ /0\.0\.([0-9]+)/ | |
build = $1.to_i | |
end | |
if id && !prs.include?(build) | |
puts "Deleting build #{build_no} (#{id})" | |
delete_build(id) | |
else | |
puts "Keeping build #{build_no}" | |
end | |
end | |
puts "Page #{page_no} : DONE" | |
end | |
end | |
end | |
# get open pull requests | |
prs = [] | |
pr_txt = %x(curl -s -H "Authorization: Basic #{AUTH}" https://api.github.com/repos/#{REPO}/pulls\\?state\\=open\\&per_page\\=100) rescue nil | |
pr_json = JSON.parse(pr_txt) | |
pr_json.each do |pr| | |
prs << pr["number"].to_i if pr.fetch("number") | |
end | |
APPLICATIONS.each_pair do |application_code, pages_to_keep| | |
mayday = Mayday.new application_code, pages_to_keep | |
mayday.cleanup prs | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment