|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
1 | 3 | require 'json'
|
2 | 4 | require 'csv'
|
3 | 5 | require 'pry'
|
| 6 | +require 'optparse' |
4 | 7 |
|
5 | 8 | class GithubQuery
|
6 | 9 | attr_accessor :query, :result
|
@@ -210,3 +213,32 @@ def field_value_attributes
|
210 | 213 | end.select { |key, value| not key.nil? }
|
211 | 214 | end
|
212 | 215 | end
|
| 216 | + |
| 217 | +options = {} |
| 218 | +OptionParser.new do |opts| |
| 219 | + opts.banner = "Usage: ./github-project-to-csv.rb [options]" |
| 220 | + |
| 221 | + opts.on("--project=URL", "Url of the github project, e.g. https://github.com/users/fiedl/projects/2") do |url| |
| 222 | + options[:project_url] = url |
| 223 | + options[:org], options[:project_number] = url.scan(/https:\/\/github.com\/orgs\/([^\/]*)\/projects\/([^\/]*)/).flatten if url.include? "orgs/" |
| 224 | + options[:user], options[:project_number] = url.scan(/https:\/\/github.com\/users\/([^\/]*)\/projects\/([^\/]*)/).flatten if url.include? "users/" |
| 225 | + end |
| 226 | + |
| 227 | + opts.on("--output=FILENAME", "Name of the csv file to export the project to, e.g. project.csv") do |filename| |
| 228 | + options[:filename] = filename |
| 229 | + end |
| 230 | +end.parse! |
| 231 | + |
| 232 | +raise "Missing project url" unless options[:project_url] |
| 233 | +raise "Could not extract org or user from project url" unless options[:org] or options[:user] |
| 234 | +raise "Could not extract project number from project url" unless options[:project_number].to_i > 0 |
| 235 | + |
| 236 | +github_project = GithubProject.find_by(org: options[:org], user: options[:user], number: options[:project_number]) |
| 237 | + |
| 238 | +csv_content = github_project.to_csv |
| 239 | + |
| 240 | +if options[:filename] |
| 241 | + File.write options[:filename], csv_content |
| 242 | +else |
| 243 | + print csv_content + "\n" |
| 244 | +end |
0 commit comments