Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Last active August 29, 2015 14:19
Show Gist options
  • Save fancyremarker/70f52d2b4c6f7c3e330d to your computer and use it in GitHub Desktop.
Save fancyremarker/70f52d2b4c6f7c3e330d to your computer and use it in GitHub Desktop.
[:octopus:] Sync Waffle sources with all repos for an org
#!/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