-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgithub-project-to-csv.rb
executable file
·363 lines (325 loc) · 10.1 KB
/
github-project-to-csv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env ruby
if RUBY_VERSION < '3.2'
print "\n"
print "This script requires ruby version 3.2 or higher.\n\n"
print "Please install a newer ruby version or run the script via docker:\n"
print "See: https://github.com/fiedl/github-project-to-csv\n\n"
raise "This script requires ruby version 3.2 or higher"
end
begin
require 'json'
require 'csv'
require 'pry'
require 'optparse'
require 'httparty'
rescue LoadError => exception
print "\n"
print "This script requires gems to be installed. Please run:\n\n"
print " gem install pry httparty\n\n"
raise exception
end
class GithubQuery
attr_accessor :query, :result
class << self
attr_accessor :personal_access_token
end
def self.execute(query)
github_query = self.new
github_query.query = query
github_query.execute
github_query
end
def execute
if GithubQuery.personal_access_token
execute_with_https
else
execute_with_gh_client
end
end
def execute_with_gh_client
command = "gh api graphql -f query='#{query}' 2>&1"
@result = `#{command}`
raise "gh command line client not installed. https://cli.github.com/. install with 'brew install gh'" if @result.include? "gh: command not found"
if @result.include? "gh auth login"
system "gh auth login --scopes \"project\""
@result = `#{command}`
end
@result = JSON.parse(@result)
end
def execute_with_https
url = "https://api.github.com/graphql"
body = {query: query}.to_json
headers = {"Authorization" => "Bearer #{GithubQuery.personal_access_token}"}
@result = HTTParty.post(url, body:, headers:)
raise @result["message"] if @result["message"]
end
end
class GithubProject < GithubQuery
def self.find_by(org: nil, user: nil, number:)
if org
execute("
query{
organization(login: \"#{org}\"){
projectV2(number: #{number}) {
id
}
}
}
")
elsif user
execute("
query{
user(login: \"#{user}\"){
projectV2(number: #{number}) {
id
}
}
}
")
else
raise "Neither user nor org given"
end
end
def id
result.dig("data", "organization", "projectV2", "id") || result.dig("data", "user", "projectV2", "id")
end
def items
GithubProjectItemCollection.find_by(project_id: id).items
end
def to_csv
headers = items.collect(&:attributes).collect(&:keys).flatten.uniq
rows = items.collect do |github_project_item|
headers.collect { |h| github_project_item.attributes[h] }
end
CSV.generate(col_sep: ";") do |csv|
csv << headers
rows.each do |row|
csv << row
end
end
end
end
class GithubProjectItemCollection < GithubQuery
PAGINATION_BATCH_SIZE = 100
def self.find_by(project_id:)
execute_paginated("
query{
node(id: \"#{project_id}\") {
... on ProjectV2 {
items(first: ALL) {
pageInfo {
endCursor
hasNextPage
}
nodes {
id
content{
... on DraftIssue {
title
body
}
...on Issue {
title
body
number
url
labels(first: 20) {
nodes {
name
}
}
}
...on PullRequest {
title
body
number
url
labels(first: 20) {
nodes {
name
}
}
}
}
fieldValues(first: 50) {
nodes {
... on ProjectV2ItemFieldTextValue {
text
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldNumberValue {
number
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldDateValue {
date
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldIterationValue {
title
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldSingleSelectValue {
name
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldMilestoneValue {
milestone {
title
}
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldRepositoryValue {
repository {
name
}
field {
... on ProjectV2FieldCommon {
name
}
}
}
... on ProjectV2ItemFieldUserValue {
users(first: 10) {
nodes {
login
}
}
field {
... on ProjectV2FieldCommon {
name
}
}
}
}
}
}
}
}
}
}
")
end
def self.execute_paginated(query)
github_project_item_collection = nil
all_nodes = []
pagination_string = "first: #{PAGINATION_BATCH_SIZE}"
loop do
github_project_item_collection = page = execute(query.gsub("first: ALL", pagination_string))
all_nodes += page.result.dig("data", "node", "items", "nodes")
if page.result.dig("data", "node", "items", "pageInfo", "hasNextPage")
last_cursor_id = page.result.dig("data", "node", "items", "pageInfo", "endCursor")
pagination_string = "first: #{PAGINATION_BATCH_SIZE}, after: \"#{last_cursor_id}\""
else
break
end
end
github_project_item_collection.result["data"]["node"]["items"]["nodes"] = all_nodes
github_project_item_collection
end
def items
result.dig("data", "node", "items", "nodes").collect do |node_data|
github_project_item = GithubProjectItem.new
github_project_item.result = node_data
github_project_item
end
end
end
class GithubProjectItem < GithubQuery
def id
result.dig("id")
end
def number
"##{result.dig("content", "number")}" if result.dig("content", "number")
end
def url
result.dig("content", "url")
end
def title
[number, field_value_attributes["Title"]].join(" ")
end
def body
result.dig("content", "body")
end
def labels
result.dig("content", "labels", "nodes")&.collect { |label_data| label_data["name"] }&.join(", ")
end
def attributes
direct_attributes.merge(field_value_attributes)
end
def direct_attributes
{
id:,
number:,
title:,
body:,
url:,
labels:
}
end
def field_value_attributes
result.dig("fieldValues", "nodes").to_h do |field_value_data|
key = field_value_data.dig("field", "name")
value = \
field_value_data.dig("text") || \
field_value_data.dig("number") || \
field_value_data.dig("title") || \
field_value_data.dig("name") || \
field_value_data.dig("date") || \
field_value_data.dig("milestone", "title") || \
field_value_data.dig("repository", "name") || \
field_value_data.dig("users", "nodes", 0, "login")
[key, value]
end.select { |key, value| not key.nil? }
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ./github-project-to-csv.rb [options]"
opts.on("--project=URL", "Url of the github project, e.g. https://github.com/users/fiedl/projects/2") do |url|
options[:project_url] = url
options[:org], options[:project_number] = url.scan(/https:\/\/github.com\/orgs\/([^\/]*)\/projects\/([^\/]*)/).flatten if url.include? "orgs/"
options[:user], options[:project_number] = url.scan(/https:\/\/github.com\/users\/([^\/]*)\/projects\/([^\/]*)/).flatten if url.include? "users/"
end
opts.on("--output=FILENAME", "Name of the csv file to export the project to, e.g. project.csv") do |filename|
options[:filename] = filename
end
opts.on("--personal-access-token=TOKEN", "--token=TOKEN", "Personal access token for github, https://github.com/settings/tokens?type=beta") do |token|
options[:github_personal_access_token] = token
end
end.parse!
raise "Missing project url" unless options[:project_url]
raise "Could not extract org or user from project url" unless options[:org] or options[:user]
raise "Could not extract project number from project url" unless options[:project_number].to_i > 0
GithubQuery.personal_access_token = options[:github_personal_access_token]
github_project = GithubProject.find_by(org: options[:org], user: options[:user], number: options[:project_number])
csv_content = github_project.to_csv
if options[:filename]
File.write options[:filename], csv_content
else
print csv_content + "\n"
end