Skip to content

Commit 543f016

Browse files
committed
adding simple cli with optparse
1 parent 85c810a commit 543f016

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.csv

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# github-project-to-csv
2-
Simple cli to export github v2 project to csv
2+
3+
Simple cli to export github v2 projects to csv
4+
5+
## Usage
6+
7+
```shell
8+
./github-project-to-csv.rb --project https://github.com/users/fiedl/projects/2 --output project.csv
9+
```

github-project-to-csv.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#!/usr/bin/env ruby
2+
13
require 'json'
24
require 'csv'
35
require 'pry'
6+
require 'optparse'
47

58
class GithubQuery
69
attr_accessor :query, :result
@@ -210,3 +213,32 @@ def field_value_attributes
210213
end.select { |key, value| not key.nil? }
211214
end
212215
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

Comments
 (0)