Created
September 7, 2011 10:57
-
-
Save polarblau/1200283 to your computer and use it in GitHub Desktop.
Find TODOs in your Rails project and create Github issues accordingly
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 'net/http' | |
require 'json' | |
# For the lazy like me who find themselves | |
# frequently adding lines like | |
# TODO: add documentation | |
# | |
# Find all these lines in the current project | |
# and create Github issues accordingly | |
# The issue will contain a link to the current branch | |
# with the appropriate line number as well as the content | |
# of the "TODO" | |
namespace :utilities do | |
desc "Find TODOs and submit them to github as issues" | |
task :todos, :needs => :environment do |t| | |
# what are we looking for | |
pattern, todos = /# TODO: (.+)/, [] | |
# which file types should be parsed? | |
extensions = "rb,coffee,js,haml,sass,scss,yml,yaml,markdown" | |
# github repo | |
repo = "your_repository" | |
# get the branch you're currently on for convenience | |
current_branch = `git status`.scan(/# On branch (.*)\n#/).flatten.first | |
# post to github | |
github_issues_url = "http://github.com/api/v2/json/issues/open/#{ENV['GITHUB_USER']}/#{repo}" | |
# parse files | |
Dir.glob(::Rails.root.to_s + "/**/*.{#{extensions}}").each do |file| | |
next unless File.file?(file) | |
begin | |
File.open(file) do |f| | |
line_number = 1 | |
f.each_line do |line| | |
found = line.scan(pattern) | |
unless found.empty? | |
todos << { | |
:file => file.gsub(Rails.root.to_s, ''), | |
:todo => found.flatten, | |
:number => line_number | |
} | |
end | |
line_number += 1 | |
end | |
end | |
rescue | |
puts "ERROR: #{file} couldn't be parsed" | |
end | |
end | |
uri = URI.parse(github_issues_url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
req = Net::HTTP::Post.new(uri.path) | |
# auth | |
req.basic_auth ENV['GITHUB_USER'], ENV['GITHUB_PASSWORD'] | |
todos.each do |todo| | |
link = "https://github.com/#{ENV['GITHUB_USER']}/#{repo}/blob/#{current_branch}#{todo[:file]}#L#{todo[:number]}" | |
params = { | |
:title => todo[:todo], | |
:body => "TODO found in `#{todo[:file]}` [in line #{todo[:number]}](#{link}):\n\n#{todo[:todo]}" | |
} | |
req.set_form_data params | |
res = http.request(req) | |
if res.code.to_i == 200 || res.code.to_i == 201 | |
reponse = JSON.parse(res.body) | |
puts "Issue ##{reponse['issue']['number']} created." | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment