Skip to content

Instantly share code, notes, and snippets.

@pstaender
Last active August 29, 2015 14:22
Show Gist options
  • Save pstaender/c573d019387d43edffe0 to your computer and use it in GitHub Desktop.
Save pstaender/c573d019387d43edffe0 to your computer and use it in GitHub Desktop.
Gitracker - Keeps issues from github in your git repository uptodate (in the branch `issues`). Now you can read issues offline and keep track of them in git versioned manner
#!/usr/bin/env ruby
require "github_api"
require 'net/http'
require 'JSON'
require 'yaml'
require 'date'
module Gitracker
dateString = DateTime.now.strftime("%Y-%m-%d_%H:%M:%S")
oauthToken = '…'
repo = 'mongraph'
user = 'pstaender'
issueBranch = 'issues'
fileExtension = '.yml'
if ARGV.size == 0 || ARGV.include?('--help') || ARGV.include?('-h') || ARGV.include?('-v') || ARGV.include?('--version')
puts "gitrack v0.0.1alpha"
puts "(c) by philipp staender, 2015"
puts '
--download download open and closed issued
--help display this help
'.gsub(/\n\s+/,"\n")
exit(0)
end
@@github = Github.new oauth_token: oauthToken
def self.exec(command)
stdout = `#{command}`
return $?.success? ? stdout : false
end
def self.dataToYamlMarkdown(header, body, file = false)
yamlString = header.to_yaml.strip!+"\n--- >\n"+body
File.open(file, 'w') { |file| file.write(yamlString) } if file
end
def self.getUser(uname)
#http://www.rubydoc.info/github/peter-murach/github/master/Github/Client/Users
@@github.users.get(name: uname)
end
def self.getEmailByUsername(uname)
user = self.getUser(uname)
user['email']
end
def self.labelsToString(labelsObject)
labels = []
labelsObject.each {|label|
labels.push label['name']
}
return labels
end
if ARGV.include?('--download')
stashed = false
currentBranch = `git rev-parse --abbrev-ref HEAD`.strip!
# if currentBranch == issueBranch
# stashed = exec("git stash")
# elsif not exec("git status")
# # puts "stashing git status"
# # stashed = exec("git stash")
# puts 'please commit / stash your current state befor downloading issues'
# exit(1)
# end
if Gitracker::exec("git checkout -b #{issueBranch}")
puts "created and checkedout to branch '#{issueBranch}'"
else
Gitracker::exec("git checkout #{issueBranch}")
end
Gitracker::exec("mkdir -p open && mkdir -p closed")
puts "downloading issues…"
@@github.issues.list(user: user, repo: repo, state: 'all').each {|issue|
body = issue['body']
header = {}
header["title"] = issue['title']
header["url"] = issue['url']
header["github_id"] = issue['id']
header["user"] = Gitracker::getEmailByUsername(issue['user']['login'])
header["created_at"] = issue['created_at']
header["updated_at"] = issue['updated_at']
header["closed_at"] = issue['updated_at']
header["labels"] = self.labelsToString(issue['labels'])
header["locked"] = issue['locked']
status = issue['state']
number = issue['number']
targetFolder = "open/#{number}"
checkAgainstFolder = "closed/#{number}"
if status == 'closed'
targetFolder = checkAgainstFolder
checkAgainstFolder = "open/#{number}"
end
if File.directory?(checkAgainstFolder)
Gitracker::exec("git mv #{checkAgainstFolder} #{targetFolder}")
end
Gitracker::exec("mkdir -p #{targetFolder}")
file = "#{targetFolder}/issue#{fileExtension}"
Gitracker::dataToYamlMarkdown(header, body, file)
# comments?
commentsFolder = "#{targetFolder}/comments"
if issue.comments > 0
Gitracker::exec("mkdir -p #{commentsFolder}")
@@github.issues.comments.all user: user, repo: repo, number: issue['number'] { |comment|
number = comment['id']
header = {}
header['url'] = comment['url']
header['github_id'] = comment['id']
header["user"] = Gitracker::getEmailByUsername(comment['user']['login'])
header["created_at"] = comment['created_at']
header["updated_at"] = comment['updated_at']
body = comment['body']
file = "#{commentsFolder}/#{number}#{fileExtension}"
Gitracker::dataToYamlMarkdown(header, body, file)
}
end
}
Gitracker::exec("git add --all . && git commit -am 'updated issues from github (#{dateString})'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment