fasterer-github 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d1a9f24f4c238ba17781de42c94b0300db7bc09
4
+ data.tar.gz: 1ebeb537bcc1ed6da53d3acb59fce7ff5b42cf20
5
+ SHA512:
6
+ metadata.gz: d7f737960b42f0f28d7c2eb773a92922474c8a9cb683566ff7abe409b907633508d52816a33a6d92e8a0bcba049aeebcbf4647dc2bfdb421c4ff870fdcd7a5f8
7
+ data.tar.gz: 43d12d5e509ff27100b15e50c7210ace9ddf188d5ace7891ef87b6d6d0412b75b7d1373bf04fdc5a61ab696e33e5c8c59ac5b527e0101350b7c54781eac0ffeb
data/.gitignore ADDED
@@ -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
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.5
5
+ - 2.2.2
6
+ before_install: gem install bundler -v 1.10.5
7
+ addons:
8
+ code_climate:
9
+ repo_token:
10
+ secure: 3c0a38daec04e8d951665de52bba00cf631baa154bd9a19394dcfe1fb9e85ff1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kacper Goliński
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.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ [![Code Climate](https://codeclimate.com/github/caspg/fasterer-github/badges/gpa.svg)](https://codeclimate.com/github/caspg/fasterer-github)
2
+ [![Test Coverage](https://codeclimate.com/github/caspg/fasterer-github/badges/coverage.svg)](https://codeclimate.com/github/caspg/fasterer-github/coverage)
3
+
4
+ # fasterer-github
5
+
6
+ This is a [fasterer](https://github.com/DamirSvrtan/fasterer) extension which allows to scan GitHub repo using GitHub API.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'fasterer-github'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install fasterer-github
24
+
25
+ ## Request Rate Limit - Github Api
26
+
27
+ Github api rate limit for unauthenticated requests is 60 request per hour. Fortunately, authenticated requests get a higher rate limit, which allows to make up to 5,000 requests per hour.
28
+
29
+ ## Configuration
30
+
31
+ You can use configure block to provide `access_token`:
32
+ ```ruby
33
+ Fasterer::Github.configure do |config|
34
+ config.access_token = 'YOUR_GITHUB_ACCESS_TOKEN'
35
+ end
36
+ ```
37
+
38
+ Instead of `access_token`, you can also add `client_id` and `client_secret`:
39
+
40
+ ```ruby
41
+ Fasterer::Github.configure do |config|
42
+ config.client_id = 'YOUR_GITHUB_CLIENT_ID'
43
+ config.client_secret = 'YOUR_GITHUB_CLIENT_SECRET'
44
+ end
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ To scan whole repo, run:
50
+ ```ruby
51
+ Fasterer::Github.scan('owner', 'repo')
52
+ ```
53
+
54
+ You can also scan specific file:
55
+ ```ruby
56
+ Fasterer::Github.scan('owner', 'repo', 'lib/fasterer-github.rb')
57
+ ```
58
+
59
+ ## Example output
60
+
61
+ ```ruby
62
+ $ Fasterer::Github.scan('owner', 'repo', 'path/to/file.rb')
63
+
64
+ {
65
+ :repo_owner => 'owner',
66
+ :repo_name => 'repo',
67
+ :fasterer_offences => {
68
+ :hash_merge_bang_vs_hash_brackets => [
69
+ {
70
+ :path => "https://api.github.com/repos/owner/repo/contents/path/to/file.rb?ref=master",
71
+ :lines => [10, 17, 19]
72
+ }
73
+ ]
74
+ },
75
+ :errors => [],
76
+ :api_errors => []
77
+ }
78
+ ```
79
+
80
+ Example output when parser encounters some error and api returns error code:
81
+ ```ruby
82
+ {
83
+ :repo_owner => 'owner',
84
+ :repo_name => 'repo',
85
+ :fasterer_offences => {},
86
+ :errors => [
87
+ { path: 'path/to/file.rb' }
88
+ ],
89
+ :api_errors => [
90
+ { code: 404, msg_body: 'some message from github api', path: 'path/to/file.rb' }
91
+ ]
92
+ }
93
+ ```
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/caspg/fasterer-github.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fasterer/github/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fasterer-github'
8
+ spec.version = Fasterer::Github::VERSION
9
+ spec.authors = ['Kacper Goliński']
10
+ spec.email = ['kacper.golinski@gmail.co']
11
+
12
+ spec.summary = 'Fasterer extension which allows to scan github repo. '
13
+ spec.homepage = 'https://github.com/caspg/fasterer-github'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'webmock'
26
+ spec.add_development_dependency 'vcr'
27
+
28
+ spec.add_runtime_dependency 'fasterer', '0.1.11'
29
+ spec.add_runtime_dependency 'httparty'
30
+ end
@@ -0,0 +1,37 @@
1
+ require 'fasterer'
2
+ require 'base64'
3
+
4
+ module Fasterer
5
+ module Github
6
+ class AnalyzerExtension < Fasterer::Analyzer
7
+ def initialize(content64)
8
+ @content64 = content64
9
+ @file_content = decoded_content
10
+ end
11
+
12
+ def scan
13
+ super
14
+ end
15
+
16
+ def errors
17
+ super
18
+ end
19
+
20
+ def offences
21
+ offences = {}
22
+ errors.group_by(&:name).each do |k, v|
23
+ offences[k] = v.map(&:line_number)
24
+ end
25
+ offences
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :content64
31
+
32
+ def decoded_content
33
+ Base64.decode64(content64)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'httparty'
2
+
3
+ module Fasterer
4
+ module Github
5
+ class ApiWrapper
6
+ BASE_URL = 'https://api.github.com'
7
+
8
+ def initialize(owner, repo)
9
+ @owner = owner
10
+ @repo = repo
11
+ @access_token = Fasterer::Github.configuration.access_token.to_s
12
+ @client_id = Fasterer::Github.configuration.client_id.to_s
13
+ @client_secret = Fasterer::Github.configuration.client_secret.to_s
14
+ end
15
+
16
+ def contents(path)
17
+ url = build_url(path)
18
+ HTTParty.get(url)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :owner, :repo, :path, :client_id, :client_secret, :access_token
24
+
25
+ def build_url(path)
26
+ url = BASE_URL + "/repos/#{owner}/#{repo}/contents/#{path}"
27
+ return add_access_token(url) if access_token != ''
28
+ return add_client_id_and_secret(url) if client_id != '' && client_secret != ''
29
+ url
30
+ end
31
+
32
+ def add_access_token(url)
33
+ url + "?access_token=#{access_token}"
34
+ end
35
+
36
+ def add_client_id_and_secret(url)
37
+ url + "?client_id=#{client_id}&client_secret=#{client_secret}"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ module Fasterer
2
+ module Github
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ yield(configuration)
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.reset_configuration
16
+ Fasterer::Github.configuration = nil
17
+ Fasterer::Github.configure {}
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :client_id, :client_secret, :access_token
22
+
23
+ def initialize
24
+ @client_id = nil
25
+ @client_secret = nil
26
+ @access_token = nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,65 @@
1
+ require_relative 'api_wrapper'
2
+
3
+ module Fasterer
4
+ module Github
5
+ class GhTraverser
6
+ def initialize(owner, repo, path)
7
+ @owner = owner
8
+ @repo = repo
9
+ @path = path.to_s
10
+ end
11
+
12
+ def traverse
13
+ catch(:rate_limit) { collect_data(path) }
14
+ end
15
+
16
+ def collected_data
17
+ @collected_data ||= []
18
+ end
19
+
20
+ def api_errors
21
+ @api_errors ||= []
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :owner, :repo, :path
27
+
28
+ def wrapper
29
+ @wrapper ||= Fasterer::Github::ApiWrapper.new(owner, repo)
30
+ end
31
+
32
+ def collect_data(path)
33
+ response = wrapper.contents(path)
34
+ return store_api_error(response, path) unless response.code < 400
35
+ parsed_response = response.parsed_response
36
+
37
+ if parsed_response.is_a?(Hash)
38
+ return unless match_regex?(parsed_response['path'])
39
+ store_data(parsed_response)
40
+ else
41
+ parsed_response.each { |item| collect_data(item['path']) }
42
+ end
43
+ end
44
+
45
+ def store_api_error(response, path)
46
+ response_code = response.code
47
+ api_errors << { code: response_code, msg_body: response.body, path: path }
48
+ throw(:rate_limit) if rate_limit_error?(response)
49
+ end
50
+
51
+ def rate_limit_error?(response)
52
+ response.code == 403 && response.body =~ /rate limit exceeded/i
53
+ end
54
+
55
+ def store_data(response)
56
+ file_data = { path: response['path'], content64: response['content'] }
57
+ collected_data << file_data
58
+ end
59
+
60
+ def match_regex?(file_name)
61
+ file_name =~ /(.rb)$/
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ module Fasterer
2
+ module Github
3
+ class OutputComposer
4
+ def initialize(owner, repo)
5
+ @repo_owner = owner
6
+ @repo_name = repo
7
+ end
8
+
9
+ def add_offences(offences, path)
10
+ offences.each do |offence_name, lines|
11
+ details = { path: path, lines: lines }
12
+ (fasterer_offences[offence_name] ||= []) << details
13
+ end
14
+ end
15
+
16
+ def add_errors(path)
17
+ errors << { path: path }
18
+ end
19
+
20
+ def add_api_errors(new_api_errors)
21
+ new_api_errors.each { |e| api_errors << e }
22
+ end
23
+
24
+ def result
25
+ {
26
+ repo_owner: repo_owner,
27
+ repo_name: repo_name,
28
+ fasterer_offences: fasterer_offences,
29
+ errors: errors,
30
+ api_errors: api_errors
31
+ }
32
+ end
33
+
34
+ private
35
+
36
+ attr_accessor :repo_owner, :repo_name
37
+
38
+ def fasterer_offences
39
+ @fasterer_offenses ||= {}
40
+ end
41
+
42
+ def errors
43
+ @errors ||= []
44
+ end
45
+
46
+ def api_errors
47
+ @api_errors ||= []
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ require 'fasterer/github/gh_traverser'
2
+ require 'fasterer/github/output_composer'
3
+ require 'fasterer/github/analyzer_extension'
4
+
5
+ module Fasterer
6
+ module Github
7
+ class Scanner
8
+ def initialize(owner, repo, path)
9
+ @owner = owner
10
+ @repo = repo
11
+ @path = path
12
+ end
13
+
14
+ def run
15
+ data = traverse_and_collect_data
16
+ data.each { |d| analyze_code(d) }
17
+ end
18
+
19
+ def results
20
+ output_composer.result
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :owner, :repo, :path
26
+
27
+ def traverser
28
+ @traverser ||= Fasterer::Github::GhTraverser.new(owner, repo, path)
29
+ end
30
+
31
+ def output_composer
32
+ @output_composer ||= Fasterer::Github::OutputComposer.new(owner, repo)
33
+ end
34
+
35
+ def traverse_and_collect_data
36
+ traverser.traverse
37
+ output_composer.add_api_errors(traverser.api_errors) if traverser.api_errors.any?
38
+ traverser.collected_data
39
+ end
40
+
41
+ def analyze_code(data)
42
+ analyzer = Fasterer::Github::AnalyzerExtension.new(data[:content64])
43
+ analyzer.scan
44
+ rescue RubyParser::SyntaxError, Racc::ParseError, Timeout::Error
45
+ output_composer.add_errors(data[:path])
46
+ else
47
+ output_composer.add_offences(analyzer.offences, data[:path])
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module Fasterer
2
+ module Github
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'fasterer/github/version'
2
+ require 'fasterer/github/scanner'
3
+ require 'fasterer/github/configuration'
4
+
5
+ module Fasterer
6
+ module Github
7
+ def self.scan(owner, repo, path = nil)
8
+ scanner = Fasterer::Github::Scanner.new(owner, repo, path)
9
+ scanner.run
10
+ scanner.results
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fasterer-github
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kacper Goliński
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: webmock
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: vcr
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: fasterer
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.11
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.11
111
+ - !ruby/object:Gem::Dependency
112
+ name: httparty
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - kacper.golinski@gmail.co
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - fasterer-github.gemspec
140
+ - lib/fasterer/github.rb
141
+ - lib/fasterer/github/analyzer_extension.rb
142
+ - lib/fasterer/github/api_wrapper.rb
143
+ - lib/fasterer/github/configuration.rb
144
+ - lib/fasterer/github/gh_traverser.rb
145
+ - lib/fasterer/github/output_composer.rb
146
+ - lib/fasterer/github/scanner.rb
147
+ - lib/fasterer/github/version.rb
148
+ homepage: https://github.com/caspg/fasterer-github
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.6
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Fasterer extension which allows to scan github repo.
172
+ test_files: []
173
+ has_rdoc: