Created
September 5, 2013 21:20
-
-
Save rob-mcgrail/6456340 to your computer and use it in GitHub Desktop.
CWA eventum issues => github issues
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
| require 'rubygems' | |
| require 'httparty' | |
| require 'json' | |
| require 'csv-mapper' | |
| require 'trollop' | |
| $opts = Trollop::options do | |
| opt :username, "Github username", :default => 'USERNAME' | |
| opt :password, "Github password", :default => 'XXXXXXX' | |
| opt :repo, "Github repository name", :default => 'some-project' | |
| opt :org, "Github organisation", :default => 'cwa-lml' | |
| opt :agent, "User-Agent for API requests", :default => 'unique-user-agent' | |
| end | |
| Trollop::die "I need a path to a csv file as my first argument!" if ARGV.empty? | |
| class UserMap | |
| def self.map | |
| { | |
| 'Robert McGrail' => 'robomc' | |
| } | |
| end | |
| def self.for(assigned) | |
| users = assigned.split(', ') | |
| users.each do |user| | |
| if self.map[user] | |
| return self.map[user] | |
| end | |
| end | |
| nil | |
| end | |
| end | |
| # Parse CSV | |
| include CsvMapper | |
| file_path = ARGV[0] | |
| eventum_issues = import(file_path) do | |
| delimited_by "\t" | |
| parser_options :col_sep => "\t" | |
| read_attributes_from_file | |
| end | |
| # Create new issues from CSV data | |
| github_issues = [] | |
| eventum_issues.each do |row| | |
| issue = {} | |
| issue['title'] = row.summary | |
| issue['body'] = "Original issue at http://bugs.cwa.co.nz/view.php?id=#{row.issue_id}" | |
| user = UserMap.for(row.assigned) | |
| issue['assignee'] = user if user | |
| issue['state'] = row.status | |
| issue['labels'] = [] | |
| issue['labels'] << row.priority | |
| issue['labels'] << row.issue_type | |
| issue['labels'] << row.milestone | |
| github_issues << issue | |
| end | |
| class GitHub | |
| include HTTParty | |
| base_uri 'https://api.github.com' | |
| def initialize(u, p) | |
| @options = { | |
| :basic_auth => { :username => u, :password => p }, | |
| :headers => { "User-Agent" => $opts[:agent] } | |
| } | |
| @repo_path = "/repos/#{$opts[:org]}/#{$opts[:repo]}" | |
| end | |
| def create_issue(body) | |
| options = @options.merge({:body => body.to_json}) | |
| self.class.post("#{@repo_path}/issues", options) | |
| end | |
| def close_issue(id) | |
| options = @options.merge({:body => {:state => 'closed'}.to_json}) | |
| self.class.patch("#{@repo_path}/issues/#{id}", options) | |
| end | |
| def list_issues(opts={}) | |
| defaults = { | |
| :state => 'open', | |
| :page => 1 | |
| } | |
| page = opts[:page] || defaults[:page] | |
| state = opts[:state] || defaults[:state] | |
| options = @options | |
| self.class.get("#{@repo_path}/issues?state=#{state}&filter=all&page=#{page}", options) | |
| end | |
| end | |
| github = GitHub.new($opts[:username], $opts[:password]) | |
| github_issues.each do |issue| | |
| puts issue.to_json | |
| response = github.create_issue(issue) | |
| if issue['state'] == 'Closed' | |
| data = JSON.parse(response.body) | |
| number = data['number'] | |
| github.close_issue(number) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment