Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move TODOs from code to GitHub
require 'github_api'
path = '.'
USER = 'user'
REPO = 'repo'
OAUTH_TOKEN = 'xxxx'
def files(path)
(`ag TODO #{path} -l`).split
end
def remove_todo(file)
`sed '/^.*TODO.*$/d' #{file} > temp && mv temp #{file}`
end
def commit(file, issue_number)
`git commit #{file} -m "Moved TODO to GitHub ##{issue_number}
https://github.com/#{USER}/#{REPO}/issues/#{issue_number}"`
end
class GitHubPath
class << self
def path(file)
path = "https://github.com/#{USER}/#{REPO}/blob/"
path << sha(file)
path << '/'
path << file
path << "#L"
path << line_number(file)
end
private
def sha(file)
(`git log -n 1 --pretty=format:%H -- #{file}`).chomp
end
def line_number(file)
`ag TODO #{file} | cut -d : -f 1`
end
end
end
class GitHubIssue
GITHUBAPI = Github.new :oauth_token => OAUTH_TOKEN
class << self
def create(file)
title = todo_text(file)
body = [
GitHubPath.path(file),
'```',
context(file),
'```'
].join("\n")
issue = GITHUBAPI.issues.create #{USER}, #{REPO}, title: title, body: body, labels: ['todo']
issue.number
end
private
def todo_text(file)
raw = `ag TODO #{file}`
raw.gsub(/.*TODO ?:?(.*)(-->|%>)/, '\1').strip
end
def context(file)
`ag TODO #{file} -C3`
end
end
end
files(path).each do |f|
puts "#{f}"
issue_number = GitHubIssue.create(f)
remove_todo(f)
commit(f, issue_number)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment