Last active
August 29, 2015 14:00
-
-
Save akosednar/11073387 to your computer and use it in GitHub Desktop.
Contact-Congress Form Test & Issue Sync
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
# | |
# Name: Contact-Congress Form Test & Issue Sync | |
# Description: Keeps the form test status and issue states for [Open Congress](http://theunitedstates.io/contact-congress/) in sync | |
# Author: Anthony Kosednar (@akosednar) | |
# License: [CC0 1.0 Universal (C0 1.0) Public Domain Dedication](http://creativecommons.org/publicdomain/zero/1.0/) | |
# | |
# Usage Notes: | |
# - Required Gem: github_api gem | |
# - Required Actions: Setup two ENV variables: GITHUB_USERNAME , and GITHUB_PERSONAL_ACCESS_TOKEN (Tokens can be created here: https://github.com/settings/applications) | |
# | |
require 'open-uri' | |
require 'json' | |
require 'colorize' | |
require 'github_api' | |
########### | |
# CONFIG | |
########### | |
## | |
# NOTE: You need to set two ENV variables: GITHUB_USERNAME, and GITHUB_TOKEN | |
## | |
# The url to load form and congress member data from | |
DATA_ENDPOINT = 'http://ec2-54-215-28-56.us-west-1.compute.amazonaws.com:3000' | |
# Ignore open issues that have succeeding tests | |
IGNORE_OPENS = false | |
########### | |
# METHODS | |
########### | |
# Gets a hash of congress members from endpoint | |
def get_congress_members | |
url = DATA_ENDPOINT+ '/list-congress-members' | |
buffer = open(url, "UserAgent" => "Contact-Congress Form Test & Issue Sync").read | |
results = JSON.parse(buffer) | |
return results | |
end | |
# Gets a hash of all congress members form statuses from endpoint (format: {bio_id=>status} ) | |
def get_all_form_status(congress_members) | |
form_statuses = Hash.new | |
congress_members.each do |member| | |
form_statuses[member['bioguide_id']] = get_single_form_status(member['bioguide_id']) | |
end | |
return form_statuses | |
end | |
# Returns the status of the last form test | |
def get_single_form_status(bio_id) | |
url = DATA_ENDPOINT + '/recent-statuses-detailed/'+bio_id | |
buffer = open(url, "UserAgent" => "Contact-Congress Form Test & Issue Sync").read | |
results = JSON.parse(buffer) | |
results = results[0]["status"] | |
return results | |
end | |
# Updates Github issue statuses | |
def update_all_issue_states(congress_form_statuses) | |
github = Github.new basic_auth: login: ENV['GITHUB_USERNAME'], password: ENV['GITHUB_PERSONAL_ACCESS_TOKEN'], per_page: 100, auto_pagination: true | |
# check open issues | |
issues = github.issues.list user: 'unitedstates', repo: 'contact-congress', state: 'open' | |
issues.each do |issue| | |
# Detect if we have an issue in the congress member format (ie: [BIO_ID]) | |
matches = issue['title'].scan(/\[(.*?)\]/) | |
matches.each do |match| | |
match = match[0] # matches return as [[Match]] so we have to do this to correct | |
if congress_form_statuses.has_key?(match) | |
# Detect the current github issue state vs the tests status and open/close accordingly | |
if IGNORE_OPENS == false && issue['state'] == "open" && congress_form_statuses[match] == "success" | |
puts "* Setting issue state for #{match}".colorize(:color => :white, :background => :red) | |
github.issues.edit user: 'unitedstates', repo: 'contact-congress', number: issue['number'].to_s, state: 'closed' | |
github.issues.comments.create 'unitedstates', 'contact-congress', issue['number'].to_s, body: 'Issue Closed: Tests Succeeding (AUTOMATED CHECK)' | |
elsif issue['state'] == "closed" && congress_form_statuses[match] == "failed" | |
puts "* Setting issue state for #{match}".colorize(:color => :white, :background => :red) | |
github.issues.edit user: 'unitedstates', repo: 'contact-congress', number: issue['number'].to_s, state: 'open' | |
github.issues.comments.create 'unitedstates', 'contact-congress', issue['number'].to_s, body: 'Issue Opened: Tests Failing (AUTOMATED CHECK)' | |
else | |
puts "* #{match} - OK".colorize(:color => :white, :background => :green) | |
end | |
else | |
# It looks like an issue was detected to have the [BIO_ID] format but no hash key for it | |
puts "- Note: #{issue['title']} does not match up to any congress members".colorize(:color => :black, :background => :white) | |
end | |
end | |
end | |
# check closed issues | |
issues = github.issues.list user: 'unitedstates', repo: 'contact-congress', state: 'closed' | |
issues.each do |issue| | |
# Detect if we have an issue in the congress member format (ie: [BIO_ID]) | |
matches = issue['title'].scan(/\[(.*?)\]/) | |
matches.each do |match| | |
match = match[0] # matches return as [[Match]] so we have to do this to correct | |
if congress_form_statuses.has_key?(match) | |
# Detect the current github issue state vs the tests status and open/close accordingly | |
if IGNORE_OPENS == false && issue['state'] == "open" && congress_form_statuses[match] == "success" | |
puts "* Setting issue state for #{match}".colorize(:color => :white, :background => :red) | |
github.issues.edit user: 'unitedstates', repo: 'contact-congress', number: issue['number'].to_s, state: 'closed' | |
github.issues.comments.create 'unitedstates', 'contact-congress', issue['number'].to_s, body: 'Issue Closed: Tests Succeeding (AUTOMATED CHECK)' | |
elsif issue['state'] == "closed" && congress_form_statuses[match] == "failed" | |
puts "* Setting issue state for #{match}".colorize(:color => :white, :background => :red) | |
github.issues.edit user: 'unitedstates', repo: 'contact-congress', number: issue['number'].to_s, state: 'open' | |
github.issues.comments.create 'unitedstates', 'contact-congress', issue['number'].to_s, body: 'Issue Opened: Tests Failing (AUTOMATED CHECK)' | |
else | |
puts "* #{match} - OK".colorize(:color => :white, :background => :green) | |
end | |
else | |
# It looks like an issue was detected to have the [BIO_ID] format but no hash key for it | |
puts "- Note: #{issue['title']} does not match up to any congress members".colorize(:color => :black, :background => :white) | |
end | |
end | |
end | |
end | |
########### | |
# MAIN | |
########### | |
# Get Congress Member Info | |
puts "* Pulling Congress Members List".colorize(:color => :white, :background => :blue) | |
congress_members = get_congress_members | |
puts "* Finished Pulling Congress Members List".colorize(:color => :white, :background => :blue) | |
# Get Congress Form Statuses | |
puts "* Pulling Form Test Status Data".colorize(:color => :white, :background => :blue) | |
congress_form_statuses = get_all_form_status(congress_members) | |
puts "* Finished Pulling Form Test Status Data".colorize(:color => :white, :background => :blue) | |
# Update Github Issue Statuses | |
puts "* Checking Issue Statuses on Github".colorize(:color => :white, :background => :blue) | |
update_all_issue_states(congress_form_statuses) | |
puts "* Finished Issue Statuses on Github".colorize(:color => :white, :background => :blue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment