export-pull-requests 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/epr +159 -0
  3. metadata +88 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7bcd3a99bc36a8a9c42ae375bd29113f4b1de1f7
4
+ data.tar.gz: ea1fc9e512b536b7c5f6c7d553944d7b44783271
5
+ SHA512:
6
+ metadata.gz: 56a71220ae9b7492d8ed4fa41f79a40e3d2eea3e2883b5e59d8efb9d3a8bce89820eaa5fa799e11f465a2417ddfdb265f4401335ed28203060f8f9dfe83a31c8
7
+ data.tar.gz: 793e9283143b22969f9663f355036c0d8c8da497181ddb520f4e445ec1a13571e16b9d0d675ab34404c986c91e407ef76c34ff15ab00cd7838e9b4dd74f92a16
data/bin/epr ADDED
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "csv"
4
+ require "optparse"
5
+ require "time"
6
+ require "github_api"
7
+ require "gitlab"
8
+
9
+ VERSION = "0.1.0"
10
+
11
+ def localtime(t)
12
+ Time.parse(t).localtime.strftime("%x %X")
13
+ end
14
+
15
+ def parse_repos(repos)
16
+ repos.map do |r|
17
+ abort "invalid repository #{r}" unless r =~ %r{\A(\S+)/(\S+)\z}
18
+ [ $1, $2 ]
19
+ end
20
+ end
21
+
22
+ def skip_user?(user)
23
+ $exclude_users.include?(user) || $include_users.any? && !$include_users.include?(user)
24
+ end
25
+
26
+ def lookup_token
27
+ return ENV["EPR_TOKEN"] unless ENV["EPR_TOKEN"].to_s.strip.empty?
28
+
29
+ begin
30
+ %w[epr.token github.oauth-token].each do |setting|
31
+ token = `git config #{setting}`.chomp
32
+ return token unless token.empty?
33
+ end
34
+ rescue Errno::ENOENT
35
+ # git not found, ignore
36
+ end
37
+ end
38
+
39
+ def github(user, repo)
40
+ rows = []
41
+
42
+ $gh ||= Github.new(:oauth_token => $token, :auto_pagination => true)
43
+ $gh.pull_requests.list(:user => user, :repo => repo, :state => $filter).each_page do |page|
44
+ next if page.size.zero? # Needed for auto_pagination
45
+
46
+ page.each do |pr|
47
+ next if skip_user?(pr.user.login)
48
+
49
+ rows << [
50
+ "#{user}/#{repo}",
51
+ pr.number,
52
+ pr.user.login,
53
+ pr.title,
54
+ pr.state,
55
+ localtime(pr.created_at),
56
+ localtime(pr.updated_at),
57
+ pr.html_url,
58
+ ]
59
+ end
60
+ end
61
+
62
+ rows
63
+ end
64
+
65
+ def gitlab(user, repo)
66
+ rows = []
67
+
68
+ # Do we care about this differing in output?
69
+ state = $filter == "open" ? "opened" : $filter
70
+
71
+ # TODO: custom endpoint
72
+ $gitlab ||= Gitlab.client(:token => $token, :endpoint => "https://gitlab.com/api/v4")
73
+ $gitlab.merge_requests("#{user}/#{repo}", :state => state).auto_paginate do |mr|
74
+ next if skip_user?(mr.author.username)
75
+
76
+ rows << [
77
+ "#{user}/#{repo}",
78
+ # Yes, it's called iid
79
+ mr.iid,
80
+ mr.author.username,
81
+ mr.title,
82
+ mr.state,
83
+ localtime(mr.created_at),
84
+ localtime(mr.updated_at),
85
+ mr.web_url
86
+ ]
87
+ end
88
+
89
+ rows
90
+ end
91
+
92
+ def export_repos(repos)
93
+ rows = []
94
+ rows << %w[Repository # User Title State Created Updated URL]
95
+
96
+ repos.each do |user, repo|
97
+ case $provider
98
+ when "github"
99
+ rows.concat(github(user, repo))
100
+ when "gitlab"
101
+ rows.concat(gitlab(user, repo))
102
+ else
103
+ abort "unknown service provider: #{service}"
104
+ end
105
+
106
+ rows.each do |r|
107
+ # Repository column is dropped if we only have one repo
108
+ r.shift unless repos.size > 1
109
+ puts r.to_csv
110
+ end
111
+ rows.clear
112
+ end
113
+ end
114
+
115
+ $exclude_users = []
116
+ $include_users = []
117
+ $filter = "open"
118
+ $provider = "github"
119
+ $token = lookup_token
120
+
121
+ parser = OptionParser.new do |opts|
122
+ opts.banner = "usage: #{File.basename($0)} [-hv] [-s state] [-t token] [-c user1,user2...] user/repo1 [user/repo2...]"
123
+
124
+ opts.on "-c", "--creator=user1,user2,...", Array, "Export PRs created by given username(s); prepend `!' to exclude user" do |u|
125
+ $exclude_users, $include_users = u.partition { |name| name.start_with?("!") }
126
+ $exclude_users.map! { |name| name[1..-1] } # remove "!"
127
+ end
128
+
129
+ opts.on "-h", "--help", "Show this message" do
130
+ puts opts
131
+ exit
132
+ end
133
+
134
+ opts.on "-t", "--token=TOKEN", "API token" do |t|
135
+ $token = t
136
+ end
137
+
138
+ opts.on "-p, --provider=NAME", %w[github gitlab], "Service provider: gitlab or github; defaults to github" do |name|
139
+ $provider = name
140
+ end
141
+
142
+ opts.on "-s", "--state=STATE", %w[open closed all merged], "Export PRs in the given state, defaults to open" do |f|
143
+ $filter = f
144
+ end
145
+
146
+ opts.on "-v", "--version", "epr version" do
147
+ puts "v#{VERSION} (GitHub API v#{Github::VERSION}, GitLab API v#{Gitlab::VERSION})"
148
+ exit
149
+ end
150
+ end
151
+
152
+ parser.parse!
153
+ abort parser.banner if ARGV.empty?
154
+
155
+ begin
156
+ export_repos(parse_repos(ARGV))
157
+ rescue => e
158
+ abort "Export failed: #{e}"
159
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: export-pull-requests
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: github_api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gitlab
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2
55
+ description: Program to export GitHub pull requests or GitLab merge requests to CSV
56
+ a file.
57
+ email: skye.shaw@gmail.com
58
+ executables:
59
+ - epr
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - bin/epr
64
+ homepage: https://github.com/sshaw/export-pull-requests
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Export pull requests to a CSV file.
88
+ test_files: []