Last active
August 29, 2015 14:19
-
-
Save fancyremarker/70f52d2b4c6f7c3e330d to your computer and use it in GitHub Desktop.
[:octopus:] Sync Waffle sources with all repos for an org
This file contains hidden or 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 | |
# ruby sync-waffle.rb aptible aptible/support | |
require 'octokit' | |
require 'faraday' | |
require 'json' | |
org_login = ARGV[0] | |
waffle_project = ARGV[1] | |
print 'GitHub Username: ' | |
username = $stdin.gets.chomp | |
print 'GitHub Password: ' | |
password = $stdin.gets.chomp | |
Octokit.configure do |c| | |
c.login = username | |
c.password = password | |
end | |
print 'Waffle express:sess cookie: ' | |
session_cookie = $stdin.gets.chomp | |
print 'Waffle express:sess.sig cookie: ' | |
session_sig_cookie = $stdin.gets.chomp | |
# Fetch internal Waffle ID for repo | |
# (Also, fetch GitHub Provider ID) | |
conn = Faraday.new(url: "https://waffle.io/api/#{waffle_project}") | |
response = conn.get do |req| | |
req.headers['Cookie'] = "express:sess=#{session_cookie}; " \ | |
"express:sess.sig=#{session_sig_cookie}; " | |
end | |
json = JSON.parse(response.body) | |
waffle_project_id = json['_id'] | |
github_provider_id = json['sources'][0]['provider']['_id'] | |
linked_projects = json['sources'].map { |el| el['repoPath'] } | |
Octokit.auto_paginate = true | |
Octokit.org_repos(org_login).each do |repo| | |
if repo.private? | |
puts "Skipping #{repo.full_name}, private repo..." | |
elsif linked_projects.include?(repo.full_name) | |
puts "Skipping #{repo.full_name}, already linked..." | |
elsif !repo.has_issues? | |
puts "Skipping #{repo.full_name}, issues disabled..." | |
else | |
puts "Linking #{repo.full_name}..." | |
url = "https://waffle.io/api/projects/#{waffle_project_id}/sources" | |
conn = Faraday.new(url: url) | |
conn.post do |req| | |
req.headers['Cookie'] = "express:sess=#{session_cookie}; " \ | |
"express:sess.sig=#{session_sig_cookie}; " | |
req.headers['Content-Type'] = 'application/json; charset=UTF-8' | |
req.body = { | |
'private' => repo.private?, | |
'repoPath' => repo.full_name, | |
'type' => 'github', | |
'provider' => github_provider_id | |
}.to_json | |
end | |
# Sleep to avoid hitting rate limit | |
sleep 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment