Created
February 23, 2017 18:48
-
-
Save camertron/6f2de68b97dd1635fba6980746d5d6ee to your computer and use it in GitHub Desktop.
Txgh validation script
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
#! /usr/bin/env ruby | |
ENV['TXGH_CONFIG'] = "file://#{File.expand_path('../../config/txgh-config.yml', __FILE__)}" | |
require 'bundler' | |
Bundler.setup | |
require 'txgh' | |
config = begin | |
Txgh::Config::KeyManager.config_from_project(ARGV[0]) | |
rescue Txgh::ProjectConfigNotFoundError | |
begin | |
Txgh::Config::KeyManager.config_from_repo(ARGV[0]) | |
rescue Txgh::RepoConfigNotFoundError | |
puts "Couldn't find config for '#{ARGV[0]}' in #{ENV['TXGH_CONFIG']}" | |
puts "Found these instead:" | |
Txgh::Config::KeyManager.project_names.each_with_index do |project_name, idx| | |
config = Txgh::Config::KeyManager.config_from_project(project_name) | |
puts "#{idx + 1}) #{project_name} (#{config.github_repo.name})" | |
end | |
exit 1 | |
end | |
end | |
github_api = config.github_repo.api | |
transifex_api = config.transifex_project.api | |
transifex_api.connection.builder.delete(Faraday::Response::Logger) # be quiet | |
def handle_github_error | |
yield; puts 'ok' | |
rescue Octokit::Unauthorized | |
puts 'Github authorization failed' | |
rescue Octokit::Error => e | |
puts e.message | |
rescue Exception => e | |
puts e.message | |
end | |
def handle_transifex_error | |
yield; puts 'ok' | |
rescue Txgh::TransifexUnauthorizedError | |
puts 'Transifex authorization failed' | |
rescue Txgh::TransifexApiError => e | |
puts e.message | |
rescue Exception => e | |
puts e.message | |
end | |
def handle_config_error | |
yield; puts 'ok' | |
rescue Txgh::ConfigNotFoundError | |
puts "Configuration could not be found in #{config.transifex_project.tx_config_uri}" | |
rescue Txgh::TxghError => e | |
puts e.message | |
rescue Exception => e | |
puts e.message | |
end | |
# check github access | |
STDOUT.write("Checking Github access... ") | |
handle_github_error do | |
github_api.client.rate_limit | |
end | |
# check repo exists | |
STDOUT.write("Checking txgh-user has write access... ") | |
handle_github_error do | |
repo_data = github_api.client.repo(config.github_repo.name) | |
unless repo_data[:permissions][:push] | |
raise StandardError, 'No write access' | |
end | |
end | |
# check transifex access | |
STDOUT.write("Checking Transifex access... ") | |
handle_transifex_error do | |
transifex_api.get_formats | |
end | |
# check project exists | |
STDOUT.write("Checking that the Transifex project exists... ") | |
handle_transifex_error do | |
transifex_api.get_project(config.transifex_project.name) | |
end | |
# check for tx config | |
STDOUT.write("Checking tx config... ") | |
tx_config = nil | |
handle_config_error do | |
tx_config = Txgh::Config::TxManager.tx_config( | |
config.transifex_project, config.github_repo, 'heads/master' | |
) | |
end | |
if tx_config | |
STDOUT.write('Examining configured resources... ') | |
if tx_config.resources.size == 0 | |
puts 'no resources found' | |
puts 'Make sure your tx config contains at least one resource' | |
exit 1 | |
else | |
puts 'ok' | |
end | |
# check for master resources | |
puts "Checking master resources" | |
tx_config.resources.each do |tx_resource| | |
branch_resource = Txgh::TxBranchResource.new(tx_resource, 'heads/master') | |
STDOUT.write(" -> #{branch_resource.resource_slug}... ") | |
handle_transifex_error do | |
transifex_api.get_resource( | |
branch_resource.project_slug, branch_resource.resource_slug | |
) | |
end | |
end | |
else | |
puts "** Can't check master resources, tx config couldn't be loaded **" | |
end | |
puts "\nAs a robot, I can only get you this far. Don't forget to:\n\n" | |
puts "1) Make sure txgh-user has push access to master" | |
puts " https://github.com/#{config.github_repo.name}/settings/branches/master" | |
puts '' | |
puts "2) Set up Github and Transifex webhooks using the correct secrets" | |
puts " https://github.com/#{config.github_repo.name}/settings/hooks (secret: #{config.github_repo.webhook_secret})" | |
puts " https://transifex.com/#{config.transifex_project.organization}/#{config.transifex_project.name}/settings/features/ (secret: #{config.transifex_project.webhook_secret})" | |
puts "\nOver and out." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment