gitsheet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f8d5e460c2b28208e31e32355ead94f9403a30d
4
+ data.tar.gz: 826011497a63d519d40f38da56a0871c1fdbddc8
5
+ SHA512:
6
+ metadata.gz: 3066c0369ecdc76667cc2c31efe5788d0439c631deb5159a0215b1e182c85b15441bb7af7c1712c6b494f6646515029d2cb593f12d9700b2bccac8833270c835
7
+ data.tar.gz: 0a1999ca57f59fc9b38549f2700db8086196899382f97f138724d16f212a2cfff639d40dcf023edf68af0b085ac253c2137baf1222762c5c9f8efc58f25d4593
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gitsheet.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hbbb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,109 @@
1
+ # Gitsheet
2
+
3
+ Gitsheet is a command line utility designed to fetch/parse/format GitHub API data.
4
+ Primary use cases include analytics and report-generating.
5
+
6
+ Gitsheet requests are authenticated using your Github credentials. As such, the results of all queries are limited to the permissions
7
+ of your Github account.
8
+
9
+ ## Installation
10
+
11
+ Clone the repository:
12
+
13
+ ```bash
14
+ $ git clone ssh@github.com/vervewireless/gitsheet.git
15
+ $ cd gitsheet/
16
+ ```
17
+
18
+ Mac OSX Users should have a system version of Ruby installed:
19
+
20
+ ```bash
21
+ $ ruby -v
22
+ ```
23
+
24
+ Install Bundler:
25
+
26
+ ```bash
27
+ $ gem install bundler
28
+ ```
29
+
30
+ Install Gitsheet dependencies:
31
+
32
+ ```bash
33
+ $ bundle install
34
+ ```
35
+
36
+ ## Configuration
37
+
38
+ Place `bin/gitsheet` in your `$PATH` to be able to run the command globally:
39
+
40
+ ```bash
41
+ $ cp gitsheet/bin/gitsheet usr/bin
42
+ ```
43
+
44
+ It is helpful to create aliases in your `.bashrc` file for common operations:
45
+
46
+ ```bash
47
+ # .bashrc
48
+ alias report_git_repos='gitsheet repos -u maiyln -o vervewireless'
49
+ alias report_git_users='gitsheet users -u maiyln -o vervewireless'
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Synopsis:
55
+
56
+ ```bash
57
+ $ gitsheet [type] -u [username] -p [password] -o [organization] -f [format]
58
+ ```
59
+ Arguments:
60
+
61
+ `type` is the data type to fetch. Can be either 'repos' or 'users'.
62
+
63
+ `-o` the GitHub organization to search within eg: 'vervewireless'
64
+
65
+ ## Output
66
+
67
+ By default, gitsheet outputs raw csv data to stdout. This is useful because you can pass the output to various shell commands.
68
+
69
+ Write to a file:
70
+
71
+ ```bash
72
+ $ gitsheet ... > report.csv
73
+ ```
74
+
75
+ Keyword search through results:
76
+
77
+ ```bash
78
+ $ gitsheet ... | grep [keyword]
79
+ ```
80
+
81
+
82
+ ## Examples
83
+
84
+ Get all repositories in an organization:
85
+
86
+ ```bash
87
+ $ gitsheet repos -u testuser -p secret -o vervewireless
88
+ ```
89
+
90
+ Get all members of an organization:
91
+
92
+ ```bash
93
+ $ gitsheet users -u testuser -p secret -o vervewireless
94
+ ```
95
+
96
+ Get all repositories and write output to a .csv file:
97
+
98
+ ```bash
99
+ $ gitsheet repos -u testuser -p secret -o vervewireless -f csv > report.csv
100
+ ```
101
+
102
+ ## TODO
103
+
104
+ Here are the next steps for Gitsheet
105
+ - Add unit tests
106
+ - Leverage Ruby gem to handle command line argument parsing, control flow etc.
107
+ - Better error handling
108
+ - More informative error messages
109
+ - A much better name!
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gitsheet"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'io/console'
4
+ require 'gitsheet'
5
+ require 'formatter'
6
+
7
+ # Print help text
8
+ if ARGV[0] == '--help' or ARGV[0] == '-h'
9
+ help_text = %q(usage: gitsheet <command> [<args>]
10
+
11
+ Some gitsheet commands are:
12
+ repos fetch all Github repositories within an organization
13
+ users fetch all Github users belonging to an organization
14
+
15
+ Examples
16
+ gitsheet repos -u hbbb -o vervewireless -f csv
17
+ gitsheet users -u hbbb -o vervewireless -f csv
18
+ )
19
+
20
+ $stdout.puts help_text
21
+ Process.exit!(true)
22
+ end
23
+
24
+ # Prompt for password
25
+ $stderr.print 'Password: '
26
+ $stderr.flush
27
+ password = $stdin.noecho(&:gets).chomp
28
+ $stderr.puts ''
29
+
30
+ # Parse arguments
31
+ report_type = ARGV.shift
32
+ arguments = Hash[*ARGV]
33
+
34
+ # Validate input
35
+ if !arguments['-u'] || !password || !arguments['-o'] || !arguments['-f']
36
+ puts 'Must provide username, password, organization and format'
37
+ Process.exit!(false)
38
+ end
39
+
40
+
41
+ # Initialize GitHub connection
42
+ client = Gitsheet.new(arguments['-u'], password, arguments['-o'])
43
+
44
+ # Request Github
45
+ begin
46
+ github_results = client.send(report_type)
47
+ rescue => e
48
+ $stderr.puts "GitHub communication error: #{e.message}"
49
+ Process.exit!(false)
50
+ end
51
+
52
+ # Format Results
53
+ begin
54
+ formatted_results = Formatter.format(github_results, arguments['-f'])
55
+ rescue => e
56
+ $stderr.puts "A formatting error occured: #{e.message}"
57
+ Process.exit!(false)
58
+ end
59
+
60
+ $stdout.puts formatted_results
61
+ Process.exit!(true)
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gitsheet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gitsheet"
8
+ spec.version = Gitsheet::VERSION
9
+ spec.authors = ["Hbbb"]
10
+ spec.email = ["h.borges10592@gmail.com"]
11
+
12
+ spec.summary = %q{Github API command line tool.}
13
+ spec.description = %q{Gitsheet is a small command line utility to quickly gather user and repository data from Github's API.}
14
+ spec.homepage = "https://github.com/Hbbb/gitx"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "bin"
27
+ spec.executables = ["gitsheet"]
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "rest-client"
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "rspec-mocks"
35
+ spec.add_development_dependency "webmock"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "vcr"
38
+ spec.add_development_dependency "pry-byebug"
39
+ end
@@ -0,0 +1,31 @@
1
+ require 'csv'
2
+ require 'pp'
3
+ require 'json'
4
+
5
+
6
+ module Formatter
7
+ def self.format(data, type = 'csv')
8
+ if !['csv', 'json', 'pretty_print'].include?(type)
9
+ raise 'Formatter: invalid format'
10
+ end
11
+
12
+ self.send(type.to_sym, data)
13
+ end
14
+
15
+ private
16
+
17
+ def self.csv(data)
18
+ CSV.generate do |csv|
19
+ csv << data.first.to_h.keys
20
+
21
+ data.each do |d|
22
+ csv << d.to_h.values
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.json(data)
28
+ JSON.generate(data)
29
+ end
30
+
31
+ end
@@ -0,0 +1,82 @@
1
+ require 'rest-client'
2
+ require 'gitsheet/version'
3
+ require 'gitsheet/pagelinks'
4
+
5
+ class Gitsheet
6
+ def initialize(username, password, organization)
7
+ @auth = "#{username}:#{password}"
8
+ @org = organization
9
+ end
10
+
11
+ def repos
12
+ repos = self.fetch_repos
13
+ self.add_commits repos
14
+ end
15
+
16
+ def users
17
+ users = self.fetch_users({per_page: 100})
18
+ end
19
+
20
+ def count_repos
21
+ self.fetch_repos.size
22
+ end
23
+
24
+ def count_users
25
+ self.fetch_users({per_page: 100}).size
26
+ end
27
+
28
+ def add_commits(repos)
29
+ repos.each do |r|
30
+ $stderr.print "#{r['name']}"
31
+
32
+ sleep(1)
33
+ $stderr.print '...'
34
+
35
+ r['latest_commit'] = self.latest_commit(r['name'])
36
+
37
+ $stderr.puts '✔︎'
38
+ end
39
+ end
40
+
41
+ def latest_commit(repo)
42
+ commits = self.fetch_commits(repo)
43
+ commits.first['commit']['author']['date']
44
+ end
45
+
46
+ def fetch_repos(options = {})
47
+ url = "https://#{@auth}@api.github.com/orgs/#{@org}/repos"
48
+ repos = []
49
+
50
+ # Initial request
51
+ response = self.fetch(url, {per_page: 100})
52
+ page_header = response.headers[:link]
53
+ repos.concat JSON.parse(response)
54
+
55
+ # Parse page headers
56
+ pages = PageLinks.new page_header
57
+ return repos if pages.none?
58
+
59
+ # Fetch additional pages
60
+ 2.upto(pages.last) do |page|
61
+ sleep(1)
62
+ repos.concat JSON.parse(self.fetch(url, {per_page: 100, page: page}))
63
+ end
64
+
65
+ repos
66
+ end
67
+
68
+ def fetch_users(options = {})
69
+ url = "https://#{@auth}@api.github.com/orgs/#{@org}/members"
70
+ JSON.parse self.fetch(url, options)
71
+ end
72
+
73
+ def fetch_commits(repo, options = {})
74
+ url = "https://#{@auth}@api.github.com/repos/#{@org}/#{repo}/commits"
75
+ options.merge!(per_page: 1)
76
+ JSON.parse self.fetch(url, options)
77
+ end
78
+
79
+ def fetch(url, params = {})
80
+ RestClient.get url, {:params => params}
81
+ end
82
+ end
@@ -0,0 +1,50 @@
1
+
2
+ class PageLinks
3
+
4
+ SPLIT_LINKS = ','
5
+ SPLIT_LINK = ';'
6
+
7
+ def initialize(link)
8
+ @links = {}
9
+
10
+ link.split(PageLinks::SPLIT_LINKS).each do |link|
11
+ section = link.split PageLinks::SPLIT_LINK
12
+
13
+ raise 'Page Links could not be split on ;' if section.length < 2
14
+
15
+ page_number = section[0].slice(section[0].length - 2, 1)
16
+ page_relation = section[1].gsub(/rel="(.*)"/, '\1').lstrip
17
+
18
+ @links[page_relation] = page_number
19
+ end
20
+ end
21
+
22
+ def next
23
+ @links['next'].to_i
24
+ end
25
+
26
+ def prev
27
+ @links['prev'].to_i
28
+ end
29
+
30
+ def first
31
+ @links['first'].to_i
32
+ end
33
+
34
+ def last
35
+ @links['last'].to_i
36
+ end
37
+
38
+ def next?
39
+ !!@links['next']
40
+ end
41
+
42
+ def prev?
43
+ !!@links['prev']
44
+ end
45
+
46
+ def none?
47
+ @links.empty?
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ class Gitsheet
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitsheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hbbb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Gitsheet is a small command line utility to quickly gather user and repository
140
+ data from Github's API.
141
+ email:
142
+ - h.borges10592@gmail.com
143
+ executables:
144
+ - gitsheet
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - CODE_OF_CONDUCT.md
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - bin/console
156
+ - bin/gitsheet
157
+ - bin/setup
158
+ - gitsheet.gemspec
159
+ - lib/formatter.rb
160
+ - lib/gitsheet.rb
161
+ - lib/gitsheet/pagelinks.rb
162
+ - lib/gitsheet/version.rb
163
+ homepage: https://github.com/Hbbb/gitx
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.5
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Github API command line tool.
187
+ test_files: []
188
+ has_rdoc: